ble

Dependencies:   HC_SR04_Ultrasonic_Library Servo mbed

Fork of FIP_REV1 by Robotique FIP

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 #include "Servo.h"
00004 #include "debug.h"
00005 #include "btle.h"
00006 //#include "BlueNRGDevice.h"//User does not use any platform specific header file
00007 #include "BLEDevice.h"
00008 #include "UUID.h"
00009 #include "Utils.h"
00010 
00011 BLEDevice dev;
00012 DigitalOut myled(LED1);
00013 Servo servo1(D15);
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 {
00067     if(attributeHandle == updateRate.getHandle()){
00068     servo1.write(upd[0]);
00069     }
00070     DEBUG("Write Callback!! %d %d \n\r", attributeHandle, updateRate.getHandle());
00071 
00072 }
00073 
00074 void onConnectionCallback(uint16_t Handle_t) {
00075     //myled = 1; // LED is ON
00076     DEBUG("Connected BlueNRG!!\n\r");
00077     }
00078  
00079  
00080 void onNotifyEnabled(uint16_t charHandle) {
00081     //myled = 1; // LED is ON
00082     DEBUG("onNotifyEnabled!!\n\r");
00083     }
00084     
00085 void onNotifyDisabled(uint16_t charHandle) {
00086     //myled = 1; // LED is ON
00087     DEBUG("onNotifyDisabled!!\n\r");
00088     }
00089  
00090 void onDataSentNotify() {
00091     //myled = 1; // LED is ON
00092     DEBUG("on Data Sent Notify!!\n\r");
00093     }
00094  
00095  
00096 /**
00097  * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
00098  */
00099 void periodicCallback()
00100 {
00101 }
00102  
00103 int main() {   
00104     
00105     //Ticker ticker; //For Tick interrupt if used later on (periodic data updates?)
00106     //event_button.mode(PullUp);          // Setup the internall pull-up resistor
00107     //event_button.fall(&ISR_pressed);    // Set the ISR associated to event fall (push the button)
00108     //ticker.attach(periodicCallback, 1);
00109 
00110     myled = 0;//Switch OFF LED1
00111      
00112     DEBUG("Initializing BlueNRG...\n\r");
00113     dev.init();
00114     
00115     dev.onConnection(onConnectionCallback);
00116     dev.onDisconnection(disconnectionCallback);
00117     dev.onDataWritten(onWriteCallback);
00118     dev.onUpdatesEnabled(onNotifyEnabled);
00119     dev.onDataSent(onDataSentNotify);
00120     dev.onUpdatesDisabled(onNotifyDisabled);
00121     
00122     dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Will reset the device and re-init()
00123 
00124     /* setup advertising */
00125     dev.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00126     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
00127     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00128     dev.setAdvertisingType (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00129     dev.setAdvertisingInterval (160); /* 100ms; in multiples of 0.625ms. */
00130     DEBUG("Starting Advertising...\n\r");
00131     dev.startAdvertising();
00132     dev.addService(proxService);
00133     ultrasonic mu(D8, D9, .1, 1, &dist);    
00134     mu.startUpdates();
00135     
00136     while(1) {
00137         /*
00138         if(go_to_sleep)
00139         {
00140             myled = 0;
00141             event_button.enable_irq();  // Enable the interrupt request
00142             //sleep();                  // Enter Low Power Mode
00143             deepsleep();                // Enter Low Power Mode (deep)
00144             wait_ms(200);               // Wait 200ms for debounce
00145             event_button.enable_irq();  // Enable the interrupt request
00146         }*/
00147         //else
00148         mu.checkDistance();  
00149         myled = 1;
00150         wait(1);
00151         periodicCallback();//Works from here!!
00152         dev.waitForEvent();
00153     }
00154 }