![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
If you wont to knock me down, look this file...
Communication/XBee/XBee.cpp@0:4df75b08b14a, 2019-05-03 (annotated)
- Committer:
- Ryosei
- Date:
- Fri May 03 11:47:27 2019 +0000
- Revision:
- 0:4df75b08b14a
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ryosei | 0:4df75b08b14a | 1 | #include "XBee.h" |
Ryosei | 0:4df75b08b14a | 2 | |
Ryosei | 0:4df75b08b14a | 3 | #include <stdint.h> |
Ryosei | 0:4df75b08b14a | 4 | #include "mbed.h" |
Ryosei | 0:4df75b08b14a | 5 | |
Ryosei | 0:4df75b08b14a | 6 | namespace XBEE |
Ryosei | 0:4df75b08b14a | 7 | { |
Ryosei | 0:4df75b08b14a | 8 | Ticker xbee_timer; |
Ryosei | 0:4df75b08b14a | 9 | Serial xbee_uart(XBEE_TX, XBEE_RX); |
Ryosei | 0:4df75b08b14a | 10 | DigitalOut XBee_LED(LED1); |
Ryosei | 0:4df75b08b14a | 11 | |
Ryosei | 0:4df75b08b14a | 12 | void uartUpdate(void); |
Ryosei | 0:4df75b08b14a | 13 | void lostCheck(void); |
Ryosei | 0:4df75b08b14a | 14 | |
Ryosei | 0:4df75b08b14a | 15 | namespace |
Ryosei | 0:4df75b08b14a | 16 | { |
Ryosei | 0:4df75b08b14a | 17 | ControllerData ctrData; |
Ryosei | 0:4df75b08b14a | 18 | ControllerData keepCtrData; |
Ryosei | 0:4df75b08b14a | 19 | const uint8_t defaultData[4] = CTR_DEFAULT_DATA; |
Ryosei | 0:4df75b08b14a | 20 | const char check[] = "DT="; |
Ryosei | 0:4df75b08b14a | 21 | volatile char packet[24]; |
Ryosei | 0:4df75b08b14a | 22 | |
Ryosei | 0:4df75b08b14a | 23 | bool controllerLost = false; |
Ryosei | 0:4df75b08b14a | 24 | uint8_t timerCount = 0; |
Ryosei | 0:4df75b08b14a | 25 | } |
Ryosei | 0:4df75b08b14a | 26 | |
Ryosei | 0:4df75b08b14a | 27 | void Controller::Initialize(void) { |
Ryosei | 0:4df75b08b14a | 28 | xbee_timer.attach(lostCheck, 0.025); |
Ryosei | 0:4df75b08b14a | 29 | xbee_uart.baud(4800); |
Ryosei | 0:4df75b08b14a | 30 | xbee_uart.attach(uartUpdate, Serial::RxIrq); |
Ryosei | 0:4df75b08b14a | 31 | DataReset(); |
Ryosei | 0:4df75b08b14a | 32 | } |
Ryosei | 0:4df75b08b14a | 33 | |
Ryosei | 0:4df75b08b14a | 34 | ControllerData* Controller::GetData(void) { |
Ryosei | 0:4df75b08b14a | 35 | __disable_irq(); |
Ryosei | 0:4df75b08b14a | 36 | for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) keepCtrData.buf[i] = ctrData.buf[i]; |
Ryosei | 0:4df75b08b14a | 37 | __enable_irq(); |
Ryosei | 0:4df75b08b14a | 38 | return &keepCtrData; |
Ryosei | 0:4df75b08b14a | 39 | } |
Ryosei | 0:4df75b08b14a | 40 | |
Ryosei | 0:4df75b08b14a | 41 | void Controller::DataReset(void) { |
Ryosei | 0:4df75b08b14a | 42 | __disable_irq(); |
Ryosei | 0:4df75b08b14a | 43 | for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) ctrData.buf[i] = defaultData[i]; |
Ryosei | 0:4df75b08b14a | 44 | __enable_irq(); |
Ryosei | 0:4df75b08b14a | 45 | } |
Ryosei | 0:4df75b08b14a | 46 | |
Ryosei | 0:4df75b08b14a | 47 | bool Controller::CheckControllerLost(void) { |
Ryosei | 0:4df75b08b14a | 48 | return controllerLost; |
Ryosei | 0:4df75b08b14a | 49 | } |
Ryosei | 0:4df75b08b14a | 50 | |
Ryosei | 0:4df75b08b14a | 51 | void uartUpdate(void) { |
Ryosei | 0:4df75b08b14a | 52 | static bool phase = false; |
Ryosei | 0:4df75b08b14a | 53 | static uint8_t count = 0; |
Ryosei | 0:4df75b08b14a | 54 | |
Ryosei | 0:4df75b08b14a | 55 | char data = xbee_uart.getc(); |
Ryosei | 0:4df75b08b14a | 56 | |
Ryosei | 0:4df75b08b14a | 57 | if(phase) |
Ryosei | 0:4df75b08b14a | 58 | { |
Ryosei | 0:4df75b08b14a | 59 | packet[count] = data; |
Ryosei | 0:4df75b08b14a | 60 | if(count < 2) |
Ryosei | 0:4df75b08b14a | 61 | { |
Ryosei | 0:4df75b08b14a | 62 | if(data != check[count]) |
Ryosei | 0:4df75b08b14a | 63 | { |
Ryosei | 0:4df75b08b14a | 64 | phase = false; |
Ryosei | 0:4df75b08b14a | 65 | controllerLost = true; |
Ryosei | 0:4df75b08b14a | 66 | XBee_LED = LED_OFF; |
Ryosei | 0:4df75b08b14a | 67 | } |
Ryosei | 0:4df75b08b14a | 68 | } |
Ryosei | 0:4df75b08b14a | 69 | else if(count == 8) |
Ryosei | 0:4df75b08b14a | 70 | { |
Ryosei | 0:4df75b08b14a | 71 | if(data != '\r') |
Ryosei | 0:4df75b08b14a | 72 | { |
Ryosei | 0:4df75b08b14a | 73 | phase = false; |
Ryosei | 0:4df75b08b14a | 74 | controllerLost = true; |
Ryosei | 0:4df75b08b14a | 75 | XBee_LED = LED_OFF; |
Ryosei | 0:4df75b08b14a | 76 | } |
Ryosei | 0:4df75b08b14a | 77 | else |
Ryosei | 0:4df75b08b14a | 78 | { |
Ryosei | 0:4df75b08b14a | 79 | ctrData.buf[0] = packet[4]; |
Ryosei | 0:4df75b08b14a | 80 | ctrData.buf[1] = packet[5]; |
Ryosei | 0:4df75b08b14a | 81 | ctrData.buf[2] = packet[6]; |
Ryosei | 0:4df75b08b14a | 82 | ctrData.buf[3] = packet[7]; |
Ryosei | 0:4df75b08b14a | 83 | phase = false; |
Ryosei | 0:4df75b08b14a | 84 | timerCount = 0; |
Ryosei | 0:4df75b08b14a | 85 | controllerLost = false; |
Ryosei | 0:4df75b08b14a | 86 | XBee_LED = LED_ON; |
Ryosei | 0:4df75b08b14a | 87 | } |
Ryosei | 0:4df75b08b14a | 88 | } |
Ryosei | 0:4df75b08b14a | 89 | count++; |
Ryosei | 0:4df75b08b14a | 90 | } |
Ryosei | 0:4df75b08b14a | 91 | else |
Ryosei | 0:4df75b08b14a | 92 | { |
Ryosei | 0:4df75b08b14a | 93 | if(data == '@') |
Ryosei | 0:4df75b08b14a | 94 | { |
Ryosei | 0:4df75b08b14a | 95 | count = 0; |
Ryosei | 0:4df75b08b14a | 96 | phase = true; |
Ryosei | 0:4df75b08b14a | 97 | } |
Ryosei | 0:4df75b08b14a | 98 | } |
Ryosei | 0:4df75b08b14a | 99 | } |
Ryosei | 0:4df75b08b14a | 100 | |
Ryosei | 0:4df75b08b14a | 101 | void lostCheck(void) { |
Ryosei | 0:4df75b08b14a | 102 | timerCount++; |
Ryosei | 0:4df75b08b14a | 103 | if(timerCount == 2) XBee_LED = LED_OFF; |
Ryosei | 0:4df75b08b14a | 104 | if(timerCount >= 20) { |
Ryosei | 0:4df75b08b14a | 105 | controllerLost = true; |
Ryosei | 0:4df75b08b14a | 106 | Controller::DataReset(); |
Ryosei | 0:4df75b08b14a | 107 | timerCount = 0; |
Ryosei | 0:4df75b08b14a | 108 | XBee_LED = LED_OFF; |
Ryosei | 0:4df75b08b14a | 109 | } |
Ryosei | 0:4df75b08b14a | 110 | } |
Ryosei | 0:4df75b08b14a | 111 | } |
Ryosei | 0:4df75b08b14a | 112 |