Demo to get the die sensors value of 51822
Dependencies: BLE_API Sensors mbed nRF51822
Fork of LinkNode_DataAdvertising by
main.cpp@7:f3e587ca9139, 2016-04-06 (annotated)
- Committer:
- helloqi
- Date:
- Wed Apr 06 08:57:58 2016 +0000
- Revision:
- 7:f3e587ca9139
- Parent:
- 6:b3d3351aadc6
change the name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunsmile2015 | 0:3dc6e424dba0 | 1 | #include "mbed.h" |
sunsmile2015 | 0:3dc6e424dba0 | 2 | #include "ble/BLE.h" |
helloqi | 5:d30cc8560678 | 3 | #include "Sensors/Sensors.h" |
sunsmile2015 | 0:3dc6e424dba0 | 4 | |
sunsmile2015 | 4:e5fa4c8838db | 5 | #define APP_SPECIFIC_ID_TEST 0xFEFE |
sunsmile2015 | 3:3eda308b78e6 | 6 | |
sunsmile2015 | 3:3eda308b78e6 | 7 | #pragma pack(1) |
helloqi | 7:f3e587ca9139 | 8 | struct ApplicationData_t |
helloqi | 7:f3e587ca9139 | 9 | { |
sunsmile2015 | 4:e5fa4c8838db | 10 | uint16_t applicationSpecificId; /* An ID used to identify temperature value |
sunsmile2015 | 4:e5fa4c8838db | 11 | in the manufacture specific AD data field */ |
helloqi | 5:d30cc8560678 | 12 | Sensors::tmpSensorValue_t tmpSensorValue; /* User defined application data */ |
sunsmile2015 | 4:e5fa4c8838db | 13 | }; |
sunsmile2015 | 3:3eda308b78e6 | 14 | #pragma pack() |
sunsmile2015 | 3:3eda308b78e6 | 15 | |
sunsmile2015 | 0:3dc6e424dba0 | 16 | BLE ble; |
helloqi | 5:d30cc8560678 | 17 | Sensors tempSensor; |
helloqi | 7:f3e587ca9139 | 18 | DigitalOut led(P0_19); |
sunsmile2015 | 4:e5fa4c8838db | 19 | static bool triggerTempValueUpdate = false; |
helloqi | 7:f3e587ca9139 | 20 | const static char DEVICE_NAME[8] = "Linkaa"; /*The size of the DEVICE_NAME[] can't change */ |
sunsmile2015 | 0:3dc6e424dba0 | 21 | |
sunsmile2015 | 2:b935358da5ba | 22 | void periodicCallback(void) |
sunsmile2015 | 2:b935358da5ba | 23 | { |
sunsmile2015 | 2:b935358da5ba | 24 | /* Do blinky on LED1 while we're waiting for BLE events */ |
helloqi | 5:d30cc8560678 | 25 | led = !led; |
sunsmile2015 | 4:e5fa4c8838db | 26 | triggerTempValueUpdate = true; |
sunsmile2015 | 4:e5fa4c8838db | 27 | } |
sunsmile2015 | 4:e5fa4c8838db | 28 | |
sunsmile2015 | 4:e5fa4c8838db | 29 | void accumulateApplicationData(ApplicationData_t &appData) |
sunsmile2015 | 4:e5fa4c8838db | 30 | { |
sunsmile2015 | 4:e5fa4c8838db | 31 | appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; |
helloqi | 5:d30cc8560678 | 32 | /* Read a new data value */ |
sunsmile2015 | 4:e5fa4c8838db | 33 | appData.tmpSensorValue = tempSensor.get(); |
sunsmile2015 | 0:3dc6e424dba0 | 34 | } |
sunsmile2015 | 0:3dc6e424dba0 | 35 | |
sunsmile2015 | 2:b935358da5ba | 36 | void temperatureValueAdvertising(void) |
helloqi | 5:d30cc8560678 | 37 | { |
sunsmile2015 | 4:e5fa4c8838db | 38 | ApplicationData_t appData; |
sunsmile2015 | 3:3eda308b78e6 | 39 | |
sunsmile2015 | 4:e5fa4c8838db | 40 | accumulateApplicationData(appData); |
sunsmile2015 | 3:3eda308b78e6 | 41 | |
sunsmile2015 | 3:3eda308b78e6 | 42 | /* Setup advertising payload */ |
sunsmile2015 | 3:3eda308b78e6 | 43 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */ |
sunsmile2015 | 3:3eda308b78e6 | 44 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */ |
helloqi | 5:d30cc8560678 | 45 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
sunsmile2015 | 4:e5fa4c8838db | 46 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); /* Set data */ |
sunsmile2015 | 3:3eda308b78e6 | 47 | /* Setup advertising parameters */ |
sunsmile2015 | 2:b935358da5ba | 48 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
sunsmile2015 | 2:b935358da5ba | 49 | ble.gap().setAdvertisingInterval(500); |
sunsmile2015 | 3:3eda308b78e6 | 50 | /* Start advertising */ |
sunsmile2015 | 2:b935358da5ba | 51 | ble.gap().startAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 52 | } |
sunsmile2015 | 2:b935358da5ba | 53 | |
sunsmile2015 | 4:e5fa4c8838db | 54 | void updateSensorValueInAdvPayload(void) |
sunsmile2015 | 4:e5fa4c8838db | 55 | { |
sunsmile2015 | 4:e5fa4c8838db | 56 | ApplicationData_t appData; |
sunsmile2015 | 4:e5fa4c8838db | 57 | |
sunsmile2015 | 4:e5fa4c8838db | 58 | accumulateApplicationData(appData); |
sunsmile2015 | 4:e5fa4c8838db | 59 | |
sunsmile2015 | 4:e5fa4c8838db | 60 | /* Stop advertising first */ |
sunsmile2015 | 4:e5fa4c8838db | 61 | ble.gap().stopAdvertising(); |
helloqi | 5:d30cc8560678 | 62 | /* Only update data value field */ |
sunsmile2015 | 4:e5fa4c8838db | 63 | ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); |
sunsmile2015 | 4:e5fa4c8838db | 64 | /* Start advertising again */ |
sunsmile2015 | 4:e5fa4c8838db | 65 | ble.gap().startAdvertising(); |
sunsmile2015 | 4:e5fa4c8838db | 66 | } |
sunsmile2015 | 4:e5fa4c8838db | 67 | |
sunsmile2015 | 2:b935358da5ba | 68 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 69 | { |
sunsmile2015 | 0:3dc6e424dba0 | 70 | Ticker ticker; |
helloqi | 7:f3e587ca9139 | 71 | ticker.attach(periodicCallback, 0.3); |
sunsmile2015 | 0:3dc6e424dba0 | 72 | ble.init(); |
helloqi | 5:d30cc8560678 | 73 | /* Start data advertising */ |
sunsmile2015 | 4:e5fa4c8838db | 74 | temperatureValueAdvertising(); |
sunsmile2015 | 4:e5fa4c8838db | 75 | |
helloqi | 7:f3e587ca9139 | 76 | while (true) |
helloqi | 7:f3e587ca9139 | 77 | { |
helloqi | 7:f3e587ca9139 | 78 | |
helloqi | 5:d30cc8560678 | 79 | if (triggerTempValueUpdate) |
helloqi | 5:d30cc8560678 | 80 | { |
helloqi | 5:d30cc8560678 | 81 | /* Update data value */ |
sunsmile2015 | 4:e5fa4c8838db | 82 | updateSensorValueInAdvPayload(); |
sunsmile2015 | 4:e5fa4c8838db | 83 | triggerTempValueUpdate = false; |
sunsmile2015 | 0:3dc6e424dba0 | 84 | } |
sunsmile2015 | 0:3dc6e424dba0 | 85 | ble.waitForEvent(); |
sunsmile2015 | 0:3dc6e424dba0 | 86 | } |
sunsmile2015 | 0:3dc6e424dba0 | 87 | } |