-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonView.cpp
More file actions
197 lines (172 loc) · 6.17 KB
/
ButtonView.cpp
File metadata and controls
197 lines (172 loc) · 6.17 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "ButtonView.h"
void ButtonView::setup(Balancer *balancer, GyroQuartiles *gyroQuartiles) {
this->balancer = balancer;
buttonUp.setup(buttonConfigUp);
buttonCenter.setup(buttonConfigCenter);
buttonDown.setup(buttonConfigDown);
buttonUp.setOnPressCallback(&ButtonView::onUpPressCallback, this);
buttonUp.setOnReleaseCallback(&ButtonView::onUpReleaseCallback, this);
buttonCenter.setOnPressCallback(&ButtonView::onCenterPressCallback, this);
buttonCenter.setOnReleaseCallback(&ButtonView::onCenterReleaseCallback, this);
buttonDown.setOnPressCallback(&ButtonView::onDownPressCallback, this);
buttonDown.setOnReleaseCallback(&ButtonView::onDownReleaseCallback, this);
lastButton = &buttonUp;
}
void ButtonView::registerViewObserver(ViewObserver *viewObserver) {
this->viewObserver = viewObserver;
}
void ButtonView::loop() {
buttonUp.loop();
buttonCenter.loop();
buttonDown.loop();
if (!hasRecentlySwitchedMode && buttonCenter.isPressed() && buttonCenter.getPressedButtonDurationMs() >= SERVICE_MODE_BUTTON_PRESS_MS) {
setMode(mode == Mode::SERVICE ? Mode::NORMAL : Mode::SERVICE);
if (mode == Mode::SERVICE)
buttonCenter.setLedBlinking(500);
else
buttonCenter.setLedOff();
buttonUp.setLedOff();
buttonDown.setLedOff();
hasRecentlySwitchedMode = true;
}
}
bool isFinalBalancerState(State balancerState) {
switch (balancerState) {
case State::ZeroState:
case State::FinalState:
case State::BalancedState:
return true;
break;
default:
return false;
break;
}
}
void ButtonView::onUpPress() {
CommandResult command = getCommandForUpDown(ViewCommand::TO_ZERO, MOTOR_REVERSE_STATE);
onCommandCallbackSafe(command);
buttonUp.setLedOn();
buttonDown.setLedOff();
lastButton = &buttonUp;
if (mode != Mode::SERVICE)
buttonCenter.setLedOff();
}
void ButtonView::onUpRelease() {
// We want the led to keep blinking if final state was reached
if (!isFinalBalancerState(balancer->getState()))
buttonUp.setLedOff();
CommandResult command = CommandResult(ViewCommand::STOP_ALL);
onCommandCallbackSafe(command);
}
void ButtonView::onCenterPress() {
hasRecentlySwitchedMode = false;
}
void ButtonView::onCenterRelease() {
if (mode == Mode::NORMAL) {
switch (centerButtonState) {
case CenterButtonState::NONE:
centerButtonState = CenterButtonState::BALANCE;
onCommandCallbackSafe(CommandResult(ViewCommand::BALANCE));
buttonCenter.setLedOn();
break;
case CenterButtonState::BALANCE:
centerButtonState = CenterButtonState::STOP;
onCommandCallbackSafe(CommandResult(ViewCommand::STOP_ALL));
buttonCenter.setLedOff();
break;
case CenterButtonState::STOP:
centerButtonState = CenterButtonState::TO_ZERO;
onCommandCallbackSafe(CommandResult(ViewCommand::TO_ZERO));
buttonCenter.setLedOn();
break;
case CenterButtonState::TO_ZERO:
centerButtonState = CenterButtonState::NONE;
onCommandCallbackSafe(CommandResult(ViewCommand::STOP_ALL));
buttonCenter.setLedOff();
break;
}
} else if (mode == Mode::SERVICE) {
selectedLegId = (selectedLegId + 1) % MAX_LEGS;
}
}
void ButtonView::onDownPress() {
CommandResult command = getCommandForUpDown(ViewCommand::TO_FINAL, MOTOR_FORWARD_STATE);
onCommandCallbackSafe(command);
buttonUp.setLedOff();
buttonDown.setLedOn();
lastButton = &buttonDown;
if (mode != Mode::SERVICE)
buttonCenter.setLedOff();
}
void ButtonView::onDownRelease() {
// We want the led to keep blinking if final state was reached
if (!isFinalBalancerState(balancer->getState()))
buttonDown.setLedOff();
CommandResult command = CommandResult(ViewCommand::STOP_ALL);
onCommandCallbackSafe(command);
}
void ButtonView::notifyStateChange(State newState) {
switch (newState) {
case State::ZeroState:
case State::FinalState:
case State::BalancedState:
// Do not react if command run from center button
if (lastButton->isPressed())
lastButton->setLedBlinking(1000);
if (mode == Mode::NORMAL)
buttonCenter.setLedOff();
break;
case State::ErrorState:
buttonCenter.setLedBlinking(2000);
break;
case State::NoState:
case State::GroundState:
case State::ToZeroState:
case State::ToFinalState:
case State::ToGroundState:
case State::BalancingState:
break;
}
}
CommandResult ButtonView::getCommandForUpDown(ViewCommand normalViewCommand, int singleLegDirection) {
switch (mode) {
case Mode::NORMAL:
return normalViewCommand;
case Mode::SERVICE:
{
CommandResult commandResult(ViewCommand::START_SINGLE_MOTOR);
commandResult.legId = selectedLegId;
commandResult.direction = singleLegDirection;
return commandResult;
}
default:
return CommandResult(ViewCommand::NONE);
}
}
void ButtonView::setMode(Mode mode) {
this->mode = mode;
}
void ButtonView::onCommandCallbackSafe(CommandResult commandResult) {
if (viewObserver != NULL) {
viewObserver->onCommand(commandResult);
}
}
// http://tedfelix.com/software/c++-callbacks.html
void ButtonView::onUpPressCallback(void *p) {
((ButtonView *)p) -> onUpPress();
}
void ButtonView::onUpReleaseCallback(void *p) {
((ButtonView *)p) -> onUpRelease();
}
void ButtonView::onCenterPressCallback(void *p) {
((ButtonView *)p) -> onCenterPress();
}
void ButtonView::onCenterReleaseCallback(void *p) {
((ButtonView *)p) -> onCenterRelease();
}
void ButtonView::onDownPressCallback(void *p) {
((ButtonView *)p) -> onDownPress();
}
void ButtonView::onDownReleaseCallback(void *p) {
((ButtonView *)p) -> onDownRelease();
}