Code to process UI input messages and verify them with checksum.
Embed:
(wiki syntax)
Show/hide line numbers
UserInterface.cpp
00001 #include "mbed.h" 00002 #include "MODSERIAL.h" 00003 #include "initDatabed.h" 00004 #include "UserInterface.h" 00005 00006 // UI button variables 00007 //int buttonA = 1; // state of remote button 00008 //int buttonA_prev = 1; 00009 00010 // push hold vars 00011 //float tHold = .6; // hold time for sit/stand 00012 //float tIdle = 2; // time out to confirm sit or stance (s) 00013 //int SSconfirm = 0; // sit/stand variable for confirmation process 00014 //float tRelease = .3; // time since last transmission to determine button release 00015 00016 00017 // various timers 00018 //Timer time_StateChange; 00019 //Timer time_pressA; 00020 //Timer time_pressB; 00021 00022 //int buttonB = 1; // state of remote button 00023 //int buttonB_prev = 1; 00024 //char __xbeeBuffer[250]; 00025 //int _dataCnt=0; 00026 int UI = 0; 00027 00028 //float _time_sinceA, _time_sinceB; //time since the button was first pressed 00029 00030 UserInterface::UserInterface(void): _buttonA(1), _buttonA_prev(1), _tHold(.6), _tIdle(2), _SSconfirm(0), _tRelease(.3), _buttonB(1), _buttonB_prev(1), _dataCnt(0) { 00031 } 00032 00033 void UserInterface::readBuffer() 00034 { 00035 while (xbeeUI.readable() && _dataCnt<250) { 00036 _xbeeBuffer[_dataCnt] = xbeeUI.getc(); 00037 _dataCnt++; 00038 } 00039 } 00040 00041 /** 00042 * This function returns an array of information about the received characters. count[0] is # of data bytes, count[1] is expected length of the message, given the data length bytes and presence 00043 * of escape characters. count[2] is position of the first data byte. 00044 * @param idx char array containing received data, beginning with the start byte 0x7e 00045 * @param count array that will contain the returned values 00046 * @author Michael Ling 00047 * @date 2/2/2015 00048 */ 00049 void UserInterface::find_length(char *idx, int *count) 00050 { 00051 int pos = 0; 00052 short length; 00053 //This segment reads the length bytes (bytes 2-3 of the message) 00054 if (*(idx+1) == 0x7d) { 00055 length = (*(idx+2) ^ 0x20) << 8; 00056 pos = 3; 00057 } else { 00058 length = *(idx+1) << 8; 00059 pos = 2; 00060 } 00061 if (*(idx+pos) == 0x7d) { 00062 length = length | (*(idx+pos+1)^0x20); 00063 pos += 2; 00064 } else { 00065 length = length | *(idx+pos); 00066 pos += 1; 00067 } 00068 count[0] = (int)length; 00069 //Length incremented by 1--we treat the checksum as a data byte, since it can also be escaped 00070 length += 1; 00071 //Checks for escape characters--for every escape char found, the data section gets 1 byte longer 00072 for (short i = 0; i < length; i += 1) { 00073 if (*(idx+i) == 0x7d) { 00074 length += 1; 00075 } 00076 } 00077 count[1] = (int) length + 3; 00078 count[2] = pos; 00079 } 00080 00081 /** 00082 * This function returns true if calculated checksum matches the checksum at the end of the message. 00083 * @param idx char array containing received data, starting with start byte 0x7e 00084 * @param length number of data bytes in the message 00085 * @author Michael Ling 00086 * @date 2/2/2015 00087 */ 00088 bool UserInterface::checksum_check(char *idx, int length) 00089 { 00090 int pos = 0; 00091 int sum = 0; 00092 for(int i = 0; i < length; i++) { 00093 //In case of an escape character, the true value of the byte is the following byte XOR with 0x20 00094 if (*(idx+pos) == 0x7d) { 00095 sum += (*(idx+pos+1) ^ 0x20); 00096 pos += 2; 00097 } else { 00098 sum += *(idx+pos); 00099 pos += 1; 00100 } 00101 } 00102 //XBEE checksum: sum all data bytes, truncate the sum to the rightmost 8 bytes, and subtract that from 0xff 00103 sum = sum & 0x0ff; 00104 char calcsum = (char)(0xff - sum); 00105 char checksum; 00106 if (*(idx+pos) == 0x7d) { 00107 checksum = *(idx+pos)^0x20; 00108 } else { 00109 checksum = *(idx+pos); 00110 } 00111 if (checksum != calcsum) { 00112 return false; 00113 } 00114 return true; 00115 } 00116 00117 void UserInterface::checkUI_XBee() 00118 { 00119 int sum = 0; 00120 _buttonA_prev = _buttonA; 00121 _buttonB_prev = _buttonB; 00122 char * idx = strchr(_xbeeBuffer,0x7e); 00123 if (idx != NULL) { 00124 int size[3]; 00125 find_length(idx, size); 00126 printf("Size: %d, Datacount: %d\r\n", size[1], _dataCnt); 00127 if(_dataCnt >= size[1]) { 00128 00129 _buttonA = (_xbeeBuffer[idx-_xbeeBuffer+21]>>1) & 1; // on DIO1 00130 _buttonB = (_xbeeBuffer[idx-_xbeeBuffer+21]>>2) & 1; // on DIO2 00131 00132 if (checksum_check(idx+size[2], size[0])) { 00133 printf("Checksums match\r\n"); 00134 } else { 00135 printf("Checksums don't match\r\n"); 00136 } 00137 _dataCnt = 0; 00138 } 00139 if(sum == 0x79c) { 00140 if (_buttonA == 0 && _buttonA_prev == 1) {//buton was just pressed 00141 _time_pressA.reset(); 00142 _time_pressA.start(); 00143 _time_sinceA = 0; 00144 } 00145 00146 else if(_buttonA == 0) { 00147 _time_sinceA = _time_pressA; //button is still pressed 00148 } 00149 00150 if (_buttonB == 0 && _buttonB_prev == 1) {//button was just pressed 00151 _time_pressB.reset(); 00152 _time_pressB.start(); 00153 _time_sinceB = 0; 00154 } else if(_buttonB == 0) { 00155 _time_sinceB = _time_pressB; //button is still pressed 00156 } 00157 } 00158 00159 if((_time_pressA-_time_sinceA) >= _tRelease) { //button was released 00160 if(_time_pressA-_tRelease >= _tHold) { //if the button was held before released 00161 UI = 3; //UI command is a held A button 00162 } else { 00163 UI = 1; //UI command is a pressed A button 00164 } 00165 _buttonA = 1; //button A is released 00166 _time_pressA.stop(); //reset the button A timer 00167 _time_pressA.reset(); 00168 } 00169 if(_time_pressB-_time_sinceB >= _tRelease) { //button was released 00170 if(_time_pressB-_tRelease >= _tHold) { //if the button was held before released 00171 UI = 4; //UI command is a held B button 00172 } else { 00173 UI = 2; //UI command is a pressed B button 00174 } 00175 _buttonB = 1; //button B is released 00176 _time_pressB.stop(); //reset the button B timer 00177 _time_pressB.reset(); 00178 } 00179 } 00180 memset(_xbeeBuffer,0xFF,250); 00181 _dataCnt = 0; 00182 } 00183 00184 /*void checkUI_XBee() 00185 { 00186 00187 _buttonA_prev = _buttonA; 00188 buttonB_prev = buttonB; 00189 while (xbeeUI.readable() && _dataCnt<250) { 00190 __xbeeBuffer[_dataCnt] = xbeeUI.getc(); 00191 _dataCnt++; 00192 if (__xbeeBuffer[_dataCnt]==0x7e) { 00193 for(int i=0; i<22; i++) { 00194 if(xbeeUI.readable() { 00195 __xbeeBuffer[_dataCnt] = xbeeUI.getc(); 00196 _dataCnt++; 00197 } 00198 } 00199 } 00200 char * idx=strchr(__xbeeBuffer,0x7e); 00201 _buttonA = (_xbeeBuffer[idx-_xbeeBuffer+21]>>1) & 1; // on DIO1 00202 buttonB = (_xbeeBuffer[idx-_xbeeBuffer+21]>>2) & 1; // on DIO2 00203 pc.printf("%x\r\n", *(idx+2)); 00204 _dataCnt=0; 00205 if (buttonA == 0 && buttonA_prev==1) {//buton was just pressed 00206 time_pressA.reset(); 00207 time_pressA.start(); 00208 _time_sinceA=0; 00209 } 00210 00211 else if(buttonA==0) { 00212 _time_sinceA=time_pressA; //button is still pressed 00213 } 00214 00215 if (buttonB == 0 && buttonB_prev==1) {//button was just pressed 00216 time_pressB.reset(); 00217 time_pressB.start(); 00218 _time_sinceB=0; 00219 } else if(buttonB==0) { 00220 _time_sinceB=time_pressB; //button is still pressed 00221 } 00222 } 00223 if((time_pressA-_time_sinceA)>=tRelease) { //button was released 00224 if(time_pressA-tRelease>=tHold) { //if the button was held before released 00225 UI=3; //UI command is a held A button 00226 } else { 00227 UI=1; //UI command is a pressed A button 00228 } 00229 buttonA=1; //button A is released 00230 time_pressA.stop(); //reset the button A timer 00231 time_pressA.reset(); 00232 } 00233 if(time_pressB-_time_sinceB>=tRelease) { //button was released 00234 if(time_pressB-tRelease>=tHold) { //if the button was held before released 00235 UI=4; //UI command is a held B button 00236 } else { 00237 UI=2; //UI command is a pressed B button 00238 } 00239 buttonB=1; //button B is released 00240 time_pressB.stop(); //reset the button B timer 00241 time_pressB.reset(); 00242 } 00243 00244 _dataCnt=0; 00245 memset(__xbeeBuffer,0xF,250); 00246 }*/ 00247 00248 void UserInterface::initializeUI() 00249 { 00250 xbeeUI.baud(115200); 00251 mainPower = 0; 00252 00253 }
Generated on Thu Jul 14 2022 01:55:52 by
1.7.2