Skip to content

Commit d6a4d36

Browse files
committed
Arduino BLE new library
1. Add new library 2. Resolve the connect request was not scheduled issue Note: need change the compiler parameter in platform.txt. "compiler.c.flags=-c -std=gnu11 -mcpu=quarkse_em -mlittle-endian -g -Os ..." to "compiler.c.flags=-c -std=gnu11 -mcpu=quarkse_em -mlittle-endian -g -O0" "compiler.cpp.flags=-c -mcpu=quarkse_em -mlittle-endian -g -Os ..." to "compiler.cpp.flags=-c -mcpu=quarkse_em -mlittle-endian -g -O0 ..." 3. Add BLEPeripheral library back compatible features 4. Fix IMU build issue 5. Fix the memset crash issue i. The FIRQ doesn't save the LP_COUNTER register. ii. Update the code to save LP related register
1 parent 249fb3b commit d6a4d36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+11811
-177
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <ArduinoBLE.h>
2+
3+
/*
4+
This sketch example works with IMUBleNotification.ino
5+
6+
IMUBleNotification.ino will send notification to this central sketch.
7+
This sketch will receive the notifications and output the received data in the serial monitor.
8+
It also illustrates using a non-typed characteristic.
9+
Set the baud rate to 115200 on the serial monitor to accomodate the speed of constant data updates from IMU subsystem.
10+
*/
11+
12+
#define LED_PIN 13
13+
#define MAX_IMU_RECORD 1
14+
15+
// define a structure that will serve as buffer for holding IMU data
16+
17+
typedef struct {
18+
int index;
19+
unsigned int slot[3];
20+
} imuFrameType;
21+
22+
imuFrameType imuBuf[MAX_IMU_RECORD];
23+
24+
void setup()
25+
{
26+
// This is set to higher baud rate because accelerometer data changes very quickly
27+
Serial.begin(9600); // initialize serial communication
28+
while (!Serial);
29+
pinMode(LED_PIN, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
30+
31+
/* Now activate the BLE device. It will start continuously transmitting BLE
32+
advertising packets and will be visible to remote BLE central devices
33+
until it receives a new connection */
34+
BLE.begin();
35+
Serial.println(BLE.address());
36+
37+
BLE.scanForName("Imu");
38+
}
39+
40+
41+
void loop()
42+
{
43+
BLEDevice peripheral = BLE.available();
44+
//pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
45+
if (peripheral)
46+
{
47+
Serial.println(peripheral.address());
48+
BLE.stopScan();
49+
delay (1000);
50+
// central connected to peripheral
51+
controlImu(peripheral);
52+
delay (4000);
53+
BLE.scanForName("Imu");
54+
}
55+
}
56+
57+
void controlImu(BLEDevice peripheral)
58+
{
59+
static bool discovered = false;
60+
// connect to the peripheral
61+
Serial.print("Connecting ... ");
62+
Serial.println(peripheral.address());
63+
64+
if (peripheral.connect())
65+
{
66+
Serial.print("Connected: ");
67+
Serial.println(peripheral.address());
68+
}
69+
else
70+
{
71+
Serial.println("Failed to connect!");
72+
return;
73+
}
74+
75+
peripheral.discoverAttributes();
76+
77+
BLECharacteristic bleImuChar = peripheral.characteristic("F7580003-153E-D4F6-F26D-43D8D98EEB13");
78+
79+
if (!bleImuChar)
80+
{
81+
peripheral.disconnect();
82+
Serial.println("Peripheral does not have IMU characteristic!");
83+
delay(5000);
84+
return;
85+
}
86+
bleImuChar.subscribe();
87+
88+
89+
discovered = false;
90+
while (peripheral.connected())
91+
{
92+
if (bleImuChar.valueUpdated())
93+
{
94+
const unsigned char *cvalue = bleImuChar.value();
95+
const imuFrameType *value = (const imuFrameType *)cvalue;
96+
Serial.print("\r\nCharacteristic event, written: ");
97+
Serial.print(value->index);
98+
Serial.print("\t");
99+
Serial.print(value->slot[0]);
100+
Serial.print("\t");
101+
Serial.print(value->slot[1]);
102+
Serial.print("\t");
103+
Serial.println(value->slot[2]);
104+
}
105+
}
106+
Serial.print("Disconnected");
107+
Serial.println(peripheral.address());
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Arduino BLE Central LED Control example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
21+
#include <ArduinoBLE.h>
22+
23+
// variables for button
24+
const int buttonPin = 2;
25+
int oldButtonState = LOW;
26+
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
31+
// configure the button pin as input
32+
pinMode(buttonPin, INPUT);
33+
34+
// initialize the BLE hardware
35+
BLE.begin();
36+
37+
Serial.println("BLE Central - LED control");
38+
39+
// start scanning for peripherals
40+
BLE.scan();
41+
}
42+
43+
void loop() {
44+
// check if a peripheral has been discovered
45+
BLEDevice peripheral = BLE.available();
46+
47+
if (peripheral) {
48+
// discovered a peripheral, print out address, local name, and advertised service
49+
Serial.print("Found ");
50+
Serial.print(peripheral.address());
51+
Serial.print(" '");
52+
Serial.print(peripheral.localName());
53+
Serial.print("' ");
54+
Serial.print(peripheral.advertisedServiceUuid());
55+
Serial.println();
56+
57+
// see if peripheral is advertising the LED service
58+
if (peripheral.advertisedServiceUuid() == "19b10000-e8f2-537e-4f6c-d104768a1214") {
59+
// stop scanning
60+
BLE.stopScan();
61+
62+
controlLed(peripheral);
63+
64+
// peripheral disconnected, start scanning again
65+
BLE.scan();
66+
}
67+
}
68+
}
69+
70+
void controlLed(BLEDevice peripheral) {
71+
// connect to the peripheral
72+
Serial.println("Connecting ...");
73+
74+
if (peripheral.connect()) {
75+
Serial.println("Connected");
76+
} else {
77+
Serial.println("Failed to connect!");
78+
return;
79+
}
80+
81+
// discover peripheral attributes
82+
Serial.println("Discovering attributes ...");
83+
if (peripheral.discoverAttributes()) {
84+
Serial.println("Attributes discovered");
85+
} else {
86+
Serial.println("Attribute discovery failed!");
87+
peripheral.disconnect();
88+
return;
89+
}
90+
91+
// retrieve the LED characteristic
92+
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
93+
94+
if (!ledCharacteristic) {
95+
Serial.println("Peripheral does not have LED characteristic!");
96+
peripheral.disconnect();
97+
return;
98+
} else if (!ledCharacteristic.canWrite()) {
99+
Serial.println("Peripheral does not have a writable LED characteristic!");
100+
peripheral.disconnect();
101+
return;
102+
}
103+
104+
while (peripheral.connected()) {
105+
// while the peripheral is connection
106+
107+
// read the button pin
108+
int buttonState = digitalRead(buttonPin);
109+
110+
if (oldButtonState != buttonState) {
111+
// button changed
112+
oldButtonState = buttonState;
113+
114+
if (buttonState) {
115+
Serial.println("button pressed");
116+
117+
// button is pressed, write 0x01 to turn the LED on
118+
ledCharacteristic.writeByte(0x01);
119+
} else {
120+
Serial.println("button released");
121+
122+
// button is released, write 0x00 to turn the LED of
123+
ledCharacteristic.writeByte(0x00);
124+
}
125+
}
126+
}
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
Arduino BLE Central peripheral explorer example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <ArduinoBLE.h>
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
25+
// initialize the BLE hardware
26+
BLE.begin();
27+
28+
Serial.println("BLE Central - Peripheral Explorer");
29+
30+
// start scanning for peripherals
31+
BLE.scan();
32+
}
33+
34+
void loop() {
35+
// check if a peripheral has been discovered
36+
BLEDevice peripheral = BLE.available();
37+
38+
if (peripheral) {
39+
// discovered a peripheral, print out address, local name, and advertised service
40+
Serial.print("Found ");
41+
Serial.print(peripheral.address());
42+
Serial.print(" '");
43+
Serial.print(peripheral.localName());
44+
Serial.print("' ");
45+
Serial.print(peripheral.advertisedServiceUuid());
46+
Serial.println();
47+
48+
// see if peripheral is a LED
49+
if (peripheral.localName() == "LED") {
50+
// stop scanning
51+
BLE.stopScan();
52+
53+
explorerPeripheral(peripheral);
54+
55+
// peripheral disconnected, we are done
56+
while (1) {
57+
// do nothing
58+
}
59+
}
60+
}
61+
}
62+
63+
void explorerPeripheral(BLEDevice peripheral) {
64+
// connect to the peripheral
65+
Serial.println("Connecting ...");
66+
67+
if (peripheral.connect()) {
68+
Serial.println("Connected");
69+
} else {
70+
Serial.println("Failed to connect!");
71+
return;
72+
}
73+
74+
// discover peripheral attributes
75+
Serial.println("Discovering attributes ...");
76+
if (peripheral.discoverAttributes()) {
77+
Serial.println("Attributes discovered");
78+
} else {
79+
Serial.println("Attribute discovery failed!");
80+
peripheral.disconnect();
81+
return;
82+
}
83+
84+
// read and print device name of peripheral
85+
Serial.println();
86+
Serial.print("Device name: ");
87+
Serial.println(peripheral.deviceName());
88+
89+
// read and print appearance of peripheral
90+
Serial.print("Appearance: ");
91+
Serial.println(peripheral.appearance());
92+
Serial.println();
93+
94+
// loop the services of the peripheral and explore each
95+
for (int i = 0; i < peripheral.serviceCount(); i++) {
96+
BLEService service = peripheral.service(i);
97+
98+
exploreService(service);
99+
}
100+
101+
Serial.println();
102+
103+
// we are done exploring, disconnect
104+
Serial.println("Disconnecting ...");
105+
peripheral.disconnect();
106+
Serial.println("Disconnected");
107+
}
108+
109+
void exploreService(BLEService service) {
110+
// print the UUID of the service
111+
Serial.print("Service ");
112+
Serial.println(service.uuid());
113+
114+
// loop the characteristics of the service and explore each
115+
for (int i = 0; i < service.characteristicCount(); i++) {
116+
BLECharacteristic characteristic = service.characteristic(i);
117+
118+
exploreCharacteristic(characteristic);
119+
}
120+
}
121+
122+
void exploreCharacteristic(BLECharacteristic characteristic) {
123+
// print the UUID and properies of the characteristic
124+
Serial.print("\tCharacteristic ");
125+
Serial.print(characteristic.uuid());
126+
Serial.print(", properties 0x");
127+
Serial.print(characteristic.properties());
128+
129+
// check if the characteristic is readable
130+
if (characteristic.canRead()) {
131+
// read the characteristic value
132+
characteristic.read();
133+
134+
// print out the value of the characteristic
135+
Serial.print(", value 0x");
136+
printData(characteristic.value(), characteristic.valueLength());
137+
}
138+
139+
Serial.println();
140+
141+
}
142+
143+
void printData(const unsigned char data[], int length) {
144+
for (int i = 0; i < length; i++) {
145+
unsigned char b = data[i];
146+
147+
if (b < 16) {
148+
Serial.print("0");
149+
}
150+
151+
Serial.print(b, HEX);
152+
}
153+
}
154+

0 commit comments

Comments
 (0)