Skip to content

Commit c48bc68

Browse files
committed
Merge branch 'release/0.5.1'
2 parents 0557d79 + 45f5620 commit c48bc68

File tree

5 files changed

+136
-23
lines changed

5 files changed

+136
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Arduino Library for controlling Powered UP and Boost controllers
55

66
*Disclaimer*: LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this project.
77

8+
Remote control your boost model example (just click the image to see the video)
9+
10+
[![Legoino Boost control example](http://img.youtube.com/vi/UtHMKe2Insw/mqdefault.jpg)](https://youtu.be/UtHMKe2Insw "Legoino boost control example")
11+
812
Simple Train example (just click the image to see the video)
913

1014
[![Legoino TrainHub color control example](http://img.youtube.com/vi/GZ0fqe3-Bhw/mqdefault.jpg)](https://youtu.be/GZ0fqe3-Bhw "Legoino TrainHub color control example")

examples/PoweredUpRemote/PoweredUpRemote.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ATTENTION: The connection order is relevant!
55
* 1) Power up the ESP32
66
* 2) Power up the Remote
7-
* 3) Power up the Train Hug
7+
* 3) Power up the Train Hub
88
*
99
* You can change the motor speed with the left (A) remote buttons
1010
*
@@ -21,6 +21,7 @@ PoweredUpRemote myRemote;
2121
PoweredUpHub myHub;
2222

2323
PoweredUpRemote::Port _portLeft = PoweredUpRemote::Port::LEFT;
24+
PoweredUpRemote::Port _portRight = PoweredUpRemote::Port::RIGHT;
2425
PoweredUpHub::Port _portA = PoweredUpHub::Port::A;
2526

2627
int currentSpeed = 0;
@@ -58,27 +59,29 @@ void loop() {
5859
}
5960

6061
if (myRemote.isConnected() && myHub.isConnected() && !isInitialized) {
62+
Serial.println("System is initialized");
6163
isInitialized = true;
6264
// both activations are needed to get status updates
6365
myRemote.activateButtonReports();
6466
myRemote.activatePortDevice(_portLeft, 55);
67+
myRemote.activatePortDevice(_portRight, 55);
6568
myRemote.setLedColor(WHITE);
6669
myHub.setLedColor(WHITE);
6770
}
6871

6972
// if connected we can control the train motor on Port A with the remote
7073
if (isInitialized) {
7174

72-
if (myRemote.isLeftRemoteUpButtonPressed()) {
75+
if (myRemote.isLeftRemoteUpButtonPressed() || myRemote.isRightRemoteUpButtonPressed()) {
7376
myRemote.setLedColor(GREEN);
7477
updatedSpeed = min(100, currentSpeed+10);
75-
} else if (myRemote.isLeftRemoteDownButtonPressed()) {
78+
} else if (myRemote.isLeftRemoteDownButtonPressed() || myRemote.isRightRemoteDownButtonPressed()) {
7679
myRemote.setLedColor(BLUE);
7780
updatedSpeed = min(100, currentSpeed-10);
78-
} else if (myRemote.isLeftRemoteStopButtonPressed()) {
81+
} else if (myRemote.isLeftRemoteStopButtonPressed() || myRemote.isRightRemoteStopButtonPressed()) {
7982
myRemote.setLedColor(RED);
8083
updatedSpeed = 0;
81-
} else if (myRemote.isLeftRemoteButtonReleased()) {
84+
} else if (myRemote.isLeftRemoteButtonReleased() || myRemote.isRightRemoteButtonReleased()) {
8285
myRemote.setLedColor(WHITE);
8386
}
8487

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* A Legoino example to connect to a Powered Up remote and a boost hub to control the Vernie model
3+
*
4+
* ATTENTION: The connection order is relevant!
5+
* 1) Power up the Boost Hub
6+
* 2) Power up the ESP32
7+
* 3) Power up the PoweredUp remote control
8+
*
9+
* Just rotate the right remote stick that the buttons are aligned horizontally (+ button to the right)
10+
*
11+
* Left Button UP -> Vernie will move one step forward
12+
* Left Button DOWN -> Vernie will move one step back
13+
* Right Button UP -> Vernie will rotate to the right
14+
* Right Button DOWN -> Vernie will rotate to the left
15+
* Right Stop Button -> Vernie will fire the arrow
16+
*
17+
* (c) Copyright 2019 - Cornelius Munz
18+
* Released under MIT License
19+
*
20+
*/
21+
22+
#include "PoweredUpRemote.h"
23+
#include "BoostHub.h"
24+
25+
// create a hub instance
26+
PoweredUpRemote myRemote;
27+
BoostHub myHub;
28+
29+
PoweredUpRemote::Port _portLeft = PoweredUpRemote::Port::LEFT;
30+
PoweredUpRemote::Port _portRight = PoweredUpRemote::Port::RIGHT;
31+
BoostHub::Port _portD = BoostHub::Port::D;
32+
33+
bool isInitialized = false;
34+
35+
void setup() {
36+
Serial.begin(115200);
37+
myHub.init(); // initalize the remote instance and try to connect
38+
}
39+
40+
41+
// main loop
42+
void loop() {
43+
44+
// connect flow
45+
if (myHub.isConnecting()) {
46+
myHub.connectHub();
47+
if (myHub.isConnected()) {
48+
Serial.println("Connected to HUB");
49+
myHub.setLedColor(GREEN);
50+
myRemote.init(); // after connecting the remote, try to connect the hub
51+
} else {
52+
Serial.println("Failed to connect to Remote");
53+
}
54+
}
55+
56+
if (myRemote.isConnecting()) {
57+
myRemote.connectHub();
58+
if (myRemote.isConnected()) {
59+
Serial.println("Connected to Remote");
60+
} else {
61+
Serial.println("Failed to connect to Hub");
62+
}
63+
}
64+
65+
if (myRemote.isConnected() && myHub.isConnected() && !isInitialized) {
66+
Serial.println("Is initialized");
67+
isInitialized = true;
68+
// both activations are needed to get status updates
69+
myRemote.activateButtonReports();
70+
myRemote.activatePortDevice(_portLeft, 55);
71+
myRemote.activatePortDevice(_portRight, 55);
72+
myRemote.setLedColor(WHITE);
73+
myHub.setLedColor(WHITE);
74+
}
75+
76+
// if connected we can control vernie with the remote
77+
if (isInitialized) {
78+
79+
if (myRemote.isLeftRemoteUpButtonPressed()) {
80+
myHub.moveForward(1);
81+
Serial.println("MoveForward");
82+
} else if (myRemote.isLeftRemoteDownButtonPressed()) {
83+
myHub.moveBack(1);
84+
Serial.println("MoveBack");
85+
} else if (myRemote.isRightRemoteUpButtonPressed()) {
86+
myHub.rotateRight(30);
87+
Serial.println("RotateRight");
88+
} else if (myRemote.isRightRemoteDownButtonPressed()) {
89+
myHub.rotateLeft(30);
90+
Serial.println("RotateLeft");
91+
} else if (myRemote.isRightRemoteStopButtonPressed()) {
92+
Serial.println("Fire...");
93+
myHub.setMotorSpeedForDegrees(_portD, -80, 20);
94+
delay(500);
95+
myHub.setMotorSpeedForDegrees(_portD, 100, 180);
96+
delay(800);
97+
myHub.setMotorSpeedForDegrees(_portD, -80, 120);
98+
}
99+
100+
101+
delay(100);
102+
103+
}
104+
105+
} // End of loop

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Legoino
2-
version=0.5.0
2+
version=0.5.1
33
author=Cornelius Munz <[email protected]>
44
maintainer=Cornelius Munz <[email protected]>
55
sentence=Library for controlling Powered UP and Boost controllers

