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@3:54fef6ac433b, 2015-05-30 (annotated)
- Committer:
- dishbreak
- Date:
- Sat May 30 04:34:45 2015 +0000
- Revision:
- 3:54fef6ac433b
- Parent:
- 2:ac474fccf29b
- Child:
- 4:778626dfcc92
Adding ticker to check for output.
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 | 3:54fef6ac433b | 14 | Ticker ticker; |
dishbreak | 2:ac474fccf29b | 15 | |
dishbreak | 2:ac474fccf29b | 16 | void blankOutLeds() { |
dishbreak | 2:ac474fccf29b | 17 | bleSlaveLed = 0; |
dishbreak | 2:ac474fccf29b | 18 | bleScanningLed = 0; |
dishbreak | 2:ac474fccf29b | 19 | bleAdvRecv = 0; |
dishbreak | 2:ac474fccf29b | 20 | bleTimeStampRecv = 0; |
dishbreak | 2:ac474fccf29b | 21 | } |
dishbreak | 0:2bc22bc992ac | 22 | |
dishbreak | 2:ac474fccf29b | 23 | /*uint8_t[] toByteArray(uint32_t input) { |
dishbreak | 2:ac474fccf29b | 24 | uint8_t byte[4]; |
dishbreak | 2:ac474fccf29b | 25 | |
dishbreak | 2:ac474fccf29b | 26 | byte[0] = input & 0x000000FF; |
dishbreak | 2:ac474fccf29b | 27 | byte[1] = (input & 0x0000FF00) >> 8; |
dishbreak | 2:ac474fccf29b | 28 | byte[2] = (input & 0x00FF0000) >> 16; |
dishbreak | 2:ac474fccf29b | 29 | byte[3] = (input & 0xFF000000) >> 24; |
dishbreak | 2:ac474fccf29b | 30 | |
dishbreak | 2:ac474fccf29b | 31 | return byte; |
dishbreak | 2:ac474fccf29b | 32 | }*/ |
dishbreak | 2:ac474fccf29b | 33 | |
dishbreak | 2:ac474fccf29b | 34 | void on_adv_recv() { |
dishbreak | 2:ac474fccf29b | 35 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 36 | bleAdvRecv = 1; |
dishbreak | 0:2bc22bc992ac | 37 | uint8_t hw_addr[6]; |
dishbreak | 0:2bc22bc992ac | 38 | for (int i = 5; i >=0; i--) { |
dishbreak | 0:2bc22bc992ac | 39 | hw_addr[i] = ble112.getc(); |
dishbreak | 0:2bc22bc992ac | 40 | } |
dishbreak | 0:2bc22bc992ac | 41 | |
dishbreak | 0:2bc22bc992ac | 42 | printf("***************\r\n"); |
dishbreak | 0:2bc22bc992ac | 43 | printf("HW Addr: %02x:%02x:%02x:%02x:%02x:%02x ", |
dishbreak | 0:2bc22bc992ac | 44 | hw_addr[0], |
dishbreak | 0:2bc22bc992ac | 45 | hw_addr[1], |
dishbreak | 0:2bc22bc992ac | 46 | hw_addr[2], |
dishbreak | 0:2bc22bc992ac | 47 | hw_addr[3], |
dishbreak | 0:2bc22bc992ac | 48 | hw_addr[4], |
dishbreak | 0:2bc22bc992ac | 49 | hw_addr[5]); |
dishbreak | 0:2bc22bc992ac | 50 | printf("Obs At: %d\r\n", capPin->getTime()); |
dishbreak | 2:ac474fccf29b | 51 | } |
dishbreak | 2:ac474fccf29b | 52 | |
dishbreak | 2:ac474fccf29b | 53 | void on_master_mode() { |
dishbreak | 2:ac474fccf29b | 54 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 55 | bleScanningLed = 1; |
dishbreak | 2:ac474fccf29b | 56 | } |
dishbreak | 2:ac474fccf29b | 57 | |
dishbreak | 2:ac474fccf29b | 58 | void on_slave_mode() { |
dishbreak | 2:ac474fccf29b | 59 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 60 | bleSlaveLed = 1; |
dishbreak | 1:cb941edd7bce | 61 | intByteArray bitArray; |
dishbreak | 1:cb941edd7bce | 62 | bitArray.integer = capPin->getTime(); |
dishbreak | 3:54fef6ac433b | 63 | printf("Sending timestamp %d (%08x)\r\n", bitArray.integer, bitArray.integer); |
dishbreak | 2:ac474fccf29b | 64 | uint8_t event = (uint8_t) EventCode::SET_TIMESTAMP; |
dishbreak | 2:ac474fccf29b | 65 | printf("Sending event: %x\r\n", event); |
dishbreak | 2:ac474fccf29b | 66 | ble112.putc(0x05); |
dishbreak | 3:54fef6ac433b | 67 | for (int i = 0; i < 4; i++) { |
dishbreak | 1:cb941edd7bce | 68 | ble112.putc(bitArray.byte[i]); |
dishbreak | 1:cb941edd7bce | 69 | } |
dishbreak | 0:2bc22bc992ac | 70 | } |
dishbreak | 0:2bc22bc992ac | 71 | |
dishbreak | 2:ac474fccf29b | 72 | void on_offset_recv() { |
dishbreak | 2:ac474fccf29b | 73 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 74 | bleTimeStampRecv = 1; |
dishbreak | 2:ac474fccf29b | 75 | intByteArray bitArray; |
dishbreak | 2:ac474fccf29b | 76 | for (int i = 3; i >= 0; i--) { |
dishbreak | 2:ac474fccf29b | 77 | bitArray.byte[i] = ble112.getc(); |
dishbreak | 2:ac474fccf29b | 78 | } |
dishbreak | 2:ac474fccf29b | 79 | printf("Got offset %d\r\n", bitArray.signed_integer); |
dishbreak | 2:ac474fccf29b | 80 | } |
dishbreak | 2:ac474fccf29b | 81 | |
dishbreak | 2:ac474fccf29b | 82 | |
dishbreak | 2:ac474fccf29b | 83 | void on_serial_rcv() { |
dishbreak | 2:ac474fccf29b | 84 | uint8_t command = ble112.getc(); |
dishbreak | 2:ac474fccf29b | 85 | printf("Command: %x\r\n", command); |
dishbreak | 2:ac474fccf29b | 86 | switch (command) { |
dishbreak | 2:ac474fccf29b | 87 | case EventCode::ADV_RECV: |
dishbreak | 2:ac474fccf29b | 88 | on_adv_recv(); |
dishbreak | 2:ac474fccf29b | 89 | break; |
dishbreak | 2:ac474fccf29b | 90 | case EventCode::SLAVE_MODE: |
dishbreak | 2:ac474fccf29b | 91 | on_slave_mode(); |
dishbreak | 2:ac474fccf29b | 92 | break; |
dishbreak | 2:ac474fccf29b | 93 | case EventCode::MASTER_MODE: |
dishbreak | 2:ac474fccf29b | 94 | on_master_mode(); |
dishbreak | 2:ac474fccf29b | 95 | break; |
dishbreak | 2:ac474fccf29b | 96 | case EventCode::SET_TIMESTAMP: |
dishbreak | 2:ac474fccf29b | 97 | |
dishbreak | 2:ac474fccf29b | 98 | break; |
dishbreak | 2:ac474fccf29b | 99 | case EventCode::OFFSET_RECV: |
dishbreak | 2:ac474fccf29b | 100 | on_offset_recv(); |
dishbreak | 2:ac474fccf29b | 101 | break; |
dishbreak | 2:ac474fccf29b | 102 | default: |
dishbreak | 2:ac474fccf29b | 103 | printf("Got unexpected byte '%x' from terminal!\r\n",command); |
dishbreak | 2:ac474fccf29b | 104 | break; |
dishbreak | 2:ac474fccf29b | 105 | } |
dishbreak | 2:ac474fccf29b | 106 | } |
dishbreak | 2:ac474fccf29b | 107 | |
dishbreak | 3:54fef6ac433b | 108 | void on_tick() { |
dishbreak | 3:54fef6ac433b | 109 | uint32_t timestamp = capPin->getTime(); |
dishbreak | 3:54fef6ac433b | 110 | printf("+++Capture register: %d (%08x)\r\n", timestamp, timestamp); |
dishbreak | 3:54fef6ac433b | 111 | } |
dishbreak | 3:54fef6ac433b | 112 | |
dishbreak | 0:2bc22bc992ac | 113 | int main() { |
dishbreak | 2:ac474fccf29b | 114 | printf("Booted!\r\n"); |
dishbreak | 3:54fef6ac433b | 115 | ticker.attach(&on_tick, 5.0); |
dishbreak | 0:2bc22bc992ac | 116 | TimerCapture::startTimer(); |
dishbreak | 0:2bc22bc992ac | 117 | capPin = new TimerCapture(p30); |
dishbreak | 3:54fef6ac433b | 118 | ble112.baud(9600); |
dishbreak | 3:54fef6ac433b | 119 | ble112.set_flow_control(SerialBase::RTSCTS, p7, p8); |
dishbreak | 0:2bc22bc992ac | 120 | ble112.attach(&on_serial_rcv); |
dishbreak | 2:ac474fccf29b | 121 | uint8_t command = (uint8_t) EventCode::GET_STATE; |
dishbreak | 2:ac474fccf29b | 122 | printf("Sending command %x\r\n", command); |
dishbreak | 2:ac474fccf29b | 123 | ble112.putc(command); |
dishbreak | 2:ac474fccf29b | 124 | for (int i = 0; i < 4; i++) { |
dishbreak | 2:ac474fccf29b | 125 | ble112.putc(0x00); |
dishbreak | 2:ac474fccf29b | 126 | } |
dishbreak | 0:2bc22bc992ac | 127 | } |