Skip to content

Commit 3b8efcc

Browse files
tigoesandeepmistry
authored andcommitted
Style changes to BLE library examples
1 parent a5c7f84 commit 3b8efcc

File tree

4 files changed

+87
-107
lines changed

4 files changed

+87
-107
lines changed
Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,100 @@
11
/*
2-
* Copyright (c) 2015 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) 2015 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+
*/
1818
#include <CurieBle.h>
1919

2020
/*
21-
* This sketch example partially implements the standard Bluetooth Low-Energy "Battery" service.
22-
* For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
23-
*/
21+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
22+
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
23+
*/
2424

25-
/* BLE Peripheral Device (this Intel Curie device) */
26-
BLEPeripheral blePeripheral;
25+
/* */
26+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
27+
BLEService batteryService("180F"); // BLE Battery Service
2728

28-
/* BLE Battery Service */
29-
BLEService battSvc("180F");
29+
// BLE Battery Level Characteristic"
30+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
31+
BLERead | BLENotify); // remote clients will be able to
32+
// get notifications if this characteristic changes
3033

31-
/* BLE Battery Level Characteristic */
32-
BLEUnsignedCharCharacteristic battLvlChar("2A19", /* standard 16-bit characteristic UUID */
33-
BLERead | BLENotify /* remote clients will be able to get notifications if this characteristic changes */
34-
);
35-
36-
/* Variable to keep track of last battery level reading from analog input */
37-
uint8_t oldBattLvl = 0;
38-
unsigned long previousMillis = 0;
34+
int oldBatteryLevel = 0; // last battery level reading from analog input
35+
long previousMillis = 0; // last time the battery level was checked, in ms
3936

4037
void setup() {
41-
Serial.begin(9600);
42-
43-
pinMode(13, OUTPUT);
38+
Serial.begin(9600); // initialize serial communication
39+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
4440

4541
/* Set a name for the BLE device
46-
* We give it an arbitrary name which will appear in advertising packets
47-
* and can be used by remote peers to identify this BLE device
48-
* The name can be changed but must not exceed 20 characters in length */
49-
blePeripheral.setLocalName("AE_BATTMON");
50-
blePeripheral.setAdvertisedServiceUuid(battSvc.uuid());
51-
52-
/* Add the BLE Battery service, and include the UUID in BLE advertising data */
53-
blePeripheral.addAttribute(battSvc);
54-
55-
/* This service will have just one characteristic that reflects the current
56-
* percentage-charge level of the "battery" */
57-
blePeripheral.addAttribute(battLvlChar);
58-
59-
/* Set an initial value for this characteristic; refreshed later the loop() function */
60-
battLvlChar.setValue(oldBattLvl);
42+
This name will appear in advertising packets
43+
and can be used by remote devices to identify this BLE device
44+
The name can be changed but must not exceed 20 characters in length */
45+
blePeripheral.setLocalName("BatteryMonitorSketch");
46+
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
47+
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
48+
blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic
49+
batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic
6150

6251
/* Now activate the BLE device. It will start continuously transmitting BLE
63-
* advertising packets and thus become visible to remote BLE central devices
64-
* (e.g smartphones) until it receives a new connection */
52+
advertising packets and will be visible to remote BLE central devices
53+
until it receives a new connection */
6554
blePeripheral.begin();
6655
Serial.println("Bluetooth device active, waiting for connections...");
6756
}
6857

6958
void loop() {
59+
// listen for BLE peripherals to connect:
7060
BLECentral central = blePeripheral.central();
7161

62+
// if a central is connected to peripheral:
7263
if (central) {
73-
// central connected to peripheral
74-
Serial.print(F("Connected to central: "));
64+
Serial.print("Connected to central: ");
65+
// print the central's MAC address:
7566
Serial.println(central.address());
76-
67+
// turn on the LED to indicate the connection:
7768
digitalWrite(13, HIGH);
7869

70+
// check the battery level every 200ms
71+
// as long as the central is still connected:
7972
while (central.connected()) {
80-
// central still connected to peripheral
81-
82-
unsigned long currentMillis = millis();
83-
73+
long currentMillis = millis();
74+
// if 200ms have passed, check the battery level:
8475
if (currentMillis - previousMillis >= 200) {
8576
previousMillis = currentMillis;
8677
updateBatteryLevel();
8778
}
8879
}
89-
80+
// when the central disconnects, turn off the LED:
9081
digitalWrite(13, LOW);
91-
92-
// central disconnected
93-
Serial.print(F("Disconnected from central: "));
94-
Serial.println(central.address());
82+
Serial.print("Disconnected from central: ");
83+
Serial.println(central.address());
9584
}
9685
}
9786

