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:
Sat May 19 14:10:17 2018 +0000
Revision:
26:dce30a5341bb
Parent:
25:339931243be4
Published

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 25:339931243be4 29 int main(void)
hux 22:d467526abc4a 30 {
hux 25:339931243be4 31 O o; // Oh-ohh - our Blob (BLuetooth OBject)
hux 25:339931243be4 32 init(o); // init BLE base layer, always do first
hux 25:339931243be4 33
hux 23:2e73c391bb12 34 onDisconnect(o,cbDisconnect); // setup disconnection callback
hux 23:2e73c391bb12 35 onWritten(o,cbWritten); // setup data written callback
hux 23:2e73c391bb12 36
hux 23:2e73c391bb12 37 device(o,"NAND Gate"); // setup device name
hux 25:339931243be4 38 name(o,"N06#2.0 NAND"); // add name to device
hux 23:2e73c391bb12 39 enroll(o,tiny); // enroll (register) TINY service
screamer 0:eb7f02ad28a7 40
hux 23:2e73c391bb12 41 advertise(o,"C:ng",100); // start advertising @ 100 msec interval
apalmieri 13:227a0149b677 42
hux 23:2e73c391bb12 43 for(;;) sleep(o); // low power waiting for BLE events
hux 23:2e73c391bb12 44 }