Skip to content

Commit abbc14a

Browse files
committed
Adjust the example comments and functions
1. Fix Jira 675 - BLE callbacks should use passed arguments instead of global variables 2. Adjust the code struture to align with Arduino's requirement
1 parent 05d6469 commit abbc14a

File tree

5 files changed

+227
-189
lines changed

5 files changed

+227
-189
lines changed

libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characte
6161
// central wrote new value to characteristic, update LED
6262
Serial.print("Characteristic event, written: ");
6363

64-
if (switchChar.value()) {
64+
if (characteristic.value()) {
6565
Serial.println("LED on");
6666
digitalWrite(ledPin, HIGH);
6767
} else {

libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
/*
2-
Copyright (c) 2016 Intel Corporation. All rights reserved.
3-
4-
This library is free software; you can redistribute it and/or
5-
modify it under the terms of the GNU Lesser General Public
6-
License as published by the Free Software Foundation; either
7-
version 2.1 of the License, or (at your option) any later version.
8-
9-
This library is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12-
Lesser General Public License for more details.
13-
14-
You should have received a copy of the GNU Lesser General Public
15-
License along with this library; if not, write to the Free Software
16-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17-
*/
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
185

196
#include <CurieBLE.h>
207

@@ -44,6 +31,42 @@ BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // stand
4431
BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to
4532
// get notifications if this characteristic changes
4633

34+
bool adv_found(uint8_t type,
35+
const uint8_t *data,
36+
uint8_t data_len,
37+
void *user_data);
38+
39+
void setup()
40+
{
41+
Serial.begin(115200); // initialize serial communication
42+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
43+
44+
bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten);
45+
46+
/* Set a local name for the BLE device
47+
This name will appear in advertising packets
48+
and can be used by remote devices to identify this BLE device
49+
The name can be changed but maybe be truncated based on space
50+
left in advertisement packet */
51+
bleCentral.addAttribute(bleImuService); // Add the BLE IMU service
52+
bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic
53+
54+
/* Setup callback */
55+
bleCentral.setAdvertiseHandler(adv_found);
56+
bleCentral.setEventHandler(BLEConnected, ble_connected);
57+
58+
/* Now activate the BLE device. It will start continuously transmitting BLE
59+
advertising packets and will be visible to remote BLE central devices
60+
until it receives a new connection */
61+
bleCentral.begin();
62+
}
63+
64+
65+
void loop()
66+
{
67+
delay(2000);
68+
}
69+
4770
void ble_connected(BLEHelper &role)
4871
{
4972
BLEPeripheralHelper&peripheral = *(BLEPeripheralHelper*)(&role);
@@ -116,33 +139,20 @@ bool adv_found(uint8_t type,
116139
return true;
117140
}
118141

119-
void setup() {
120-
Serial.begin(115200); // initialize serial communication
121-
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
122-
123-
bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten);
124-
125-
/* Set a local name for the BLE device
126-
This name will appear in advertising packets
127-
and can be used by remote devices to identify this BLE device
128-
The name can be changed but maybe be truncated based on space
129-
left in advertisement packet */
130-
bleCentral.addAttribute(bleImuService); // Add the BLE IMU service
131-
bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic
132-
133-
/* Setup callback */
134-
bleCentral.setAdvertiseHandler(adv_found);
135-
bleCentral.setEventHandler(BLEConnected, ble_connected);
136-
137-
/* Now activate the BLE device. It will start continuously transmitting BLE
138-
advertising packets and will be visible to remote BLE central devices
139-
until it receives a new connection */
140-
bleCentral.begin();
141-
}
142+
/*
143+
Copyright (c) 2016 Intel Corporation. All rights reserved.
142144
145+
This library is free software; you can redistribute it and/or
146+
modify it under the terms of the GNU Lesser General Public
147+
License as published by the Free Software Foundation; either
148+
version 2.1 of the License, or (at your option) any later version.
143149
144-
void loop()
145-
{
146-
delay(2000);
147-
}
150+
This library is distributed in the hope that it will be useful,
151+
but WITHOUT ANY WARRANTY; without even the implied warranty of
152+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
153+
Lesser General Public License for more details.
148154
155+
You should have received a copy of the GNU Lesser General Public
156+
License along with this library; if not, write to the Free Software
157+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
158+
*/

libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
/*
2-
Copyright (c) 2016 Intel Corporation. All rights reserved.
3-
4-
This library is free software; you can redistribute it and/or
5-
modify it under the terms of the GNU Lesser General Public
6-
License as published by the Free Software Foundation; either
7-
version 2.1 of the License, or (at your option) any later version.
8-
9-
This library is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12-
Lesser General Public License for more details.
13-
14-
You should have received a copy of the GNU Lesser General Public
15-
License along with this library; if not, write to the Free Software
16-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17-
*/
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
185

196
#include <CurieBLE.h>
207
#include <CurieIMU.h>
@@ -41,8 +28,8 @@ BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); // Tx IMU data
4128
BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID
4229
BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to
4330
// get notifications if this characteristic changes
44-
void setup() {
45-
31+
void setup()
32+
{
4633
Serial.begin(9600); // initialize serial communication
4734
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
4835

@@ -64,23 +51,8 @@ void setup() {
6451
CurieIMU.begin();
6552
}
6653

67-
void recordImuData(int index) {
68-
/* Read IMU data.
69-
*/
70-
int ax, ay, az;
71-
int gx, gy, gz;
72-
73-
imuBuf[index].index = seqNum++;
74-
CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);
75-
76-
imuBuf[index].slot[0] = (unsigned int)((ax << 16) | (ay & 0x0FFFF));
77-
imuBuf[index].slot[1] = (unsigned int)((az << 16) | (gx & 0x0FFFF));
78-
imuBuf[index].slot[2] = (unsigned int)((gy << 16) | (gz & 0x0FFFF));
79-
80-
}
81-
82-
83-
void loop() {
54+
void loop()
55+
{
8456
// listen for BLE peripherals to connect:
8557
BLECentralHelper central = blePeripheral.central();
8658

@@ -119,3 +91,37 @@ void loop() {
11991
}
12092
}
12193

94+
void recordImuData(int index)
95+
{
96+
/* Read IMU data.
97+
*/
98+
int ax, ay, az;
99+
int gx, gy, gz;
100+
101+
imuBuf[index].index = seqNum++;
102+
CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);
103+
104+
imuBuf[index].slot[0] = (unsigned int)((ax << 16) | (ay & 0x0FFFF));
105+
imuBuf[index].slot[1] = (unsigned int)((az << 16) | (gx & 0x0FFFF));
106+
imuBuf[index].slot[2] = (unsigned int)((gy << 16) | (gz & 0x0FFFF));
107+
108+
}
109+
110+
111+
/*
112+
Copyright (c) 2016 Intel Corporation. All rights reserved.
113+
114+
This library is free software; you can redistribute it and/or
115+
modify it under the terms of the GNU Lesser General Public
116+
License as published by the Free Software Foundation; either
117+
version 2.1 of the License, or (at your option) any later version.
118+
119+
This library is distributed in the hope that it will be useful,
120+
but WITHOUT ANY WARRANTY; without even the implied warranty of
121+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
122+
Lesser General Public License for more details.
123+
124+
You should have received a copy of the GNU Lesser General Public
125+
License along with this library; if not, write to the Free Software
126+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
127+
*/

libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
/*
2-
Copyright (c) 2016 Intel Corporation. All rights reserved.
3-
4-
This library is free software; you can redistribute it and/or
5-
modify it under the terms of the GNU Lesser General Public
6-
License as published by the Free Software Foundation; either
7-
version 2.1 of the License, or (at your option) any later version.
8-
9-
This library is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12-
Lesser General Public License for more details.
13-
14-
You should have received a copy of the GNU Lesser General Public
15-
License along with this library; if not, write to the Free Software
16-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
17-
1301 USA
18-
*/
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
195

206
#include <CurieBLE.h>
217

@@ -37,6 +23,55 @@ BLEPeripheralHelper *blePeripheral1 = NULL;
3723
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
3824
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);// create switch characteristic and allow remote device to read and write
3925

