Sensor ultrasónico con GAP

Dependencies:   BLE_API HC_SR04_Ultrasonic_Library mbed

Fork of LPC1768_HCSR04_HelloWorld by jim hamblen

main.cpp

Committer:
VicenteFerrara
Date:
2017-02-22
Revision:
3:4cead4855aa7
Parent:
2:3566c2d1e86b

File content as of revision 3:4cead4855aa7:

#include "mbed.h"
#include "ultrasonic.h"
#include "ble/BLE.h"
Serial pc(USBTX, USBRX);

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
    pc.printf("Distance %d mm\r\n", distance);
}

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.
    }
}