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