this is a test about ble
Dependencies: BLE_API LinkNode_TemperatureAdvertising mbed nRF51822
Fork of LinkNode_DataAdvertising1 by
main.cpp@7:f84b0235958a, 2016-04-14 (annotated)
- Committer:
- helloqi
- Date:
- Thu Apr 14 05:20:39 2016 +0000
- Revision:
- 7:f84b0235958a
- Parent:
- 6:b3d3351aadc6
import the project
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 | 7:f84b0235958a | 17 | DigitalOut led_r(P0_20); |
helloqi | 7:f84b0235958a | 18 | DigitalOut buzzer(P0_22); |
helloqi | 7:f84b0235958a | 19 | InterruptIn btn(P0_28); |
helloqi | 7:f84b0235958a | 20 | InterruptIn btn1(P0_29); |
sunsmile2015 | 4:e5fa4c8838db | 21 | static bool triggerTempValueUpdate = false; |
helloqi | 7:f84b0235958a | 22 | static bool start_flag = false; |
helloqi | 7:f84b0235958a | 23 | const static char DEVICE_NAME[8] = "Linkab"; /*The size of the DEVICE_NAME[] can't change */ |
sunsmile2015 | 0:3dc6e424dba0 | 24 | |
sunsmile2015 | 2:b935358da5ba | 25 | void periodicCallback(void) |
sunsmile2015 | 2:b935358da5ba | 26 | { |
sunsmile2015 | 2:b935358da5ba | 27 | /* Do blinky on LED1 while we're waiting for BLE events */ |
sunsmile2015 | 4:e5fa4c8838db | 28 | triggerTempValueUpdate = true; |
sunsmile2015 | 4:e5fa4c8838db | 29 | } |
sunsmile2015 | 4:e5fa4c8838db | 30 | |
sunsmile2015 | 4:e5fa4c8838db | 31 | void accumulateApplicationData(ApplicationData_t &appData) |
sunsmile2015 | 4:e5fa4c8838db | 32 | { |
sunsmile2015 | 4:e5fa4c8838db | 33 | appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; |
helloqi | 5:d30cc8560678 | 34 | /* Read a new data value */ |
sunsmile2015 | 4:e5fa4c8838db | 35 | appData.tmpSensorValue = tempSensor.get(); |
sunsmile2015 | 0:3dc6e424dba0 | 36 | } |
sunsmile2015 | 0:3dc6e424dba0 | 37 | |
sunsmile2015 | 2:b935358da5ba | 38 | void temperatureValueAdvertising(void) |
helloqi | 5:d30cc8560678 | 39 | { |
sunsmile2015 | 4:e5fa4c8838db | 40 | ApplicationData_t appData; |
sunsmile2015 | 3:3eda308b78e6 | 41 | |
sunsmile2015 | 4:e5fa4c8838db | 42 | accumulateApplicationData(appData); |
sunsmile2015 | 3:3eda308b78e6 | 43 | |
sunsmile2015 | 3:3eda308b78e6 | 44 | /* Setup advertising payload */ |
sunsmile2015 | 3:3eda308b78e6 | 45 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */ |
sunsmile2015 | 3:3eda308b78e6 | 46 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */ |
helloqi | 5:d30cc8560678 | 47 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
sunsmile2015 | 4:e5fa4c8838db | 48 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); /* Set data */ |
sunsmile2015 | 3:3eda308b78e6 | 49 | /* Setup advertising parameters */ |
sunsmile2015 | 2:b935358da5ba | 50 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
helloqi | 7:f84b0235958a | 51 | ble.gap().setAdvertisingInterval(300); |
sunsmile2015 | 3:3eda308b78e6 | 52 | /* Start advertising */ |
sunsmile2015 | 2:b935358da5ba | 53 | ble.gap().startAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 54 | } |
sunsmile2015 | 2:b935358da5ba | 55 | |
sunsmile2015 | 4:e5fa4c8838db | 56 | void updateSensorValueInAdvPayload(void) |
sunsmile2015 | 4:e5fa4c8838db | 57 | { |
sunsmile2015 | 4:e5fa4c8838db | 58 | ApplicationData_t appData; |
sunsmile2015 | 4:e5fa4c8838db | 59 | |
sunsmile2015 | 4:e5fa4c8838db | 60 | accumulateApplicationData(appData); |
sunsmile2015 | 4:e5fa4c8838db | 61 | |
sunsmile2015 | 4:e5fa4c8838db | 62 | /* Stop advertising first */ |
sunsmile2015 | 4:e5fa4c8838db | 63 | ble.gap().stopAdvertising(); |
helloqi | 5:d30cc8560678 | 64 | /* Only update data value field */ |
sunsmile2015 | 4:e5fa4c8838db | 65 | ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); |
sunsmile2015 | 4:e5fa4c8838db | 66 | /* Start advertising again */ |
sunsmile2015 | 4:e5fa4c8838db | 67 | ble.gap().startAdvertising(); |
sunsmile2015 | 4:e5fa4c8838db | 68 | } |
sunsmile2015 | 4:e5fa4c8838db | 69 | |
helloqi | 7:f84b0235958a | 70 | void start_mode(void) |
helloqi | 7:f84b0235958a | 71 | { |
helloqi | 7:f84b0235958a | 72 | led_r=0; |
helloqi | 7:f84b0235958a | 73 | wait(0.2); |
helloqi | 7:f84b0235958a | 74 | led_r=1; |
helloqi | 7:f84b0235958a | 75 | wait(0.2); |
helloqi | 7:f84b0235958a | 76 | led_r=0; |
helloqi | 7:f84b0235958a | 77 | wait(0.2); |
helloqi | 7:f84b0235958a | 78 | led_r=1; |
helloqi | 7:f84b0235958a | 79 | ble.gap().startAdvertising(); |
helloqi | 7:f84b0235958a | 80 | } |
helloqi | 7:f84b0235958a | 81 | void stop_mode(void) |
helloqi | 7:f84b0235958a | 82 | { |
helloqi | 7:f84b0235958a | 83 | led_r=0; |
helloqi | 7:f84b0235958a | 84 | wait(0.5); |
helloqi | 7:f84b0235958a | 85 | led_r=1; |
helloqi | 7:f84b0235958a | 86 | wait(0.5); |
helloqi | 7:f84b0235958a | 87 | led_r=0; |
helloqi | 7:f84b0235958a | 88 | wait(0.5); |
helloqi | 7:f84b0235958a | 89 | led_r=1; |
helloqi | 7:f84b0235958a | 90 | ble.gap().stopAdvertising(); |
helloqi | 7:f84b0235958a | 91 | } |
helloqi | 7:f84b0235958a | 92 | |
helloqi | 7:f84b0235958a | 93 | void change_startmode(void) |
helloqi | 7:f84b0235958a | 94 | { |
helloqi | 7:f84b0235958a | 95 | |
helloqi | 7:f84b0235958a | 96 | start_flag=!start_flag; |
helloqi | 7:f84b0235958a | 97 | if(start_flag==true) |
helloqi | 7:f84b0235958a | 98 | { |
helloqi | 7:f84b0235958a | 99 | start_mode(); |
helloqi | 7:f84b0235958a | 100 | } |
helloqi | 7:f84b0235958a | 101 | else |
helloqi | 7:f84b0235958a | 102 | { |
helloqi | 7:f84b0235958a | 103 | stop_mode(); |
helloqi | 7:f84b0235958a | 104 | } |
helloqi | 7:f84b0235958a | 105 | printf("The state is %d\n",start_flag); |
helloqi | 7:f84b0235958a | 106 | } |
helloqi | 7:f84b0235958a | 107 | |
helloqi | 7:f84b0235958a | 108 | |
sunsmile2015 | 2:b935358da5ba | 109 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 110 | { |
sunsmile2015 | 0:3dc6e424dba0 | 111 | Ticker ticker; |
helloqi | 7:f84b0235958a | 112 | ticker.attach(periodicCallback,300); |
sunsmile2015 | 0:3dc6e424dba0 | 113 | ble.init(); |
helloqi | 7:f84b0235958a | 114 | led_r=1; |
helloqi | 7:f84b0235958a | 115 | buzzer=0; |
sunsmile2015 | 4:e5fa4c8838db | 116 | temperatureValueAdvertising(); |
helloqi | 7:f84b0235958a | 117 | btn.fall(&change_startmode); |
helloqi | 7:f84b0235958a | 118 | btn1.fall(&updateSensorValueInAdvPayload); |
helloqi | 7:f84b0235958a | 119 | printf("This is a test!\n"); |
helloqi | 7:f84b0235958a | 120 | while(true) |
helloqi | 7:f84b0235958a | 121 | { |
helloqi | 7:f84b0235958a | 122 | if(start_flag==true) |
helloqi | 7:f84b0235958a | 123 | { |
helloqi | 7:f84b0235958a | 124 | if (triggerTempValueUpdate) |
helloqi | 7:f84b0235958a | 125 | { |
helloqi | 7:f84b0235958a | 126 | updateSensorValueInAdvPayload(); |
helloqi | 7:f84b0235958a | 127 | triggerTempValueUpdate = false; |
helloqi | 7:f84b0235958a | 128 | } |
helloqi | 7:f84b0235958a | 129 | ble.waitForEvent(); |
helloqi | 7:f84b0235958a | 130 | } |
helloqi | 7:f84b0235958a | 131 | else |
helloqi | 5:d30cc8560678 | 132 | { |
helloqi | 7:f84b0235958a | 133 | ble.gap().stopAdvertising(); |
helloqi | 7:f84b0235958a | 134 | ble.waitForEvent(); |
sunsmile2015 | 0:3dc6e424dba0 | 135 | } |
helloqi | 7:f84b0235958a | 136 | } |
sunsmile2015 | 0:3dc6e424dba0 | 137 | } |