Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliusmunz committed Dec 22, 2020
2 parents 2e39088 + e3e0687 commit 434b74f
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ You can find different Examples in the "examples" folder. You can select the exa
* **HubEmulation.ino:** Example of an emulated PoweredUp Hub two port hub (train hub) which could receive signals from the PoweredUp app and will send out the signals as IR commands to a Powerfunction remote receiver. https://www.youtube.com/watch?v=RTNexxT4-yQ
* **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.
* **ControlPlusHub.ino:** Example of connection of ControlPlusHub (TechnicHub) where a Tacho Motor on Port D is controlled.
* **Mario.ino** Example of connection to a Mario Hub to read in sensor notifications about the Barcode/Tag sensor, Color sensor, Pants sensor and Gesture sensor.


# Setup and Usage
Expand Down Expand Up @@ -392,6 +393,16 @@ myBoostHub.moveArcRight(90) // move with an arc for 90 degrees to the right
myBoostHub.moveArc(270) // move with an arc for 270 degrees to the right (positive angles means right, negative means left)
myBoostHub.moveArc(-90) // move with an arc for 90 degrees to the left (positive angles means right, negative means left)
```

## Mario Hub (#71360)
With legoino it is possible to connect to the Mario Hub and read out sensor values from the
* Pant Sensor
* Color and Tag/Barcode Sensor
* Gesture Sensor
* Voltage Sensor

You can do this via the standard "sensor notification" procedure. Just have a look into the **Mario.ino** example sketch.

# Connection to more than 3 hubs
It is possible to connect to up to 9 hubs in parallel with an common ESP32 board. To enable the connection to more than 3 hubs, you have to change a single configuration of the NimBLE library. Just open the ```nimconfig.h``` file located in your arduino library folder in the directory ```NimBLE-Arduino/src```. Open the file with an editor and change the following settings to your demands
```
Expand Down Expand Up @@ -431,11 +442,9 @@ Thanks to [@wmarkow](https://github.com/wmarkow) for his detailed input about hu
# Remarks
Prerequisite of that library is the NimBLE-Arduino library (https://github.com/h2zero/NimBLE-Arduino) with at least version 1.0.1 Otherwise the notifcations of changed charachteristic values will not work. So just install as a prerequesite the version 1.0.1 of that library via the Arduino Library manager or the platform.io library manager (https://www.arduinolibraries.info/libraries/nim-ble-arduino)

Up to now the Library is only teseted for a Powered Up Train controllers, Boost controllers, Control+ Hubs, PoweredUp Remote and Duplo Train Hub. You can connect to your HUB, set the LED color, set the Hub name, control the motors (speed, port, movements) and shut down the HUB via a Arduino command. You also are able to read in hub device infos (rssi, battery level, tilt) and sensor values (color, distance, rotation angle).
Up to now the Library is only teseted for a Powered Up Train controllers, Boost controllers, Control+ Hubs, PoweredUp Remote, Duplo Train Hub and Mario Hub. You can connect to your HUB, set the LED color, set the Hub name, control the motors (speed, port, movements) and shut down the HUB via a Arduino command. You also are able to read in hub device infos (rssi, battery level, tilt) and sensor values (color, distance, rotation angle).


# ToDo
* Virtual Ports
* HW Families
* Mario Hub

125 changes: 125 additions & 0 deletions examples/Mario/Mario.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
*
* A basic example which will connect to a Mario hub and request updates for
* the Pant, Color/Barcode and Gesture sensor.
*
* (c) Copyright 2020 - Cornelius Munz
* Released under MIT License
*
*/

#include "Lpf2Hub.h"

DeviceType pantSensor = DeviceType::MARIO_HUB_PANT_SENSOR;
DeviceType gestureSensor = DeviceType::MARIO_HUB_GESTURE_SENSOR;
DeviceType barcodeSensor = DeviceType::MARIO_HUB_BARCODE_SENSOR;

bool isPantSensorInitialized = false;
bool isGestureSensorInitialized = false;
bool isBarcodeSensorInitialized = false;

// create a hub instance
Lpf2Hub myHub;

void MarioCallback(void *hub, byte portNumber, DeviceType deviceType, uint8_t *pData)
{
Lpf2Hub *myHub = (Lpf2Hub *)hub;

Serial.print("sensorMessage callback for port: ");
Serial.println(portNumber, HEX);
if (deviceType == DeviceType::MARIO_HUB_PANT_SENSOR)
{
MarioPant pant = myHub->parseMarioPant(pData);
Serial.print("Mario Pant: ");
Serial.println((byte)pant, DEC);
}
if (deviceType == DeviceType::MARIO_HUB_BARCODE_SENSOR)
{
MarioBarcode barcode = myHub->parseMarioBarcode(pData);
Serial.print("Mario Barcode: ");
Serial.println((byte)barcode, HEX);
MarioColor color = myHub->parseMarioColor(pData);
Serial.print("Mario Color: ");
Serial.println((byte)color, HEX);
}
if (deviceType == DeviceType::MARIO_HUB_GESTURE_SENSOR)
{
MarioGesture gesture = myHub->parseMarioGesture(pData);
if (gesture != MarioGesture::NONE) // filter out NONE values
{
Serial.print("Mario Gesture: ");
Serial.println((int)gesture, HEX);
}
}
}

void setup()
{
Serial.begin(115200);
myHub.init(); // initalize the MoveHub instance
}

// main loop
void loop()
{

// connect flow. Search for BLE services and try to connect
if (myHub.isConnecting())
{
myHub.connectHub();
if (myHub.isConnected())
{
Serial.println("Connected to HUB");
}
else
{
Serial.println("Failed to connect to HUB");
}
}

if (myHub.isConnected() && !isGestureSensorInitialized)
{
delay(200);
Serial.print("check ports... if needed sensor is already connected: ");
byte portForDevice = myHub.getPortForDeviceType((byte)gestureSensor);
Serial.println(portForDevice, DEC);
if (portForDevice != 255)
{
Serial.println("activatePortDevice");
myHub.activatePortDevice(portForDevice, MarioCallback);
delay(200);
isGestureSensorInitialized = true;
};
}

if (myHub.isConnected() && !isPantSensorInitialized)
{
delay(200);
Serial.print("check ports... if needed sensor is already connected: ");
byte portForDevice = myHub.getPortForDeviceType((byte)pantSensor);
Serial.println(portForDevice, DEC);
if (portForDevice != 255)
{
Serial.println("activatePortDevice");
myHub.activatePortDevice(portForDevice, MarioCallback);
delay(200);
isPantSensorInitialized = true;
};
}

if (myHub.isConnected() && !isBarcodeSensorInitialized)
{
delay(200);
Serial.print("check ports... if needed sensor is already connected: ");
byte portForDevice = myHub.getPortForDeviceType((byte)barcodeSensor);
Serial.println(portForDevice, DEC);
if (portForDevice != 255)
{
Serial.println("activatePortDevice");
myHub.activatePortDevice(portForDevice, MarioCallback);
delay(200);
isBarcodeSensorInitialized = true;
};
}

} // End of loop
4 changes: 4 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ parseBoostTiltSensor KEYWORD2
parseControlPlusHubTiltSensorX KEYWORD2
parseControlPlusHubTiltSensorY KEYWORD2
parseControlPlusHubTiltSensorZ KEYWORD2
parseMarioPant KEYWORD2
parseMarioGesture KEYWORD2
parseMarioBarcode KEYWORD2
parseMarioColor KEYWORD2
parseRemoteButton KEYWORD2
parsePortAction KEYWORD2
parseSystemTypeId KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Legoino
version=1.0.4
version=1.1.0
author=Cornelius Munz <cornelius.munz@gmx.de>
maintainer=Cornelius Munz <cornelius.munz@gmx.de>
sentence=Library for controlling PoweredUp, Boost, Corntrol+ controllers/hubs
Expand Down
72 changes: 72 additions & 0 deletions src/Lpf2Hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class Lpf2HubAdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
case CONTROL_PLUS_HUB_ID:
_lpf2Hub->_hubType = HubType::CONTROL_PLUS_HUB;
break;
case MARIO_HUB_ID:
_lpf2Hub->_hubType = HubType::MARIO_HUB;
break;
default:
_lpf2Hub->_hubType = HubType::UNKNOWNHUB;
break;
Expand Down Expand Up @@ -305,6 +308,54 @@ void Lpf2Hub::parsePortMessage(uint8_t *pData)
}
}

