![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TOF based Presence Detector
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
detection.cpp@28:a23b16555909, 2018-08-20 (annotated)
- Committer:
- hux
- Date:
- Mon Aug 20 16:42:59 2018 +0000
- Revision:
- 28:a23b16555909
- Parent:
- 27:32267cee7cb8
TOF Detector - Basis for Presence Detection
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 27:32267cee7cb8 | 1 | // detection.cpp - setup and handle DETECTION services |
hux | 27:32267cee7cb8 | 2 | |
hux | 27:32267cee7cb8 | 3 | #include "detection.h" |
hux | 27:32267cee7cb8 | 4 | |
hux | 27:32267cee7cb8 | 5 | |
hux | 27:32267cee7cb8 | 6 | // declare a CharacteristicDataInitializer which can be used to initialize |
hux | 27:32267cee7cb8 | 7 | // all our characteristics |
hux | 27:32267cee7cb8 | 8 | |
hux | 27:32267cee7cb8 | 9 | CharacteristicInitializer ini; |
hux | 27:32267cee7cb8 | 10 | |
hux | 27:32267cee7cb8 | 11 | // Detection Service |
hux | 27:32267cee7cb8 | 12 | |
hux | 27:32267cee7cb8 | 13 | Service detection(0xA010,"Detection"); // Detection Service |
hux | 27:32267cee7cb8 | 14 | Characteristic<Bool> chrPresence(detection,0xA011, "n", "Presence"); |
hux | 27:32267cee7cb8 | 15 | |
hux | 27:32267cee7cb8 | 16 | // Address Service |
hux | 27:32267cee7cb8 | 17 | |
hux | 27:32267cee7cb8 | 18 | Service address(0xA020,"Address"); // Address Service |
hux | 27:32267cee7cb8 | 19 | Characteristic<ObjectId> chrId (address, 0xA021, "na", "ID"); |
hux | 28:a23b16555909 | 20 | //Characteristic<ObjectName> chrName (address, 0xA022, "na", "Name"); |
hux | 27:32267cee7cb8 | 21 | //Characteristic<ObjectName> chrLayout (address, 0xA023, "na", "Layout"); |
hux | 27:32267cee7cb8 | 22 | |
hux | 27:32267cee7cb8 | 23 | // here is the issue: if above declaration of the characteristic chrName |
hux | 27:32267cee7cb8 | 24 | // is being uncommented the GATT database will be corrupted. It has been |
hux | 27:32267cee7cb8 | 25 | // found out by trial & error that below 'tedious' code is a fix. |
hux | 28:a23b16555909 | 26 | /* |
hux | 27:32267cee7cb8 | 27 | UserDescriptor(nameLayout,attLayout,dscLayout,"Layout") |
hux | 27:32267cee7cb8 | 28 | static ReadOnlyGattCharacteristic<ObjectName> _chrLayout(0xA023,ini.pObjectName(),0,dscLayout,1); |
hux | 27:32267cee7cb8 | 29 | |
hux | 27:32267cee7cb8 | 30 | static GattCharacteristic *addressList[] = {&chrId,&chrName,&_chrLayout}; |
hux | 27:32267cee7cb8 | 31 | static GattService _address(0xA020, addressList, GattListLength(addressList)); |
hux | 28:a23b16555909 | 32 | */ |
hux | 27:32267cee7cb8 | 33 | // Debug Service |
hux | 27:32267cee7cb8 | 34 | |
hux | 27:32267cee7cb8 | 35 | Service debug(0xA030,"Debug"); // Debug Service |
hux | 27:32267cee7cb8 | 36 | Characteristic<Bool> chrTest (debug, 0xA031, "w", "Test"); |
hux | 27:32267cee7cb8 | 37 | Characteristic<DateTime> chrTimestamp (debug, 0xA032, "r", "Timestamp"); |
hux | 27:32267cee7cb8 | 38 | |
hux | 27:32267cee7cb8 | 39 | //============================================================================== |
hux | 27:32267cee7cb8 | 40 | // Update Callback |
hux | 27:32267cee7cb8 | 41 | //============================================================================== |
hux | 27:32267cee7cb8 | 42 | |
hux | 27:32267cee7cb8 | 43 | static void cbWritten(Blob &o) // handle updates |
hux | 27:32267cee7cb8 | 44 | { |
hux | 27:32267cee7cb8 | 45 | Bool value; |
hux | 27:32267cee7cb8 | 46 | |
hux | 27:32267cee7cb8 | 47 | if (updated(o,chrTest)) // has chrTest been updated? |
hux | 27:32267cee7cb8 | 48 | { |
hux | 27:32267cee7cb8 | 49 | get(o,chrTest,value); // get value of chrTest |
hux | 27:32267cee7cb8 | 50 | set(o,chrPresence,value); // and store this value to chrPresence |
hux | 27:32267cee7cb8 | 51 | |
hux | 27:32267cee7cb8 | 52 | print(o,value,"test"); |
hux | 27:32267cee7cb8 | 53 | if (value == 0) |
hux | 27:32267cee7cb8 | 54 | blinkConnected(o,"x x "); |
hux | 27:32267cee7cb8 | 55 | else if (value == 1) |
hux | 27:32267cee7cb8 | 56 | blinkConnected(o,"xxx xxx "); |
hux | 27:32267cee7cb8 | 57 | else |
hux | 27:32267cee7cb8 | 58 | blinkConnected(o,"x xxx x xxx "); |
hux | 27:32267cee7cb8 | 59 | } |
hux | 27:32267cee7cb8 | 60 | else if (updated(o,chrPresence)) // has chrPresence been updated? |
hux | 27:32267cee7cb8 | 61 | { |
hux | 27:32267cee7cb8 | 62 | get(o,chrPresence,value); |
hux | 27:32267cee7cb8 | 63 | print(o,value,"test"); |
hux | 27:32267cee7cb8 | 64 | } |
hux | 27:32267cee7cb8 | 65 | } |
hux | 27:32267cee7cb8 | 66 | |
hux | 27:32267cee7cb8 | 67 | //============================================================================== |
hux | 27:32267cee7cb8 | 68 | // Register Services and Callbacks |
hux | 27:32267cee7cb8 | 69 | //============================================================================== |
hux | 27:32267cee7cb8 | 70 | |
hux | 27:32267cee7cb8 | 71 | void services(Blob &o) // enroll all services & characteristics |
hux | 27:32267cee7cb8 | 72 | { |
hux | 27:32267cee7cb8 | 73 | enroll(o,detection); // enroll Detection Service |
hux | 28:a23b16555909 | 74 | enroll(o,address); // enroll Address Service |
hux | 27:32267cee7cb8 | 75 | enroll(o,debug); // enroll Debug Service |
hux | 27:32267cee7cb8 | 76 | |
hux | 27:32267cee7cb8 | 77 | onWritten(o,cbWritten); // setup 'data written' callback |
hux | 27:32267cee7cb8 | 78 | } |
hux | 27:32267cee7cb8 | 79 | |
hux | 27:32267cee7cb8 | 80 | //eof |