p kj
/
LPC824-BalanceCar
Microduino
Fork of BalanceCar by
Microduino_Protocol_HardSer.cpp@3:c6caae712d5d, 2016-06-07 (annotated)
- Committer:
- lixianyu
- Date:
- Tue Jun 07 08:14:15 2016 +0000
- Revision:
- 3:c6caae712d5d
- Parent:
- 2:99785a1007a4
??????????work? ?????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 0:a4d8f5b3c546 | 1 | #include "Microduino_Protocol_HardSer.h" |
lixianyu | 0:a4d8f5b3c546 | 2 | #include "MicroduinoPinNames.h" |
lixianyu | 0:a4d8f5b3c546 | 3 | |
lixianyu | 0:a4d8f5b3c546 | 4 | extern Timer g_Timer; |
lixianyu | 0:a4d8f5b3c546 | 5 | //DigitalOut myled(D13); |
lixianyu | 0:a4d8f5b3c546 | 6 | uint8_t getChecksum(uint8_t length, uint8_t cmd, uint8_t mydata[]) |
lixianyu | 0:a4d8f5b3c546 | 7 | { |
lixianyu | 0:a4d8f5b3c546 | 8 | //三个参数分别为: 数据长度 , 指令代码 , 实际数据数组 |
lixianyu | 0:a4d8f5b3c546 | 9 | uint8_t checksum = 0; |
lixianyu | 0:a4d8f5b3c546 | 10 | checksum ^= (length & 0xFF); |
lixianyu | 0:a4d8f5b3c546 | 11 | checksum ^= (cmd & 0xFF); |
lixianyu | 0:a4d8f5b3c546 | 12 | for (int i = 0; i < length; i++) checksum ^= (mydata[i] & 0xFF); |
lixianyu | 0:a4d8f5b3c546 | 13 | return checksum; |
lixianyu | 0:a4d8f5b3c546 | 14 | } |
lixianyu | 0:a4d8f5b3c546 | 15 | |
lixianyu | 0:a4d8f5b3c546 | 16 | /* Protocol::Protocol(PRO_PORT *ser , byte _channel) { |
lixianyu | 0:a4d8f5b3c546 | 17 | // common_init(); // Set everything to common state, then... |
lixianyu | 0:a4d8f5b3c546 | 18 | this->channel = _channel; |
lixianyu | 0:a4d8f5b3c546 | 19 | this->num = 0; |
lixianyu | 0:a4d8f5b3c546 | 20 | this->sta = false; |
lixianyu | 0:a4d8f5b3c546 | 21 | this->error = false; |
lixianyu | 0:a4d8f5b3c546 | 22 | P_Serial = ser; // ...override P_Serial with value passed. |
lixianyu | 0:a4d8f5b3c546 | 23 | } */ |
lixianyu | 2:99785a1007a4 | 24 | #if 0 |
lixianyu | 0:a4d8f5b3c546 | 25 | static void uart_callback(void) |
lixianyu | 0:a4d8f5b3c546 | 26 | { |
lixianyu | 0:a4d8f5b3c546 | 27 | //myled = !myled; |
lixianyu | 0:a4d8f5b3c546 | 28 | } |
lixianyu | 2:99785a1007a4 | 29 | #endif |
lixianyu | 0:a4d8f5b3c546 | 30 | void Protocol::begin(uint16_t _baud) |
lixianyu | 0:a4d8f5b3c546 | 31 | { |
lixianyu | 0:a4d8f5b3c546 | 32 | //myled = 0; |
lixianyu | 0:a4d8f5b3c546 | 33 | //_timer.start(); |
lixianyu | 0:a4d8f5b3c546 | 34 | //P_Serial->begin(_baud); |
lixianyu | 0:a4d8f5b3c546 | 35 | P_Serial->baud(_baud); |
lixianyu | 0:a4d8f5b3c546 | 36 | //P_Serial->attach(uart_callback, Serial::RxIrq); |
lixianyu | 0:a4d8f5b3c546 | 37 | //delay(20); |
lixianyu | 0:a4d8f5b3c546 | 38 | //wait_ms(20); |
lixianyu | 0:a4d8f5b3c546 | 39 | } |
lixianyu | 0:a4d8f5b3c546 | 40 | |
lixianyu | 0:a4d8f5b3c546 | 41 | bool Protocol::available(bool _sta) |
lixianyu | 0:a4d8f5b3c546 | 42 | { |
lixianyu | 0:a4d8f5b3c546 | 43 | //if (P_Serial->available() > 0) { |
lixianyu | 0:a4d8f5b3c546 | 44 | if (P_Serial->readable() > 0) { |
lixianyu | 0:a4d8f5b3c546 | 45 | if (_sta) { |
lixianyu | 0:a4d8f5b3c546 | 46 | this->inCache = this->inChar; |
lixianyu | 0:a4d8f5b3c546 | 47 | //this->inChar = P_Serial->read(); |
lixianyu | 0:a4d8f5b3c546 | 48 | this->inChar = P_Serial->getc(); |
lixianyu | 0:a4d8f5b3c546 | 49 | this->buffer[num] = this->inChar; |
lixianyu | 0:a4d8f5b3c546 | 50 | |
lixianyu | 0:a4d8f5b3c546 | 51 | if (this->num > BUFFER_MAX - 1) { |
lixianyu | 0:a4d8f5b3c546 | 52 | this->num = 0; |
lixianyu | 0:a4d8f5b3c546 | 53 | return false; |
lixianyu | 0:a4d8f5b3c546 | 54 | } else { |
lixianyu | 0:a4d8f5b3c546 | 55 | this->num++; |
lixianyu | 0:a4d8f5b3c546 | 56 | } |
lixianyu | 0:a4d8f5b3c546 | 57 | } |
lixianyu | 0:a4d8f5b3c546 | 58 | return true; |
lixianyu | 0:a4d8f5b3c546 | 59 | } |
lixianyu | 0:a4d8f5b3c546 | 60 | return false; |
lixianyu | 0:a4d8f5b3c546 | 61 | } |
lixianyu | 0:a4d8f5b3c546 | 62 | |
lixianyu | 0:a4d8f5b3c546 | 63 | uint8_t Protocol::parse(uint16_t* _data, bool _mod) |
lixianyu | 0:a4d8f5b3c546 | 64 | { |
lixianyu | 0:a4d8f5b3c546 | 65 | if (available(!_mod)) { |
lixianyu | 0:a4d8f5b3c546 | 66 | //time = millis(); |
lixianyu | 0:a4d8f5b3c546 | 67 | //time = _timer.read_ms(); |
lixianyu | 0:a4d8f5b3c546 | 68 | time = g_Timer.read_ms(); |
lixianyu | 0:a4d8f5b3c546 | 69 | do { |
lixianyu | 0:a4d8f5b3c546 | 70 | if (this->sta) { |
lixianyu | 0:a4d8f5b3c546 | 71 | this->sta = false; |
lixianyu | 0:a4d8f5b3c546 | 72 | this->num = 0; |
lixianyu | 0:a4d8f5b3c546 | 73 | if (this->inChar == this->channel) { |
lixianyu | 0:a4d8f5b3c546 | 74 | this->error = false; |
lixianyu | 0:a4d8f5b3c546 | 75 | if (!_mod) { |
lixianyu | 0:a4d8f5b3c546 | 76 | return P_BUSY; |
lixianyu | 0:a4d8f5b3c546 | 77 | } |
lixianyu | 0:a4d8f5b3c546 | 78 | } else { |
lixianyu | 0:a4d8f5b3c546 | 79 | this->error = true; |
lixianyu | 0:a4d8f5b3c546 | 80 | return P_ERROR; |
lixianyu | 0:a4d8f5b3c546 | 81 | } |
lixianyu | 0:a4d8f5b3c546 | 82 | } |
lixianyu | 0:a4d8f5b3c546 | 83 | |
lixianyu | 0:a4d8f5b3c546 | 84 | if (this->inChar == 0xBB && this->inCache == 0xAA) { |
lixianyu | 0:a4d8f5b3c546 | 85 | this->sta = true; |
lixianyu | 0:a4d8f5b3c546 | 86 | if (!_mod) { |
lixianyu | 0:a4d8f5b3c546 | 87 | return P_BUSY; |
lixianyu | 0:a4d8f5b3c546 | 88 | } |
lixianyu | 0:a4d8f5b3c546 | 89 | } |
lixianyu | 0:a4d8f5b3c546 | 90 | |
lixianyu | 0:a4d8f5b3c546 | 91 | if (this->num == (CHANNEL_NUM * 2 + 1) && !this->error) { |
lixianyu | 0:a4d8f5b3c546 | 92 | this->inCache = this->buffer[CHANNEL_NUM * 2]; |
lixianyu | 0:a4d8f5b3c546 | 93 | this->buffer[CHANNEL_NUM * 2] = NULL; |
lixianyu | 0:a4d8f5b3c546 | 94 | this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer); |
lixianyu | 0:a4d8f5b3c546 | 95 | |
lixianyu | 0:a4d8f5b3c546 | 96 | this->num = 0; |
lixianyu | 0:a4d8f5b3c546 | 97 | if (!this->error && this->inCache == this->inChar) { |
lixianyu | 0:a4d8f5b3c546 | 98 | for (uint8_t a = 0; a < CHANNEL_NUM; a++) { |
lixianyu | 0:a4d8f5b3c546 | 99 | _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8); |
lixianyu | 0:a4d8f5b3c546 | 100 | } |
lixianyu | 0:a4d8f5b3c546 | 101 | return P_FINE; |
lixianyu | 0:a4d8f5b3c546 | 102 | } else { |
lixianyu | 0:a4d8f5b3c546 | 103 | return P_ERROR; |
lixianyu | 0:a4d8f5b3c546 | 104 | } |
lixianyu | 0:a4d8f5b3c546 | 105 | } else if (!_mod) { |
lixianyu | 0:a4d8f5b3c546 | 106 | return P_BUSY; |
lixianyu | 0:a4d8f5b3c546 | 107 | } |
lixianyu | 0:a4d8f5b3c546 | 108 | } while (_mod && (available(true) && g_Timer.read_ms() - time < 100)); |
lixianyu | 0:a4d8f5b3c546 | 109 | |
lixianyu | 0:a4d8f5b3c546 | 110 | if (_mod) { |
lixianyu | 0:a4d8f5b3c546 | 111 | return P_TIMEOUT; |
lixianyu | 0:a4d8f5b3c546 | 112 | } |
lixianyu | 0:a4d8f5b3c546 | 113 | } else { |
lixianyu | 0:a4d8f5b3c546 | 114 | return P_NONE; |
lixianyu | 0:a4d8f5b3c546 | 115 | } |
lixianyu | 2:99785a1007a4 | 116 | return P_NONE; |
lixianyu | 0:a4d8f5b3c546 | 117 | } |