-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtray.cpp
180 lines (150 loc) · 3.57 KB
/
tray.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "tray.h"
Tray::Tray(uint8_t clsdPIPin, uint8_t openPIPin,
uint8_t motorCtrlEnabledPin, uint8_t motorDirection1Pin, uint8_t motorDirection2Pin):
_desiredState(Off),
_controllerState(Off),
_closingDelay(0),
_openingDelay(0) {
this->_clsdPIPin = clsdPIPin;
this->_openPIPin = openPIPin;
this->_motorCtrlEnabledPin = motorCtrlEnabledPin;
this->_motorDirection1Pin = motorDirection1Pin;
this->_motorDirection2Pin = motorDirection2Pin;
_clsdPI = new Bounce();
_openPI = new Bounce();
_closingTimer = new Timer(_closingDelay);
_openingTimer = new Timer(_openingDelay);
_closingTimer->setEnabled(false);
_openingTimer->setEnabled(false);
}
Tray::~Tray() {
delete _clsdPI;
delete _openPI;
delete _closingTimer;
delete _openingTimer;
}
void Tray::init() {
_clsdPI->attach(_clsdPIPin, INPUT);
_openPI->attach(_openPIPin, INPUT);
pinMode(_motorCtrlEnabledPin, OUTPUT);
pinMode(_motorDirection1Pin, OUTPUT);
pinMode(_motorDirection2Pin, OUTPUT);
}
Tray::State Tray::getCurrentState() {
if (!_clsdPI->read()) {
return Closed;
} else if (!_openPI->read()) {
return Open;
} else {
if (_desiredState == Off) {
return Off;
} else if (_desiredState == Open) {
return Opening;
} else if (_desiredState == Closed) {
return Closing;
}
}
return Off;
}
Tray::State Tray::getDesiredState() {
return _desiredState;
}
void Tray::setDesiredState(State desiredState) {
switch (desiredState) {
case Open:
case Opening:
this->_desiredState = Open;
break;
case Closed:
case Closing:
this->_desiredState = Closed;
break;
case Off:
default:
this->_desiredState = Off;
}
}
unsigned long Tray::getClosingDelay() {
return _closingDelay;
}
void Tray::setClosingDelay(unsigned long delay) {
_closingDelay = delay;
}
unsigned long Tray::getOpeningDelay() {
return _openingDelay;
}
void Tray::setOpeningDelay(unsigned long delay) {
_openingDelay = delay;
}
void Tray::update() {
_clsdPI->update();
_openPI->update();
State ctrlState = _determineControllerState();
if (_controllerState != ctrlState) {
State previousState = _controllerState;
_controllerState = ctrlState;
switch (_controllerState) {
case Opening:
_openingTimer->setEnabled(false);
_closingTimer->setEnabled(false);
_open();
break;
case Closing:
_openingTimer->setEnabled(false);
_closingTimer->setEnabled(false);
_close();
break;
case Off:
default:
switch (previousState) {
case Opening:
_openingTimer->reset(_openingDelay);
_openingTimer->setEnabled(true);
break;
case Closing:
_closingTimer->reset(_closingDelay);
_closingTimer->setEnabled(true);
break;
default:
_off();
}
}
}
if (_openingTimer->fire()) {
_openingTimer->setEnabled(false);
_off();
}
if (_closingTimer->fire()) {
_closingTimer->setEnabled(false);
_off();
}
}
Tray::State Tray::_determineControllerState() {
if (_desiredState == Open) {
if (getCurrentState() != Open) {
return Opening;
}
} else if (_desiredState == Closed) {
if (getCurrentState() != Closed) {
return Closing;
}
}
return Off;
}
void Tray::_open() {
digitalWrite(_motorCtrlEnabledPin, LOW);
digitalWrite(_motorDirection1Pin, LOW);
digitalWrite(_motorDirection2Pin, HIGH);
digitalWrite(_motorCtrlEnabledPin, HIGH);
}
void Tray::_close() {
digitalWrite(_motorCtrlEnabledPin, LOW);
digitalWrite(_motorDirection1Pin, HIGH);
digitalWrite(_motorDirection2Pin, LOW);
digitalWrite(_motorCtrlEnabledPin, HIGH);
}
void Tray::_off() {
digitalWrite(_motorCtrlEnabledPin, LOW);
digitalWrite(_motorDirection1Pin, LOW);
digitalWrite(_motorDirection2Pin, LOW);
}