ble

Dependencies:   HC_SR04_Ultrasonic_Library Servo mbed

Fork of FIP_REV1 by Robotique FIP

Revision:
2:b5166e24c7a6
Parent:
0:3d641e170a74
Child:
3:829f081fde15
--- a/main.cpp	Tue May 19 16:53:44 2015 +0000
+++ b/main.cpp	Tue May 19 17:46:51 2015 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include "ultrasonic.h"
  
 DigitalOut myled(LED1);
  
@@ -11,49 +12,48 @@
  
 BLEDevice dev;
  
-const static char  DEVICE_NAME[] = "Proximity Sensor";
-const uint8_t device_address[6] = { 0xaa, 0x00, 0x00, 0xE1, 0x80, 0x03 }; //Peripheral address
- 
-void Append128bitUUID(uint8_t *uuid128_list, const LongUUID_t HRM_SERVICE_UUID_128);
-void print_array(uint8_t *array);
- 
-/*********128 bit UUIDs. Not defined in GattCharacteristic.h and GattService.h*************/
- 
-InterruptIn event_button(USER_BUTTON);
+const static char  DEVICE_NAME[] = "FearInProgress";
+const uint8_t device_address[6] = { 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }; //Peripheral address
+//InterruptIn event_button(USER_BUTTON);
 volatile bool go_to_sleep = true;
 
-#define MAX_SERVICES_NOS 1                                                                                                                                       
-//typedef uint8_t UUID_128_BIT[16]; 
-const LongUUID_t HEART_RATE_CHAR_UUID_128 = {0x42,0x82,0x1a,0x40, 0xe4,0x77, 0x11,0xe2, 0x82,0xd0, 0x00,0x02,0xa5,0xd5,0xc5,0x1a};
-const LongUUID_t HRM_SERVICE_UUID_128 = {0x42,0x82,0x1a,0x40, 0xe4,0x77, 0x11,0xe2, 0x82,0xd0, 0x00,0x02,0xa5,0xd5,0xc5,0x1b};
- 
-uint8_t UUID_Count=0;
+#define MAX_SERVICES_NOS 1                                                                                                                                   
+
+static uint8_t proximity = 0;
+static uint8_t mm[2] = {0x00, proximity};
+static uint8_t rate = 1;
+static uint8_t upd[2] = {0x00, rate};
  
-static uint8_t hrmCounter = 100;
-static uint8_t bpm[2] = {0x00, hrmCounter};
-static uint8_t hcpCounter = 99;
-static uint8_t hcp[2] = {0x00, hrmCounter};
- 
-GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), 
+GattCharacteristic proxLevel(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, mm, sizeof(mm), sizeof(mm), 
                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
                             /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE*/);
  
-GattCharacteristic hcpControlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, hcp, sizeof(hcp), sizeof(hcp), 
+GattCharacteristic updateRate(GattCharacteristic::UUID_MEASUREMENT_INTERVAL_CHAR , upd, sizeof(upd), sizeof(upd), 
                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);                            
  
-GattCharacteristic *hrmChars[] = {&hrmRate, &hcpControlPoint };
-UUID HRMSERVICE(GattService::UUID_HEART_RATE_SERVICE);
-GattService hrmService(HRMSERVICE/*GattService::UUID_HEART_RATE_SERVICE*/, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
+GattCharacteristic *proxChars[] = {&proxLevel, &updateRate };
+//UUID PROXSERVICE(GattService::UUID_IMMEDIATE_ALERT_SERVICE);
+GattService proxService(GattService::UUID_IMMEDIATE_ALERT_SERVICE, proxChars, sizeof(proxChars) / sizeof(GattCharacteristic *));
  
-uint8_t uuid128_list[16*MAX_SERVICES_NOS];// = {HRM_SERVICE_UUID_128[0], HRM_SERVICE_UUID_128[1]};
-static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
+static const uint16_t uuid16_list[] = {GattService::UUID_IMMEDIATE_ALERT_SERVICE};
+
+void dist(int distance)
+{
+
+    if (dev.getGapState().connected) {
+        mm[1] = distance;
+        dev.updateCharacteristicValue(proxLevel.getHandle(), mm, sizeof(mm));
+    }
+    printf("Distance changed to %dmm\r\n", distance);
+}
 
 void ISR_pressed()                      // ISR for the button press
 {
-    printf("Button pressed\n");         // Show that the button has pressed
+    DEBUG("Button pressed\n");         // Show that the button has pressed
     go_to_sleep = !go_to_sleep;         // Toogle the sleep state
-    event_button.disable_irq();         // Disable the interrupt request
+    //mu.checkDistance();
+    //event_button.disable_irq();         // Disable the interrupt request
 }
 
 void disconnectionCallback(uint16_t Handle_t)
@@ -88,15 +88,6 @@
     DEBUG("on Data Sent Notify!!\n\r");
     }
  
