Michael Ling / UI

Fork of UI by Bradley Perry

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UserInterface.cpp Source File

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 
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     pc.printf("Length: %d\r\n", length);
00079     count[1] = (int) length + 3;
00080     count[2] = pos;
00081 }
00082 
00083 /**
00084 * This function returns true if calculated checksum matches the checksum at the end of the message.
00085 * @param idx    char array containing received data, starting with start byte 0x7e
00086 * @param length    number of data bytes in the message
00087 * @author Michael Ling
00088 * @date 2/2/2015
00089 */
00090 bool UserInterface::checksum_check(char *idx, int length)
00091 {
00092     int pos = 0;
00093     int sum = 0;
00094     for(int i = 0; i < length; i++) {
00095         //In case of an escape character, the true value of the byte is the following byte XOR with 0x20
00096         if (*(idx+pos) == 0x7d) {
00097             sum += (*(idx+pos+1) ^ 0x20);
00098             pos += 2;
00099         } else {
00100             sum += *(idx+pos);
00101             pos += 1;
00102         }
00103     }
00104     //XBEE checksum: sum all data bytes, truncate the sum to the rightmost 8 bytes, and subtract that from 0xff
00105     sum = sum & 0x0ff;
00106     char calcsum = (char)(0xff - sum);
00107     char checksum;
00108     if (*(idx+pos) == 0x7d) {
00109         checksum = *(idx+pos)^0x20;
00110     } else {
00111         checksum = *(idx+pos);
00112     }
00113     if (checksum != calcsum) {
00114         return false;
00115     }
00116     return true;
00117 }
00118 
00119 
00120 /**
00121 * This function pulls a crutch message from the XBee, and changes UI if the message is valid
00122 * @author Michael Ling
00123 * @date 5/11/2015
00124 */
00125 void UserInterface::checkUI_XBee()
00126 {
00127     //int sum = 0;
00128     _buttonA_prev = _buttonA;
00129     _buttonB_prev = _buttonB;
00130     //Find the start of a message in the xbee buffer
00131     char * idx = strchr(_xbeeBuffer,0x7e);
00132     
00133     if (idx != NULL) {
00134         
00135         int size[3];
00136         find_length(idx, size);
00137         printf("Size: %d, Datacount: %d\r\n", size[1], _dataCnt);
00138         _buttonA = (_xbeeBuffer[idx-_xbeeBuffer+21]>>1) & 1;  // on DIO1
00139         _buttonB = (_xbeeBuffer[idx-_xbeeBuffer+21]>>2) & 1;  // on DIO2
00140         //Proceed if the amount of data matches up with the calculated message length
00141         if(_dataCnt >= size[1]) {
00142             //Compare calculated and actual checksums
00143             if (checksum_check(idx+size[2], size[0])) {
00144                 printf("Checksums match\r\n");
00145             } else {
00146                 printf("Checksums don't match\r\n");
00147                 UI = _prev_UI;
00148                 return;
00149             }
00150             _dataCnt = 0;
00151         } else {
00152             //UI = _prev_UI;
00153            // return;
00154         }
00155        
00156         if (_buttonA == 0 && _buttonA_prev == 1) {//buton was just pressed
00157             _time_pressA.reset();
00158             _time_pressA.start();
00159             _time_sinceA = 0;
00160         }
00161 
00162         else if(_buttonA == 0) {
00163             printf("!\r\n");
00164             _time_sinceA = _time_pressA; //button is still pressed
00165         }
00166 
00167         if (_buttonB == 0 && _buttonB_prev == 1) {//button was just pressed
00168             _time_pressB.reset();
00169             _time_pressB.start();
00170             _time_sinceB = 0;
00171         } else if(_buttonB == 0) {
00172             _time_sinceB = _time_pressB; //button is still pressed
00173         }
00174     }
00175 
00176     if((_time_pressA-_time_sinceA) >= _tRelease) { //button was released
00177         if(_time_pressA-_tRelease >= _tHold) { //if the button was held before released
00178             UI = 3; //UI command is a held A button
00179         } else {
00180             UI = 1; //UI command is a pressed A button
00181         }
00182         _buttonA = 1; //button A is released
00183         _time_pressA.stop(); //reset the button A timer
00184         _time_pressA.reset();
00185     }
00186     if(_time_pressB-_time_sinceB >= _tRelease) { //button was released
00187         if(_time_pressB-_tRelease >= _tHold) { //if the button was held before released
00188             UI = 4; //UI command is a held B button
00189         } else {
00190             UI = 2; //UI command is a pressed B button
00191         }
00192         _buttonB = 1; //button B is released
00193         _time_pressB.stop(); //reset the button B timer
00194         _time_pressB.reset();
00195     }
00196 
00197     memset(_xbeeBuffer,0xFF,250);
00198     _dataCnt = 0;
00199     _prev_UI = UI;
00200 }
00201 
00202 void UserInterface::initializeUI()
00203 {
00204     xbeeUI.baud(115200);
00205     mainPower = 0;
00206 
00207 }