wu le
/
CubeFine
test
Fork of CubeFine by
Embed:
(wiki syntax)
Show/hide line numbers
Microduino_Protocol_HardSer.cpp
00001 #include "Microduino_Protocol_HardSer.h" 00002 #include "MicroduinoPinNames.h" 00003 //Timer _timer; 00004 extern Timer g_cubeTimer; 00005 00006 uint8_t getChecksum(uint8_t length, uint8_t cmd, uint8_t mydata[]) 00007 { 00008 //三个参数分别为: 数据长度 , 指令代码 , 实际数据数组 00009 uint8_t checksum = 0; 00010 checksum ^= (length & 0xFF); 00011 checksum ^= (cmd & 0xFF); 00012 for (int i = 0; i < length; i++) checksum ^= (mydata[i] & 0xFF); 00013 return checksum; 00014 } 00015 00016 00017 void Protocol::begin(uint16_t _baud) 00018 { 00019 P_Serial->baud(_baud); 00020 } 00021 00022 bool Protocol::available(bool _sta) 00023 { 00024 //if (P_Serial->available() > 0) { 00025 if (P_Serial->readable() > 0) { 00026 if (_sta) { 00027 this->inCache = this->inChar; 00028 //this->inChar = P_Serial->read(); 00029 this->inChar = P_Serial->getc(); 00030 this->buffer[num] = this->inChar; 00031 00032 if (this->num > BUFFER_MAX - 1) { 00033 this->num = 0; 00034 return false; 00035 } else { 00036 this->num++; 00037 } 00038 } 00039 return true; 00040 } 00041 return false; 00042 } 00043 00044 uint8_t Protocol::parse(uint16_t* _data, bool _mod) 00045 { 00046 if (available(!_mod)) { 00047 //time = millis(); 00048 //time = _timer.read_ms(); 00049 time = g_cubeTimer.read_ms(); 00050 do { 00051 if (this->sta) { 00052 this->sta = false; 00053 this->num = 0; 00054 if (this->inChar == this->channel) { 00055 this->error = false; 00056 if (!_mod) { 00057 return P_BUSY; 00058 } 00059 } else { 00060 this->error = true; 00061 return P_ERROR; 00062 } 00063 } 00064 00065 if (this->inChar == 0xBB && this->inCache == 0xAA) { 00066 this->sta = true; 00067 if (!_mod) { 00068 return P_BUSY; 00069 } 00070 } 00071 00072 if (this->num == (CHANNEL_NUM * 2 + 1) && !this->error) { 00073 this->inCache = this->buffer[CHANNEL_NUM * 2]; 00074 this->buffer[CHANNEL_NUM * 2] = NULL; 00075 this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer); 00076 00077 this->num = 0; 00078 if (!this->error && this->inCache == this->inChar) { 00079 for (uint8_t a = 0; a < CHANNEL_NUM; a++) { 00080 _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8); 00081 } 00082 return P_FINE; 00083 } else { 00084 return P_ERROR; 00085 } 00086 } else if (!_mod) { 00087 return P_BUSY; 00088 } 00089 } while (_mod && (available(true) && g_cubeTimer.read_ms() - time < 100)); 00090 00091 if (_mod) { 00092 return P_TIMEOUT; 00093 } 00094 } 00095 00096 return P_NONE; 00097 00098 }
Generated on Wed Jul 20 2022 02:00:50 by 1.7.2