A cute tiny piece of code implementing an IoT NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service. The code has been tested on a Nordic nRF51822-DK.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sun Jan 08 23:15:53 2017 +0000
Revision:
23:2e73c391bb12
Parent:
22:d467526abc4a
Child:
25:339931243be4
A cute tiny piece of code implementing an Iot NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 23:2e73c391bb12 1 // N06_NAND: demonstration of 'NAND' GATT service
hux 23:2e73c391bb12 2 // Program has been tested on nRF51822-DK
hux 23:2e73c391bb12 3
hux 23:2e73c391bb12 4 #include "bricks/bricks.h"
apalmieri 13:227a0149b677 5
hux 23:2e73c391bb12 6 Service tiny(0xA000,"Tiny"); // Tiny Service
hux 23:2e73c391bb12 7 Characteristic<Digital> chrInput1(tiny, 0xA001, "w", "Input1");
hux 23:2e73c391bb12 8 Characteristic<Digital> chrInput2(tiny, 0xA002, "w", "Input2");
hux 23:2e73c391bb12 9 Characteristic<Digital> chrOutput(tiny, 0xA003, "n", "Output");
screamer 0:eb7f02ad28a7 10
hux 23:2e73c391bb12 11 void cbWritten(O&o) // data written callback
hux 22:d467526abc4a 12 {
hux 23:2e73c391bb12 13 Digital in1, in2, out; // need some local variables
hux 23:2e73c391bb12 14
hux 23:2e73c391bb12 15 if (updated(o,chrInput1) || updated(o,chrInput2)) // changed inputs?
hux 23:2e73c391bb12 16 {
hux 23:2e73c391bb12 17 get(o,chrInput1,in1); // get value of Input1 characteristics
hux 23:2e73c391bb12 18 get(o,chrInput2,in2); // get value of Input2 characteristics
hux 23:2e73c391bb12 19 out = !(in1 && in2); // perform NAND (Not AND) operation
hux 23:2e73c391bb12 20 set(o,chrOutput,out); // store result to Output characteristics
hux 23:2e73c391bb12 21 }
hux 22:d467526abc4a 22 }
hux 22:d467526abc4a 23
hux 23:2e73c391bb12 24 void cbDisconnect(O&o) // disconnection callback
hux 22:d467526abc4a 25 {
hux 23:2e73c391bb12 26 advertise(o); // resume advertising on disconnection
hux 22:d467526abc4a 27 }
hux 23:2e73c391bb12 28
hux 23:2e73c391bb12 29 void cbSetup(O&o) // setup calback (after BLE init)
hux 22:d467526abc4a 30 {
hux 23:2e73c391bb12 31 onDisconnect(o,cbDisconnect); // setup disconnection callback
hux 23:2e73c391bb12 32 onWritten(o,cbWritten); // setup data written callback
hux 23:2e73c391bb12 33
hux 23:2e73c391bb12 34 device(o,"NAND Gate"); // setup device name
hux 23:2e73c391bb12 35 name(o,"N04#1.0 NAND"); // add name to device
hux 23:2e73c391bb12 36 enroll(o,tiny); // enroll (register) TINY service
screamer 0:eb7f02ad28a7 37
hux 23:2e73c391bb12 38 advertise(o,"C:ng",100); // start advertising @ 100 msec interval
hux 22:d467526abc4a 39 }
apalmieri 13:227a0149b677 40
hux 22:d467526abc4a 41 int main(void)
hux 22:d467526abc4a 42 {
hux 23:2e73c391bb12 43 O o; // Oh-ohh - our Blob (BLuetooth OBject)
hux 23:2e73c391bb12 44 init(o,cbSetup); // init BLE base layer, always do first
hux 23:2e73c391bb12 45 for(;;) sleep(o); // low power waiting for BLE events
hux 23:2e73c391bb12 46 }