Firmware for the mbed in the BlueSync Sensor platform. Intended to communicate with a BlueGiga BLE112 Bluetooth LE module over UART.

Dependencies:   TimerCapture mbed

Revision:
2:ac474fccf29b
Parent:
1:cb941edd7bce
Child:
3:54fef6ac433b
--- a/main.cpp	Sun May 24 06:28:41 2015 +0000
+++ b/main.cpp	Sat May 30 01:53:17 2015 +0000
@@ -7,10 +7,32 @@
 
 Serial ble112(p9, p10); ///< Serial connection for communicating with the BLE112.
 TimerCapture * capPin; ///< Capture pin, wired to GPIO pin on BLE112.
-DigitalOut bleScanningLed(LED1);
+DigitalOut bleSlaveLed(LED1);
+DigitalOut bleScanningLed(LED2);
+DigitalOut bleAdvRecv(LED3);
+DigitalOut bleTimeStampRecv(LED4);
+
+void blankOutLeds() {
+    bleSlaveLed = 0;
+    bleScanningLed = 0;
+    bleAdvRecv = 0;
+    bleTimeStampRecv = 0;
+}
 
-void on_serial_rcv() {
-    ble112.getc();
+/*uint8_t[] toByteArray(uint32_t input) {
+    uint8_t byte[4]; 
+    
+    byte[0] = input & 0x000000FF;
+    byte[1] = (input & 0x0000FF00) >> 8;
+    byte[2] = (input & 0x00FF0000) >> 16;
+    byte[3] = (input & 0xFF000000) >> 24;
+    
+    return byte;
+}*/
+
+void on_adv_recv() {
+    blankOutLeds();
+    bleAdvRecv = 1;
     uint8_t hw_addr[6];
     for (int i = 5; i >=0; i--) {
         hw_addr[i] = ble112.getc();
@@ -25,19 +47,74 @@
         hw_addr[4], 
         hw_addr[5]);
     printf("Obs At: %d\r\n", capPin->getTime());
-    
+}
+
+void on_master_mode() {
+    blankOutLeds();
+    bleScanningLed = 1;
+}
+
+void on_slave_mode() {
+    blankOutLeds();
+    bleSlaveLed = 1;
     intByteArray bitArray;
     bitArray.integer = capPin->getTime();
-    
+    printf("Sending timestamp %d (%x)\r\n", bitArray.integer, bitArray.integer);
+    uint8_t event = (uint8_t) EventCode::SET_TIMESTAMP;
+    printf("Sending event: %x\r\n", event);
+    ble112.putc(0x05);   
     for (int i = 3; i >= 0; i--) {
         ble112.putc(bitArray.byte[i]);
     }
 }
 
+void on_offset_recv() {
+    blankOutLeds();
+    bleTimeStampRecv = 1;
+    intByteArray bitArray;
+    for (int i = 3; i >= 0; i--) {
+        bitArray.byte[i] = ble112.getc();
+    }
+    printf("Got offset %d\r\n", bitArray.signed_integer);
+}
+
+
+void on_serial_rcv() {
+    uint8_t command = ble112.getc();
+    printf("Command: %x\r\n", command);
+    switch (command) {
+    case EventCode::ADV_RECV:
+        on_adv_recv();
+        break;
+    case EventCode::SLAVE_MODE:
+        on_slave_mode();
+        break;
+    case EventCode::MASTER_MODE:
+        on_master_mode();
+        break;
+    case EventCode::SET_TIMESTAMP:
+        
+        break;
+    case EventCode::OFFSET_RECV:
+        on_offset_recv();
+        break;
+    default:
+        printf("Got unexpected byte '%x' from terminal!\r\n",command);
+        break;
+    } 
+}
+
 int main() {
+    printf("Booted!\r\n");
     TimerCapture::startTimer();
     capPin = new TimerCapture(p30);
     ble112.baud(115200);
-    ble112.set_flow_control(SerialBase::RTSCTS, p7, p8);
+    //ble112.set_flow_control(SerialBase::RTSCTS, p7, p8);
     ble112.attach(&on_serial_rcv);
+    uint8_t command = (uint8_t) EventCode::GET_STATE;
+    printf("Sending command %x\r\n", command);
+    ble112.putc(command);
+    for (int i = 0; i < 4; i++) {
+        ble112.putc(0x00);
+    }
 }