p kj
/
MKS22-CubeFine
LPC824
Fork of CubeFine by
Microduino_Protocol_HardSer.cpp@2:7964622fb5a5, 2016-07-04 (annotated)
- Committer:
- beian10
- Date:
- Mon Jul 04 07:17:39 2016 +0000
- Revision:
- 2:7964622fb5a5
- Parent:
- 0:362c1482232c
V2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 0:362c1482232c | 1 | #include "Microduino_Protocol_HardSer.h" |
lixianyu | 0:362c1482232c | 2 | #include "MicroduinoPinNames.h" |
lixianyu | 0:362c1482232c | 3 | //Timer _timer; |
lixianyu | 0:362c1482232c | 4 | extern Timer g_cubeTimer; |
beian10 | 2:7964622fb5a5 | 5 | |
lixianyu | 0:362c1482232c | 6 | uint8_t getChecksum(uint8_t length, uint8_t cmd, uint8_t mydata[]) |
lixianyu | 0:362c1482232c | 7 | { |
lixianyu | 0:362c1482232c | 8 | //三个参数分别为: 数据长度 , 指令代码 , 实际数据数组 |
lixianyu | 0:362c1482232c | 9 | uint8_t checksum = 0; |
lixianyu | 0:362c1482232c | 10 | checksum ^= (length & 0xFF); |
lixianyu | 0:362c1482232c | 11 | checksum ^= (cmd & 0xFF); |
lixianyu | 0:362c1482232c | 12 | for (int i = 0; i < length; i++) checksum ^= (mydata[i] & 0xFF); |
lixianyu | 0:362c1482232c | 13 | return checksum; |
lixianyu | 0:362c1482232c | 14 | } |
lixianyu | 0:362c1482232c | 15 | |
beian10 | 2:7964622fb5a5 | 16 | |
lixianyu | 0:362c1482232c | 17 | void Protocol::begin(uint16_t _baud) |
lixianyu | 0:362c1482232c | 18 | { |
lixianyu | 0:362c1482232c | 19 | P_Serial->baud(_baud); |
lixianyu | 0:362c1482232c | 20 | } |
lixianyu | 0:362c1482232c | 21 | |
lixianyu | 0:362c1482232c | 22 | bool Protocol::available(bool _sta) |
lixianyu | 0:362c1482232c | 23 | { |
lixianyu | 0:362c1482232c | 24 | //if (P_Serial->available() > 0) { |
lixianyu | 0:362c1482232c | 25 | if (P_Serial->readable() > 0) { |
lixianyu | 0:362c1482232c | 26 | if (_sta) { |
lixianyu | 0:362c1482232c | 27 | this->inCache = this->inChar; |
lixianyu | 0:362c1482232c | 28 | //this->inChar = P_Serial->read(); |
lixianyu | 0:362c1482232c | 29 | this->inChar = P_Serial->getc(); |
lixianyu | 0:362c1482232c | 30 | this->buffer[num] = this->inChar; |
lixianyu | 0:362c1482232c | 31 | |
lixianyu | 0:362c1482232c | 32 | if (this->num > BUFFER_MAX - 1) { |
lixianyu | 0:362c1482232c | 33 | this->num = 0; |
lixianyu | 0:362c1482232c | 34 | return false; |
lixianyu | 0:362c1482232c | 35 | } else { |
lixianyu | 0:362c1482232c | 36 | this->num++; |
lixianyu | 0:362c1482232c | 37 | } |
lixianyu | 0:362c1482232c | 38 | } |
lixianyu | 0:362c1482232c | 39 | return true; |
lixianyu | 0:362c1482232c | 40 | } |
lixianyu | 0:362c1482232c | 41 | return false; |
lixianyu | 0:362c1482232c | 42 | } |
lixianyu | 0:362c1482232c | 43 | |
lixianyu | 0:362c1482232c | 44 | uint8_t Protocol::parse(uint16_t* _data, bool _mod) |
lixianyu | 0:362c1482232c | 45 | { |
lixianyu | 0:362c1482232c | 46 | if (available(!_mod)) { |
lixianyu | 0:362c1482232c | 47 | //time = millis(); |
lixianyu | 0:362c1482232c | 48 | //time = _timer.read_ms(); |
lixianyu | 0:362c1482232c | 49 | time = g_cubeTimer.read_ms(); |
lixianyu | 0:362c1482232c | 50 | do { |
lixianyu | 0:362c1482232c | 51 | if (this->sta) { |
lixianyu | 0:362c1482232c | 52 | this->sta = false; |
lixianyu | 0:362c1482232c | 53 | this->num = 0; |
lixianyu | 0:362c1482232c | 54 | if (this->inChar == this->channel) { |
lixianyu | 0:362c1482232c | 55 | this->error = false; |
lixianyu | 0:362c1482232c | 56 | if (!_mod) { |
lixianyu | 0:362c1482232c | 57 | return P_BUSY; |
lixianyu | 0:362c1482232c | 58 | } |
lixianyu | 0:362c1482232c | 59 | } else { |
lixianyu | 0:362c1482232c | 60 | this->error = true; |
lixianyu | 0:362c1482232c | 61 | return P_ERROR; |
lixianyu | 0:362c1482232c | 62 | } |
lixianyu | 0:362c1482232c | 63 | } |
lixianyu | 0:362c1482232c | 64 | |
lixianyu | 0:362c1482232c | 65 | if (this->inChar == 0xBB && this->inCache == 0xAA) { |
lixianyu | 0:362c1482232c | 66 | this->sta = true; |
lixianyu | 0:362c1482232c | 67 | if (!_mod) { |
lixianyu | 0:362c1482232c | 68 | return P_BUSY; |
lixianyu | 0:362c1482232c | 69 | } |
lixianyu | 0:362c1482232c | 70 | } |
lixianyu | 0:362c1482232c | 71 | |
lixianyu | 0:362c1482232c | 72 | if (this->num == (CHANNEL_NUM * 2 + 1) && !this->error) { |
lixianyu | 0:362c1482232c | 73 | this->inCache = this->buffer[CHANNEL_NUM * 2]; |
lixianyu | 0:362c1482232c | 74 | this->buffer[CHANNEL_NUM * 2] = NULL; |
lixianyu | 0:362c1482232c | 75 | this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer); |
lixianyu | 0:362c1482232c | 76 | |
lixianyu | 0:362c1482232c | 77 | this->num = 0; |
lixianyu | 0:362c1482232c | 78 | if (!this->error && this->inCache == this->inChar) { |
lixianyu | 0:362c1482232c | 79 | for (uint8_t a = 0; a < CHANNEL_NUM; a++) { |
lixianyu | 0:362c1482232c | 80 | _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8); |
lixianyu | 0:362c1482232c | 81 | } |
lixianyu | 0:362c1482232c | 82 | return P_FINE; |
lixianyu | 0:362c1482232c | 83 | } else { |
lixianyu | 0:362c1482232c | 84 | return P_ERROR; |
lixianyu | 0:362c1482232c | 85 | } |
lixianyu | 0:362c1482232c | 86 | } else if (!_mod) { |
lixianyu | 0:362c1482232c | 87 | return P_BUSY; |
lixianyu | 0:362c1482232c | 88 | } |
lixianyu | 0:362c1482232c | 89 | } while (_mod && (available(true) && g_cubeTimer.read_ms() - time < 100)); |
lixianyu | 0:362c1482232c | 90 | |
lixianyu | 0:362c1482232c | 91 | if (_mod) { |
lixianyu | 0:362c1482232c | 92 | return P_TIMEOUT; |
lixianyu | 0:362c1482232c | 93 | } |
lixianyu | 0:362c1482232c | 94 | } |
beian10 | 2:7964622fb5a5 | 95 | |
beian10 | 2:7964622fb5a5 | 96 | return P_NONE; |
beian10 | 2:7964622fb5a5 | 97 | |
lixianyu | 0:362c1482232c | 98 | } |