-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car.h
95 lines (81 loc) · 1.84 KB
/
Car.h
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
#include "Controller.h"
#include "Indicators.h"
#include "Headlights.h"
#include "Enums.h"
#ifndef Car_h
#define Car_h
class Car {
public:
Car() {}
void toggleLights() {
headlights.next();
}
void update() {
DrivingState lastState = state;
// PWM should be HIGH for ~0-2000ms, higher values indicates that it's reading LOW.
if (throttlePulses < THROTTLE_MAX + 100) {
throttle = throttlePulses;
}
if (steeringPulses < STEERING_MAX + 100) {
steering = steeringPulses;
}
if (throttle > THROTTLE_NEUTRAL + 100) {
state = DRIVING;
didBrake = false;
} else if (throttle < THROTTLE_NEUTRAL - 100) {
if (didBrake) {
state = REVERSING;
} else {
state = BRAKING;
}
} else {
if (state == BRAKING) {
didBrake = true;
}
state = NEUTRAL;
}
if (lastState != state) {
setState();
}
indicators.update(state);
headlights.update(state);
}
private:
Indicators indicators;
Headlights headlights;
DrivingState state = NEUTRAL;
bool didBrake = false;
void setState() {
Serial.print("THROTTLE ");
switch (state) {
case DRIVING:
Serial.println("DRIVE");
brakes(false);
reverse(false);
break;
case NEUTRAL:
Serial.println("NEUTRAL");
brakes(false);
break;
case BRAKING:
Serial.println("BRAKE");
brakes(true);
break;
case REVERSING:
Serial.println("REVERSE");
reverse(true);
break;
}
}
void brakes(bool on) {
//Rear Left: 12-15 Rear Right 8-11
setColorLoop(8, 8, on ? CRGB::Red : CRGB::Black);
FastLED.show();
}
void reverse(bool on) {
//Rear Left: 12-15 Rear Right 8-11
setColorLoop(8, 8, on ? CRGB::White : CRGB::Black);
FastLED.show();
}
};
#endif