src/Lpf2Hub.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ void Lpf2Hub::activatePortDevice(byte portNumber, byte deviceType)
177177
LOGLINE(mode, HEX);
178178
byte activatePortDeviceMessage[8] = {0x41, portNumber, mode, 0x01, 0x00, 0x00, 0x00, 0x01};
179179
WriteValue(activatePortDeviceMessage, 8);
180+
//register device
181+
Device newDevice = {portNumber, deviceType};
182+
connectedDevices[numberOfConnectedDevices] = newDevice;
183+
numberOfConnectedDevices++;
180184
}
181185

182186
void Lpf2Hub::deactivatePortDevice(byte portNumber)
@@ -190,6 +194,20 @@ void Lpf2Hub::deactivatePortDevice(byte portNumber, byte deviceType)
190194
byte mode = getModeForDeviceType(deviceType);
191195
byte deactivatePortDeviceMessage[8] = {0x41, portNumber, mode, 0x01, 0x00, 0x00, 0x00, 0x00};
192196
WriteValue(deactivatePortDeviceMessage, 8);
197+
//unregister device
198+
bool hasReachedRemovedIndex = false;
199+
for (int i = 0; i < numberOfConnectedDevices; i++)
200+
{
201+
if (hasReachedRemovedIndex)
202+
{
203+
connectedDevices[i - 1] = connectedDevices[i];
204+
}
205+
if (!hasReachedRemovedIndex && connectedDevices[i].PortNumber == portNumber)
206+
{
207+
hasReachedRemovedIndex = true;
208+
}
209+
}
210+
numberOfConnectedDevices--;
193211
}
194212

195213
void Lpf2Hub::activateButtonReports()
@@ -323,27 +341,10 @@ void Lpf2Hub::parsePortMessage(uint8_t *pData)
323341
{
324342
LOG(" is connected with device ");
325343
LOGLINE(pData[5], DEC);
326-
Device newDevice = {port, pData[5]};
327-
connectedDevices[numberOfConnectedDevices] = newDevice;
328-
numberOfConnectedDevices++;
329344
}
330345
else
331346
{
332347
LOGLINE(" is disconnected");
333-
bool hasReachedRemovedIndex = false;
334-
for (int i = 0; i < numberOfConnectedDevices; i++)
335-
{
336-
if (hasReachedRemovedIndex)
337-
{
338-
connectedDevices[i - 1] = connectedDevices[i];
339-
}
340-
if (!hasReachedRemovedIndex && connectedDevices[i].PortNumber == port)
341-
{
342-
hasReachedRemovedIndex = true;
343-
}
344-
}
345-
346-
numberOfConnectedDevices--;
347348
}
348349
}
349350

0 commit comments

Comments
 (0)