|
41 | 41 |
|
42 | 42 | void begin(uint32_t baud) { |
43 | 43 | this->stream.begin(baud); |
| 44 | + buf_offset=0; |
| 45 | + _data_start=false; |
44 | 46 | } |
45 | 47 | void begin() { |
46 | 48 | this->stream.begin(115200); |
| 49 | + buf_offset=0; |
| 50 | + _data_start=false; |
47 | 51 | } |
48 | 52 | bool read(float *buf,int num_items){ |
49 | | - uint8_t buf_cnt=0; |
| 53 | + |
| 54 | + |
50 | 55 | char *addr; |
51 | | - bool result=false; |
| 56 | + char c; |
| 57 | + bool valid_data=false; |
52 | 58 | this->stream.RxModePortSet(RxMode_IMU_PORT); |
53 | | - delay(3); |
54 | | - int rbytes=this->stream.available(); |
55 | | - memset(_imu_data, NULL, sizeof(_imu_data)); |
56 | | - if(rbytes>62){ // Some serial datas conficted, select only IMU data with size |
57 | | - for(int n=0;n<rbytes;n++){ |
58 | | - this->stream.read((_imu_data+buf_cnt),1); |
59 | | - if(_imu_data[buf_cnt]==0x0a){ //end of string |
| 59 | + |
| 60 | + int16_t rbytes=this->stream.available(); |
| 61 | + while(rbytes--){ |
| 62 | + this->stream.read((_imu_data+buf_offset),1); |
| 63 | + if(_imu_data[buf_offset]=='\n'){ //LF end of string |
| 64 | + if (_data_start){ |
60 | 65 | addr=strtok(_imu_data,","); |
61 | 66 | for(int i=0;i<num_items;i++){ |
62 | 67 | buf[i]=atof(addr); |
63 | 68 | addr=strtok(NULL,","); |
64 | 69 | } |
65 | | - result= true; |
66 | | - }else if(_imu_data[buf_cnt]=='*'){ |
67 | | - buf_cnt=-1; |
| 70 | + _data_start=false; |
| 71 | + valid_data=true; |
68 | 72 | } |
69 | | - buf_cnt++; |
70 | | - if(buf_cnt>=IMU_DATA_SIZE)buf_cnt=0; |
| 73 | + }else if(_imu_data[buf_offset]=='*'){ //start of string |
| 74 | + buf_offset=-1; |
| 75 | + _data_start=true; |
71 | 76 | } |
72 | | - //Serial.print("IMU_buff "); |
73 | | - //Serial.println(_imu_data); |
| 77 | + // ordinary characters |
| 78 | + buf_offset++; |
| 79 | + if(buf_offset>=IMU_DATA_SIZE)buf_offset=0; |
74 | 80 | } |
75 | 81 |
|
76 | | - return result; |
| 82 | + |
| 83 | + return valid_data; |
| 84 | + |
77 | 85 | } |
78 | 86 |
|
| 87 | + |
| 88 | + void rx_empty(void) |
| 89 | + { |
| 90 | + char a; |
| 91 | + while(this->stream.available() > 0) { |
| 92 | + this->stream.read(&a,1); |
| 93 | + } |
| 94 | + } |
79 | 95 | int connected() { return this->conn && this->stream; } |
80 | 96 | int available() { return this->stream.available(); } |
81 | 97 | private: |
|
98 | 114 |
|
99 | 115 | char _imu_data[IMU_DATA_SIZE]; |
100 | 116 | size_t _buff_size=IMU_DATA_SIZE; |
| 117 | + uint8_t buf_offset; |
| 118 | + bool _data_start; |
101 | 119 | }; |
102 | 120 |
|
103 | 121 |
|
|
0 commit comments