Dissertation project, looking at BLE communication in cars. This project is BLE peripheral acting as car indicator stalk

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_test1 by Alexander Lea

Revision:
16:d821122547b3
Parent:
15:ad1d37e51a26
Child:
17:df2f1d5c69a8
--- a/main.cpp	Tue Apr 14 15:57:47 2015 +0000
+++ b/main.cpp	Thu Apr 16 17:58:05 2015 +0000
@@ -9,6 +9,7 @@
 BLEDevice ble;
 DigitalOut led1(LED1);
 InterruptIn indicatorIn(p5);
+InterruptIn brakeIn(p2);
 Serial pc(USBTX, USBRX);
 
 /*Variable Declarations*/
@@ -17,8 +18,12 @@
 static volatile bool    indicatorSent       = false;
 uint8_t                 cmdIndicatorOn[8]   = { 0x69,0x6e,0x64,0x69,0xff,0xff,0xff,0xff }; // = I   N   D   I   255 255 255 255
 uint8_t                 cmdIndicatorOff[8]  = { 0x69,0x6e,0x64,0x69,0x00,0x00,0x00,0x00 }; // = I   N   D   I   0   0   0   0 
+uint8_t                 cmdBrakeOn[8]   = { 0x42, 0x52, 0x41, 0x4b, 0xff,0xff,0xff,0xff }; // = B   R   A   K   255 255 255 255
+uint8_t                 cmdBrakeOff[8]  = { 0x42, 0x52, 0x41, 0x4b,0x00,0x00,0x00,0x00 }; // =  B   R   A   K   0   0   0   0 
 static const uint16_t uuid16_list[]        = {BroadcasterService::BROADCAST_SERVICE_UUID};
 
+BroadcasterService* broadcasterService;
+
 /** Callback function for ticker */
 void blink(void)
 {
@@ -27,16 +32,38 @@
 
 /** Callback routine is interrupt activated by a debounced button hit*/
 void indicatorSwitchOn(void) {
-    indicatorOn = true;   
-    indicatorSent = false;
-    pc.printf("Indicator switch on.\r\n");
+    //indicatorOn = true;   
+//    indicatorSent = false;
+//    pc.printf("Indicator switch on.\r\n");
+    pc.printf("Command Indicator on\r\n");
+    broadcasterService->sendCommand(cmdIndicatorOn);
 }
 
 /** Callback routine is interrupt activated by a debounced button release*/
 void indicatorSwitchOff(void) {
-    indicatorOn = false;   
-    indicatorSent = false;
-    pc.printf("Indicator switch off.\r\n");
+    //indicatorOn = false;   
+//    indicatorSent = false;
+//    pc.printf("Indicator switch off.\r\n");
+    pc.printf("Command Indicator off\r\n");
+    broadcasterService->sendCommand(cmdIndicatorOff);
+}
+
+/** Callback routine is interrupt activated by a debounced button hit*/
+void brakeOn(void) {
+    //indicatorOn = true;   
+//    indicatorSent = false;
+//    pc.printf("Indicator switch on.\r\n");
+    pc.printf("Command Brake on\r\n");
+    broadcasterService->sendCommand(cmdBrakeOn);
+}
+
+/** Callback routine is interrupt activated by a debounced button release*/
+void brakeOff(void) {
+    //indicatorOn = false;   
+//    indicatorSent = false;
+//    pc.printf("Indicator switch off.\r\n");
+    pc.printf("Command Brake off\r\n");
+    broadcasterService->sendCommand(cmdBrakeOff);
 }
 
 /** Callback function for BLE disconnection */
@@ -57,12 +84,15 @@
 {    
     /*Setup switch input*/
     indicatorIn.mode(PullUp);
+    brakeIn.mode(PullUp);
     // Delay for initial pullup to take effect
     wait(.01);    
     // Setup Interrupt callback function for a button hit
     indicatorIn.fall(&indicatorSwitchOn);
     indicatorIn.rise(&indicatorSwitchOff);
     
+    brakeIn.fall(&brakeOn);
+    brakeIn.rise(&brakeOff);
     //TODO: Initial light input - if switch is ON at program start
     
     /*Setup Blinky*/
@@ -76,7 +106,8 @@
     ble.onConnection(connectionCallback);
 
     //create services  
-    BroadcasterService broadcasterService(ble);
+    //BroadcasterService broadcasterService(ble);
+    broadcasterService = new BroadcasterService(ble);
 
     /*
     * BREDR_NOT_SUPPORTED = BLE only
@@ -97,18 +128,18 @@
     while(true) {
         ble.waitForEvent(); // this should return upon any system event (such as an interrupt or a ticker wakeup)        
         
-        if(indicatorOn) { //if button is pressed
+        /*if(indicatorOn) { //if button is pressed
             if(!indicatorSent) { //should only fire the first time!
                 pc.printf("Command on\r\n");
-                broadcasterService.sendCommand(cmdIndicatorOn);
+                broadcasterService->sendCommand(cmdIndicatorOn);
                 indicatorSent = true; //set true to stop multiple firing
             }
         } else {
             if(!indicatorSent) {
                 pc.printf("Command off\r\n");
-                broadcasterService.sendCommand(cmdIndicatorOff);
+                broadcasterService->sendCommand(cmdIndicatorOff);
                 indicatorSent = true; //set true to stop multiple firing
             }
-        }
+        }*/
     }
 }
\ No newline at end of file