Li Weiyi
/
BalanceCar
虽然移植完毕,但是不work。需要细调……
Embed:
(wiki syntax)
Show/hide line numbers
Microduino_Protocol_HardSer.cpp
00001 #include "Microduino_Protocol_HardSer.h" 00002 #include "MicroduinoPinNames.h" 00003 00004 extern Timer g_Timer; 00005 //DigitalOut myled(D13); 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 /* Protocol::Protocol(PRO_PORT *ser , byte _channel) { 00017 // common_init(); // Set everything to common state, then... 00018 this->channel = _channel; 00019 this->num = 0; 00020 this->sta = false; 00021 this->error = false; 00022 P_Serial = ser; // ...override P_Serial with value passed. 00023 } */ 00024 #if 0 00025 static void uart_callback(void) 00026 { 00027 //myled = !myled; 00028 } 00029 #endif 00030 void Protocol::begin(uint16_t _baud) 00031 { 00032 //myled = 0; 00033 //_timer.start(); 00034 //P_Serial->begin(_baud); 00035 P_Serial->baud(_baud); 00036 //P_Serial->attach(uart_callback, Serial::RxIrq); 00037 //delay(20); 00038 //wait_ms(20); 00039 } 00040 00041 bool Protocol::available(bool _sta) 00042 { 00043 //if (P_Serial->available() > 0) { 00044 if (P_Serial->readable() > 0) { 00045 if (_sta) { 00046 this->inCache = this->inChar; 00047 //this->inChar = P_Serial->read(); 00048 this->inChar = P_Serial->getc(); 00049 this->buffer[num] = this->inChar; 00050 00051 if (this->num > BUFFER_MAX - 1) { 00052 this->num = 0; 00053 return false; 00054 } else { 00055 this->num++; 00056 } 00057 } 00058 return true; 00059 } 00060 return false; 00061 } 00062 00063 uint8_t Protocol::parse(uint16_t* _data, bool _mod) 00064 { 00065 if (available(!_mod)) { 00066 //time = millis(); 00067 //time = _timer.read_ms(); 00068 time = g_Timer.read_ms(); 00069 do { 00070 if (this->sta) { 00071 this->sta = false; 00072 this->num = 0; 00073 if (this->inChar == this->channel) { 00074 this->error = false; 00075 if (!_mod) { 00076 return P_BUSY; 00077 } 00078 } else { 00079 this->error = true; 00080 return P_ERROR; 00081 } 00082 } 00083 00084 if (this->inChar == 0xBB && this->inCache == 0xAA) { 00085 this->sta = true; 00086 if (!_mod) { 00087 return P_BUSY; 00088 } 00089 } 00090 00091 if (this->num == (CHANNEL_NUM * 2 + 1) && !this->error) { 00092 this->inCache = this->buffer[CHANNEL_NUM * 2]; 00093 this->buffer[CHANNEL_NUM * 2] = NULL; 00094 this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer); 00095 00096 this->num = 0; 00097 if (!this->error && this->inCache == this->inChar) { 00098 for (uint8_t a = 0; a < CHANNEL_NUM; a++) { 00099 _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8); 00100 } 00101 return P_FINE; 00102 } else { 00103 return P_ERROR; 00104 } 00105 } else if (!_mod) { 00106 return P_BUSY; 00107 } 00108 } while (_mod && (available(true) && g_Timer.read_ms() - time < 100)); 00109 00110 if (_mod) { 00111 return P_TIMEOUT; 00112 } 00113 } else { 00114 return P_NONE; 00115 } 00116 return P_NONE; 00117 }
Generated on Wed Jul 13 2022 03:34:51 by 1.7.2