Robotique FIP / Mbed 2 deprecated BlueNRG

Dependencies:   mbed HC_SR04_Ultrasonic_Library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ultrasonic.h"
00003  
00004 DigitalOut myled(LED1);
00005  
00006 #include "debug.h"
00007 #include "btle.h"
00008 //#include "BlueNRGDevice.h"//User does not use any platform specific header file
00009 #include "BLEDevice.h"
00010 #include "UUID.h"
00011 #include "Utils.h"
00012  
00013 BLEDevice dev;
00014  
00015 const static char  DEVICE_NAME[] = "FearInProgress";
00016 const uint8_t device_address[6] = { 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }; //Peripheral address
00017 //InterruptIn event_button(USER_BUTTON);
00018 volatile bool go_to_sleep = true;
00019 
00020 #define MAX_SERVICES_NOS 1                                                                                                                                   
00021 
00022 
00023 static uint8_t mm[2] = {0x00, 0x00};
00024 static uint8_t upd[2] = {0x00, 0x00};
00025  
00026 GattCharacteristic proxLevel(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, mm, sizeof(mm), sizeof(mm), 
00027                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00028                             /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE*/);
00029  
00030 GattCharacteristic updateRate(GattCharacteristic::UUID_MEASUREMENT_INTERVAL_CHAR , upd, sizeof(upd), sizeof(upd), 
00031                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
00032                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);                            
00033  
00034 GattCharacteristic *proxChars[] = {&proxLevel, &updateRate };
00035 //UUID PROXSERVICE(GattService::UUID_IMMEDIATE_ALERT_SERVICE);
00036 GattService proxService(GattService::UUID_IMMEDIATE_ALERT_SERVICE, proxChars, sizeof(proxChars) / sizeof(GattCharacteristic *));
00037  
00038 static const uint16_t uuid16_list[] = {GattService::UUID_IMMEDIATE_ALERT_SERVICE};
00039 
00040 void dist(int distance)
00041 {
00042     //if (dev.getGapState().connected) {
00043     mm[0] = distance & 0xff;
00044     mm[1] = (distance >> 8);
00045     dev.updateCharacteristicValue(proxLevel.getHandle(), mm, sizeof(mm));
00046     //}
00047     DEBUG("%d%d Distance changed to %dmm\r\n", mm[1], mm[0], distance);
00048 }
00049 
00050 void ISR_pressed()                      // ISR for the button press
00051 {
00052     DEBUG("Button pressed\n");         // Show that the button has pressed
00053     go_to_sleep = !go_to_sleep;         // Toogle the sleep state
00054     //mu.checkDistance();
00055     //event_button.disable_irq();         // Disable the interrupt request
00056 }
00057 
00058 void disconnectionCallback(uint16_t Handle_t)
00059 {
00060     DEBUG("Disconnected!\n\r");
00061     DEBUG("Restarting the advertising process\n\r");
00062     dev.startAdvertising();
00063 }
00064  
00065 void onWriteCallback(uint16_t attributeHandle) {
00066     DEBUG("Write Callback!!\n\r");
00067     }
00068     
00069 void onConnectionCallback(uint16_t Handle_t) {
00070     //myled = 1; // LED is ON
00071     DEBUG("Connected BlueNRG!!\n\r");
00072     }
00073  
00074  
00075 void onNotifyEnabled(uint16_t charHandle) {
00076     //myled = 1; // LED is ON
00077     DEBUG("onNotifyEnabled!!\n\r");
00078     }
00079     
00080 void onNotifyDisabled(uint16_t charHandle) {
00081     //myled = 1; // LED is ON
00082     DEBUG("onNotifyDisabled!!\n\r");
00083     }
00084  
00085 void onDataSentNotify() {
00086     //myled = 1; // LED is ON
00087     DEBUG("on Data Sent Notify!!\n\r");
00088     }
00089  
00090  
00091 /**
00092  * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
00093  */
00094 void periodicCallback()
00095 {
00096 }
00097  
00098 int main() {   
00099     
00100     //Ticker ticker; //For Tick interrupt if used later on (periodic data updates?)
00101     //event_button.mode(PullUp);          // Setup the internall pull-up resistor
00102     //event_button.fall(&ISR_pressed);    // Set the ISR associated to event fall (push the button)
00103     //ticker.attach(periodicCallback, 1);
00104 
00105     myled = 0;//Switch OFF LED1
00106      
00107     DEBUG("Initializing BlueNRG...\n\r");
00108     dev.init();
00109     
00110     dev.onConnection(onConnectionCallback);
00111     dev.onDisconnection(disconnectionCallback);
00112     dev.onDataWritten(onWriteCallback);
00113     dev.onUpdatesEnabled(onNotifyEnabled);
00114     dev.onDataSent(onDataSentNotify);
00115     dev.onUpdatesDisabled(onNotifyDisabled);
00116     
00117     dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Will reset the device and re-init()
00118 
00119     /* setup advertising */
00120     dev.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00121     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
00122     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00123     dev.setAdvertisingType (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00124     dev.setAdvertisingInterval (160); /* 100ms; in multiples of 0.625ms. */
00125     DEBUG("Starting Advertising...\n\r");
00126     dev.startAdvertising();
00127     dev.addService(proxService);
00128     ultrasonic mu(D8, D9, .1, 1, &dist);    
00129     mu.startUpdates();
00130     
00131     while(1) {
00132         /*
00133         if(go_to_sleep)
00134         {
00135             myled = 0;
00136             event_button.enable_irq();  // Enable the interrupt request
00137             //sleep();                  // Enter Low Power Mode
00138             deepsleep();                // Enter Low Power Mode (deep)
00139             wait_ms(200);               // Wait 200ms for debounce
00140             event_button.enable_irq();  // Enable the interrupt request
00141         }*/
00142         //else
00143         mu.checkDistance();  
00144         myled = 1;
00145         wait(1);
00146         periodicCallback();//Works from here!!
00147         dev.waitForEvent();
00148     }
00149 }