8000 Add BLE Central feature · arduino/ArduinoCore-arc32@f2a001f · GitHub
[go: up one dir, main page]

Skip to content

Commit f2a001f

Browse files
committed
Add BLE Central feature
2 parents 77d9086 + d2b7d1d commit f2a001f

File tree

116 files changed

+17171
-5764
lines changed

Some content is hidden

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

116 files changed

+17171
-5764
lines changed

README.md

Lines changed: 18 additions & 0 deletions
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

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

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
#include <CurieBLE.h>
77

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

1317
/* */
1418
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
1519
BLEService batteryService("180F"); // BLE Battery Service
1620

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

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

4650
void loop() {
4751
// listen for BLE peripherals to connect:
48-
BLECentral central = blePeripheral.central();
52+
BLECentralHelper central = blePeripheral.central();
4953

5054
// if a central is connected to peripheral:
5155
if (central) {
@@ -63,6 +67,14 @@ void loop() {
6367
if (currentMillis - previousMillis >= 200) {
6468
previousMillis = currentMillis;
6569
updateBatteryLevel();
70+
71+
static unsigned short count = 0;
72+
count++;
73+
// update the connection interval
74+
if(count%5 == 0){
75+
delay(1000);
76+
updateIntervalParams(central);
77+
}
6678
}
6779
}
6880
// when the central disconnects, turn off the LED:
@@ -87,6 +99,31 @@ void updateBatteryLevel() {
8799
}
88100
}
89101

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

libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
* See the bottom of this file for the license terms.
44
*/
55

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

817
const int ledPin = 13; // set ledPin to on-board LED
918
const int buttonPin = 4; // set buttonPin to digital pin 4
1019

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

1424

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

2031
void setup() {
21< 10000 /code>32
Serial.begin(9600);
@@ -32,6 +43,7 @@ void setup() {
3243
blePeripheral.addAttribute(ledCharacteristic);
3344
blePeripheral.addAttribute(buttonCharacteristic);
3445

46+
// set initial values for led and button characteristic
3547
ledCharacteristic.setValue(0);
3648
buttonCharacteristic.setValue(0);
3749

@@ -59,10 +71,13 @@ void loop() {
5971

6072
if (ledCharacteristic.written() || buttonChanged) {
6173
// update LED, either central has written to characteristic or button state has changed
74+
// if you are using a phone or a BLE central device that is aware of this characteristic, writing a value of 0x40 for example
75+
// Will be interpreted as written
6276
if (ledCharacteristic.value()) {
6377
Serial.println("LED on");
6478
digitalWrite(ledPin, HIGH);
6579
} else {
80+
// If central writes a 0 value then it is interpreted as no value and turns off the LED
6681
Serial.println("LED off");
6782
digitalWrite(ledPin, LOW);
6883
}

libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino

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

618
#include <CurieBLE.h>
719

820
const int ledPin = 13; // set ledPin to use on-board LED
921
BLEPeripheral blePeripheral; // create peripheral instance
22+
BLECentralHelper *bleCentral1 = NULL; // peer central device
1023

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

1327
// create switch characteristic and allow remote device to read and write
1428
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
@@ -45,19 +59,25 @@ void loop() {
4559
blePeripheral.poll();
4660
}
4761

48-
void blePeripheralConnectHandler(BLECentral& central) {
62+
// The function parameter (BLEHelper& central) is for peripheral devices
63+
// This enable us to have access to the central's data like its bluetooth address
64+
65+
void blePeripheralConnectHandler(BLEHelper& central) {
4966
// central connected event handler
67+
bleCentral1 = blePeripheral.getPeerCentralBLE(central);
5068
Serial.print("Connected event, central: ");
51-
Serial.println(central.address());
69+
Serial.println(bleCentral1->address());
5270
}
5371

54-
void blePeripheralDisconnectHandler(BLECentral& central) {
72+
void blePeripheralDisconnectHandler(BLEHelper& central) {
5573
// central disconnected event handler
5674
Serial.print("Disconnected event, central: ");
5775
Serial.println(central.address());
5876
}
5977

60-
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
78+
// In addtion to the BLECentral& central parameter, we also have to have to BLECharacteristic& characteristic parameter
79+
80+
void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) {
6181
// central wrote new value to characteristic, update LED
6282
Serial.print("Characteristic event, written: ");
6383

0 commit comments

Comments
 (0)
0