/**
* @brief Parse Mario pant sensor
* @param [in] pData The pointer to the received data
* @return Pant type
*/
MarioPant Lpf2Hub::parseMarioPant(uint8_t *pData)
{
int value = LegoinoCommon::ReadInt8(pData, 4);
log_d("Mario Pant: %d", value);
return (MarioPant)value;
}

/**
* @brief Parse Mario gesture sensor
* @param [in] pData The pointer to the received data
* @return Gesture
*/
MarioGesture Lpf2Hub::parseMarioGesture(uint8_t *pData)
{
int value = LegoinoCommon::ReadInt16LE(pData, 4);
log_d("Mario Gesture: %d", value);
return (MarioGesture)value;
}

/**
* @brief Parse Mario barcode sensor
* @param [in] pData The pointer to the received data
* @return MarioBarcode
*/
MarioBarcode Lpf2Hub::parseMarioBarcode(uint8_t *pData)
{
int value = LegoinoCommon::ReadInt16LE(pData, 4);
log_d("Mario Barcode: %d", value);
return MarioBarcode(value);
}

/**
* @brief Parse Mario color sensor
* @param [in] pData The pointer to the received data
* @return MarioColor
*/
MarioColor Lpf2Hub::parseMarioColor(uint8_t *pData)
{
int value = LegoinoCommon::ReadInt16LE(pData, 6);
log_d("Mario Color: %d", value);
return (MarioColor)value;
}