26+
bool adv_found(uint8_t type,
27+
const uint8_t *data,
28+
uint8_t data_len,
29+
void *user_data);
30+
31+
void setup()
32+
{
33+
Serial.begin(9600);
34+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
35+
36+
// add service and characteristic
37+
bleCentral.addAttribute(ledService);
38+
bleCentral.addAttribute(switchChar);
39+
40+
// assign event handlers for connected, disconnected to central
41+
bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler);
42+
bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler);
43+
44+
// advertise the service
45+
bleCentral.setAdvertiseHandler(adv_found);
46+
47+
// assign event handlers for characteristic
48+
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
49+
50+
bleCentral.begin();
51+
Serial.println(("Bluetooth device active, waiting for connections..."));
52+
}
53+
54+
void loop()
55+
{
56+
static unsigned int counter = 0;
57+
static char ledstate = 0;
58+
delay(2000);
59+
if (blePeripheral1)
60+
{
61+
counter++;
62+
63+
if (counter % 3)
64+
{
65+
switchChar.read(*blePeripheral1);
66+
}
67+
else
68+
{
69+
ledstate = !ledstate;
70+
switchChar.write(*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate));
71+
}
72+
}
73+
}
74+
4075
void bleCentralConnectHandler(BLEHelper& peripheral)
4176
{
4277
// peripheral connected event handler
@@ -61,7 +96,7 @@ void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& chara
6196
// Read response/Notification wrote new value to characteristic, update LED
6297
Serial.print("Characteristic event, written: ");
6398

64-
if (switchChar.value())
99+
if (characteristic.value())
65100
{
66101
Serial.println("LED on");
67102
digitalWrite(ledPin, HIGH);
@@ -121,45 +156,20 @@ bool adv_found(uint8_t type,
121156
return true;
122157
}
123158

124-
void setup() {
125-
Serial.begin(9600);
126-
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
127-
128-
// add service and characteristic
129-
bleCentral.addAttribute(ledService);
130-
bleCentral.addAttribute(switchChar);
131-
132-
// assign event handlers for connected, disconnected to central
133-
bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler);
134-
bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler);
135-
136-
// advertise the service
137-
bleCentral.setAdvertiseHandler(adv_found);
159+
/*
160+
Copyright (c) 2016 Intel Corporation. All rights reserved.
138161
139-
// assign event handlers for characteristic
140-
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
162+
This library is free software; you can redistribute it and/or
163+
modify it under the terms of the GNU Lesser General Public
164+
License as published by the Free Software Foundation; either
165+
version 2.1 of the License, or (at your option) any later version.
141166
142-
bleCentral.begin();
143-
Serial.println(("Bluetooth device active, waiting for connections..."));
144-
}
167+
This library is distributed in the hope that it will be useful,
168+
but WITHOUT ANY WARRANTY; without even the implied warranty of
169+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
170+
Lesser General Public License for more details.
145171
146-
void loop()
147-
{
148-
static unsigned int counter = 0;
149-
static char ledstate = 0;
150-
delay(2000);
151-
if (blePeripheral1)
152-
{
153-
counter++;
154-
155-
if (counter % 3)
156-
{
157-
switchChar.read(*blePeripheral1);
158-
}
159-
else
160-
{
161-
ledstate = !ledstate;
162-
switchChar.write(*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate));
163-
}
164-
}
165-
}
172+
You should have received a copy of the GNU Lesser General Public
173+
License along with this library; if not, write to the Free Software
174+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
175+
*/

0 commit comments

Comments
 (0)