HCSR04 Ultrasonic sensor exposed over BLE
Dependencies: BLE_API HCSR04 mbed nRF51822
main.cpp@0:470a1164da56, 2016-05-09 (annotated)
- Committer:
- janjongboom
- Date:
- Mon May 09 10:26:40 2016 +0000
- Revision:
- 0:470a1164da56
HCSR04 ultrasonic sensor over BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
janjongboom | 0:470a1164da56 | 1 | #include "mbed.h" |
janjongboom | 0:470a1164da56 | 2 | #include "ble/BLE.h" |
janjongboom | 0:470a1164da56 | 3 | #include "UltrasonicService.h" |
janjongboom | 0:470a1164da56 | 4 | #include "hcsr04.h" |
janjongboom | 0:470a1164da56 | 5 | |
janjongboom | 0:470a1164da56 | 6 | #define ULTRASONIC_UPDATE_INTERVAL 1.0f // in seconds |
janjongboom | 0:470a1164da56 | 7 | |
janjongboom | 0:470a1164da56 | 8 | #define LED_ON 0 |
janjongboom | 0:470a1164da56 | 9 | #define LED_OFF 1 |
janjongboom | 0:470a1164da56 | 10 | |
janjongboom | 0:470a1164da56 | 11 | static DigitalOut alivenessLED(LED1, LED_OFF); // green |
janjongboom | 0:470a1164da56 | 12 | static HCSR04 usensor(D0, D1); |
janjongboom | 0:470a1164da56 | 13 | |
janjongboom | 0:470a1164da56 | 14 | const static char DEVICE_NAME[] = "HPE_ULTRASONIC"; |
janjongboom | 0:470a1164da56 | 15 | static const uint16_t uuid16_list[] = { |
janjongboom | 0:470a1164da56 | 16 | UltrasonicService::SERVICE_UUID |
janjongboom | 0:470a1164da56 | 17 | }; |
janjongboom | 0:470a1164da56 | 18 | |
janjongboom | 0:470a1164da56 | 19 | static UltrasonicService *uServicePtr; |
janjongboom | 0:470a1164da56 | 20 | |
janjongboom | 0:470a1164da56 | 21 | static Ticker ledTicker; |
janjongboom | 0:470a1164da56 | 22 | static Ticker uTicker; |
janjongboom | 0:470a1164da56 | 23 | static Timeout cbTimeout; |
janjongboom | 0:470a1164da56 | 24 | |
janjongboom | 0:470a1164da56 | 25 | void readDistanceCallback(void) |
janjongboom | 0:470a1164da56 | 26 | { |
janjongboom | 0:470a1164da56 | 27 | uint32_t dist = usensor.get_dist_cm(); |
janjongboom | 0:470a1164da56 | 28 | |
janjongboom | 0:470a1164da56 | 29 | uServicePtr->updateState(dist); |
janjongboom | 0:470a1164da56 | 30 | // should add a stop function? usensor.stop(); |
janjongboom | 0:470a1164da56 | 31 | } |
janjongboom | 0:470a1164da56 | 32 | |
janjongboom | 0:470a1164da56 | 33 | |
janjongboom | 0:470a1164da56 | 34 | void readDistance(void) |
janjongboom | 0:470a1164da56 | 35 | { |
janjongboom | 0:470a1164da56 | 36 | usensor.start(); |
janjongboom | 0:470a1164da56 | 37 | |
janjongboom | 0:470a1164da56 | 38 | // need to like have it on for for 0.5s |
janjongboom | 0:470a1164da56 | 39 | cbTimeout.attach(&readDistanceCallback, 0.5f); |
janjongboom | 0:470a1164da56 | 40 | } |
janjongboom | 0:470a1164da56 | 41 | |
janjongboom | 0:470a1164da56 | 42 | void periodicCallback(void) |
janjongboom | 0:470a1164da56 | 43 | { |
janjongboom | 0:470a1164da56 | 44 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
janjongboom | 0:470a1164da56 | 45 | } |
janjongboom | 0:470a1164da56 | 46 | |
janjongboom | 0:470a1164da56 | 47 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
janjongboom | 0:470a1164da56 | 48 | { |
janjongboom | 0:470a1164da56 | 49 | printf("Disconnected over BLE!\r\n"); |
janjongboom | 0:470a1164da56 | 50 | |
janjongboom | 0:470a1164da56 | 51 | BLE::Instance().gap().startAdvertising(); |
janjongboom | 0:470a1164da56 | 52 | |
janjongboom | 0:470a1164da56 | 53 | ledTicker.attach(periodicCallback, 1); |
janjongboom | 0:470a1164da56 | 54 | uTicker.detach(); |
janjongboom | 0:470a1164da56 | 55 | } |
janjongboom | 0:470a1164da56 | 56 | |
janjongboom | 0:470a1164da56 | 57 | void connectionCallback(const Gap::ConnectionCallbackParams_t * params) |
janjongboom | 0:470a1164da56 | 58 | { |
janjongboom | 0:470a1164da56 | 59 | printf("Connected over BLE!\r\n"); |
janjongboom | 0:470a1164da56 | 60 | |
janjongboom | 0:470a1164da56 | 61 | // blink a bit faster when connected |
janjongboom | 0:470a1164da56 | 62 | ledTicker.attach(periodicCallback, 0.5); |
janjongboom | 0:470a1164da56 | 63 | |
janjongboom | 0:470a1164da56 | 64 | // only do ultrasonic readings whenever we're connected over BLE |
janjongboom | 0:470a1164da56 | 65 | uTicker.attach(readDistance, ULTRASONIC_UPDATE_INTERVAL); |
janjongboom | 0:470a1164da56 | 66 | } |
janjongboom | 0:470a1164da56 | 67 | |
janjongboom | 0:470a1164da56 | 68 | void onBleInitError(BLE &ble, ble_error_t error) |
janjongboom | 0:470a1164da56 | 69 | { |
janjongboom | 0:470a1164da56 | 70 | // blink fast when we encountered an error |
janjongboom | 0:470a1164da56 | 71 | ledTicker.attach(periodicCallback, 0.2); |
janjongboom | 0:470a1164da56 | 72 | } |
janjongboom | 0:470a1164da56 | 73 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
janjongboom | 0:470a1164da56 | 74 | { |
janjongboom | 0:470a1164da56 | 75 | BLE& ble = params->ble; |
janjongboom | 0:470a1164da56 | 76 | ble_error_t error = params->error; |
janjongboom | 0:470a1164da56 | 77 | |
janjongboom | 0:470a1164da56 | 78 | if (error != BLE_ERROR_NONE) { |
janjongboom | 0:470a1164da56 | 79 | /* In case of error, forward the error handling to onBleInitError */ |
janjongboom | 0:470a1164da56 | 80 | onBleInitError(ble, error); |
janjongboom | 0:470a1164da56 | 81 | return; |
janjongboom | 0:470a1164da56 | 82 | } |
janjongboom | 0:470a1164da56 | 83 | |
janjongboom | 0:470a1164da56 | 84 | /* Ensure that it is the default instance of BLE */ |
janjongboom | 0:470a1164da56 | 85 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
janjongboom | 0:470a1164da56 | 86 | return; |
janjongboom | 0:470a1164da56 | 87 | } |
janjongboom | 0:470a1164da56 | 88 | |
janjongboom | 0:470a1164da56 | 89 | ble.gap().onDisconnection(disconnectionCallback); |
janjongboom | 0:470a1164da56 | 90 | ble.gap().onConnection(connectionCallback); |
janjongboom | 0:470a1164da56 | 91 | |
janjongboom | 0:470a1164da56 | 92 | // Begin - If you add a new service, add it here! |
janjongboom | 0:470a1164da56 | 93 | uServicePtr = new UltrasonicService(ble); |
janjongboom | 0:470a1164da56 | 94 | // End - If you add a new service, add it here! |
janjongboom | 0:470a1164da56 | 95 | |
janjongboom | 0:470a1164da56 | 96 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
janjongboom | 0:470a1164da56 | 97 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
janjongboom | 0:470a1164da56 | 98 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
janjongboom | 0:470a1164da56 | 99 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
janjongboom | 0:470a1164da56 | 100 | ble.gap().setAdvertisingInterval(1000); |
janjongboom | 0:470a1164da56 | 101 | ble.gap().startAdvertising(); |
janjongboom | 0:470a1164da56 | 102 | } |
janjongboom | 0:470a1164da56 | 103 | |
janjongboom | 0:470a1164da56 | 104 | int main(void) |
janjongboom | 0:470a1164da56 | 105 | { |
janjongboom | 0:470a1164da56 | 106 | // Blink the green LED! |
janjongboom | 0:470a1164da56 | 107 | ledTicker.attach(periodicCallback, 1); |
janjongboom | 0:470a1164da56 | 108 | |
janjongboom | 0:470a1164da56 | 109 | BLE &ble = BLE::Instance(); |
janjongboom | 0:470a1164da56 | 110 | ble.init(bleInitComplete); |
janjongboom | 0:470a1164da56 | 111 | |
janjongboom | 0:470a1164da56 | 112 | /* SpinWait for initialization to complete. This is necessary because the |
janjongboom | 0:470a1164da56 | 113 | * BLE object is used in the main loop below. */ |
janjongboom | 0:470a1164da56 | 114 | while (ble.hasInitialized() == false) { /* spin loop */ } |
janjongboom | 0:470a1164da56 | 115 | |
janjongboom | 0:470a1164da56 | 116 | while (true) { |
janjongboom | 0:470a1164da56 | 117 | ble.waitForEvent(); |
janjongboom | 0:470a1164da56 | 118 | } |
janjongboom | 0:470a1164da56 | 119 | } |