summaryrefslogtreecommitdiffstats
path: root/source/Concept/Framework/engine.h
blob: 2d8c4097e4aad1db701dae674a39005438d762c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef _ENGINE_H
#define _ENGINE_H

#include "stdafx.h"

class Engine : public IO_Module
{
public:
	Engine()
	{
		this->enabled = false;
		this->curSpeed = 0;
		this->parent = NULL;
		this->moduleId = 0;
		this->hardwarePort = NULL;
		this->pwmSpeed = NULL;
		this->pwmPort = NULL;
		this->pinForward = 0;
		this->pinReverse = 0;
		this->pinPwm = 0;
	}

	Engine(uint32 engineId)
	{
		this->enabled = false;
		this->curSpeed = 0;
		this->parent = NULL;
		this->moduleId = engineId;
		this->pwmSpeed = NULL;
		this->pwmPort = NULL;
		this->pinPwm = 0;
		
		switch(engineId)
		{
			case IO_ENGINE_DRIVE_LEFT:
				this->hardwarePort = &PORTB;
				this->pwmSpeed = &OCR1A;
				this->pinForward = (1 << 0);
				this->pinReverse = (1 << 1);
				break;
			case IO_ENGINE_DRIVE_RIGHT:
				this->hardwarePort = &PORTB;
				this->pwmSpeed = &OCR1B;
				this->pinForward = (1 << 2);
				this->pinReverse = (1 << 3);
				break;
			case IO_ENGINE_DRIVE_BACK:
				this->hardwarePort = &PORTD;
				this->pwmSpeed = &OCR3A;
				this->pinForward = (1 << 5);
				this->pinReverse = (1 << 4);
				break;
			case IO_ENGINE_DRIBBLER:
				this->hardwarePort = &PORTD;
				this->pwmPort = &PORTA;
				this->pinForward = (1 << 6);
				this->pinReverse = (1 << 7);
				this->pinPwm = (1 << 5);
				break;
			default:
				this->hardwarePort = NULL;
				this->pwmSpeed = NULL;
				this->pinForward = 0;
				this->pinReverse = 0;
				break;
		}

		*this->pwmSpeed = 0;
	}

protected:
	bool enabled;
	float curSpeed;

	//Hardware
	volatile uint8* hardwarePort;
	volatile uint16* pwmSpeed;
	uint8 pinForward;
	uint8 pinReverse;
	//Dribbler only
	volatile uint8* pwmPort;
	uint8 pinPwm;

	void UpdateDirection()
	{
		if(enabled)
		{
			if(curSpeed > 0)
			{
				*hardwarePort |= pinForward;
				*hardwarePort &= ~pinReverse;
			}
			else if(curSpeed < 0)
			{
				*hardwarePort |= pinReverse;
				*hardwarePort &= ~pinForward;
			}
			else
			{
				*hardwarePort |= pinForward;
				*hardwarePort |= pinReverse;
			}
		}
		else
		{
			*hardwarePort &= ~pinForward;
			*hardwarePort &= ~pinReverse;
		}
	}

public:
	float GetSpeed()
	{
		return curSpeed;
	}

	void SetSpeed(float newSpeed)
	{
		curSpeed = newSpeed;

		if(pwmSpeed)
		{
			*pwmSpeed = abs(newSpeed / SPEED_PER_PWM);
		}
		else if(pwmPort && (uint16)(abs(newSpeed / SPEED_PER_PWM)))
		{
			*pwmPort |= pinPwm;
		}
		else if(pwmPort)
		{
			*pwmPort &= ~pinPwm;
		}

		UpdateDirection();
	}

	bool GetEnabled()
	{
		return enabled;
	}

	void SetEnabled(bool newStatus)
	{
		enabled = newStatus;

		UpdateDirection();
	}
};

#endif