Vishal Kotcherlakota / Mbed 2 deprecated bluesync

Dependencies:   TimerCapture mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 #include "mbed.h"
00002 #include "TimerCapture.h"
00003 #include "bluesync_types.h"
00004 
00005 /** @file main.cpp
00006 @brief Main for the BlueSync mbed application. */
00007 
00008 Serial ble112(p9, p10); ///< Serial connection for communicating with the BLE112.
00009 TimerCapture * capPin; ///< Capture pin, wired to GPIO pin on BLE112.
00010 TimerCapture * evtPin; 
00011 DigitalOut bleSlaveLed(LED1);
00012 DigitalOut bleScanningLed(LED2);
00013 DigitalOut bleAdvRecv(LED3);
00014 DigitalOut statusLed(LED4);
00015 Ticker ticker;
00016 InterruptIn evtWatchPin(p28);
00017 
00018 void blankOutLeds() {
00019     bleSlaveLed = 0;
00020     bleScanningLed = 0;
00021     bleAdvRecv = 0;
00022     //statusLed = 0;
00023 }
00024 
00025 /*uint8_t[] toByteArray(uint32_t input) {
00026     uint8_t byte[4]; 
00027     
00028     byte[0] = input & 0x000000FF;
00029     byte[1] = (input & 0x0000FF00) >> 8;
00030     byte[2] = (input & 0x00FF0000) >> 16;
00031     byte[3] = (input & 0xFF000000) >> 24;
00032     
00033     return byte;
00034 }*/
00035 
00036 void on_adv_recv() {
00037     blankOutLeds();
00038     bleAdvRecv = 1;
00039     uint8_t hw_addr[6];
00040     for (int i = 5; i >=0; i--) {
00041         hw_addr[i] = ble112.getc();
00042     }
00043     
00044     printf("***************\r\n");
00045     printf("HW Addr: %02x:%02x:%02x:%02x:%02x:%02x ", 
00046         hw_addr[0], 
00047         hw_addr[1], 
00048         hw_addr[2], 
00049         hw_addr[3], 
00050         hw_addr[4], 
00051         hw_addr[5]);
00052     printf("Obs At: %d\r\n", capPin->getTime());
00053 }
00054 
00055 void on_master_mode() {
00056     blankOutLeds();
00057     bleScanningLed = 1;
00058 }
00059 
00060 void on_slave_mode() {
00061     blankOutLeds();
00062     bleSlaveLed = 1;
00063     intByteArray bitArray;
00064     bitArray.integer = capPin->getTime();
00065     printf("Sending timestamp %d (%08x)\r\n", bitArray.integer, bitArray.integer);
00066     uint8_t event = (uint8_t) EventCode::SET_TIMESTAMP;
00067     printf("Sending event: %x\r\n", event);
00068     ble112.putc(0x05);   
00069     for (int i = 0; i < 4; i++) {
00070         ble112.putc(bitArray.byte[i]);
00071     }
00072 }
00073 
00074 void on_offset_recv() {
00075     blankOutLeds();
00076     statusLed = 1;
00077     intByteArray bitArray;
00078     printf("Trying to get offset\r\n");
00079     for (int i = 0; i < 4; i++) {
00080         printf("Reading byte[%d]\r\n", i);
00081         bitArray.byte[i] = ble112.getc();
00082     }
00083     printf("Got offset %d\r\n", bitArray.signed_integer);
00084     
00085     LPC_TIM2->TC += bitArray.signed_integer;
00086 }
00087 
00088 void on_interrupt_recv() {
00089     uint8_t event = (uint8_t) EventCode::SET_SENSOR_TIME;
00090     intByteArray bitArray;
00091     bitArray.integer = evtPin->getTime();
00092     printf("Sending sensor time %d (%08x)\r\n", bitArray.integer, bitArray.integer);
00093     ble112.putc(event);
00094     for (int i = 0; i < 4; i++) {
00095         ble112.putc(bitArray.byte[i]);
00096     }
00097 }
00098 
00099 void on_serial_rcv() {
00100     uint8_t command = ble112.getc();
00101     printf("Command: %x\r\n", command);
00102     switch (command) {
00103     case EventCode::ADV_RECV:
00104         on_adv_recv();
00105         break;
00106     case EventCode::SLAVE_MODE:
00107         on_slave_mode();
00108         break;
00109     case EventCode::MASTER_MODE:
00110         on_master_mode();
00111         break;
00112     case EventCode::SET_TIMESTAMP:
00113         
00114         break;
00115     case EventCode::OFFSET_RECV:
00116         on_offset_recv();
00117         break;
00118     default:
00119         printf("Got unexpected byte '%x' from terminal!\r\n",command);
00120         break;
00121     } 
00122 }
00123 
00124 void on_tick() {
00125     uint32_t timestamp = capPin->getTime();
00126     printf("+++Capture register: %d (%08x) timer: %d (%08x)\r\n", timestamp, timestamp,
00127         LPC_TIM2->TC, LPC_TIM2->TC
00128     );
00129     statusLed = !statusLed;
00130 }  
00131 
00132 int main() {
00133     printf("Booted!\r\n");
00134     printf("SystemCoreClock is %d, %d ticks per ms\r\n", SystemCoreClock, SystemCoreClock/1000);
00135     ticker.attach(&on_tick, 5.0);
00136     statusLed = 1;
00137     TimerCapture::startTimer();
00138     capPin = new TimerCapture(p30);
00139     evtPin = new TimerCapture(p29);
00140     evtWatchPin.rise(&on_interrupt_recv);
00141     ble112.baud(9600);
00142     ble112.set_flow_control(SerialBase::RTSCTS, p7, p8);
00143     ble112.attach(&on_serial_rcv);
00144     uint8_t command = (uint8_t) EventCode::GET_STATE;
00145     printf("Sending command %x\r\n", command);
00146     ble112.putc(command);
00147     for (int i = 0; i < 4; i++) {
00148         ble112.putc(0x00);
00149     }
00150 }