Henry Herman / Mbed 2 deprecated touchy_fg_bg

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers touchmachine.cpp Source File

touchmachine.cpp

00001 #include "touchmachine.h"
00002 #include "mbed.h"
00003 #include "com.h"
00004 TouchButton touch0(p20,p19,p18, THRESHOLD, TIMER_THRESHOLD,"0");
00005 TouchButton touch1(p17,p16,p15, THRESHOLD, TIMER_THRESHOLD,"1");
00006 Handler handler("Serial");
00007 
00008 void runser() {
00009     handler.run();
00010 }
00011 
00012 TouchMachine::TouchMachine(char *c) {
00013 #ifdef DEBUGTM
00014     com.printf("INIT TM\r\n");
00015 #endif    
00016     touchState=WAITING;
00017     t0 = &touch0;
00018     t1 = &touch1;
00019     hand = &handler;
00020     com.attach(runser);
00021     
00022 };
00023 
00024 void TouchMachine::check() {
00025 #ifdef DEBUGTM
00026     com.printf("CHECK BUTTONS\r\n");
00027 #endif    
00028     t0state = t0->checkState();
00029     t1state = t1->checkState();
00030     if (t0state && t1state) {
00031     #ifdef DEBUGTM1
00032         com.printf("ERR-CHK\r\n");
00033     #endif
00034         touchState=ERROR;
00035     }else if (t0state) {
00036     #ifdef DEBUGTM1
00037         com.printf("ZERO-CHK\r\n");
00038     #endif
00039         touchState=ZERO;
00040     } else if(t1state) {
00041     #ifdef DEBUGTM1
00042         com.printf("ONE-CHK\r\n");
00043     #endif    
00044         touchState=ONE;
00045     } else {
00046     #ifdef DEBUGTM
00047         com.printf("WAIT-CHK\r\n");
00048     #endif    
00049         touchState=WAITING;
00050     }
00051 }
00052 
00053 bool TouchMachine::isMatch() {
00054     if (touchState==MATCH) {
00055 #ifdef DEBUGTM1
00056         com.printf("MATCH-TM\r\n");
00057 #endif    
00058         return true;
00059     }
00060     return false;
00061 }
00062 
00063 bool TouchMachine::isOne() {
00064     if (touchState==ONE) {
00065 #ifdef DEBUGTM
00066         com.printf("ONE-TM\r\n");
00067 #endif    
00068         return true;
00069     }
00070     return false;
00071 }
00072 
00073 
00074 bool TouchMachine::isZero() {
00075     if (touchState==ZERO) {
00076 #ifdef DEBUGTM
00077         com.printf("ZERO-TM\r\n");
00078 #endif    
00079         return true;
00080     }
00081     return false;
00082 }
00083 
00084 bool TouchMachine::isWaiting() {
00085     if (touchState==WAITING) {
00086 #ifdef DEBUGTM
00087         com.printf("WAITING-TM\r\n");
00088 #endif    
00089         return true;
00090     }
00091     return false;
00092 }
00093 
00094 bool TouchMachine::isError() {
00095     if (touchState==ERROR) {
00096 #ifdef DEBUGTM
00097         com.printf("ERROR-TM\r\n");
00098 #endif    
00099         return true;
00100     }
00101     return false;
00102 }
00103 
00104 void TouchMachine::setError() {
00105     touchState=ERROR;
00106 }
00107 
00108 void TouchMachine::setMatch() {
00109     touchState=MATCH;
00110 }
00111 
00112 void TouchMachine::resetTouch() {
00113 #ifdef DEBUGTM1
00114     com.printf("RESET FOR TOUCH\r\n");
00115 #endif    
00116     touchState=WAITING;
00117     loadMatchStr();
00118 }
00119 
00120 char TouchMachine::getMatchChar(){
00121     if(*pmatchstr!=NULL){
00122     return *pmatchstr++;
00123     }
00124       return NULL;
00125 }
00126 
00127 char TouchMachine::nextMatchChar(){
00128     return *(pmatchstr);
00129 }
00130 
00131 void TouchMachine::loadMatchStr(){
00132     pmatchstr=hand->getMatchString();
00133 #ifdef DEBUGTM1
00134     com.printf("LOAD MATCH STR-%s\r\n",pmatchstr);
00135 #endif
00136 }
00137 
00138 void TouchMachine::touchError() {
00139     com.printf("TOUCH ERROR\r\n");
00140 }
00141 
00142 void TouchMachine::touchMatch() {
00143     com.printf("***MATCH***\r\n");
00144 }
00145 
00146 void TouchMachine::run() {    
00147     while(1) {
00148         if (hand->isReady()) {
00149             resetTouch();
00150             loadMatchStr();
00151             checkTouchForMatch();
00152         }    
00153      }
00154 }
00155 
00156 void TouchMachine::checkTouchForMatch() {
00157     char c;
00158     while(( isWaiting() || isOne() || isZero())&& hand->isReady()) {
00159             check();
00160             if(isOne() || isZero()) {
00161                 
00162                 c = getMatchChar();
00163                 #ifdef DEBUGTM1
00164                     com.printf("CHR TO MATCH %c\r\n",c);
00165                 #endif
00166                 switch(c) {
00167                 case NULL:
00168                     setMatch();
00169                     break;                    
00170                 case ZEROCHR:
00171                     if(!isZero()) 
00172                         setError();
00173                      
00174                     break;
00175                 case ONECHR:
00176                     if(!isOne()) 
00177                         setError();
00178                     
00179                 default:
00180                     break;
00181                     
00182                
00183                 }
00184             }
00185             
00186             
00187             if(isError()) {
00188                 touchError();
00189                 resetTouch();
00190             }
00191             checkMatch();
00192             if(isMatch()) {
00193                 touchMatch();
00194                 resetTouch();
00195             }
00196             waitForRelease();
00197 
00198     }
00199 }
00200 
00201 void TouchMachine::waitForRelease() {
00202     while(!isWaiting() && hand->isReady()) {
00203         check();
00204     }
00205 }
00206 
00207 void TouchMachine::checkMatch() {
00208  if(!isError() && nextMatchChar()==NULL) {
00209                setMatch();
00210   }
00211  }