Sensor ultrasónico con GAP
Dependencies: BLE_API HC_SR04_Ultrasonic_Library mbed
Fork of LPC1768_HCSR04_HelloWorld by
Revision 3:4cead4855aa7, committed 2017-02-22
- Comitter:
- VicenteFerrara
- Date:
- Wed Feb 22 23:27:38 2017 +0000
- Parent:
- 2:3566c2d1e86b
- Commit message:
- a
Changed in this revision
diff -r 3566c2d1e86b -r 4cead4855aa7 BLE_API.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLE_API.lib Wed Feb 22 23:27:38 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/VicenteFerrara/code/BLE_API/#e3d4f8c7787b
diff -r 3566c2d1e86b -r 4cead4855aa7 main.cpp --- a/main.cpp Tue Sep 13 18:16:42 2016 +0000 +++ b/main.cpp Wed Feb 22 23:27:38 2017 +0000 @@ -1,21 +1,79 @@ #include "mbed.h" #include "ultrasonic.h" +#include "ble/BLE.h" +Serial pc(USBTX, USBRX); - void dist(int distance) +const static char DEVICE_NAME[] = "EVA BRA TEMP"; + + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + BLE::Instance().gap().startAdvertising(); +} + +void onBleInitError(BLE &ble, ble_error_t error) +{ + /* Avoid compiler warnings */ + (void) ble; + (void) error; + + /* Initialization error handling should go here */ +} + +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ + BLE& ble = params->ble; + ble_error_t error = params->error; + + if (error != BLE_ERROR_NONE) { + /* In case of error, forward the error handling to onBleInitError */ + onBleInitError(ble, error); + return; + } + + /* Ensure that it is the default instance of BLE */ + if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { + return; + } + /* Set device name characteristic data */ + ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME); + + /* Optional: add callback for disconnection */ + ble.gap().onDisconnection(disconnectionCallback); + + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, distance); + + /* Sacrifice 3B of 31B to Advertising Flags */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + + + /* Set advertising interval. Longer interval == longer battery life */ + ble.gap().setAdvertisingInterval(100); /* 100ms */ + + /* Start advertising */ + ble.gap().startAdvertising(); + + } + + void dist(uint8_t distance) { //put code here to execute when the distance has changed - printf("Distance %d mm\r\n", distance); + pc.printf("Distance %d mm\r\n", distance); } -ultrasonic mu(p6, p7, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 +ultrasonic mu(D6, D7, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 //have updates every .1 seconds and a timeout after 1 //second, and call dist when the distance changes int main() { + BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); + ble.init(bleInitComplete); + mu.startUpdates();//start measuring the distance while(1) { + ble.waitForEvent(); //Do something else here mu.checkDistance(); //call checkDistance() as much as possible, as this is where //the class checks if dist needs to be called.
diff -r 3566c2d1e86b -r 4cead4855aa7 mbed.bld --- a/mbed.bld Tue Sep 13 18:16:42 2016 +0000 +++ b/mbed.bld Wed Feb 22 23:27:38 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/ef9c61f8c49f \ No newline at end of file