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:
12:f5b12e8b6043
Parent:
11:35b63ab4c76c
Child:
13:a578c382cb7d
--- a/main.cpp	Thu Mar 26 15:06:21 2015 +0000
+++ b/main.cpp	Fri Mar 27 12:21:12 2015 +0000
@@ -2,53 +2,51 @@
 #include "BLEDevice.h"
 #include "BroadcasterService.h"
 #include "ErrorService.h"
-#include "PinDetect.h"
 #include <string>
 
 using namespace std;
 
 BLEDevice ble;
 DigitalOut led1(LED1);
-
-PinDetect pIn(p5);
-
+InterruptIn indicatorIn(p5);
 Serial pc(USBTX, USBRX);
 
 /*Variable Declarations*/
 const static char     DEVICE_NAME[]        = "BLE_Broadcaster";
-
 static volatile bool indicatorOn = false;
 static volatile bool indicatorSent = false;
-//TODO: I want to be able to send something like:
-    
-//with first 4 bits being type code, and last 4 being command, or something
 uint8_t cmdOn[8] = { 0x4d,0x32,0x81,0xc0,0x4d,0x4d,0x4d,0x4d };
-uint8_t cmdOff[8] = { 0x4d,0x32,0x81,0xc0,0x00,0x00,0x00,0x00 };
-    
+uint8_t cmdOff[8] = { 0x4d,0x32,0x81,0xc0,0x32,0x32,0x32,0x32 };
+
+/** Callback function for ticker */
 void blink(void)
 {
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */    
+    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */  
+    //pc.printf("Tick\r\n");  
 }
 
-/** Callback routine is interrupt activated by a debounced pb hit*/
-void keyPressed(void) {
+/** Callback routine is interrupt activated by a debounced button hit*/
+void buttonPressed(void) {
     indicatorOn = true;   
     indicatorSent = false;
-    pc.printf("Switch changed. Is now %d \r\n", indicatorOn);
+    pc.printf("Switch on. Is now %d \r\n", indicatorOn);
 }
 
-void keyReleased(void) {
+/** Callback function for button (indicatorIn) being released */
+void buttonReleased(void) {
     indicatorOn = false;   
     indicatorSent = false;
-    pc.printf("Switch changed. Is now %d \r\n", indicatorOn);
+    pc.printf("Switch off. Is now %d \r\n", indicatorOn);
 }
 
+/** Callback function for BLE disconnection */
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
     pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason);
     ble.startAdvertising();
 }
 
+/** Callback function for BLE connection */
 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms)
 {
     pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr);
@@ -56,25 +54,19 @@
 }
 
 int main(void)
-{
+{    
     /*Setup switch input*/
-    pIn.mode(PullUp);
+    indicatorIn.mode(PullUp);
     // Delay for initial pullup to take effect
     wait(.01);    
-        
-    //set asserted = 0 volts and deasserted = 5 (or 3.3, actually)  
-    pIn.setAssertValue(0);
-    // Setup Interrupt callback function for a pb hit
-    pIn.attach_asserted( &keyPressed );
-    pIn.attach_deasserted( &keyReleased );
-    
-    // Start sampling pb input using interrupts
-    pIn.setSampleFrequency(); 
+    // Setup Interrupt callback function for a button hit
+    indicatorIn.fall(&buttonPressed);
+    indicatorIn.rise(&buttonReleased);
     
     /*Setup Blinky*/
-    //led1 = 1;
-    //Ticker t;
-    //t.attach(blink, 1f);
+    led1 = 1;
+    Ticker t;
+    t.attach(&blink, 1);
 
     /*Setup BLE*/
     ble.init();
@@ -102,23 +94,18 @@
 
     //loop forever
     while(true) {
-
-        ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)        
+        ble.waitForEvent(); // this should return upon any system event (such as an interrupt or a ticker wakeup)        
         
         if(indicatorOn) { //if button is pressed
             if(!indicatorSent) { //should only fire the first time!
                 pc.printf("Command on = %u\r\n", cmdOn);
                 broadcasterService.sendCommand(cmdOn);
-                led1 = true;
-                //errorService.registerError(ErrorService::ERROR_GENERIC);
                 indicatorSent = true; //set true to stop multiple firing
             }
         } else {
             if(!indicatorSent) {
                 pc.printf("Command off = %u\r\n", cmdOff);
                 broadcasterService.sendCommand(cmdOff);
-                led1 = false;
-                //errorService.registerError(ErrorService::ERROR_DISCONNECTION);
                 indicatorSent = true; //set true to stop multiple firing
             }
         }