9887
void updateBatteryLevel() {
9988
/* Read the current voltage level on the A0 analog input pin.
100-
* This is used here to simulate the charge level of a "battery".
101-
* The following tutorial shows how a potentiometer could be used
102-
* to vary the voltage on an analog input pin:
103-
* https://www.arduino.cc/en/Tutorial/Potentiometer
104-
*/
105-
uint8_t battLvl = map(analogRead(A0), 0, 1023, 0, 100);
106-
107-
if (battLvl != oldBattLvl) {
108-
Serial.print("Battery Level % is now: ");
109-
Serial.println(battLvl);
110-
111-
/* If the voltage level has changed, we update the value of the
112-
* Battery Level BLE characteristic. Because we have enabled
113-
* notifications for this characteristic, the remote device can
114-
* receive automatic updates when this value is changed. */
115-
battLvlChar.setValue(battLvl);
116-
oldBattLvl = battLvl;
89+
This is used here to simulate the charge level of a battery.
90+
*/
91+
int battery = analogRead(A0);
92+
int batteryLevel = map(battery, 0, 1023, 0, 100);
93+
94+
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
95+
Serial.print("Battery Level % is now: "); // print it
96+
Serial.println(batteryLevel);
97+
batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic
98+
oldBatteryLevel = batteryLevel; // save the level for next comparison
11799
}
118100
}

libraries/CurieBle/examples/BatteryMonitor/BatteryMonitor.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

libraries/CurieBle/examples/CallbackLED/CallbackLED.ino

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead
3030

3131
void setup() {
3232
Serial.begin(9600);
33-
3433
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
3534

3635
// set a name for the local peripheral
37-
blePeripheral.setLocalName("Curie LED Sketch");
36+
blePeripheral.setLocalName("LEDCallback");
3837
// set the UUID for the service this peripheral advertises
3938
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
4039

@@ -48,12 +47,11 @@ void setup() {
4847

4948
// assign event handlers for characteristic
5049
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
51-
50+
// set an initial value for the characteristic
5251
switchChar.setValue(0);
5352

5453
// advertise the service
5554
blePeripheral.begin();
56-
5755
Serial.println(("Bluetooth device active, waiting for connections..."));
5856
}
5957

@@ -76,7 +74,7 @@ void blePeripheralDisconnectHandler(BLECentral& central) {
7674

7775
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
7876
// central wrote new value to characteristic, update LED
79-
Serial.print("Characteristic event, writen: ");
77+
Serial.print("Characteristic event, written: ");
8078

8179
if (switchChar.value()) {
8280
Serial.println("LED on");

libraries/CurieBle/examples/LED/LED.ino

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
*/
1818
#include <CurieBle.h>
1919

20-
/* BLE Peripheral (this Intel Curie device) */
21-
BLEPeripheral blePeripheral;
20+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
21+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
2222

23-
/* BLE LED Service */
24-
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");
25-
26-
/* BLE Switch Characteristic - custom 128-bit UUID, read and writable by central */
23+
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
2724
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
2825

2926
const int ledPin = 13; // pin to use for the LED
@@ -34,45 +31,49 @@ void setup() {
3431
// set LED pin to output mode
3532
pinMode(ledPin, OUTPUT);
3633

37-
// set advertised local name and service UUID
38-
blePeripheral.setLocalName("LED");
34+
// set advertised local name and service UUID:
35+
blePeripheral.setLocalName("LED Sketch");
3936
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
4037

41-
// add service and characteristic
38+
// add service and characteristic:
4239
blePeripheral.addAttribute(ledService);
4340
blePeripheral.addAttribute(switchCharacteristic);
4441

42+
// set the initial value for the characeristic:
4543
switchCharacteristic.setValue(0);
4644

47-
// begin initialization
45+
// begin advertising BLE service:
4846
blePeripheral.begin();
4947

50-
Serial.println(F("BLE LED Peripheral"));
48+
Serial.println("BLE LED Peripheral");
5149
}
5250

5351
void loop() {
52+
// listen for BLE peripherals to connect:
5453
BLECentral central = blePeripheral.central();
5554

55+
// if a central is connected to peripheral:
5656
if (central) {
57-
// central connected to peripheral
58-
Serial.print(F("Connected to central: "));
57+
Serial.print("Connected to central: ");
58+
// print the central's MAC address:
5959
Serial.println(central.address());
6060

61+
// while the central is still connected to peripheral:
6162
while (central.connected()) {
62-
// central still connected to peripheral
63+
// if the remote device wrote to the characteristic,
64+
// use the value to control the LED:
6365
if (switchCharacteristic.written()) {
64-
// central wrote new value to characteristic, update LED
65-
if (switchCharacteristic.value()) {
66-
Serial.println(F("LED on"));
67-
digitalWrite(ledPin, HIGH);
68-
} else {
66+
if (switchCharacteristic.value()) { // any value other than 0
67+
Serial.println("LED on");
68+
digitalWrite(ledPin, HIGH); // will turn the LED on
69+
} else { // a 0 value
6970
Serial.println(F("LED off"));
70-
digitalWrite(ledPin, LOW);
71+
digitalWrite(ledPin, LOW); // will turn the LED off
7172
}
7273
}
7374
}
7475

75-
// central disconnected
76+
// when the central disconnects, print it out:
7677
Serial.print(F("Disconnected from central: "));
7778
Serial.println(central.address());
7879
}

0 commit comments

Comments
 (0)