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