Li Weiyi
/
Cube
Microduino的cube小车。
Microduino_Protocol_HardSer.cpp
- Committer:
- lixianyu
- Date:
- 2016-05-27
- Revision:
- 3:e4ac7c1a14de
- Child:
- 4:0670023d3f36
File content as of revision 3:e4ac7c1a14de:
#include "Microduino_Protocol_HardSer.h" uint8_t getChecksum(uint8_t length, uint8_t cmd, uint8_t mydata[]) { //三个参数分别为: 数据长度 , 指令代码 , 实际数据数组 uint8_t checksum = 0; checksum ^= (length & 0xFF); checksum ^= (cmd & 0xFF); for (int i = 0; i < length; i++) checksum ^= (mydata[i] & 0xFF); return checksum; } /* Protocol::Protocol(PRO_PORT *ser , byte _channel) { // common_init(); // Set everything to common state, then... this->channel = _channel; this->num = 0; this->sta = false; this->error = false; P_Serial = ser; // ...override P_Serial with value passed. } */ void Protocol::begin(uint16_t _baud) { //P_Serial->begin(_baud); P_Serial->baud(_baud); //delay(20); wait_ms(20); } bool Protocol::available(bool _sta) { //if (P_Serial->available() > 0) { if (P_Serial->readable() > 0) { if (_sta) { this->inCache = this->inChar; //this->inChar = P_Serial->read(); this->inChar = P_Serial->getc(); this->buffer[num] = this->inChar; if (this->num > BUFFER_MAX - 1) { this->num = 0; return false; } else { this->num++; } } return true; } return false; } uint8_t Protocol::parse(uint16_t* _data, bool _mod) { if (available(!_mod)) { //time = millis(); time = _timer.read_ms(); do { if (this->sta) { this->sta = false; this->num = 0; if (this->inChar == this->channel) { this->error = false; if (!_mod) { return P_BUSY; } } else { this->error = true; return P_ERROR; } } if (this->inChar == 0xBB && this->inCache == 0xAA) { this->sta = true; if (!_mod) { return P_BUSY; } } if (this->num == (CHANNEL_NUM * 2 + 1) && !this->error) { this->inCache = this->buffer[CHANNEL_NUM * 2]; this->buffer[CHANNEL_NUM * 2] = NULL; this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer); this->num = 0; if (!this->error && this->inCache == this->inChar) { for (uint8_t a = 0; a < CHANNEL_NUM; a++) { _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8); } return P_FINE; } else { return P_ERROR; } } else if (!_mod) { return P_BUSY; } } while (_mod && (available(true) && _timer.read_ms() - time < 100)); if (_mod) { return P_TIMEOUT; } } else { return P_NONE; } }