A nice BLE demo program which allows remote switch of an LED via GATT interface.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Button by Bluetooth Low Energy

Committer:
hux
Date:
Sat Oct 21 19:56:15 2017 +0000
Revision:
13:0563f1aa6a75
Parent:
12:0d0ca44397dd
Switch LED via BLE GATT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 12:0d0ca44397dd 1 // M18_LED_Switch
hux 11:80f2c19ecbce 2
hux 11:80f2c19ecbce 3 #include "bricks/bricks.h"
hux 11:80f2c19ecbce 4
hux 12:0d0ca44397dd 5 Service svcLedSwitch(0xFF10,"LED Switch");
hux 12:0d0ca44397dd 6 Characteristic<Bool> chrSwitch(svcLedSwitch,0xFF11,"rw","Switch");
hux 12:0d0ca44397dd 7 Characteristic<Bool> chrState(svcLedSwitch,0xFF12,"n","State");
hux 11:80f2c19ecbce 8
hux 12:0d0ca44397dd 9 void cbWritten(O&o) // data written callback
hux 11:80f2c19ecbce 10 {
hux 12:0d0ca44397dd 11 Bool value; // need some local variables
hux 12:0d0ca44397dd 12
hux 12:0d0ca44397dd 13 if (updated(o,chrSwitch)) // changed inputs?
hux 12:0d0ca44397dd 14 {
hux 12:0d0ca44397dd 15 get(o,chrSwitch,value); // get value of Input1 characteristics
hux 12:0d0ca44397dd 16 set(o,chrState,value); // store result to Output characteristics
hux 12:0d0ca44397dd 17 blink(o,value ? "x" : " "); // depending on state change blinking
hux 12:0d0ca44397dd 18 }
hux 11:80f2c19ecbce 19 }
hux 12:0d0ca44397dd 20
hux 11:80f2c19ecbce 21 //==============================================================================
hux 11:80f2c19ecbce 22 // BLE Callbacks
hux 11:80f2c19ecbce 23 //==============================================================================
rgrover1 0:28f095301cb2 24
hux 12:0d0ca44397dd 25 void cbError(O&o) // Error Reporting Callback
hux 11:80f2c19ecbce 26 {
hux 12:0d0ca44397dd 27 blinkError(o); // indicate advertising
hux 11:80f2c19ecbce 28 }
rgrover1 0:28f095301cb2 29
hux 12:0d0ca44397dd 30 void cbConnect(O&o) // Connection Callback
hux 11:80f2c19ecbce 31 {
hux 12:0d0ca44397dd 32 blinkConnected(o,"x x x "); // indicate 'just connected'
hux 11:80f2c19ecbce 33 }
andresag 10:7943b5c1117a 34
hux 12:0d0ca44397dd 35 void cbDisconnect(O&o) // Disconnection Callback
hux 11:80f2c19ecbce 36 {
hux 12:0d0ca44397dd 37 advertise(o); // start advertising on client disconnect
hux 12:0d0ca44397dd 38 blinkAdvertise(o); // indicate advertising
hux 12:0d0ca44397dd 39 }
andresag 10:7943b5c1117a 40
hux 12:0d0ca44397dd 41 void cbSetup(O&o)
hux 12:0d0ca44397dd 42 {
hux 12:0d0ca44397dd 43 onConnect(o,cbConnect); // setup connection callback
hux 12:0d0ca44397dd 44 onDisconnect(o,cbDisconnect); // setup disconnection callback
hux 12:0d0ca44397dd 45
hux 12:0d0ca44397dd 46 enroll(o,svcLedSwitch); // enroll service functionality
hux 12:0d0ca44397dd 47 onWritten(o,cbWritten); // setup 'data written' callback
hux 11:80f2c19ecbce 48
hux 12:0d0ca44397dd 49 device(o,"N18#1.0 LED Switch"); // setup advertising name
hux 12:0d0ca44397dd 50 name(o,"LED Switch"); // setup device name
hux 11:80f2c19ecbce 51
hux 12:0d0ca44397dd 52 advertise(o,"C:ng",100); // start advertising @ 100 msec interval
hux 12:0d0ca44397dd 53 blinkAdvertise(o); // show that board is advertising
hux 11:80f2c19ecbce 54 }
andresag 10:7943b5c1117a 55
hux 11:80f2c19ecbce 56 main(void)
hux 11:80f2c19ecbce 57 {
hux 12:0d0ca44397dd 58 O o; // Oh ohh - declare a blob (BT OBject)
hux 12:0d0ca44397dd 59 blinkIdle(o); // idle blinking - just started!
andresag 10:7943b5c1117a 60
hux 12:0d0ca44397dd 61 init(o,cbSetup,cbError); // init BLE base layer, always do first
hux 11:80f2c19ecbce 62
hux 12:0d0ca44397dd 63 while (true) // Infinite loop waiting for BLE events
hux 12:0d0ca44397dd 64 sleep(o); // low power waiting for BLE events
hux 12:0d0ca44397dd 65 }
rgrover1 0:28f095301cb2 66