Skip to content

Commit 7f0ee89

Browse files
committed
Add BLE Central feature
2 parents b329d08 + 1537c0a commit 7f0ee89

File tree

113 files changed

+16133
-5716
lines changed

Some content is hidden

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

113 files changed

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

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

+34-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void setup() {
4545

4646
void loop() {
4747
// listen for BLE peripherals to connect:
48-
BLECentral central = blePeripheral.central();
48+
BLECentralHelper central = blePeripheral.central();
4949

5050
// if a central is connected to peripheral:
5151
if (central) {
@@ -63,6 +63,14 @@ void loop() {
6363
if (currentMillis - previousMillis >= 200) {
6464
previousMillis = currentMillis;
6565
updateBatteryLevel();
66+
67+
static unsigned short count = 0;
68+
count++;
69+
// update the connection interval
70+
if(count%5 == 0){
71+
delay(1000);
72+
updateIntervalParams(central);
73+
}
6674
}
6775
}
6876
// when the central disconnects, turn off the LED:
@@ -87,6 +95,31 @@ void updateBatteryLevel() {
8795
}
8896
}
8997

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

libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ void loop() {
4545
blePeripheral.poll();
4646
}
4747

48-
void blePeripheralConnectHandler(BLECentral& central) {
48+
void blePeripheralConnectHandler(BLEHelper& central) {
4949
// central connected event handler
5050
Serial.print("Connected event, central: ");
5151
Serial.println(central.address());
5252
}
5353

54-
void blePeripheralDisconnectHandler(BLECentral& central) {
54+
void blePeripheralDisconnectHandler(BLEHelper& central) {
5555
// central disconnected event handler
5656
Serial.print("Disconnected event, central: ");
5757
Serial.println(central.address());
5858
}
5959

60-
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
60+
void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) {
6161
// central wrote new value to characteristic, update LED
6262
Serial.print("Characteristic event, written: ");
6363

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
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+
*/
18+
19+
#include <CurieBLE.h>
20+
21+
/*
22+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
23+
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
24+
*/
25+
26+
#define MAX_IMU_RECORD 1
27+
28+
struct bt_le_conn_param conn_param = {0x18, 0x28, 0, 400};
29+
typedef struct {
30+
int index;
31+
unsigned int slot[3];
32+
} imuFrameType;
33+
34+
imuFrameType imuBuf[MAX_IMU_RECORD];
35+
BLECentral bleCentral; // BLE Central Device (the board you're programming)
36+
37+
BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13");
38+
BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID
39+
BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to
40+
// get notifications if this characteristic changes
41+
42+
void ble_connected(BLEHelper &role)
43+
{
44+
BLEPeripheralHelper&peripheral = *(BLEPeripheralHelper*)(&role);
45+
Serial.println("Connected");
46+
47+
// Start discovery the profiles in peripheral device
48+
peripheral.discover();
49+
}
50+
51+
void bleImuCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic)
52+
{
53+
// Peripheral wrote new value to characteristic by Notification/Indication
54+
const unsigned char *cvalue = characteristic.value();
55+
const imuFrameType *value = (const imuFrameType *)cvalue;
56+
Serial.print("\r\nCharacteristic event, written: ");
57+
Serial.print(value->index);
58+
Serial.print("\t");
59+
Serial.print(value->slot[0]);
60+
Serial.print("\t");
61+
Serial.print(value->slot[1]);
62+
Serial.print("\t");
63+
Serial.println(value->slot[2]);
64+
}
65+
66+
bool adv_found(uint8_t type,
67+
const uint8_t *data,
68+
uint8_t data_len,
69+
void *user_data)
70+
{
71+
bt_addr_le_t *addr = (bt_addr_le_t *)user_data;
72+
int i;
73+
74+
Serial.print("[AD]:");
75+
Serial.print(type);
76+
Serial.print(" data_len ");
77+
Serial.println(data_len);
78+
79+
switch (type)
80+
{
81+
case BT_DATA_UUID128_SOME:
82+
case BT_DATA_UUID128_ALL:
83+
{
84+
if (data_len % MAX_UUID_SIZE != 0)
85+
{
86+
Serial.println("AD malformed");
87+
return true;
88+
}
89+
struct bt_uuid * serviceuuid = bleImuService.uuid();
90+
for (i = 0; i < data_len; i += MAX_UUID_SIZE)
91+
{
92+
if (memcmp (((struct bt_uuid_128*)serviceuuid)->val, &data[i], MAX_UUID_SIZE) != 0)
93+
{
94+
continue;
95+
}
96+
97+
// Accept the advertisement
98+
if (!bleCentral.stopScan())
99+
{
100+
Serial.println("Stop LE scan failed");
101+
continue;
102+
}
103+
Serial.println("Connecting");
104+
// Connect to peripheral
105+
bleCentral.connect(addr, &conn_param);
106+
return false;
107+
}
108+
}
109+
}
110+
111+
return true;
112+
}
113+
114+
void setup() {
115+
Serial.begin(115200); // initialize serial communication
116+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
117+
118+
bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten);
119+
120+
/* Set a local name for the BLE device
121+
This name will appear in advertising packets
122+
and can be used by remote devices to identify this BLE device
123+
The name can be changed but maybe be truncated based on space
124+
left in advertisement packet */
125+
bleCentral.addAttribute(bleImuService); // Add the BLE IMU service
126+
bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic
127+
128+
/* Setup callback */
129+
bleCentral.setAdvertiseHandler(adv_found);
130+
bleCentral.setEventHandler(BLEConnected, ble_connected);
131+
132+
/* Now activate the BLE device. It will start continuously transmitting BLE
133+
advertising packets and will be visible to remote BLE central devices
134+
until it receives a new connection */
135+
bleCentral.begin();
136+
}
137+
138+
139+
void loop()
140+
{
141+
delay(2000);
142+
}
143+

0 commit comments

Comments
 (0)