/**
* @brief Parse boost hub tilt sensor message (x axis)
* @param [in] pData The pointer to the received data
Expand Down Expand Up @@ -597,6 +648,8 @@ byte Lpf2Hub::getModeForDeviceType(byte deviceType)
return (byte)HubPropertyOperation::ENABLE_UPDATES_DOWNSTREAM;
case (byte)DeviceType::TECHNIC_XLARGE_LINEAR_MOTOR:
return (byte)HubPropertyOperation::ENABLE_UPDATES_DOWNSTREAM;
case (byte)DeviceType::MARIO_HUB_GESTURE_SENSOR:
return 0x01;
default:
return 0x00;
}
Expand Down Expand Up @@ -673,6 +726,25 @@ void Lpf2Hub::parseSensorMessage(uint8_t *pData)
parseRemoteButton(pData);
return;
}
else if (deviceType == (byte)DeviceType::MARIO_HUB_GESTURE_SENSOR)
{
int port = pData[3];
parseMarioGesture(pData);
return;
}
else if (deviceType == (byte)DeviceType::MARIO_HUB_BARCODE_SENSOR)
{
int port = pData[3];
parseMarioBarcode(pData);
parseMarioColor(pData);
return;
}
else if (deviceType == (byte)DeviceType::MARIO_HUB_PANT_SENSOR)
{
int port = pData[3];
parseMarioPant(pData);
return;
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Lpf2Hub.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class Lpf2Hub
int parseControlPlusHubTiltSensorX(uint8_t *pData);
int parseControlPlusHubTiltSensorY(uint8_t *pData);
int parseControlPlusHubTiltSensorZ(uint8_t *pData);
MarioPant parseMarioPant(uint8_t *pData);
MarioGesture parseMarioGesture(uint8_t *pData);
MarioBarcode parseMarioBarcode(uint8_t *pData);
MarioColor parseMarioColor(uint8_t *pData);
ButtonState parseRemoteButton(uint8_t *pData);
void parsePortAction(uint8_t *pData);
uint8_t parseSystemTypeId(uint8_t *pData);
Expand Down
Loading

0 comments on commit 434b74f

Please sign in to comment.