![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Bluetooth Connected TOF Sensor
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@28:def5e0f0fb06, 2017-02-01 (annotated)
- Committer:
- hux
- Date:
- Wed Feb 01 22:08:56 2017 +0000
- Revision:
- 28:def5e0f0fb06
- Parent:
- 27:32267cee7cb8
- Child:
- 29:cf61a5826426
First S16_Blue_ToF application which runs nicely!; Digital display can be switched on/off with red slider
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 28:def5e0f0fb06 | 1 | // S16_Blue_ToF - Blue button and Time-of-Flight Sensor Service over GATT |
hux | 28:def5e0f0fb06 | 2 | |
hux | 28:def5e0f0fb06 | 3 | #include "bricks/bricks.h" |
hux | 28:def5e0f0fb06 | 4 | #include "shields/shields.h" |
hux | 24:0f08f68579bd | 5 | |
hux | 28:def5e0f0fb06 | 6 | typedef DisplayShield Dish; |
hux | 28:def5e0f0fb06 | 7 | |
hux | 28:def5e0f0fb06 | 8 | static Dish *pShield; |
hux | 28:def5e0f0fb06 | 9 | Blinker blink; |
hux | 24:0f08f68579bd | 10 | |
hux | 22:d467526abc4a | 11 | //============================================================================== |
hux | 28:def5e0f0fb06 | 12 | // GATT Database Setup |
hux | 28:def5e0f0fb06 | 13 | //============================================================================== |
hux | 28:def5e0f0fb06 | 14 | |
hux | 28:def5e0f0fb06 | 15 | // Detection Service |
hux | 28:def5e0f0fb06 | 16 | |
hux | 28:def5e0f0fb06 | 17 | Service detection(0xA010,"Detection"); // Detection Service |
hux | 28:def5e0f0fb06 | 18 | Characteristic<Bool> chrPresence(detection,0xA011, "n", "Presence"); |
hux | 28:def5e0f0fb06 | 19 | |
hux | 28:def5e0f0fb06 | 20 | // Debug Service |
hux | 28:def5e0f0fb06 | 21 | |
hux | 28:def5e0f0fb06 | 22 | Service debug(0xA030,"Debug"); // Debug Service |
hux | 28:def5e0f0fb06 | 23 | Characteristic<Bool> chrTest (debug, 0xA031, "w", "Test"); |
hux | 28:def5e0f0fb06 | 24 | |
hux | 28:def5e0f0fb06 | 25 | static void cbWritten(Blob &o) // handle updates |
hux | 28:def5e0f0fb06 | 26 | { |
hux | 28:def5e0f0fb06 | 27 | Bool value; |
hux | 28:def5e0f0fb06 | 28 | |
hux | 28:def5e0f0fb06 | 29 | if (updated(o,chrTest)) // has chrTest been updated? |
hux | 28:def5e0f0fb06 | 30 | { |
hux | 28:def5e0f0fb06 | 31 | get(o,chrTest,value); // get value of chrTest |
hux | 28:def5e0f0fb06 | 32 | set(o,chrPresence,value); // and store this value to chrPresence |
hux | 28:def5e0f0fb06 | 33 | } |
hux | 28:def5e0f0fb06 | 34 | } |
hux | 28:def5e0f0fb06 | 35 | |
hux | 28:def5e0f0fb06 | 36 | void services(O&o) |
hux | 28:def5e0f0fb06 | 37 | { |
hux | 28:def5e0f0fb06 | 38 | enroll(o,detection); // enroll detection service |
hux | 28:def5e0f0fb06 | 39 | enroll(o,debug); // enroll debug service |
hux | 28:def5e0f0fb06 | 40 | |
hux | 28:def5e0f0fb06 | 41 | onWritten(o,cbWritten); // setup 'data written' callback |
hux | 28:def5e0f0fb06 | 42 | } |
hux | 28:def5e0f0fb06 | 43 | |
hux | 28:def5e0f0fb06 | 44 | //============================================================================== |
hux | 28:def5e0f0fb06 | 45 | // Button Functionality |
hux | 22:d467526abc4a | 46 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 47 | |
hux | 28:def5e0f0fb06 | 48 | typedef enum state { OFF, ON, IDLE } State; |
hux | 28:def5e0f0fb06 | 49 | |
hux | 28:def5e0f0fb06 | 50 | InterruptIn button(USER_BUTTON); // declare blue user button |
hux | 28:def5e0f0fb06 | 51 | State state = IDLE; |
hux | 28:def5e0f0fb06 | 52 | |
hux | 28:def5e0f0fb06 | 53 | static void cbRise(void) |
hux | 28:def5e0f0fb06 | 54 | { |
hux | 28:def5e0f0fb06 | 55 | O o; // declare a blob (BLE OBject) |
hux | 28:def5e0f0fb06 | 56 | Bool value = 0; |
hux | 28:def5e0f0fb06 | 57 | |
hux | 28:def5e0f0fb06 | 58 | if (o.hasInitialized()) |
hux | 28:def5e0f0fb06 | 59 | { |
hux | 28:def5e0f0fb06 | 60 | blink.off();; |
hux | 28:def5e0f0fb06 | 61 | state = OFF; |
hux | 28:def5e0f0fb06 | 62 | set(o,chrPresence,value); // and store this value to chrPresence |
hux | 28:def5e0f0fb06 | 63 | } |
hux | 28:def5e0f0fb06 | 64 | } |
hux | 28:def5e0f0fb06 | 65 | |
hux | 28:def5e0f0fb06 | 66 | static void cbFall(void) |
hux | 23:677689000369 | 67 | { |
hux | 28:def5e0f0fb06 | 68 | O o; // declare a blob (BLE OBject) |
hux | 28:def5e0f0fb06 | 69 | Bool value = 1; |
hux | 28:def5e0f0fb06 | 70 | |
hux | 28:def5e0f0fb06 | 71 | if (o.hasInitialized()) |
hux | 28:def5e0f0fb06 | 72 | { |
hux | 28:def5e0f0fb06 | 73 | blink.on(); |
hux | 28:def5e0f0fb06 | 74 | state = ON; |
hux | 28:def5e0f0fb06 | 75 | set(o,chrPresence,value); // and store this value to chrPresence |
hux | 28:def5e0f0fb06 | 76 | } |
hux | 28:def5e0f0fb06 | 77 | } |
hux | 28:def5e0f0fb06 | 78 | |
hux | 28:def5e0f0fb06 | 79 | void setupButton() // enroll button functionality |
hux | 28:def5e0f0fb06 | 80 | { |
hux | 28:def5e0f0fb06 | 81 | button.rise(&cbRise); // interrupt callback setup |
hux | 28:def5e0f0fb06 | 82 | button.fall(&cbFall); // interrupt callback setup |
hux | 28:def5e0f0fb06 | 83 | } |
hux | 28:def5e0f0fb06 | 84 | |
hux | 28:def5e0f0fb06 | 85 | //============================================================================== |
hux | 28:def5e0f0fb06 | 86 | // Setup BLE Functionality |
hux | 28:def5e0f0fb06 | 87 | //============================================================================== |
hux | 28:def5e0f0fb06 | 88 | |
hux | 28:def5e0f0fb06 | 89 | void cbError(Blob &o) // Error Reporting Callback |
hux | 28:def5e0f0fb06 | 90 | { |
hux | 28:def5e0f0fb06 | 91 | blink.error(); // 'error' blink sequence |
hux | 24:0f08f68579bd | 92 | } |
apalmieri | 13:227a0149b677 | 93 | |
hux | 28:def5e0f0fb06 | 94 | void cbConnect(Blob &o) // Connection Callback |
hux | 22:d467526abc4a | 95 | { |
hux | 28:def5e0f0fb06 | 96 | blink.connected(); // 'error' blink sequence |
hux | 22:d467526abc4a | 97 | } |
apalmieri | 13:227a0149b677 | 98 | |
hux | 28:def5e0f0fb06 | 99 | void cbDisconnect(Blob &o) // Disconnection Callback |
hux | 22:d467526abc4a | 100 | { |
hux | 27:32267cee7cb8 | 101 | advertise(o); // start advertising on client disconnect |
hux | 28:def5e0f0fb06 | 102 | blink.advertise(); // 'advertise' blink sequence |
hux | 22:d467526abc4a | 103 | } |
hux | 22:d467526abc4a | 104 | |
hux | 28:def5e0f0fb06 | 105 | void cbInitComplete(Blob &o) // Immediately After Initializing BLE |
hux | 24:0f08f68579bd | 106 | { |
hux | 27:32267cee7cb8 | 107 | services(o); // enroll all services & setup callbacks |
hux | 24:0f08f68579bd | 108 | |
hux | 27:32267cee7cb8 | 109 | onConnect(o,cbConnect); // setup connection callback |
hux | 27:32267cee7cb8 | 110 | onDisconnect(o,cbDisconnect); // setup disconnection callback |
hux | 24:0f08f68579bd | 111 | |
hux | 28:def5e0f0fb06 | 112 | device(o,"S16#2.0 Blue TOF"); |
hux | 28:def5e0f0fb06 | 113 | name(o,"TOF HR"); |
hux | 24:0f08f68579bd | 114 | |
hux | 27:32267cee7cb8 | 115 | advertise(o,"C:ng",100); // start advertising @ 100 msec interval |
hux | 28:def5e0f0fb06 | 116 | blink.advertise(); // 'advertise' blink sequence |
hux | 28:def5e0f0fb06 | 117 | } |
hux | 28:def5e0f0fb06 | 118 | |
hux | 28:def5e0f0fb06 | 119 | //============================================================================== |
hux | 28:def5e0f0fb06 | 120 | // Simple Loop - Cannot Change Mode |
hux | 28:def5e0f0fb06 | 121 | //============================================================================== |
hux | 28:def5e0f0fb06 | 122 | |
hux | 28:def5e0f0fb06 | 123 | void distanceMeasurement(Dish &d) |
hux | 28:def5e0f0fb06 | 124 | { |
hux | 28:def5e0f0fb06 | 125 | uint32_t dist; |
hux | 28:def5e0f0fb06 | 126 | |
hux | 28:def5e0f0fb06 | 127 | pShield->pBoard->sensor_top->GetDistance(&dist); |
hux | 28:def5e0f0fb06 | 128 | |
hux | 28:def5e0f0fb06 | 129 | if (dist == 0xFF) |
hux | 28:def5e0f0fb06 | 130 | dist = 0xFFFFFFFF; |
hux | 28:def5e0f0fb06 | 131 | |
hux | 28:def5e0f0fb06 | 132 | d.data.range_mm = dist; |
hux | 22:d467526abc4a | 133 | } |
hux | 24:0f08f68579bd | 134 | |
hux | 28:def5e0f0fb06 | 135 | void loop(Blob &o, Dish &d) |
hux | 28:def5e0f0fb06 | 136 | { |
hux | 28:def5e0f0fb06 | 137 | Bool state = false; |
hux | 28:def5e0f0fb06 | 138 | |
hux | 28:def5e0f0fb06 | 139 | for (;;) |
hux | 28:def5e0f0fb06 | 140 | { |
hux | 28:def5e0f0fb06 | 141 | distanceMeasurement(d); |
hux | 28:def5e0f0fb06 | 142 | if (d.data.range_mm != 0xFFFFFFFF) |
hux | 28:def5e0f0fb06 | 143 | { if (state == false) |
hux | 28:def5e0f0fb06 | 144 | { blink.on(); // switch LED on |
hux | 28:def5e0f0fb06 | 145 | state = true; |
hux | 28:def5e0f0fb06 | 146 | set(o,chrPresence,state); |
hux | 28:def5e0f0fb06 | 147 | } |
hux | 28:def5e0f0fb06 | 148 | } |
hux | 28:def5e0f0fb06 | 149 | else |
hux | 28:def5e0f0fb06 | 150 | { if (state == true) |
hux | 28:def5e0f0fb06 | 151 | { if (isConnected(o)) |
hux | 28:def5e0f0fb06 | 152 | blink.connected(); // switch LED off |
hux | 28:def5e0f0fb06 | 153 | else |
hux | 28:def5e0f0fb06 | 154 | blink.advertise(); // switch LED off |
hux | 28:def5e0f0fb06 | 155 | state = false; |
hux | 28:def5e0f0fb06 | 156 | set(o,chrPresence,state); |
hux | 28:def5e0f0fb06 | 157 | } |
hux | 28:def5e0f0fb06 | 158 | } |
hux | 28:def5e0f0fb06 | 159 | |
hux | 28:def5e0f0fb06 | 160 | if (d.red()) // red slider switched to 'RANGE'? |
hux | 28:def5e0f0fb06 | 161 | d.refresh(); // refresh display |
hux | 28:def5e0f0fb06 | 162 | sleep(o); // 'sleep(o,10)' causes connect issues |
hux | 28:def5e0f0fb06 | 163 | } |
hux | 28:def5e0f0fb06 | 164 | } |
hux | 28:def5e0f0fb06 | 165 | |
hux | 22:d467526abc4a | 166 | //============================================================================== |
hux | 22:d467526abc4a | 167 | // Main Program |
hux | 22:d467526abc4a | 168 | //============================================================================== |
hux | 22:d467526abc4a | 169 | |
hux | 22:d467526abc4a | 170 | int main(void) |
hux | 22:d467526abc4a | 171 | { |
hux | 28:def5e0f0fb06 | 172 | Blob o; |
hux | 28:def5e0f0fb06 | 173 | verbose(o); // enable all trace messages |
hux | 28:def5e0f0fb06 | 174 | trace(o,1,"\nS16_Blue_ToF\n"); |
hux | 28:def5e0f0fb06 | 175 | blink.idle(); |
hux | 28:def5e0f0fb06 | 176 | |
hux | 28:def5e0f0fb06 | 177 | pShield = new Dish("Init"); // init shield |
hux | 28:def5e0f0fb06 | 178 | pShield->setup(InterruptTOF); // setup fot ToF measurement only |
hux | 28:def5e0f0fb06 | 179 | |
hux | 28:def5e0f0fb06 | 180 | init(o,cbInitComplete); // init BLE system |
hux | 28:def5e0f0fb06 | 181 | setupButton(); // enroll button functionality |
hux | 28:def5e0f0fb06 | 182 | |
hux | 28:def5e0f0fb06 | 183 | loop(o,*pShield); // run measurement loop |
hux | 28:def5e0f0fb06 | 184 | } |
apalmieri | 13:227a0149b677 | 185 |