Skip to content

Commit 9c41887

Browse files
committed
Merge branch 'develop'
2 parents 147a1a9 + ec017df commit 9c41887

File tree

7 files changed

+103
-3
lines changed

7 files changed

+103
-3
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/legoinochat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2+
![Release Version](https://img.shields.io/github/release/corneliusmunz/legoino.svg?style=plastic)
23
# Legoino
34

45
Arduino Library for controlling Powered UP and Boost controllers
@@ -32,7 +33,7 @@ You can find different Examples in the "examples" folder. You can select the exa
3233
* **TrainHub.ino:** Example for a PowererdUp Hub to set the speed of a train model. http://www.youtube.com/watch?v=o1hgZQz3go4
3334
* **TrainColor.ino:** Example of PoweredUp Hub combined with color sensor to control the speed of the train dependent on the detected color. https://youtu.be/GZ0fqe3-Bhw
3435
* **PoweredUpRemoteAutoDetection.ino:** Example of connection of PoweredUp and PoweredUpRemote where the device type is fetched automatically and the order in which you switched on the hubs is no longer relevant.
35-
36+
* **ControlPlusHub.ino:** Example of connection of ControlPlusHub (TechnicHub) where a Tacho Motor on Port D is controlled.
3637

3738
# Setup and Usage
3839
Just install the Library via the Arduino Library Manager.
@@ -61,6 +62,16 @@ represented by a hex string of the format: ```00:00:00:00:00:00```
6162
myBoostHub.init("90:84:2b:03:19:7f");
6263
```
6364

65+
If you want to change the BLE scan duration to detect a new Hub you can use the init function with a time parameter in seconds. The default value is 1 second. In former versions of the library it was 30 seconds which sometimes led to errors during the connection process.
66+
```c
67+
myBoostHub.init(2); // 2 seconds scan duration
68+
```
69+
70+
Alternatively you can set the scan duration and the specific address of a hub in the init function with two parameters.
71+
```c
72+
myBoostHub.init("90:84:2b:03:19:7f", 2); // connect to hub with address and a scan duration of 2 seconds
73+
```
74+
6475
In the main ```loop``` just add the following connection flow
6576
```c
6677
if (myBoostHub.isConnecting()) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* A Legoino example to control a control plus hub
3+
* with a Motor connected on Port D
4+
*
5+
* (c) Copyright 2020 - Cornelius Munz
6+
* Released under MIT License
7+
*
8+
*/
9+
10+
#include "ControlPlusHub.h"
11+
12+
// create a hub instance
13+
ControlPlusHub myHub;
14+
ControlPlusHub::Port _port = ControlPlusHub::Port::D;
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
}
19+
20+
21+
// main loop
22+
void loop() {
23+
24+
if (!myHub.isConnected() && !myHub.isConnecting())
25+
{
26+
myHub.init();
27+
}
28+
29+
// connect flow. Search for BLE services and try to connect if the uuid of the hub is found
30+
if (myHub.isConnecting()) {
31+
myHub.connectHub();
32+
if (myHub.isConnected()) {
33+
Serial.println("Connected to HUB");
34+
} else {
35+
Serial.println("Failed to connect to HUB");
36+
}
37+
}
38+
39+
// if connected, you can set the name of the hub, the led color and shut it down
40+
if (myHub.isConnected()) {
41+
42+
char hubName[] = "myControlPlusHub";
43+
myHub.setHubName(hubName);
44+
45+
myHub.setLedColor(GREEN);
46+
delay(1000);
47+
myHub.setLedColor(RED);
48+
delay(1000);
49+
myHub.setMotorSpeed(_port, 35);
50+
delay(1000);
51+
myHub.stopMotor(_port);
52+
delay(1000);
53+
myHub.setMotorSpeed(_port, -35);
54+
delay(1000);
55+
myHub.stopMotor(_port);
56+
delay(1000);
57+
58+
} else {
59+
Serial.println("ControlPlus hub is disconnected");
60+
}
61+
62+
} // 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.7.1
2+
version=0.8.0
33
author=Cornelius Munz <cornelius.munz@gmx.de>
44
maintainer=Cornelius Munz <cornelius.munz@gmx.de>
55
sentence=Library for controlling PoweredUp, Boost, Corntrol+ controllers/hubs

src/ControlPlusHub.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010

1111
ControlPlusHub::ControlPlusHub(){};
1212

13+
/**
14+
* @brief Stop the motor on a defined port.
15+
* @param [in] port Port of the Hub on which the motor will be stopped (A, B, AB, C, D)
16+
*/
17+
void ControlPlusHub::stopMotor(Port port)
18+
{
19+
setMotorSpeed(port, 0);
20+
}
21+
1322
/**
1423
* @brief Set the motor speed on a defined port.
1524
* @param [in] port Port of the Hub on which the speed of the motor will set (A, B, AB)

src/ControlPlusHub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ControlPlusHub : public Lpf2Hub
3737
//Basic Motor methods
3838
void setAccelerationProfile(Port port, int16_t time, int8_t profileNumber);
3939
void setDecelerationProfile(Port port, int16_t time, int8_t profileNumber);
40+
void stopMotor(Port port);
4041
void setMotorSpeed(Port port, int speed);
4142
void setMotorSpeedForTime(Port port, int speed, int16_t time);
4243
void setMotorSpeedForDegrees(Port port, int speed, int32_t degrees);

src/Lpf2Hub.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ void Lpf2Hub::init()
698698
pBLEScan->setAdvertisedDeviceCallbacks(new Lpf2HubAdvertisedDeviceCallbacks(this));
699699

700700
pBLEScan->setActiveScan(true);
701-
pBLEScan->start(30);
701+
pBLEScan->start(_scanDuration);
702702
}
703703

704704
/**
@@ -711,6 +711,19 @@ void Lpf2Hub::init(std::string deviceAddress)
711711
init();
712712
}
713713

714+
void Lpf2Hub::init(uint32_t scanDuration)
715+
{
716+
_scanDuration = scanDuration;
717+
init();
718+
}
719+
720+
void Lpf2Hub::init(std::string deviceAddress, uint32_t scanDuration)
721+
{
722+
_requestedDeviceAddress = new BLEAddress(deviceAddress);
723+
_scanDuration = scanDuration;
724+
init();
725+
}
726+
714727
/**
715728
* @brief Register the connected devices to map the ports to the device types
716729
* @param [in] connectedDevices[] Array to a device struct of all connected devices

src/Lpf2Hub.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class Lpf2Hub
4141
public:
4242
Lpf2Hub();
4343
void init();
44+
void init(uint32_t scanDuration);
4445
void init(std::string deviceAddress);
46+
void init(std::string deviceAddress, uint32_t scanDuration);
4547
void initConnectedDevices(Device devices[], byte deviceNumbers);
4648
bool connectHub();
4749
bool isConnected();
@@ -128,6 +130,8 @@ class Lpf2Hub
128130
Device connectedDevices[13];
129131
int numberOfConnectedDevices = 0;
130132

133+
//BLE settings
134+
uint32_t _scanDuration = 1;
131135
// Hub information values
132136
int _lpf2HubRssi;
133137
uint8_t _lpf2HubBatteryLevel;

0 commit comments

Comments
 (0)