A blue button is always a nice toy ...

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sun Oct 01 12:49:25 2017 +0000
Revision:
29:a6b74dfdd5f2
Parent:
28:307f58df778a
A blue button is always a nice toy ...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 28:307f58df778a 1 // S15_Blue_Button - GATT Detector setup with
hux 28:307f58df778a 2 // Blue Button connected to Presence characteristic
hux 24:0f08f68579bd 3
hux 26:fd06c8b57d16 4 #include "bricks/bricks.h"
hux 28:307f58df778a 5
hux 28:307f58df778a 6 Blinker blink;
hux 28:307f58df778a 7
hux 28:307f58df778a 8 //==============================================================================
hux 28:307f58df778a 9 // GATT Database Setup
hux 28:307f58df778a 10 //==============================================================================
hux 28:307f58df778a 11
hux 28:307f58df778a 12 // Detection Service
hux 28:307f58df778a 13
hux 28:307f58df778a 14 Service detection(0xA010,"Detection"); // Detection Service
hux 28:307f58df778a 15 Characteristic<Bool> chrPresence(detection,0xA011, "n", "Presence");
hux 28:307f58df778a 16
hux 28:307f58df778a 17 // Debug Service
hux 28:307f58df778a 18
hux 28:307f58df778a 19 Service debug(0xA030,"Debug"); // Debug Service
hux 28:307f58df778a 20 Characteristic<Bool> chrTest (debug, 0xA031, "w", "Test");
hux 28:307f58df778a 21
hux 28:307f58df778a 22 static void cbWritten(Blob &o) // handle updates
hux 28:307f58df778a 23 {
hux 28:307f58df778a 24 Bool value;
hux 28:307f58df778a 25
hux 28:307f58df778a 26 if (updated(o,chrTest)) // has chrTest been updated?
hux 28:307f58df778a 27 {
hux 28:307f58df778a 28 get(o,chrTest,value); // get value of chrTest
hux 28:307f58df778a 29 set(o,chrPresence,value); // and store this value to chrPresence
hux 28:307f58df778a 30 }
hux 28:307f58df778a 31 }
hux 24:0f08f68579bd 32
hux 28:307f58df778a 33 void services(O&o)
hux 28:307f58df778a 34 {
hux 28:307f58df778a 35 enroll(o,detection); // enroll detection service
hux 28:307f58df778a 36 enroll(o,debug); // enroll debug service
hux 28:307f58df778a 37
hux 28:307f58df778a 38 onWritten(o,cbWritten); // setup 'data written' callback
hux 28:307f58df778a 39 }
hux 28:307f58df778a 40
hux 28:307f58df778a 41 //==============================================================================
hux 28:307f58df778a 42 // Button Functionality
hux 28:307f58df778a 43 //==============================================================================
hux 28:307f58df778a 44
hux 28:307f58df778a 45 typedef enum state { OFF, ON, IDLE } State;
hux 28:307f58df778a 46
hux 28:307f58df778a 47 State state = IDLE;
hux 28:307f58df778a 48
hux 28:307f58df778a 49 static void cbRise(void)
hux 28:307f58df778a 50 {
hux 28:307f58df778a 51 O o; // declare a blob (BLE OBject)
hux 28:307f58df778a 52 Bool value = 0;
hux 28:307f58df778a 53
hux 28:307f58df778a 54 if (o.hasInitialized())
hux 28:307f58df778a 55 {
hux 28:307f58df778a 56 state = OFF;
hux 28:307f58df778a 57 set(o,chrPresence,value); // and store this value to chrPresence
hux 28:307f58df778a 58 }
hux 28:307f58df778a 59 }
hux 28:307f58df778a 60
hux 28:307f58df778a 61 static void cbFall(void)
hux 28:307f58df778a 62 {
hux 28:307f58df778a 63 O o; // declare a blob (BLE OBject)
hux 28:307f58df778a 64 Bool value = 1;
hux 28:307f58df778a 65
hux 28:307f58df778a 66 if (o.hasInitialized())
hux 28:307f58df778a 67 {
hux 28:307f58df778a 68 state = ON;
hux 28:307f58df778a 69 set(o,chrPresence,value); // and store this value to chrPresence
hux 28:307f58df778a 70 }
hux 28:307f58df778a 71 }
hux 28:307f58df778a 72
hux 22:d467526abc4a 73 //==============================================================================
hux 24:0f08f68579bd 74 // Callbacks
hux 22:d467526abc4a 75 //==============================================================================
screamer 0:eb7f02ad28a7 76
hux 27:32267cee7cb8 77 void cbError(O&o) // Error Reporting Callback
hux 23:677689000369 78 {
hux 28:307f58df778a 79 blink.error(); // 'error' blink sequence
hux 24:0f08f68579bd 80 }
apalmieri 13:227a0149b677 81
hux 27:32267cee7cb8 82 void cbConnect(O&o) // Connection Callback
hux 22:d467526abc4a 83 {
hux 28:307f58df778a 84 blink.connected(); // 'error' blink sequence
hux 22:d467526abc4a 85 }
apalmieri 13:227a0149b677 86
hux 27:32267cee7cb8 87 void cbDisconnect(O&o) // Disconnection Callback
hux 22:d467526abc4a 88 {
hux 27:32267cee7cb8 89 advertise(o); // start advertising on client disconnect
hux 28:307f58df778a 90 blink.advertise(); // 'advertise' blink sequence
hux 22:d467526abc4a 91 }
hux 22:d467526abc4a 92
hux 27:32267cee7cb8 93 void cbSetup(O&o) // Immediately After Initializing BLE
hux 24:0f08f68579bd 94 {
hux 27:32267cee7cb8 95 services(o); // enroll all services & setup callbacks
hux 24:0f08f68579bd 96
hux 27:32267cee7cb8 97 onConnect(o,cbConnect); // setup connection callback
hux 27:32267cee7cb8 98 onDisconnect(o,cbDisconnect); // setup disconnection callback
hux 24:0f08f68579bd 99
hux 28:307f58df778a 100 device(o,"S15#2.0 Blue"); // setup device name
hux 27:32267cee7cb8 101 name(o,"Detector"); // setup advertising name
hux 27:32267cee7cb8 102 data(o,"My Layout"); // setup advertising data
hux 24:0f08f68579bd 103
hux 27:32267cee7cb8 104 advertise(o,"C:ng",100); // start advertising @ 100 msec interval
hux 28:307f58df778a 105 blink.advertise(); // 'advertise' blink sequence
hux 22:d467526abc4a 106 }
hux 24:0f08f68579bd 107
hux 22:d467526abc4a 108 //==============================================================================
hux 22:d467526abc4a 109 // Main Program
hux 22:d467526abc4a 110 //==============================================================================
hux 22:d467526abc4a 111
hux 22:d467526abc4a 112 int main(void)
hux 22:d467526abc4a 113 {
hux 27:32267cee7cb8 114 O o; // declare a blob (BLE OBject)
hux 27:32267cee7cb8 115 verbose(o); // enable all trace messages
hux 28:307f58df778a 116 blink.idle(); // idle blinking - just started!
hux 28:307f58df778a 117
hux 28:307f58df778a 118 InterruptIn button(USER_BUTTON); // declare blue user button
hux 28:307f58df778a 119 button.rise(&cbRise); // interrupt callback setup
hux 28:307f58df778a 120 button.fall(&cbFall); // interrupt callback setup
apalmieri 13:227a0149b677 121
hux 27:32267cee7cb8 122 init(o,cbSetup,cbError); // init BLE base layer, always do first
hux 22:d467526abc4a 123
hux 22:d467526abc4a 124 while (true) // Infinite loop waiting for BLE events
hux 28:307f58df778a 125 {
hux 27:32267cee7cb8 126 sleep(o); // low power waiting for BLE events
hux 28:307f58df778a 127
hux 28:307f58df778a 128 if (state == ON) // detection active ?
hux 28:307f58df778a 129 blink.blink("xxx ");
hux 28:307f58df778a 130
hux 28:307f58df778a 131 if (state == OFF) // detection inactive ?
hux 28:307f58df778a 132 blink.blink("x ");
hux 28:307f58df778a 133
hux 28:307f58df778a 134 state = IDLE; // reset to IDLE state
hux 28:307f58df778a 135 }
hux 24:0f08f68579bd 136 }