Firmware for the mbed in the BlueSync Sensor platform. Intended to communicate with a BlueGiga BLE112 Bluetooth LE module over UART.
Dependencies: TimerCapture mbed
main.cpp@2:ac474fccf29b, 2015-05-30 (annotated)
- Committer:
- dishbreak
- Date:
- Sat May 30 01:53:17 2015 +0000
- Revision:
- 2:ac474fccf29b
- Parent:
- 1:cb941edd7bce
- Child:
- 3:54fef6ac433b
Disabled flow control to reduce errors.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dishbreak | 0:2bc22bc992ac | 1 | #include "mbed.h" |
dishbreak | 0:2bc22bc992ac | 2 | #include "TimerCapture.h" |
dishbreak | 1:cb941edd7bce | 3 | #include "bluesync_types.h" |
dishbreak | 0:2bc22bc992ac | 4 | |
dishbreak | 1:cb941edd7bce | 5 | /** @file main.cpp |
dishbreak | 1:cb941edd7bce | 6 | @brief Main for the BlueSync mbed application. */ |
dishbreak | 1:cb941edd7bce | 7 | |
dishbreak | 1:cb941edd7bce | 8 | Serial ble112(p9, p10); ///< Serial connection for communicating with the BLE112. |
dishbreak | 1:cb941edd7bce | 9 | TimerCapture * capPin; ///< Capture pin, wired to GPIO pin on BLE112. |
dishbreak | 2:ac474fccf29b | 10 | DigitalOut bleSlaveLed(LED1); |
dishbreak | 2:ac474fccf29b | 11 | DigitalOut bleScanningLed(LED2); |
dishbreak | 2:ac474fccf29b | 12 | DigitalOut bleAdvRecv(LED3); |
dishbreak | 2:ac474fccf29b | 13 | DigitalOut bleTimeStampRecv(LED4); |
dishbreak | 2:ac474fccf29b | 14 | |
dishbreak | 2:ac474fccf29b | 15 | void blankOutLeds() { |
dishbreak | 2:ac474fccf29b | 16 | bleSlaveLed = 0; |
dishbreak | 2:ac474fccf29b | 17 | bleScanningLed = 0; |
dishbreak | 2:ac474fccf29b | 18 | bleAdvRecv = 0; |
dishbreak | 2:ac474fccf29b | 19 | bleTimeStampRecv = 0; |
dishbreak | 2:ac474fccf29b | 20 | } |
dishbreak | 0:2bc22bc992ac | 21 | |
dishbreak | 2:ac474fccf29b | 22 | /*uint8_t[] toByteArray(uint32_t input) { |
dishbreak | 2:ac474fccf29b | 23 | uint8_t byte[4]; |
dishbreak | 2:ac474fccf29b | 24 | |
dishbreak | 2:ac474fccf29b | 25 | byte[0] = input & 0x000000FF; |
dishbreak | 2:ac474fccf29b | 26 | byte[1] = (input & 0x0000FF00) >> 8; |
dishbreak | 2:ac474fccf29b | 27 | byte[2] = (input & 0x00FF0000) >> 16; |
dishbreak | 2:ac474fccf29b | 28 | byte[3] = (input & 0xFF000000) >> 24; |
dishbreak | 2:ac474fccf29b | 29 | |
dishbreak | 2:ac474fccf29b | 30 | return byte; |
dishbreak | 2:ac474fccf29b | 31 | }*/ |
dishbreak | 2:ac474fccf29b | 32 | |
dishbreak | 2:ac474fccf29b | 33 | void on_adv_recv() { |
dishbreak | 2:ac474fccf29b | 34 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 35 | bleAdvRecv = 1; |
dishbreak | 0:2bc22bc992ac | 36 | uint8_t hw_addr[6]; |
dishbreak | 0:2bc22bc992ac | 37 | for (int i = 5; i >=0; i--) { |
dishbreak | 0:2bc22bc992ac | 38 | hw_addr[i] = ble112.getc(); |
dishbreak | 0:2bc22bc992ac | 39 | } |
dishbreak | 0:2bc22bc992ac | 40 | |
dishbreak | 0:2bc22bc992ac | 41 | printf("***************\r\n"); |
dishbreak | 0:2bc22bc992ac | 42 | printf("HW Addr: %02x:%02x:%02x:%02x:%02x:%02x ", |
dishbreak | 0:2bc22bc992ac | 43 | hw_addr[0], |
dishbreak | 0:2bc22bc992ac | 44 | hw_addr[1], |
dishbreak | 0:2bc22bc992ac | 45 | hw_addr[2], |
dishbreak | 0:2bc22bc992ac | 46 | hw_addr[3], |
dishbreak | 0:2bc22bc992ac | 47 | hw_addr[4], |
dishbreak | 0:2bc22bc992ac | 48 | hw_addr[5]); |
dishbreak | 0:2bc22bc992ac | 49 | printf("Obs At: %d\r\n", capPin->getTime()); |
dishbreak | 2:ac474fccf29b | 50 | } |
dishbreak | 2:ac474fccf29b | 51 | |
dishbreak | 2:ac474fccf29b | 52 | void on_master_mode() { |
dishbreak | 2:ac474fccf29b | 53 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 54 | bleScanningLed = 1; |
dishbreak | 2:ac474fccf29b | 55 | } |
dishbreak | 2:ac474fccf29b | 56 | |
dishbreak | 2:ac474fccf29b | 57 | void on_slave_mode() { |
dishbreak | 2:ac474fccf29b | 58 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 59 | bleSlaveLed = 1; |
dishbreak | 1:cb941edd7bce | 60 | intByteArray bitArray; |
dishbreak | 1:cb941edd7bce | 61 | bitArray.integer = capPin->getTime(); |
dishbreak | 2:ac474fccf29b | 62 | printf("Sending timestamp %d (%x)\r\n", bitArray.integer, bitArray.integer); |
dishbreak | 2:ac474fccf29b | 63 | uint8_t event = (uint8_t) EventCode::SET_TIMESTAMP; |
dishbreak | 2:ac474fccf29b | 64 | printf("Sending event: %x\r\n", event); |
dishbreak | 2:ac474fccf29b | 65 | ble112.putc(0x05); |
dishbreak | 1:cb941edd7bce | 66 | for (int i = 3; i >= 0; i--) { |
dishbreak | 1:cb941edd7bce | 67 | ble112.putc(bitArray.byte[i]); |
dishbreak | 1:cb941edd7bce | 68 | } |
dishbreak | 0:2bc22bc992ac | 69 | } |
dishbreak | 0:2bc22bc992ac | 70 | |
dishbreak | 2:ac474fccf29b | 71 | void on_offset_recv() { |
dishbreak | 2:ac474fccf29b | 72 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 73 | bleTimeStampRecv = 1; |
dishbreak | 2:ac474fccf29b | 74 | intByteArray bitArray; |
dishbreak | 2:ac474fccf29b | 75 | for (int i = 3; i >= 0; i--) { |
dishbreak | 2:ac474fccf29b | 76 | bitArray.byte[i] = ble112.getc(); |
dishbreak | 2:ac474fccf29b | 77 | } |
dishbreak | 2:ac474fccf29b | 78 | printf("Got offset %d\r\n", bitArray.signed_integer); |
dishbreak | 2:ac474fccf29b | 79 | } |
dishbreak | 2:ac474fccf29b | 80 | |
dishbreak | 2:ac474fccf29b | 81 | |
dishbreak | 2:ac474fccf29b | 82 | void on_serial_rcv() { |
dishbreak | 2:ac474fccf29b | 83 | uint8_t command = ble112.getc(); |
dishbreak | 2:ac474fccf29b | 84 | printf("Command: %x\r\n", command); |
dishbreak | 2:ac474fccf29b | 85 | switch (command) { |
dishbreak | 2:ac474fccf29b | 86 | case EventCode::ADV_RECV: |
dishbreak | 2:ac474fccf29b | 87 | on_adv_recv(); |
dishbreak | 2:ac474fccf29b | 88 | break; |
dishbreak | 2:ac474fccf29b | 89 | case EventCode::SLAVE_MODE: |
dishbreak | 2:ac474fccf29b | 90 | on_slave_mode(); |
dishbreak | 2:ac474fccf29b | 91 | break; |
dishbreak | 2:ac474fccf29b | 92 | case EventCode::MASTER_MODE: |
dishbreak | 2:ac474fccf29b | 93 | on_master_mode(); |
dishbreak | 2:ac474fccf29b | 94 | break; |
dishbreak | 2:ac474fccf29b | 95 | case EventCode::SET_TIMESTAMP: |
dishbreak | 2:ac474fccf29b | 96 | |
dishbreak | 2:ac474fccf29b | 97 | break; |
dishbreak | 2:ac474fccf29b | 98 | case EventCode::OFFSET_RECV: |
dishbreak | 2:ac474fccf29b | 99 | on_offset_recv(); |
dishbreak | 2:ac474fccf29b | 100 | break; |
dishbreak | 2:ac474fccf29b | 101 | default: |
dishbreak | 2:ac474fccf29b | 102 | printf("Got unexpected byte '%x' from terminal!\r\n",command); |
dishbreak | 2:ac474fccf29b | 103 | break; |
dishbreak | 2:ac474fccf29b | 104 | } |
dishbreak | 2:ac474fccf29b | 105 | } |
dishbreak | 2:ac474fccf29b | 106 | |
dishbreak | 0:2bc22bc992ac | 107 | int main() { |
dishbreak | 2:ac474fccf29b | 108 | printf("Booted!\r\n"); |
dishbreak | 0:2bc22bc992ac | 109 | TimerCapture::startTimer(); |
dishbreak | 0:2bc22bc992ac | 110 | capPin = new TimerCapture(p30); |
dishbreak | 0:2bc22bc992ac | 111 | ble112.baud(115200); |
dishbreak | 2:ac474fccf29b | 112 | //ble112.set_flow_control(SerialBase::RTSCTS, p7, p8); |
dishbreak | 0:2bc22bc992ac | 113 | ble112.attach(&on_serial_rcv); |
dishbreak | 2:ac474fccf29b | 114 | uint8_t command = (uint8_t) EventCode::GET_STATE; |
dishbreak | 2:ac474fccf29b | 115 | printf("Sending command %x\r\n", command); |
dishbreak | 2:ac474fccf29b | 116 | ble112.putc(command); |
dishbreak | 2:ac474fccf29b | 117 | for (int i = 0; i < 4; i++) { |
dishbreak | 2:ac474fccf29b | 118 | ble112.putc(0x00); |
dishbreak | 2:ac474fccf29b | 119 | } |
dishbreak | 0:2bc22bc992ac | 120 | } |