-
Notifications
You must be signed in to change notification settings - Fork 38
/
Mario.ino
133 lines (119 loc) · 3.54 KB
/
Mario.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
/**
*
* 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;
bool isVolumeSet = 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;
};
}
if (myHub.isConnected() && !isVolumeSet)
{
Serial.println("set volume to 50%");
myHub.setMarioVolume(0x32);
isVolumeSet = true;
}
} // End of loop