Jan Jongboom / Mbed 2 deprecated HPE_UltrasonicSensor

Dependencies:   BLE_API HCSR04 mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 #include "UltrasonicService.h"
00004 #include "hcsr04.h"
00005 
00006 #define ULTRASONIC_UPDATE_INTERVAL          1.0f        // in seconds
00007 
00008 #define LED_ON          0
00009 #define LED_OFF         1
00010 
00011 static DigitalOut alivenessLED(LED1, LED_OFF); // green
00012 static HCSR04 usensor(D0, D1);
00013 
00014 const static char     DEVICE_NAME[] = "HPE_ULTRASONIC";
00015 static const uint16_t uuid16_list[] = { 
00016     UltrasonicService::SERVICE_UUID
00017 };
00018 
00019 static UltrasonicService *uServicePtr;
00020 
00021 static Ticker ledTicker;
00022 static Ticker uTicker;
00023 static Timeout cbTimeout;
00024 
00025 void readDistanceCallback(void)
00026 {
00027     uint32_t dist = usensor.get_dist_cm();
00028 
00029     uServicePtr->updateState(dist);
00030     // should add a stop function? usensor.stop();
00031 }
00032     
00033 
00034 void readDistance(void)
00035 {
00036     usensor.start();
00037     
00038     // need to like have it on for for 0.5s
00039     cbTimeout.attach(&readDistanceCallback, 0.5f);
00040 }
00041 
00042 void periodicCallback(void)
00043 {
00044     alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
00045 }
00046 
00047 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00048 {
00049     printf("Disconnected over BLE!\r\n");
00050     
00051     BLE::Instance().gap().startAdvertising();
00052     
00053     ledTicker.attach(periodicCallback, 1);
00054     uTicker.detach();
00055 }
00056 
00057 void connectionCallback(const Gap::ConnectionCallbackParams_t * params)
00058 {
00059     printf("Connected over BLE!\r\n");
00060     
00061     // blink a bit faster when connected
00062     ledTicker.attach(periodicCallback, 0.5);
00063     
00064     // only do ultrasonic readings whenever we're connected over BLE
00065     uTicker.attach(readDistance, ULTRASONIC_UPDATE_INTERVAL);
00066 }
00067 
00068 void onBleInitError(BLE &ble, ble_error_t error)
00069 {
00070     // blink fast when we encountered an error
00071     ledTicker.attach(periodicCallback, 0.2);
00072 }
00073 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00074 {
00075     BLE&        ble   = params->ble;
00076     ble_error_t error = params->error;
00077 
00078     if (error != BLE_ERROR_NONE) {
00079         /* In case of error, forward the error handling to onBleInitError */
00080         onBleInitError(ble, error);
00081         return;
00082     }
00083 
00084     /* Ensure that it is the default instance of BLE */
00085     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00086         return;
00087     }
00088  
00089     ble.gap().onDisconnection(disconnectionCallback);
00090     ble.gap().onConnection(connectionCallback);
00091 
00092     // Begin - If you add a new service, add it here!
00093     uServicePtr = new UltrasonicService(ble);
00094     // End - If you add a new service, add it here!
00095 
00096     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00097     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00098     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00099     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00100     ble.gap().setAdvertisingInterval(1000);
00101     ble.gap().startAdvertising();
00102 }
00103 
00104 int main(void)
00105 {
00106     // Blink the green LED!
00107     ledTicker.attach(periodicCallback, 1);
00108     
00109     BLE &ble = BLE::Instance();
00110     ble.init(bleInitComplete);
00111 
00112     /* SpinWait for initialization to complete. This is necessary because the
00113      * BLE object is used in the main loop below. */
00114     while (ble.hasInitialized()  == false) { /* spin loop */ }
00115 
00116     while (true) {
00117         ble.waitForEvent();
00118     }
00119 }