Skip to content

Improvements for CurieBLE library based on the master branch #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,21 @@ them to the [support forum](https://forum.arduino.cc/index.php?board=103).
> "How do I use this library?"

> "I can't get this example sketch to work. What am I doing wrong?"

# Enable debug interface on Serail1

* Default disable the debug interface.

If you want to enable debug trace on Serial1 to debug corelib, follow these instructions.

1. Shut down the IDE
2. Go to Arduino15 directory
* Windows: `C:\Users\<user>\AppData\Roaming\Arduino15`
* OS X: `~/Library/Arduino15`
* Linux: `~/.arduino15`
3. Modify the platform.txt
* Find `compiler.c.flags` and add `-DCONFIGURE_DEBUG_CORELIB_ENABLED` at the end of this line
* Find `compiler.cpp.flags` and add `-DCONFIGURE_DEBUG_CORELIB_ENABLED` at the end of this line
4. Initial Serial1 in your sketch
* Add `Serial1.begin(115200);` in your `setup()`
5. Adjust the output level at log_init function in log.c
11 changes: 8 additions & 3 deletions cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
void String::move(String &rhs)
{
if (buffer) {
if (capacity >= rhs.len) {
if (rhs && capacity >= rhs.len) {
strcpy(buffer, rhs.buffer);
len = rhs.len;
rhs.len = 0;
Expand Down Expand Up @@ -810,6 +810,11 @@ long String::toInt(void) const

float String::toFloat(void) const
{
if (buffer) return float(atof(buffer));
return 0;
return (float)toDouble();
}

double String::toDouble(void) const
{
if (buffer) return atof(buffer);
return 0;
}
1 change: 1 addition & 0 deletions cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class String
// parsing/conversion
long toInt(void) const;
float toFloat(void) const;
double toDouble(void) const;
char * getCSpec(int base, bool issigned, bool islong);

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef NORDIC_INTERFACE_H
#define NORDIC_INTERFACE_H
#include "infra/ipc_uart.h"
#include <stdarg.h>
#include <cstdio>
#include "UARTClass.h"

void uart_ipc_message_cback(uint8_t cpu_id, int channel, int len, void * p_data);
int send_message_ipc_uart(struct message * message);
void free_message_ipc_uart(struct message * message);
int nordic_interface_init(T_QUEUE queue);
extern "C" void printk(const char *fmt, va_list args);
extern UARTClass Serial1;
#define PRINTK_BUFSIZ 256

void printk(const char *fmt, va_list args)
{
#ifdef CONFIGURE_DEBUG_CORELIB_ENABLED
int len = 0;

char tmp[PRINTK_BUFSIZ];

len = vsnprintf(tmp, PRINTK_BUFSIZ, fmt, args);

tmp[len] = '\0';
Serial1.println(tmp);
#endif
}

#endif // NORDIC_INTERFACE_H
2 changes: 1 addition & 1 deletion cores/arduino/stdlib_noniso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ char *dtostrf(double number, signed char width, unsigned char prec, char *s)

// generate chars for each digit of the integral part
i = before;
while (integer > 10) {
while (integer >= 10) {
digit = integer % 10;
out[(i--) - 1] = ASCII_ZERO + digit;
integer /= 10;
Expand Down
116 changes: 116 additions & 0 deletions libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* Please see code copyright at the bottom of this example code */

/*
This example can work with phone BLE app.

This sketch illustrates how to change the advertising data so that it is visible but not
connectable. Then after 10 seconds it changes to being connectable.
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.

This sketch is not paired with a specific central example sketch,
but to see how it works you need to use a BLE APP on your phone or central device
and try connecting when it is either a connectable or not connectable state
as displayed in the serial monitor.
*/

#include <CurieBLE.h>

BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
BLEService batteryService("180F"); // BLE Battery Service
int count = 0;
// BLE Battery Level Characteristic"
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to
// get notifications if this characteristic changes

void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
while (!Serial) {
; //wait for Serial to connect
}
/* Set a local name for the BLE device
This name will appear in advertising packets
and can be used by remote devices to identify this BLE device
The name can be changed but maybe be truncated based on space left in advertisement packet */
blePeripheral.setLocalName("BatteryAdvChangeSketch");
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic

/* Now activate the BLE device. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection */

blePeripheral.begin();
Serial.println("Bluetooth device active, waiting for connections...");
Serial.println("Starts in Connectable mode");
}

void loop() {
// listen for BLE peripherals to connect:
BLECentralHelper central = blePeripheral.central();
// wait
Serial.print(". ");
if (count == 10) {
Serial.print("\nReached count ");
Serial.println(count);

}
delay (1000);
count++;
// Switch from Connectable to Non Connectable and vice versa
if (count > 10 ) {
static bool change_discover = false;
Serial.println("Stop Adv and pausing for 10 seconds. Device should be invisible");
// Some central devices (phones included) may cache previous scan inofrmation
// restart your central and it should not see this peripheral once stopAdvertising() is called
blePeripheral.stopAdvertising();
delay(10000);

if (change_discover)
{

// Using the function setConnectable we specify that it now NOT connectable
// The loop is for 10 seconds. Your central device may timeout later than that
// and may eventually connect when we set it back to connectable mode below
blePeripheral.setConnectable(false);
Serial.println("In Non Connectable mode");

}
else
{

//using the function setConnectable we specify that it now connectable
blePeripheral.setConnectable(true);
Serial.println("In Connectable mode");
}
Serial.println("Start Adv");
blePeripheral.startAdvertising();
if (change_discover) {
Serial.println("Adding 5 second delay in Non Connect Mode");
delay(5000);
}
change_discover = !change_discover;
count = 0;
}
}

/*
Copyright (c) 2016 Intel Corporation. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

61 changes: 51 additions & 10 deletions libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/
Copyright (c) 2016 Intel Corporation. All rights reserved.
See the bottom of this file for the license terms.
*/

#include <CurieBLE.h>

/*
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
This sketch can work with UpdateConnectionInterval.

You can also use an android or IOS app that supports notifications.
This sketch example partially implements the standard Bluetooth Low-Energy Battery service
and connection interval paramater update.
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
*/

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

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

Expand All @@ -24,7 +28,11 @@ long previousMillis = 0; // last time the battery level was checked, in ms

void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
// wait for the Serial port to connect. Open the Serial Monitor to continue executing the sketch
while (!Serial) {
;
}
pinMode(LED_BUILTIN, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected

/* Set a local name for the BLE device
This name will appear in advertising packets
Expand All @@ -45,15 +53,15 @@ void setup() {

void loop() {
// listen for BLE peripherals to connect:
BLECentral central = blePeripheral.central();
BLECentralHelper central = blePeripheral.central();

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// turn on the LED to indicate the connection:
digitalWrite(13, HIGH);
digitalWrite(LED_BUILTIN, HIGH);

// check the battery level every 200ms
// as long as the central is still connected:
Expand All @@ -63,10 +71,18 @@ void loop() {
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
updateBatteryLevel();

static unsigned short count = 0;
count++;
// update the connection interval
if (count % 5 == 0) {
delay(1000);
updateIntervalParams(central);
}
}
}
// when the central disconnects, turn off the LED:
digitalWrite(13, LOW);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
Expand All @@ -87,6 +103,31 @@ void updateBatteryLevel() {
}
}

void updateIntervalParams(BLECentralHelper &central) {
// read and update the connection interval that peer central device
static unsigned short interval = 0x60;
ble_conn_param_t m_conn_param;
// Get connection interval that peer central device wanted
central.getConnParams(m_conn_param);
Serial.print("min interval = " );
Serial.println(m_conn_param.interval_min );
Serial.print("max interval = " );
Serial.println(m_conn_param.interval_max );
Serial.print("latency = " );
Serial.println(m_conn_param.latency );
Serial.print("timeout = " );
Serial.println(m_conn_param.timeout );

//Update connection interval
Serial.println("set Connection Interval");
central.setConnectionInterval(interval, interval);

interval++;
if (interval < 0x06)
interval = 0x06;
if (interval > 0x100)
interval = 0x06;
}
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.

Expand Down
29 changes: 24 additions & 5 deletions libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/
Copyright (c) 2016 Intel Corporation. All rights reserved.
See the bottom of this file for the license terms.
*/

/*
This example can work with phone BLE app.

This examples needs a button connected similarly as described here https://www.arduino.cc/en/Tutorial/Button
The only difference is that instead of connecting to pin 2, it connects to pin 4
After the sketch starts connect to a BLE app on a phone and set notification to the Characteristic and you should see it update
whenever the button is pressed. This sketch is not written to pair with any of the central examples.
*/

#include <CurieBLE.h>

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

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


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

void setup() {
Serial.begin(9600);
// wait for the Serial port to connect. Open the Serial Monitor to continue executing the sketch
while (!Serial) {
;
}
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
pinMode(buttonPin, INPUT); // use button pin 4 as an input

Expand All @@ -32,6 +47,7 @@ void setup() {
blePeripheral.addAttribute(ledCharacteristic);
blePeripheral.addAttribute(buttonCharacteristic);

// set initial values for led and button characteristic
ledCharacteristic.setValue(0);
buttonCharacteristic.setValue(0);

Expand All @@ -58,11 +74,14 @@ void loop() {
}

if (ledCharacteristic.written() || buttonChanged) {
// update LED, either central has written to characteristic or button state has changed
// update LED, either central has written to characteristic or button state has changed.
// If you are using a phone or a BLE central device that is aware of this characteristic,
// writing a value of 0x40 for example will be interpreted as written
if (ledCharacteristic.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
} else {
// If central writes a 0 value (0x00) then it is interpreted as no value and turns off the LED
Serial.println("LED off");
digitalWrite(ledPin, LOW);
}
Expand Down
Loading