-
Notifications
You must be signed in to change notification settings - Fork 34
/
TwoTrainHubs.ino
171 lines (147 loc) · 4.67 KB
/
TwoTrainHubs.ino
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
/**
* A Legoino example to connect two train hubs and one powered up remote.
*
* 1) Power up the ESP32
* 2) Power up the Remote and the Train Hubs in any order
*
* You can change the motor speed of train 1 with the left (A) remote buttons
* You can change the motor speed of train 2 with the right (B) remote buttons
*
* (c) Copyright 2020
* Released under MIT License
*
*/
#include "Lpf2Hub.h"
// create a hub instance
Lpf2Hub myRemote;
Lpf2Hub myTrainHub1;
Lpf2Hub myTrainHub2;
byte portLeft = (byte)PoweredUpRemoteHubPort::LEFT;
byte portRight = (byte)PoweredUpRemoteHubPort::RIGHT;
byte portA = (byte)PoweredUpHubPort::A;
int currentSpeedTrain1 = 0;
int currentSpeedTrain2 = 0;
int updatedSpeedTrain1 = 0;
int updatedSpeedTrain2 = 0;
bool isInitialized = false;
// callback function to handle updates of remote buttons
void remoteCallback(void *hub, byte portNumber, DeviceType deviceType, uint8_t *pData)
{
Lpf2Hub *myRemoteHub = (Lpf2Hub *)hub;
Serial.print("sensorMessage callback for port: ");
Serial.println(portNumber, DEC);
if (deviceType == DeviceType::REMOTE_CONTROL_BUTTON)
{
ButtonState buttonState = myRemoteHub->parseRemoteButton(pData);
Serial.print("Buttonstate: ");
Serial.println((byte)buttonState, HEX);
// Do the logic for left buttons of remote control and Train Hub 1
if (portNumber == (byte)portLeft && buttonState == ButtonState::UP)
{
updatedSpeedTrain1 = min(100, currentSpeedTrain1 + 10);
}
else if (portNumber == (byte)portLeft && buttonState == ButtonState::DOWN)
{
updatedSpeedTrain1 = min(100, currentSpeedTrain1 - 10);
}
else if (portNumber == (byte)portLeft && buttonState == ButtonState::STOP)
{
updatedSpeedTrain1 = 0;
}
if (currentSpeedTrain1 != updatedSpeedTrain1)
{
myTrainHub1.setBasicMotorSpeed(portA, updatedSpeedTrain1);
currentSpeedTrain1 = updatedSpeedTrain1;
}
Serial.print("Current speed train 1:");
Serial.println(currentSpeedTrain1, DEC);
// Do the logic for right buttons of remote control and Train Hub 2
if (portNumber == (byte)portRight && buttonState == ButtonState::UP)
{
updatedSpeedTrain2 = min(100, currentSpeedTrain2 + 10);
}
else if (portNumber == (byte)portRight && buttonState == ButtonState::DOWN)
{
updatedSpeedTrain2 = min(100, currentSpeedTrain2 - 10);
}
else if (portNumber == (byte)portRight && buttonState == ButtonState::STOP)
{
updatedSpeedTrain2 = 0;
}
if (currentSpeedTrain2 != updatedSpeedTrain2)
{
myTrainHub2.setBasicMotorSpeed(portA, updatedSpeedTrain2);
currentSpeedTrain2 = updatedSpeedTrain2;
}
Serial.print("Current speed train 2:");
Serial.println(currentSpeedTrain2, DEC);
}
}
void setup()
{
Serial.begin(115200);
myRemote.init(); // initialize the remote control hub
myTrainHub1.init("90:84:2b:03:19:7f"); // initialize the listening train hub 1 // here you have to use your own device ids
myTrainHub2.init("90:84:2b:06:76:a6"); // initialize the listening train hub 2 // here you have to use your own device ids
}
// main loop
void loop()
{
//wait for three elements
if (myRemote.isConnecting())
{
if (myRemote.getHubType() == HubType::POWERED_UP_REMOTE)
{
//This is the right device
if (!myRemote.connectHub())
{
Serial.println("Unable to connect to hub");
}
else
{
myRemote.setLedColor(GREEN);
Serial.println("Remote connected.");
}
}
}
if (myTrainHub1.isConnecting())
{
if (myTrainHub1.getHubType() == HubType::POWERED_UP_HUB)
{
myTrainHub1.connectHub();
myTrainHub1.setLedColor(BLUE);
Serial.println("train hub 1 connected.");
}
}
if (myTrainHub2.isConnecting())
{
if (myTrainHub2.getHubType() == HubType::POWERED_UP_HUB)
{
myTrainHub2.connectHub();
myTrainHub2.setLedColor(YELLOW);
Serial.println("train hub 2 connected.");
}
}
if (!myRemote.isConnected())
{
myRemote.init();
}
if (!myTrainHub1.isConnected())
{
myTrainHub1.init();
}
if (!myTrainHub2.isConnected())
{
myTrainHub2.init();
}
if (myRemote.isConnected() && myTrainHub1.isConnected() && myTrainHub2.isConnected() && !isInitialized)
{
Serial.println("System is initialized");
isInitialized = true;
delay(200); //needed because otherwise the message is to fast after the connection procedure and the message will get lost
// both activations are needed to get status updates
myRemote.activatePortDevice(portLeft, remoteCallback);
myRemote.activatePortDevice(portRight, remoteCallback);
myRemote.setLedColor(WHITE);
}
} // End of loop