SimpleBle-Example on mbed-os 5

Dependencies:   SimpleBLE mbed nRF51822

Fork of SimpleBLE-Example by mbed-x

Committer:
janjongboom
Date:
Tue May 17 19:34:03 2016 +0000
Revision:
2:12a235e7691a
Parent:
0:ba1c49874d3c
Child:
3:e439dd384d7e
Switch from generics to macros for declaring variables, so we can whitelist types

Who changed what in which revision?

UserRevisionLine numberNew contents of line
janjongboom 0:ba1c49874d3c 1 #include "mbed.h"
janjongboom 0:ba1c49874d3c 2 #include "SimpleBLE.h"
janjongboom 0:ba1c49874d3c 3
janjongboom 2:12a235e7691a 4 DigitalOut led(LED1);
janjongboom 2:12a235e7691a 5
janjongboom 0:ba1c49874d3c 6 // The first thing we need to do is create a SimpleBLE instance:
janjongboom 0:ba1c49874d3c 7 // * first argument is the device name
janjongboom 0:ba1c49874d3c 8 // * second is the advertisement interval in ms. (default 1000 ms.)
janjongboom 2:12a235e7691a 9 SimpleBLE ble("DEVICE_NAME");
janjongboom 0:ba1c49874d3c 10
janjongboom 0:ba1c49874d3c 11 // Now we can declare some variables that we want to expose.
janjongboom 0:ba1c49874d3c 12 // After you created the variable you can use it like any other var,
janjongboom 0:ba1c49874d3c 13 // but it's value will be automatically updated over Bluetooth!
janjongboom 0:ba1c49874d3c 14
janjongboom 0:ba1c49874d3c 15 // F.e. here we declare service 0x180d (heartrate), char 0x2a37 (curr. value) as uint8_t
janjongboom 2:12a235e7691a 16 SimpleChar<uint8_t> heartrate = ble.readOnly_u8(0x180D, 0x2A37, true /* notify */, 100 /* default value */);
janjongboom 0:ba1c49874d3c 17
janjongboom 0:ba1c49874d3c 18 // now we can use this variable everywhere in our code like a normal uint8_t
janjongboom 0:ba1c49874d3c 19 void updateHeartrate() {
janjongboom 2:12a235e7691a 20 led = !led; // keep-alive LED
janjongboom 0:ba1c49874d3c 21 // we just loop between 100 and 180
janjongboom 0:ba1c49874d3c 22 heartrate = heartrate + 1;
janjongboom 0:ba1c49874d3c 23 if (heartrate > 180) {
janjongboom 0:ba1c49874d3c 24 heartrate = 100;
janjongboom 0:ba1c49874d3c 25 }
janjongboom 0:ba1c49874d3c 26 }
janjongboom 0:ba1c49874d3c 27
janjongboom 0:ba1c49874d3c 28 // And here we create a custom service (0x9310) and char (0x9311) with a callback
janjongboom 0:ba1c49874d3c 29 void callback(uint32_t newValue) {
janjongboom 0:ba1c49874d3c 30 // whenever someone updates this var over Bluetooth, this function will be called
janjongboom 0:ba1c49874d3c 31 printf("My value was updated to %d\n", newValue);
janjongboom 0:ba1c49874d3c 32 }
janjongboom 0:ba1c49874d3c 33 // FYI, you can also use UUIDs here instead of short services :-)
janjongboom 2:12a235e7691a 34 SimpleChar<uint32_t> writeMe = ble.readWrite_u32(0x9310, 0x9311, &callback);
janjongboom 0:ba1c49874d3c 35
janjongboom 0:ba1c49874d3c 36 int main(int, char**) {
janjongboom 0:ba1c49874d3c 37 // update the heart rate every second
janjongboom 0:ba1c49874d3c 38 Ticker t;
janjongboom 0:ba1c49874d3c 39 t.attach(updateHeartrate, 1.0f);
janjongboom 0:ba1c49874d3c 40
janjongboom 0:ba1c49874d3c 41 // here's how we kick off our loop
janjongboom 0:ba1c49874d3c 42 ble.start();
janjongboom 0:ba1c49874d3c 43 while (1) {
janjongboom 0:ba1c49874d3c 44 ble.waitForEvent();
janjongboom 0:ba1c49874d3c 45 }
janjongboom 0:ba1c49874d3c 46
janjongboom 0:ba1c49874d3c 47 }