Bluetooth Connected TOF Sensor

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Fri Jan 06 15:28:18 2017 +0000
Revision:
23:677689000369
Parent:
22:d467526abc4a
Child:
24:0f08f68579bd
Already nice & easy level. Still bugs!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 22:d467526abc4a 1 #include <mbed.h>
hux 22:d467526abc4a 2 #include "ble/BLE.h"
hux 22:d467526abc4a 3 #include "bricks.h"
screamer 0:eb7f02ad28a7 4
hux 23:677689000369 5 Serial out(USBTX, USBRX); // serial port to PC terminal
hux 23:677689000369 6
hux 22:d467526abc4a 7 //==============================================================================
hux 22:d467526abc4a 8 // Custom Service Definition
hux 22:d467526abc4a 9 //==============================================================================
apalmieri 13:227a0149b677 10
hux 23:677689000369 11 // occupation service
hux 23:677689000369 12
hux 23:677689000369 13 Collection detection;
hux 23:677689000369 14 Characteristic<Digital> presence (detection,0xA011, "rn", "Presence");
hux 23:677689000369 15 Characteristic<DateTime> timestamp(detection,0xA012, "r", "Timestamp");
hux 23:677689000369 16 Characteristic<Digital> test (detection,0xA013, "w", "Test");
screamer 0:eb7f02ad28a7 17
hux 23:677689000369 18 Service service1(detection,0xA010); // our occupation service
hux 23:677689000369 19
hux 23:677689000369 20 // address service
hux 23:677689000369 21
hux 23:677689000369 22 Collection address;
hux 23:677689000369 23 Characteristic<Digital> id (address, 0xA021, "ran", "ID");
hux 23:677689000369 24 Characteristic<Digital> name (address, 0xA022, "ran", "Name");
hux 23:677689000369 25 Characteristic<Digital> layout (address, 0xA023, "ran", "Layout");
screamer 0:eb7f02ad28a7 26
hux 23:677689000369 27 Service service2(address,0xA020); // our occupation service
hux 23:677689000369 28
hux 23:677689000369 29
hux 22:d467526abc4a 30 //==============================================================================
hux 22:d467526abc4a 31 // Some Callbacks
hux 22:d467526abc4a 32 //==============================================================================
screamer 0:eb7f02ad28a7 33
hux 23:677689000369 34 void print(Blob &blue, GattCharacteristic &chr, const char *name)
hux 22:d467526abc4a 35 {
hux 23:677689000369 36 Buffer data; uint8_t *p = data;
hux 23:677689000369 37 blue.get(chr,data);
hux 23:677689000369 38
hux 23:677689000369 39 out.printf("%s: %02x-%02x-%02x-%02x-%02x-",name,(int)(p[0]),(int)(p[1]),(int)(p[2]),(int)(p[3]),(int)(p[4]));
hux 23:677689000369 40 out.printf("%02x-%02x-%02x-%02x-%02x\r\n",(int)(p[5]),(int)(p[6]),(int)(p[7]),(int)(p[8]),(int)(p[9]));
hux 23:677689000369 41 }
hux 23:677689000369 42
hux 23:677689000369 43
hux 23:677689000369 44 void onWritten(Blob &o) // Handle writes to writeCharacteristic
hux 23:677689000369 45 {
hux 23:677689000369 46 const GattWriteCallbackParams *p = o.pWritten;
hux 22:d467526abc4a 47
hux 22:d467526abc4a 48 // Check to see what characteristic was written, by handle
hux 22:d467526abc4a 49
hux 23:677689000369 50 if(p->handle == test.getValueHandle())
hux 23:677689000369 51 { Digital value;
hux 23:677689000369 52
hux 23:677689000369 53 get(o,test,value);
hux 23:677689000369 54 set(o,presence,value);
apalmieri 13:227a0149b677 55
hux 23:677689000369 56 print(o,test,"test");
hux 23:677689000369 57 blink("x x x xxx xxx xxx x ",CONNECTED);
hux 23:677689000369 58 }
hux 22:d467526abc4a 59 }
hux 22:d467526abc4a 60
apalmieri 13:227a0149b677 61
hux 22:d467526abc4a 62 void onConnect(Blob &blue) // Connection Callback
hux 22:d467526abc4a 63 {
hux 23:677689000369 64 blink("x x x ",CONNECTED); // indicate advertising
hux 22:d467526abc4a 65 }
apalmieri 13:227a0149b677 66
screamer 0:eb7f02ad28a7 67
hux 22:d467526abc4a 68 void onDisconnect(Blob &blue) // Disconnection Callback
hux 22:d467526abc4a 69 {
hux 22:d467526abc4a 70 blue.start(); // start advertising on client disconnect
hux 22:d467526abc4a 71 blink(ADVERTISE); // indicate advertising
hux 22:d467526abc4a 72 }
hux 22:d467526abc4a 73
screamer 0:eb7f02ad28a7 74
hux 22:d467526abc4a 75 void onError(Blob &blue) // Error Reporting Callback
hux 22:d467526abc4a 76 {
hux 22:d467526abc4a 77 (void) blue; // Avoid compiler warnings
hux 22:d467526abc4a 78 blink(FAULT); // indicate advertising
hux 22:d467526abc4a 79 }
hux 22:d467526abc4a 80
screamer 0:eb7f02ad28a7 81
hux 22:d467526abc4a 82 void onSetup(Blob &blue) // Immediately After Initializing BLE
hux 22:d467526abc4a 83 {
hux 23:677689000369 84 blue.config(service1); // configure custom service 1
hux 23:677689000369 85 blue.config(service1); // configure custom service 2
hux 23:677689000369 86
hux 23:677689000369 87 blue.name("T07N#8 Easy GATT"); // setup advertising name
hux 22:d467526abc4a 88 blue.device("nRF51-DK"); // setup device name
hux 22:d467526abc4a 89 blue.data("ABCDEF"); // setup advertising data
screamer 0:eb7f02ad28a7 90
hux 22:d467526abc4a 91 blue.onConnect(onConnect); // setup connection callback
hux 22:d467526abc4a 92 blue.onDisconnect(onDisconnect); // setup disconnection callback
hux 22:d467526abc4a 93 blue.onWritten(onWritten); // setup data written callback
hux 22:d467526abc4a 94
hux 22:d467526abc4a 95 blue.advertise("C:ng",100); // start advertising @ 100 msec interval
hux 22:d467526abc4a 96 blink(ADVERTISE); // show that board is advertising
hux 22:d467526abc4a 97 }
apalmieri 13:227a0149b677 98
hux 22:d467526abc4a 99 //==============================================================================
hux 22:d467526abc4a 100 // Main Program
hux 22:d467526abc4a 101 //==============================================================================
hux 22:d467526abc4a 102
hux 22:d467526abc4a 103 int main(void)
hux 22:d467526abc4a 104 {
hux 23:677689000369 105 out.printf(" hello\r\n");
hux 23:677689000369 106
hux 23:677689000369 107 verbose(10); // print all verbose messages
hux 22:d467526abc4a 108 blink(IDLE); // idle blinking - just started!
apalmieri 13:227a0149b677 109
hux 23:677689000369 110 Blob blue; // declare a blob (BLE OBject)
hux 22:d467526abc4a 111 blue.init(onSetup,onError); // init BLE base layer, always do first
hux 22:d467526abc4a 112
hux 22:d467526abc4a 113 while (true) // Infinite loop waiting for BLE events
hux 22:d467526abc4a 114 blue.sleep(); // low power waiting for BLE events
hux 22:d467526abc4a 115 }
apalmieri 14:f715c13eb84f 116