Sensor ultrasónico con GAP

Dependencies:   BLE_API HC_SR04_Ultrasonic_Library mbed

Fork of LPC1768_HCSR04_HelloWorld by jim hamblen

Committer:
VicenteFerrara
Date:
Wed Feb 22 23:27:38 2017 +0000
Revision:
3:4cead4855aa7
Parent:
2:3566c2d1e86b
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ejteb 0:1704ea055c4f 1 #include "mbed.h"
ejteb 0:1704ea055c4f 2 #include "ultrasonic.h"
VicenteFerrara 3:4cead4855aa7 3 #include "ble/BLE.h"
VicenteFerrara 3:4cead4855aa7 4 Serial pc(USBTX, USBRX);
ejteb 0:1704ea055c4f 5
VicenteFerrara 3:4cead4855aa7 6 const static char DEVICE_NAME[] = "EVA BRA TEMP";
VicenteFerrara 3:4cead4855aa7 7
VicenteFerrara 3:4cead4855aa7 8 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
VicenteFerrara 3:4cead4855aa7 9 {
VicenteFerrara 3:4cead4855aa7 10 BLE::Instance().gap().startAdvertising();
VicenteFerrara 3:4cead4855aa7 11 }
VicenteFerrara 3:4cead4855aa7 12
VicenteFerrara 3:4cead4855aa7 13 void onBleInitError(BLE &ble, ble_error_t error)
VicenteFerrara 3:4cead4855aa7 14 {
VicenteFerrara 3:4cead4855aa7 15 /* Avoid compiler warnings */
VicenteFerrara 3:4cead4855aa7 16 (void) ble;
VicenteFerrara 3:4cead4855aa7 17 (void) error;
VicenteFerrara 3:4cead4855aa7 18
VicenteFerrara 3:4cead4855aa7 19 /* Initialization error handling should go here */
VicenteFerrara 3:4cead4855aa7 20 }
VicenteFerrara 3:4cead4855aa7 21
VicenteFerrara 3:4cead4855aa7 22 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
VicenteFerrara 3:4cead4855aa7 23 {
VicenteFerrara 3:4cead4855aa7 24 BLE& ble = params->ble;
VicenteFerrara 3:4cead4855aa7 25 ble_error_t error = params->error;
VicenteFerrara 3:4cead4855aa7 26
VicenteFerrara 3:4cead4855aa7 27 if (error != BLE_ERROR_NONE) {
VicenteFerrara 3:4cead4855aa7 28 /* In case of error, forward the error handling to onBleInitError */
VicenteFerrara 3:4cead4855aa7 29 onBleInitError(ble, error);
VicenteFerrara 3:4cead4855aa7 30 return;
VicenteFerrara 3:4cead4855aa7 31 }
VicenteFerrara 3:4cead4855aa7 32
VicenteFerrara 3:4cead4855aa7 33 /* Ensure that it is the default instance of BLE */
VicenteFerrara 3:4cead4855aa7 34 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
VicenteFerrara 3:4cead4855aa7 35 return;
VicenteFerrara 3:4cead4855aa7 36 }
VicenteFerrara 3:4cead4855aa7 37 /* Set device name characteristic data */
VicenteFerrara 3:4cead4855aa7 38 ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
VicenteFerrara 3:4cead4855aa7 39
VicenteFerrara 3:4cead4855aa7 40 /* Optional: add callback for disconnection */
VicenteFerrara 3:4cead4855aa7 41 ble.gap().onDisconnection(disconnectionCallback);
VicenteFerrara 3:4cead4855aa7 42
VicenteFerrara 3:4cead4855aa7 43 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, distance);
VicenteFerrara 3:4cead4855aa7 44
VicenteFerrara 3:4cead4855aa7 45 /* Sacrifice 3B of 31B to Advertising Flags */
VicenteFerrara 3:4cead4855aa7 46 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
VicenteFerrara 3:4cead4855aa7 47 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
VicenteFerrara 3:4cead4855aa7 48
VicenteFerrara 3:4cead4855aa7 49
VicenteFerrara 3:4cead4855aa7 50 /* Set advertising interval. Longer interval == longer battery life */
VicenteFerrara 3:4cead4855aa7 51 ble.gap().setAdvertisingInterval(100); /* 100ms */
VicenteFerrara 3:4cead4855aa7 52
VicenteFerrara 3:4cead4855aa7 53 /* Start advertising */
VicenteFerrara 3:4cead4855aa7 54 ble.gap().startAdvertising();
VicenteFerrara 3:4cead4855aa7 55
VicenteFerrara 3:4cead4855aa7 56 }
VicenteFerrara 3:4cead4855aa7 57
VicenteFerrara 3:4cead4855aa7 58 void dist(uint8_t distance)
ejteb 0:1704ea055c4f 59 {
4180_1 2:3566c2d1e86b 60 //put code here to execute when the distance has changed
VicenteFerrara 3:4cead4855aa7 61 pc.printf("Distance %d mm\r\n", distance);
ejteb 0:1704ea055c4f 62 }
ejteb 0:1704ea055c4f 63
VicenteFerrara 3:4cead4855aa7 64 ultrasonic mu(D6, D7, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9
ejteb 1:4a5586eb1765 65 //have updates every .1 seconds and a timeout after 1
ejteb 1:4a5586eb1765 66 //second, and call dist when the distance changes
ejteb 0:1704ea055c4f 67
ejteb 0:1704ea055c4f 68 int main()
ejteb 0:1704ea055c4f 69 {
VicenteFerrara 3:4cead4855aa7 70 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
VicenteFerrara 3:4cead4855aa7 71 ble.init(bleInitComplete);
VicenteFerrara 3:4cead4855aa7 72
4180_1 2:3566c2d1e86b 73 mu.startUpdates();//start measuring the distance
ejteb 0:1704ea055c4f 74 while(1)
ejteb 0:1704ea055c4f 75 {
VicenteFerrara 3:4cead4855aa7 76 ble.waitForEvent();
ejteb 1:4a5586eb1765 77 //Do something else here
ejteb 1:4a5586eb1765 78 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
ejteb 1:4a5586eb1765 79 //the class checks if dist needs to be called.
ejteb 0:1704ea055c4f 80 }
ejteb 0:1704ea055c4f 81 }