Skip to content

Commit 0be139d

Browse files
committed
Adding BLE Central feature
2 parents b329d08 + 727e42a commit 0be139d

File tree

115 files changed

+17152
-5760
lines changed

Some content is hidden

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

115 files changed

+17152
-5760
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ them to the [support forum](https://forum.arduino.cc/index.php?board=103).
5353
> "How do I use this library?"
5454
5555
> "I can't get this example sketch to work. What am I doing wrong?"
56+
57+
# Enable debug interface on Serail1
58+
59+
* Default disable the debug interface.
60+
61+
If you want to enable debug trace on Serial1 to debug corelib, follow these instructions.
62+
63+
1. Shut down the IDE
64+
2. Go to Arduino15 directory
65+
* Windows: `C:\Users\<user>\AppData\Roaming\Arduino15`
66+
* OS X: `~/Library/Arduino15`
67+
* Linux: `~/.arduino15`
68+
3. Modify the platform.txt
69+
* Find `compiler.c.flags` and add `-DCONFIGURE_DEBUG_CORELIB_ENABLED` at the end of this line
70+
* Find `compiler.cpp.flags` and add `-DCONFIGURE_DEBUG_CORELIB_ENABLED` at the end of this line
71+
4. Initial Serial1 in your sketch
72+
* Add `Serial1.begin(115200);` in your `setup()`
73+
5. Adjust the output level at log_init function in log.c

system/libarc32_arduino101/framework/src/nordic_interface.h renamed to cores/arduino/printk.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,25 @@
2828
* POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
#ifndef NORDIC_INTERFACE_H
32-
#define NORDIC_INTERFACE_H
33-
#include "infra/ipc_uart.h"
31+
#include <stdarg.h>
32+
#include <cstdio>
33+
#include "UARTClass.h"
3434

35-
void uart_ipc_message_cback(uint8_t cpu_id, int channel, int len, void * p_data);
36-
int send_message_ipc_uart(struct message * message);
37-
void free_message_ipc_uart(struct message * message);
38-
int nordic_interface_init(T_QUEUE queue);
35+
extern "C" void printk(const char *fmt, va_list args);
36+
extern UARTClass Serial1;
37+
#define PRINTK_BUFSIZ 256
38+
39+
void printk(const char *fmt, va_list args)
40+
{
41+
#ifdef CONFIGURE_DEBUG_CORELIB_ENABLED
42+
int len = 0;
43+
44+
char tmp[PRINTK_BUFSIZ];
45+
46+
len = vsnprintf(tmp, PRINTK_BUFSIZ, fmt, args);
47+
48+
tmp[len] = '\0';
49+
Serial1.println(tmp);
50+
#endif
51+
}
3952

40-
#endif // NORDIC_INTERFACE_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* Please see code cpyright at the bottom of this example code */
2+
/*
3+
This sketch illustrates how to change the advertising data so that it is visible but not
4+
connectable. Then after 10 seconds it changes to being connectable
5+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
6+
7+
This sketch is not paired with a specific central example sketch,
8+
but to see how it works you need to use a BLE APP on your phone or central device
9+
and try connecting when it is either a connectable or not connectable state
10+
as displayed in the serial monitor.
11+
*/
12+
13+
#include <CurieBLE.h>
14+
15+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
16+
BLEService batteryService("180F"); // BLE Battery Service
17+
int count = 0;
18+
// BLE Battery Level Characteristic"
19+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
20+
BLERead | BLENotify); // remote clients will be able to
21+
// get notifications if this characteristic changes
22+
23+
void setup() {
24+
Serial.begin(9600); // initialize serial communication
25+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
26+
while (!Serial) {
27+
//wait for Serial to connect
28+
}
29+
/* Set a local name for the BLE device
30+
This name will appear in advertising packets
31+
and can be used by remote devices to identify this BLE device
32+
The name can be changed but maybe be truncated based on space left in advertisement packet */
33+
blePeripheral.setLocalName("BatteryAdvChangeSketch");
34+
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
35+
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
36+
blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic
37+
38+
/* Now activate the BLE device. It will start continuously transmitting BLE
39+
advertising packets and will be visible to remote BLE central devices
40+
until it receives a new connection */
41+
42+
blePeripheral.begin();
43+
Serial.println("Bluetooth device active, waiting for connections...");
44+
Serial.println("Starts in Connectable mode");
45+
}
46+
47+
void loop() {
48+
// listen for BLE peripherals to connect:
49+
BLECentralHelper central = blePeripheral.central();
50+
// wait
51+
Serial.print(". ");
52+
if (count == 10) {
53+
Serial.print("\nReached count ");
54+
Serial.println(count);
55+
56+
}
57+
delay (1000);
58+
count++;
59+
// Switch from Connectable to Non Connectable and vice versa
60+
if (count > 10 ) {
61+
static bool change_discover = false;
62+
Serial.println("Stop Adv and pausing for 10 seconds. Device should be invisible");
63+
// Some central devices (phones included) may cache previous scan inofrmation
64+
// restart your central and it should not see this peripheral once stopAdvertising() is called
65+
blePeripheral.stopAdvertising();
66+
delay(10000);
67+
68+
if (change_discover)
69+
{
70+
71+
// Using the function setConnectable we specify that it now NOT connectable
72+
// The loop is for 10 seconds. Your central device may timeout later than that
73+
// and may eventually connect when we set it back to connectable mode below
74+
blePeripheral.setConnectable(false);
75+
Serial.println("In Non Connectable mode");
76+
77+
}
78+
else
79+
{
80+
81+
//using the function setConnectable we specify that it now connectable
82+
blePeripheral.setConnectable(true);
83+
Serial.println("In Connectable mode");
84+
}
85+
Serial.println("Start Adv");
86+
blePeripheral.startAdvertising();
87+
if (change_discover) {
88+
Serial.println("Adding 5 second delay in Non Connect Mode");
89+
delay(5000);
90+
}
91+
change_discover = !change_discover;
92+
count = 0;
93+
}
94+
}
95+
96+
/*
97+
Copyright (c) 2016 Intel Corporation. All rights reserved.
98+
99+
This library is free software; you can redistribute it and/or
100+
modify it under the terms of the GNU Lesser General Public
101+
License as published by the Free Software Foundation; either
102+
version 2.1 of the License, or (at your option) any later version.
103+
104+
This library is distributed in the hope that it will be useful,
105+
but WITHOUT ANY WARRANTY; without even the implied warranty of
106+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
107+
Lesser General Public License for more details.
108+
109+
You should have received a copy of the GNU Lesser General Public
110+
License along with this library; if not, write to the Free Software
111+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
112+
*/
113+

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

