Skip to content

Commit 25d6387

Browse files
authored
Merge pull request #38 from arduino/ble_bridge
Add BLE bridge functionality to host board
2 parents 6e8d468 + 73f097f commit 25d6387

File tree

8 files changed

+406
-14
lines changed

8 files changed

+406
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
#include "thingProperties.h"
4+
5+
#include "Arduino_BHY2Host.h"
6+
Sensor tempSensor(SENSOR_ID_TEMP);
7+
8+
int printTime = 0;
9+
10+
#define DEBUG false
11+
12+
void setup() {
13+
// Initialize serial and wait for port to open:
14+
Serial.begin(115200);
15+
while(!Serial) {}
16+
#ifndef TARGET_PORTENTA_H7
17+
Serial.println("Unsupported board!");
18+
while(1);
19+
#endif
20+
21+
#if DEBUG
22+
BHY2.debug(Serial);
23+
#endif
24+
25+
// Defined in thingProperties.h
26+
initProperties();
27+
28+
// Connect to Arduino IoT Cloud
29+
if (!ArduinoCloud.begin(ArduinoIoTPreferredConnection)) {
30+
Serial.println("ArduinoCloud.begin FAILED!");
31+
}
32+
33+
setDebugMessageLevel(2);
34+
ArduinoCloud.printDebugInfo();
35+
36+
while(!ArduinoCloud.connected()) {
37+
ArduinoCloud.update();
38+
delay(10);
39+
}
40+
41+
Serial.println("Configuring Nicla...");
42+
43+
while (!BHY2Host.begin(false, NICLA_VIA_BLE)) {}
44+
Serial.println("NICLA device found!");
45+
46+
tempSensor.begin();
47+
48+
printTime = millis();
49+
}
50+
51+
void loop() {
52+
BHY2Host.update(100);
53+
54+
if (millis() - printTime > 1000) {
55+
printTime = millis();
56+
seconds = millis()/1000;
57+
temperature = tempSensor.value();
58+
59+
Serial.print("Temp: ");
60+
Serial.println(temperature, 3);
61+
62+
}
63+
64+
ArduinoCloud.update();
65+
66+
}
67+
68+
void onTemperatureChange() {
69+
}
70+
71+
void onSecondsChange() {
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "arduino_secrets.h"
2+
3+
const char THING_ID[] = "";
4+
5+
const char SSID[] = SECRET_SSID; // Network SSID (name)
6+
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
7+
8+
void onTemperatureChange();
9+
void onSecondsChange();
10+
11+
float temperature;
12+
int seconds;
13+
14+
void initProperties(){
15+
16+
ArduinoCloud.setThingId(THING_ID);
17+
ArduinoCloud.addProperty(temperature, READWRITE, ON_CHANGE, onTemperatureChange);
18+
ArduinoCloud.addProperty(seconds, READWRITE, ON_CHANGE, onSecondsChange);
19+
20+
}
21+
22+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

src/Arduino_BHY2Host.cpp

+57-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include "sensors/SensorManager.h"
55

66
Arduino_BHY2Host::Arduino_BHY2Host() :
7-
_passthrough(false)
7+
_passthrough(false),
8+
_wiring(NICLA_VIA_ESLOV),
9+
_debug(NULL)
810
{
911
}
1012

@@ -15,6 +17,20 @@ Arduino_BHY2Host::~Arduino_BHY2Host()
1517
bool Arduino_BHY2Host::begin(bool passthrough, NiclaWiring niclaConnection)
1618
{
1719
_passthrough = passthrough;
20+
_wiring = niclaConnection;
21+
if (niclaConnection == NICLA_VIA_BLE) {
22+
#ifdef __BHY2_HOST_BLE_SUPPORTED__
23+
if (_debug) {
24+
_debug->println("NICLA_VIA_BLE selected");
25+
}
26+
return bleHandler.begin();
27+
#else
28+
if (_debug) {
29+
_debug->println("Unsupported board!");
30+
}
31+
return false;
32+
#endif
33+
}
1834
if (niclaConnection == NICLA_AS_SHIELD) {
1935
eslovHandler.niclaAsShield();
2036
}
@@ -23,21 +39,38 @@ bool Arduino_BHY2Host::begin(bool passthrough, NiclaWiring niclaConnection)
2339

2440
void Arduino_BHY2Host::update()
2541
{
26-
if (_passthrough){
27-
eslovHandler.update();
42+
if (_wiring == NICLA_VIA_BLE) {
43+
#ifdef __BHY2_HOST_BLE_SUPPORTED__
44+
bleHandler.update();
45+
#endif
2846
} else {
29-
while (availableSensorData() > 0) {
30-
SensorDataPacket data;
31-
readSensorData(data);
32-
sensorManager.process(data);
47+
if (_passthrough){
48+
eslovHandler.update();
49+
} else {
50+
while (availableSensorData() > 0) {
51+
SensorDataPacket data;
52+
readSensorData(data);
53+
sensorManager.process(data);
54+
}
3355
}
3456
}
57+
}
3558

59+
void Arduino_BHY2Host::update(unsigned long ms)
60+
{
61+
update();
62+
delay(ms);
3663
}
3764

3865
void Arduino_BHY2Host::configureSensor(SensorConfigurationPacket& config)
3966
{
40-
eslovHandler.writeConfigPacket(config);
67+
if (_wiring == NICLA_VIA_BLE) {
68+
#ifdef __BHY2_HOST_BLE_SUPPORTED__
69+
bleHandler.writeConfigPacket(config);
70+
#endif
71+
} else {
72+
eslovHandler.writeConfigPacket(config);
73+
}
4174
}
4275

4376
void Arduino_BHY2Host::configureSensor(uint8_t sensorId, float sampleRate, uint32_t latency)
@@ -46,7 +79,13 @@ void Arduino_BHY2Host::configureSensor(uint8_t sensorId, float sampleRate, uint3
4679
config.sensorId = sensorId;
4780
config.sampleRate = sampleRate;
4881
config.latency = latency;
49-
eslovHandler.writeConfigPacket(config);
82+
if (_wiring == NICLA_VIA_BLE) {
83+
#ifdef __BHY2_HOST_BLE_SUPPORTED__
84+
bleHandler.writeConfigPacket(config);
85+
#endif
86+
} else {
87+
eslovHandler.writeConfigPacket(config);
88+
}
5089
}
5190

5291
uint8_t Arduino_BHY2Host::requestAck()
@@ -79,9 +118,18 @@ void Arduino_BHY2Host::parse(SensorDataPacket& data, DataOrientation& vector, fl
79118
DataParser::parseEuler(data, vector, scaleFactor);
80119
}
81120

121+
NiclaWiring Arduino_BHY2Host::getNiclaConnection()
122+
{
123+
return _wiring;
124+
}
125+
82126
void Arduino_BHY2Host::debug(Stream &stream)
83127
{
128+
_debug = &stream;
84129
eslovHandler.debug(stream);
130+
#ifdef __BHY2_HOST_BLE_SUPPORTED__
131+
bleHandler.debug(stream);
132+
#endif
85133
}
86134

87135
Arduino_BHY2Host BHY2Host;

src/Arduino_BHY2Host.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@
1414

1515
#include "sensors/SensorID.h"
1616

17+
#if defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_SAMD_MKRWIFI1010)
18+
#define __BHY2_HOST_BLE_SUPPORTED__
19+
#include "BLEHandler.h"
20+
#endif
21+
1722
enum NiclaWiring {
1823
NICLA_VIA_ESLOV = 0,
19-
NICLA_AS_SHIELD
24+
NICLA_AS_SHIELD,
25+
NICLA_VIA_BLE
2026
};
2127

2228
class Arduino_BHY2Host {
@@ -27,6 +33,7 @@ class Arduino_BHY2Host {
2733
// Necessary API. Update function should be continuously polled if PASSTHORUGH is ENABLED
2834
bool begin(bool passthrough = false, NiclaWiring niclaConnection = NICLA_VIA_ESLOV);
2935
void update();
36+
void update(unsigned long ms); // Update and then sleep
3037

3138
// Functions for controlling the BHY when PASSTHROUGH is DISABLED
3239
void configureSensor(SensorConfigurationPacket& config);
@@ -39,10 +46,14 @@ class Arduino_BHY2Host {
3946
void parse(SensorDataPacket& data, DataOrientation& vector);
4047
void parse(SensorDataPacket& data, DataOrientation& vector, float scaleFactor);
4148

49+
NiclaWiring getNiclaConnection();
50+
4251
void debug(Stream &stream);
4352

4453
private:
4554
bool _passthrough;
55+
NiclaWiring _wiring;
56+
Stream *_debug;
4657
};
4758

4859
extern Arduino_BHY2Host BHY2Host;

0 commit comments

Comments
 (0)