-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmode.cpp
More file actions
59 lines (46 loc) · 1.31 KB
/
mode.cpp
File metadata and controls
59 lines (46 loc) · 1.31 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
// Abstract base class for modes of the car.
#include "ir_keys.h"
#include "mode.h"
#include "motor.h"
ControlContext::ControlContext(Motor * motor_left, Motor * motor_right) {
this->motor_left = motor_left;
this->motor_right = motor_right;
}
void ControlContext::set_power(int left_power, int right_power) {
this->set_power_left(left_power);
this->set_power_right(right_power);
}
void ControlContext::stop() {
this->stop_left();
this->stop_right();
}
void ControlContext::set_power_right(int power) {
this->motor_right->set_power(power);
}
void ControlContext::set_power_left(int power) {
this->motor_left->set_power(power);
}
void ControlContext::stop_left() {
this->motor_left->stop_motor();
}
void ControlContext::stop_right() {
this->motor_right->stop_motor();
}
void ControlContext::set_mode(CtrlMode ctrl_mode) {
this->mode = this->mode_registry[ctrl_mode];
this->stop_left(); this->stop_right();
}
void ControlContext::register_mode(CtrlMode ctrl_mode, Mode * mode) {
this->mode_registry[ctrl_mode] = mode;
}
void ControlContext::maybe_yield_to_manual(unsigned long key_value) {
switch (key_value) {
case IR_KEY_N:
case IR_KEY_S:
case IR_KEY_E:
case IR_KEY_W:
this->set_mode(CTRL_MODE_INFRARED_MANUAL);
this->mode->handle_ir_keypress(key_value);
return;
}
}