work fine.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Microduino_Protocol_HardSer.cpp Source File

Microduino_Protocol_HardSer.cpp

00001 #include "Microduino_Protocol_HardSer.h"
00002 #include "MicroduinoPinNames.h"
00003 //Timer _timer;
00004 extern Timer g_cubeTimer;
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 static void uart_callback(void)
00025 {
00026     //myled = !myled;
00027 }
00028 void Protocol::begin(uint16_t _baud)
00029 {
00030     //myled = 0;
00031     //_timer.start();
00032     //P_Serial->begin(_baud);
00033     P_Serial->baud(_baud);
00034     //P_Serial->attach(uart_callback, Serial::RxIrq);
00035     //delay(20);
00036     //wait_ms(20);
00037 }
00038 
00039 bool Protocol::available(bool _sta)
00040 {
00041     //if (P_Serial->available() > 0) {
00042     if (P_Serial->readable() > 0) {
00043         if (_sta) {
00044             this->inCache = this->inChar;
00045             //this->inChar = P_Serial->read();
00046             this->inChar = P_Serial->getc();
00047             this->buffer[num] = this->inChar;
00048 
00049             if (this->num > BUFFER_MAX - 1) {
00050                 this->num = 0;
00051                 return false;
00052             } else {
00053                 this->num++;
00054             }
00055         }
00056         return true;
00057     }
00058     return false;
00059 }
00060 
00061 uint8_t Protocol::parse(uint16_t* _data, bool _mod)
00062 {
00063     if (available(!_mod)) {
00064         //time = millis();
00065         //time = _timer.read_ms();
00066         time = g_cubeTimer.read_ms();
00067         do {
00068             if (this->sta) {
00069                 this->sta = false;
00070                 this->num = 0;
00071                 if (this->inChar == this->channel) {
00072                     this->error = false;
00073                     if (!_mod) {
00074                         return P_BUSY;
00075                     }
00076                 } else  {
00077                     this->error = true;
00078                     return P_ERROR;
00079                 }
00080             }
00081 
00082             if (this->inChar == 0xBB && this->inCache == 0xAA) {
00083                 this->sta = true;
00084                 if (!_mod) {
00085                     return P_BUSY;
00086                 }
00087             }
00088 
00089             if (this->num  == (CHANNEL_NUM * 2 + 1) && !this->error) {
00090                 this->inCache = this->buffer[CHANNEL_NUM * 2];
00091                 this->buffer[CHANNEL_NUM * 2] = NULL;
00092                 this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer);
00093 
00094                 this->num = 0;
00095                 if (!this->error && this->inCache == this->inChar) {
00096                     for (uint8_t a = 0; a < CHANNEL_NUM; a++) {
00097                         _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8);
00098                     }
00099                     return P_FINE;
00100                 } else {
00101                     return P_ERROR;
00102                 }
00103             } else if (!_mod) {
00104                 return P_BUSY;
00105             }
00106         } while (_mod && (available(true) && g_cubeTimer.read_ms() - time < 100));
00107 
00108         if (_mod) {
00109             return P_TIMEOUT;
00110         }
00111     } else {
00112         return P_NONE;
00113     }
00114 }