summaryrefslogtreecommitdiffstats
path: root/duplo_train/src/myhub.h
diff options
context:
space:
mode:
Diffstat (limited to 'duplo_train/src/myhub.h')
-rw-r--r--duplo_train/src/myhub.h177
1 files changed, 177 insertions, 0 deletions
diff --git a/duplo_train/src/myhub.h b/duplo_train/src/myhub.h
new file mode 100644
index 0000000..2a3e690
--- /dev/null
+++ b/duplo_train/src/myhub.h
@@ -0,0 +1,177 @@
1#pragma once
2
3#include "Lpf2Hub.h"
4
5class MyHub
6 : public Lpf2Hub
7{
8public:
9 void activateBaseSpeaker()
10 {
11 byte setSoundMode[8] = { 0x41, (byte)DuploTrainHubPort::SPEAKER, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01 };
12 Lpf2Hub::WriteValue(setSoundMode, 8);
13 m_last_cmd = millis();
14 }
15
16 void playSound(byte sound)
17 {
18 byte playSound[6] = { 0x81, (byte)DuploTrainHubPort::SPEAKER, 0x11, 0x51, 0x01, sound };
19 Lpf2Hub::WriteValue(playSound, 6);
20 m_last_cmd = millis();
21 }
22
23 void activateRgbLight()
24 {
25 byte port = getPortForDeviceType((byte)DeviceType::HUB_LED);
26 byte setColorMode[8] = { 0x41, port, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 };
27 Lpf2Hub::WriteValue(setColorMode, 8);
28 m_last_cmd = millis();
29 }
30
31 void setLedColor(Color color)
32 {
33 byte port = getPortForDeviceType((byte)DeviceType::HUB_LED);
34 byte setColor[6] = { 0x81, port, 0x11, 0x51, 0x00, color };
35 Lpf2Hub::WriteValue(setColor, 6);
36 m_last_cmd = millis();
37 }
38
39 void setMotorSpeedLevel(int speed_level)
40 {
41 if (speed_level > 3)
42 speed_level = 3;
43 else if (speed_level < -3)
44 speed_level = -3;
45 Serial.print("new speed="); Serial.println(speed_level);
46
47 int speed_pct = 0;
48 switch(speed_level)
49 {
50 case 3:
51 speed_pct = 70;
52 break;
53 case 2:
54 speed_pct = 50;
55 break;
56 case 1:
57 speed_pct = 30;
58 break;
59 case -1:
60 speed_pct = -30;
61 break;
62 case -2:
63 speed_pct = -50;
64 break;
65 case -3:
66 speed_pct = -70;
67 break;
68 default:
69 speed_pct = 0;
70 break;
71 }
72
73 m_speed_level = speed_level;
74 setBasicMotorSpeed(speed_pct);
75 if (abs(m_speed_level) == 1)
76 {
77 delay(200);
78 setBasicMotorSpeed(speed_pct);
79 }
80 }
81
82 void incrMotorSpeed()
83 {
84 setMotorSpeedLevel(m_speed_level + 1);
85 }
86
87 void decrMotorSpeed()
88 {
89 setMotorSpeedLevel(m_speed_level - 1);
90 }
91
92 void stopMotor()
93 {
94 stopBasicMotor();
95 m_speed_level = 0;
96 }
97
98 void stopMotorWithBreak()
99 {
100 if (abs(m_speed_level) == 3)
101 {
102 playSound((byte)DuploTrainBaseSound::BRAKE);
103 delay(200);
104 }
105 return stopMotor();
106 }
107
108 int isMotorStopped()
109 {
110 // make sure last motor command is at least 1 seconds ago
111 // otherwise issueing a stop motor command might also falsely trigger this
112 return (m_speed_level == 0 && millis() - m_last_motor_cmd > 1000);
113 }
114
115 int currentSpeedLevel()
116 {
117 return m_speed_level;
118 }
119
120 void toggleLight()
121 {
122 if (m_color == Color::BLACK)
123 {
124 setLedColor(m_prev_color);
125 m_color = m_prev_color;
126 m_prev_color = Color::BLACK;
127 }
128 else
129 {
130 setLedColor(Color::BLACK);
131 m_prev_color = m_color;
132 m_color = Color::BLACK;
133 }
134 }
135
136 void cycleLightColor()
137 {
138 m_color = (Color)(int(m_color) + 1);
139 if (m_color >= Color::NUM_COLORS)
140 m_color = Color::PINK;
141 setLedColor(m_color);
142 }
143
144 void onConnect()
145 {
146 m_speed_level = 0;
147 m_last_cmd = m_last_motor_cmd = millis();
148 m_color = m_prev_color = Color::BLUE;
149 }
150
151 unsigned long millisLastCommand()
152 {
153 return m_last_cmd;
154 }
155
156private:
157 void stopBasicMotor()
158 {
159 Lpf2Hub::stopBasicMotor((byte)DuploTrainHubPort::MOTOR);
160 m_last_cmd = millis();
161 m_last_motor_cmd = m_last_cmd;
162 }
163
164 void setBasicMotorSpeed(int speed = 0)
165 {
166 Lpf2Hub::setBasicMotorSpeed((byte)DuploTrainHubPort::MOTOR, speed);
167 m_last_cmd = millis();
168 m_last_motor_cmd = m_last_cmd;
169 }
170
171private:
172 int m_speed_level = 0;
173 unsigned long m_last_motor_cmd = 0;
174 unsigned long m_last_cmd;
175 Color m_color = Color::BLACK;
176 Color m_prev_color = Color::BLACK;
177};