+40-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#include <CurieBLE.h>
77

88
/*
9-
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
9+
This sketch can work with UpdateConnectionInterval.
10+
You can also use an android or IOS app that supports notifications
11+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service
12+
and connection interval paramater update.
1013
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
1114
*/
1215

@@ -15,9 +18,9 @@ BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're pr
1518
BLEService batteryService("180F"); // BLE Battery Service
1619

1720
// BLE Battery Level Characteristic"
18-
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
21+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID defined in the URL above
1922
BLERead | BLENotify); // remote clients will be able to
20-
// get notifications if this characteristic changes
23+
// get notifications if this characteristic changes
2124

2225
int oldBatteryLevel = 0; // last battery level reading from analog input
2326
long previousMillis = 0; // last time the battery level was checked, in ms
@@ -45,7 +48,7 @@ void setup() {
4548

4649
void loop() {
4750
// listen for BLE peripherals to connect:
48-
BLECentral central = blePeripheral.central();
51+
BLECentralHelper central = blePeripheral.central();
4952

5053
// if a central is connected to peripheral:
5154
if (central) {
@@ -63,6 +66,14 @@ void loop() {
6366
if (currentMillis - previousMillis >= 200) {
6467
previousMillis = currentMillis;
6568
updateBatteryLevel();
69+
70+
static unsigned short count = 0;
71+
count++;
72+
// update the connection interval
73+
if(count%5 == 0){
74+
delay(1000);
75+
updateIntervalParams(central);
76+
}
6677
}
6778
}
6879
// when the central disconnects, turn off the LED:
@@ -87,6 +98,31 @@ void updateBatteryLevel() {
8798
}
8899
}
89100

101+
void updateIntervalParams(BLECentralHelper &central) {
102+
// read and update the connection interval that peer central device
103+
static unsigned short interval = 0x60;
104+
ble_conn_param_t m_conn_param;
105+
// Get connection interval that peer central device wanted
106+
central.getConnParams(m_conn_param);
107+
Serial.print("min interval = " );
108+
Serial.println(m_conn_param.interval_min );
109+
Serial.print("max interval = " );
110+
Serial.println(m_conn_param.interval_max );
111+
Serial.print("latency = " );
112+
Serial.println(m_conn_param.latency );
113+
Serial.print("timeout = " );
114+
Serial.println(m_conn_param.timeout );
115+
116+
//Update connection interval
117+
Serial.println("set Connection Interval");
118+
central.setConnectionInterval(interval,interval);
119+
120+
interval++;
121+
if(interval<0x06)
122+
interval = 0x06;
123+
if(interval>0x100)
124+
interval = 0x06;
125+
}
90126
/*
91127
Copyright (c) 2016 Intel Corporation. All rights reserved.
92128

libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@
33
* See the bottom of this file for the license terms.
44
*/
55

6+
/* This examples needs a button connected similarly as described here
7+
https://www.arduino.cc/en/Tutorial/Button
8+
The only difference is that instead of connecting to pin 2 connect to pin 4
9+
After the sketch starts connect to a BLE app on a phone and set notification to the Characteristic and you should see it update
10+
whenever the button is pressed. This sketch is not written to pair with any of the central examples.
11+
*/
12+
613
#include <CurieBLE.h>
714

815
const int ledPin = 13; // set ledPin to on-board LED
916
const int buttonPin = 4; // set buttonPin to digital pin 4
1017

1118
BLEPeripheral blePeripheral; // create peripheral instance
12-
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
19+
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes).
20+
// Long UUID denote custom user created UUID
1321

1422

1523
// create switch characteristic and allow remote device to read and write
1624
BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
1725
// create button characteristic and allow remote device to get notifications
1826
BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications
27+
// Note use of Typed Characteristics. These previous 2 characeristics are of the type char
1928

2029
void setup() {
2130
Serial.begin(9600);
@@ -32,6 +41,7 @@ void setup() {
3241
blePeripheral.addAttribute(ledCharacteristic);
3342
blePeripheral.addAttribute(buttonCharacteristic);
3443

44+
// set initial values for led and button characteristic
3545
ledCharacteristic.setValue(0);
3646
buttonCharacteristic.setValue(0);
3747

@@ -59,10 +69,13 @@ void loop() {
5969

6070
if (ledCharacteristic.written() || buttonChanged) {
6171
// update LED, either central has written to characteristic or button state has changed
72+
// if you are using a phone or a BLE central device that is aware of this characteristic, writing a value of 0x40 for example
73+
// Will be interpreted as written
6274
if (ledCharacteristic.value()) {
6375
Serial.println("LED on");
6476
digitalWrite(ledPin, HIGH);
6577
} else {
78+
// If central writes a 0 value then it is interpreted as no value and turns off the LED
6679
Serial.println("LED off");
6780
digitalWrite(ledPin, LOW);
6881
}

libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino

+22-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22
* Copyright (c) 2016 Intel Corporation. All rights reserved.
33
* See the bottom of this file for the license terms.
44
*/
5+
6+
// This example can work with LEDCentral
7+
// You should see the LED blink on and off
8+
// This example demonstrates the use of Callback or event Handlers responding to events
9+
// BLECoonected, BLEDisconnected and BLEWritten are events.
10+
// To test interactively use a Phone app like nrf Controller (Android) or Light Blue (iOS)
11+
// Connect to BLE device named LEDCB and explore characteristic with UUID 19B10001-E8F2-537E-4F6C-D104768A1214
12+
// Writing a byte value such as 0x40 should turn on the LED
13+
// Writng a byte value of 0x00 should turn off the LED
514

615
#include <CurieBLE.h>
716

817
const int ledPin = 13; // set ledPin to use on-board LED
918
BLEPeripheral blePeripheral; // create peripheral instance
19+
BLECentralHelper *bleCentral1 = NULL; // peer central device
1020

11-
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
21+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes).
22+
// Long UUID denote custom user created UUID
1223

1324
// create switch characteristic and allow remote device to read and write
1425
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
@@ -45,19 +56,25 @@ void loop() {
4556
blePeripheral.poll();
4657
}
4758

48-
void blePeripheralConnectHandler(BLECentral& central) {
59+
// The function parameter (BLEHelper& central) is for peripheral devices
60+
// This enable us to have access to the central's data like its bluetooth address
61+
62+
void blePeripheralConnectHandler(BLEHelper& central) {
4963
// central connected event handler
64+
bleCentral1 = blePeripheral.getPeerCentralBLE(central);
5065
Serial.print("Connected event, central: ");
51-
Serial.println(central.address());
66+
Serial.println(bleCentral1->address());
5267
}
5368

54-
void blePeripheralDisconnectHandler(BLECentral& central) {
69+
void blePeripheralDisconnectHandler(BLEHelper& central) {
5570
// central disconnected event handler
5671
Serial.print("Disconnected event, central: ");
5772
Serial.println(central.address());
5873
}
5974

60-
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
75+
// In addtion to the BLECentral& central parameter, we also have to have to BLECharacteristic& characteristic parameter
76+
77+
void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) {
6178
// central wrote new value to characteristic, update LED
6279
Serial.print("Characteristic event, written: ");
6380

0 commit comments

Comments
 (0)