-void Append128bitUUID(uint8_t *array, const LongUUID_t SERVICE_UUID_128) 
-{
-    for(int x=0;x<16; x++)
-    {
-        array[x+UUID_Count*16]=SERVICE_UUID_128[x];
-        }
-    UUID_Count++;
-    return; 
-}
  
 /**
  * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
@@ -104,34 +95,15 @@
 void periodicCallback(void)
 {
     //myled = !myled; /* Do blinky on LED1 while we're waiting for BLE events */
-     
-    if (dev.getGapState().connected) {
-        hrmCounter = rand()%(200-100) +100;
-        bpm[1] = hrmCounter;
-        uint16_t t = sizeof(bpm);
-        //DEBUG("Char Handle 0x%x OK.",hrmRate.getHandle());
-        //dev.readCharacteristicValue(hrmRate.getHandle(), bpm, (uint16_t *const)t);
-        dev.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm));
-        //DEBUG("Ticker CB..\n\r");
-    } //else DEBUG("Not Connected..\n\r");
-    else //if not connected
-    {
-        hrmCounter = rand()%(200-100) +100;
-    }
 }
  
 int main() {   
     
-    Ticker ticker; //For Tick interrupt if used later on (periodic data updates?)
-    event_button.mode(PullUp);          // Setup the internall pull-up resistor
-    event_button.fall(&ISR_pressed);    // Set the ISR associated to event fall (push the button)
-    
-    //LongUUID_t HEART_RATE_CHAR_UUID_128, HRM_SERVICE_UUID_128;
-    //COPY_HRM_SERVICE_UUID(HRM_SERVICE_UUID_128);
-    //COPY_HRM_CHAR_UUID(HEART_RATE_CHAR_UUID_128); 
-    UUID heart_rate_char_UUID = UUID(HEART_RATE_CHAR_UUID_128);
-    UUID hrm_service_UUID = UUID(HRM_SERVICE_UUID_128);
-    
+    //Ticker ticker; //For Tick interrupt if used later on (periodic data updates?)
+    //event_button.mode(PullUp);          // Setup the internall pull-up resistor
+    //event_button.fall(&ISR_pressed);    // Set the ISR associated to event fall (push the button)
+    //ticker.attach(periodicCallback, 1);
+
     myled = 0;//Switch OFF LED1
      
     DEBUG("Initializing BlueNRG...\n\r");
@@ -144,31 +116,22 @@
     dev.onDataSent(onDataSentNotify);
     dev.onUpdatesDisabled(onNotifyDisabled);
     
-    //TODO.
     dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Will reset the device and re-init()
-    //TODO.    
-    
-    //Append128bitUUID(uuid128_list, HRM_SERVICE_UUID_128);
-    
+
     /* setup advertising */
     dev.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    
-    //TODO:IMP STUFF: 128bit list is basically a uint8_t list. User should know how many services he supports and define the number in MAX_SERVICES_NOS
     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
-    
-    dev.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT);
-    
     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     dev.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     dev.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     DEBUG("Starting Advertising...\n\r");
     dev.startAdvertising();
-    
-    dev.addService(hrmService);
-    //ticker.attach(periodicCallback, 1); Multi threading and called from ISR context does not work!
+    dev.addService(proxService);
+    ultrasonic mu(D8, D9, .5, 1, &dist);    
+    mu.startUpdates();
     
     while(1) {
-        
+        /*
         if(go_to_sleep)
         {
             myled = 0;
@@ -177,15 +140,13 @@
             deepsleep();                // Enter Low Power Mode (deep)
             wait_ms(200);               // Wait 200ms for debounce
             event_button.enable_irq();  // Enable the interrupt request
-        }
-        else
-        {
+        }*/
+        //else
+        mu.checkDistance();  
         myled = 1;
-        wait(1); 
+        wait(1);
         //DEBUG("tic!\n\r");
         periodicCallback();//Works from here!!
-    
         dev.waitForEvent();
-        }
     }
 }