IDD hw2

Dependencies:   DebounceIn QEI mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "QEI.h"
00002 #include "mbed.h"
00003 #include "DebounceIn.h"
00004  
00005 Serial pc(USBTX, USBRX);
00006 //Use X4 encoding.
00007 //QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
00008 //Use X2 encoding by default.
00009 
00010 DebounceIn RButton(D6);
00011 DebounceIn LButton(D5);
00012 
00013 DigitalOut cLight1(D7);
00014 DigitalOut cLight2(D8);
00015 DigitalOut cLight3(D9);
00016 DigitalOut cLight4(D10);
00017 DigitalOut cLight5(D11);
00018 DigitalOut cLight6(D12);
00019 DigitalOut cLight7(D13);
00020 DigitalOut cLight8(D14);
00021 DigitalOut cLight9(D15);
00022 
00023 DigitalOut rLight1(A0);
00024 DigitalOut rLight2(A1);
00025 DigitalOut rLight3(A2);
00026 DigitalOut rLight4(A3);
00027 
00028  QEI xWheel (D0, D2, NC, 624);
00029  QEI yWheel (D3, D4, NC, 624);
00030  
00031  int yPos;
00032  int RowNum;
00033  int RowEps = 3;
00034  bool Rpress; 
00035   
00036  int xPos;
00037  int ColNum = 5;
00038  int ColEps = 3;
00039  bool Lpress;
00040  
00041  char *letter;
00042  
00043  
00044 int main() {
00045  
00046     RButton.mode(PullUp); 
00047     LButton.mode(PullUp);
00048  
00049     while(1){
00050         yPos = yWheel.getPulses();
00051         xPos = xWheel.getPulses();
00052         
00053         
00054         Rpress = !RButton.read();
00055         Lpress = !LButton.read();
00056         
00057         //Get Column position
00058         if(yPos < 2*RowEps){
00059             RowNum = 1;
00060             }
00061         if(yPos > 2*RowEps && yPos < 4*RowEps){
00062             RowNum = 2;
00063             }
00064         if(yPos > 4*RowEps && yPos < 6*RowEps){
00065             RowNum = 3;
00066             }
00067         if(yPos > 6*RowEps){
00068             RowNum = 4;
00069             }
00070             
00071             
00072         //Get Column position
00073         
00074         if(xPos < -7*ColEps){
00075             ColNum = 1;
00076             }
00077         if(xPos > -7*ColEps && xPos < -5*ColEps){
00078             ColNum = 2;
00079             }
00080         if(xPos > -5*ColEps && xPos < -3*ColEps){
00081             ColNum = 3;
00082             }
00083         if(xPos > -3*ColEps && xPos < -ColEps){
00084             ColNum = 4;
00085             }
00086         if(xPos > -ColEps && xPos < ColEps){
00087             ColNum = 5;
00088             }    
00089         if(xPos > ColEps && xPos < 3*ColEps){
00090             ColNum = 6;
00091             }
00092         if(xPos > 3*ColEps && xPos < 5*ColEps){
00093             ColNum = 7;
00094             }    
00095         if(xPos > 5*ColEps && xPos < 7*ColEps){
00096             ColNum = 8;
00097             }    
00098         if(xPos > 7*ColEps){
00099             ColNum = 9;
00100             }
00101         
00102         
00103         
00104         
00105         //Set row LEDs
00106         
00107         if(RowNum ==1){
00108             rLight1 = 1;
00109             rLight2 = 0;
00110             rLight3 = 0;
00111             rLight4 = 0;      
00112             }
00113         
00114         if(RowNum ==2){
00115             rLight1 = 0;
00116             rLight2 = 1;
00117             rLight3 = 0;
00118             rLight4 = 0;      
00119             }
00120         
00121         if(RowNum ==3){
00122             rLight1 = 0;
00123             rLight2 = 0;
00124             rLight3 = 1;
00125             rLight4 = 0;      
00126             }
00127         
00128         if(RowNum ==4){
00129             rLight1 = 0;
00130             rLight2 = 0;
00131             rLight3 = 0;
00132             rLight4 = 1;      
00133             }
00134         //set column leds
00135         
00136         if(ColNum ==1){
00137             cLight1 = 1;
00138             cLight2 = 0;
00139             cLight3 = 0;
00140             cLight4 = 0;
00141             cLight5 = 0;
00142             cLight6 = 0;
00143             cLight7 = 0;
00144             }
00145         
00146         if(ColNum ==2){
00147             cLight1 = 0;
00148             cLight2 = 1;
00149             cLight3 = 0;
00150             cLight4 = 0;
00151             cLight5 = 0;
00152             cLight6 = 0;
00153             cLight7 = 0;
00154             cLight8 = 0;
00155             cLight9 = 0;
00156             }  
00157               
00158         if(ColNum ==3){
00159             cLight1 = 0;
00160             cLight2 = 0;
00161             cLight3 = 1;
00162             cLight4 = 0;
00163             cLight5 = 0;
00164             cLight6 = 0;
00165             cLight7 = 0;
00166             cLight8 = 0;
00167             cLight9 = 0;
00168             }
00169             
00170         if(ColNum ==4){
00171             cLight1 = 0;
00172             cLight2 = 0;
00173             cLight3 = 0;
00174             cLight4 = 1;
00175             cLight5 = 0;
00176             cLight6 = 0;
00177             cLight7 = 0;
00178             cLight8 = 0;
00179             cLight9 = 0;
00180             }
00181             
00182         if(ColNum ==5){
00183             cLight1 = 0;
00184             cLight2 = 0;
00185             cLight3 = 0;
00186             cLight4 = 0;
00187             cLight5 = 1;
00188             cLight6 = 0;
00189             cLight7 = 0;
00190             cLight8 = 0;
00191             cLight9 = 0;
00192             
00193             }
00194             
00195         if(ColNum ==6){
00196             cLight1 = 0;
00197             cLight2 = 0;
00198             cLight3 = 0;
00199             cLight4 = 0;
00200             cLight5 = 0;
00201             cLight6 = 1;
00202             cLight7 = 0;
00203             cLight8 = 0;
00204             cLight9 = 0;
00205             
00206             }
00207             
00208         if(ColNum ==7){
00209             cLight1 = 0;
00210             cLight2 = 0;
00211             cLight3 = 0;
00212             cLight4 = 0;
00213             cLight5 = 0;
00214             cLight6 = 0;
00215             cLight7 = 1;
00216             cLight8 = 0;
00217             cLight9 = 0;
00218                         
00219             } 
00220             
00221         if(ColNum ==8){
00222             cLight1 = 0;
00223             cLight2 = 0;
00224             cLight3 = 0;
00225             cLight4 = 0;
00226             cLight5 = 0;
00227             cLight6 = 0;
00228             cLight7 = 0;
00229             cLight8 = 1;
00230             cLight9 = 0;
00231                         
00232             } 
00233             
00234         if(ColNum ==9){
00235             cLight1 = 0;
00236             cLight2 = 0;
00237             cLight3 = 0;
00238             cLight4 = 0;
00239             cLight5 = 0;
00240             cLight6 = 0;
00241             cLight7 = 0;
00242             cLight8 = 0;
00243             cLight9 = 1;
00244             
00245             } 
00246                 
00247                 
00248         // Set letter:
00249         
00250         
00251     if(LButton){
00252         if(RowNum == 1){
00253             if(ColNum == 1){
00254                 letter = "q";
00255                 }
00256             if(ColNum == 2){
00257                 letter = "z";
00258                 }
00259             if(ColNum == 3){
00260                 letter = "x";
00261                 }
00262             if(ColNum == 4){
00263                 letter = "c";
00264                 }
00265             if(ColNum == 5){
00266                 letter = "v";
00267                 }
00268             if(ColNum == 6){
00269                 letter = "b";
00270                 }
00271             if(ColNum == 7){
00272                 letter = "n";
00273                 }
00274             if(ColNum == 8){
00275                 letter = "m";
00276                 }
00277             if(ColNum == 9){
00278                 letter = " ";
00279                 }
00280             }
00281         
00282         if(RowNum == 2){
00283             if(ColNum == 1){
00284                 letter = "a";
00285                 }
00286             if(ColNum == 2){
00287                 letter = "s";
00288                 }
00289             if(ColNum == 3){
00290                 letter = "d";
00291                 }
00292             if(ColNum == 4){
00293                 letter = "f";
00294                 }
00295             if(ColNum == 5){
00296                 letter = "g";
00297                 }
00298             if(ColNum == 6){
00299                 letter = "h";
00300                 }
00301             if(ColNum == 7){
00302                 letter = "j";
00303                 }
00304             if(ColNum == 8){
00305                 letter = "k";
00306                 }
00307             if(ColNum == 9){
00308                 letter = "l";
00309                 }
00310             }
00311         
00312         
00313         if(RowNum == 3){
00314             if(ColNum == 1){
00315                 letter = "w";
00316                 }
00317             if(ColNum == 2){
00318                 letter = "e";
00319                 }
00320             if(ColNum == 3){
00321                 letter = "r";
00322                 }
00323             if(ColNum == 4){
00324                 letter = "t";
00325                 }
00326             if(ColNum == 5){
00327                 letter = "y";
00328                 }
00329             if(ColNum == 6){
00330                 letter = "u";
00331                 }
00332             if(ColNum == 7){
00333                 letter = "i";
00334                 }
00335             if(ColNum == 8){
00336                 letter = "o";
00337                 }
00338             if(ColNum == 9){
00339                 letter = "p";
00340                 }
00341             }
00342         
00343         if(RowNum == 4){
00344             if(ColNum == 1){
00345                 letter = "1";
00346                 }
00347             if(ColNum == 2){
00348                 letter = "2";
00349                 }
00350             if(ColNum == 3){
00351                 letter = "3";
00352                 }
00353             if(ColNum == 4){
00354                 letter = "!";
00355                 }
00356             if(ColNum == 5){
00357                 letter = "?";
00358                 }
00359             if(ColNum == 6){
00360                 letter = ".";
00361                 }
00362             if(ColNum == 7){
00363                 letter = "+";
00364                 }
00365             if(ColNum == 8){
00366                 letter = "-";
00367                 }
00368             if(ColNum == 9){
00369                 letter = "=";
00370                 }
00371             }
00372         }
00373         
00374         
00375     if(!LButton){
00376         if(RowNum == 1){
00377             if(ColNum == 1){
00378                 letter = "Q";
00379                 }
00380             if(ColNum == 2){
00381                 letter = "Z";
00382                 }
00383             if(ColNum == 3){
00384                 letter = "X";
00385                 }
00386             if(ColNum == 4){
00387                 letter = "C";
00388                 }
00389             if(ColNum == 5){
00390                 letter = "V";
00391                 }
00392             if(ColNum == 6){
00393                 letter = "B";
00394                 }
00395             if(ColNum == 7){
00396                 letter = "N";
00397                 }
00398             if(ColNum == 8){
00399                 letter = "M";
00400                 }
00401             if(ColNum == 9){
00402                 letter = " ";
00403                 }
00404             }
00405         
00406         if(RowNum == 2){
00407             if(ColNum == 1){
00408                 letter = "A";
00409                 }
00410             if(ColNum == 2){
00411                 letter = "S";
00412                 }
00413             if(ColNum == 3){
00414                 letter = "D";
00415                 }
00416             if(ColNum == 4){
00417                 letter = "F";
00418                 }
00419             if(ColNum == 5){
00420                 letter = "G";
00421                 }
00422             if(ColNum == 6){
00423                 letter = "H";
00424                 }
00425             if(ColNum == 7){
00426                 letter = "J";
00427                 }
00428             if(ColNum == 8){
00429                 letter = "K";
00430                 }
00431             if(ColNum == 9){
00432                 letter = "L";
00433                 }
00434             }
00435         
00436         
00437         if(RowNum == 3){
00438             if(ColNum == 1){
00439                 letter = "W";
00440                 }
00441             if(ColNum == 2){
00442                 letter = "E";
00443                 }
00444             if(ColNum == 3){
00445                 letter = "R";
00446                 }
00447             if(ColNum == 4){
00448                 letter = "T";
00449                 }
00450             if(ColNum == 5){
00451                 letter = "Y";
00452                 }
00453             if(ColNum == 6){
00454                 letter = "U";
00455                 }
00456             if(ColNum == 7){
00457                 letter = "I";
00458                 }
00459             if(ColNum == 8){
00460                 letter = "C";
00461                 }
00462             if(ColNum == 9){
00463                 letter = "P";
00464                 }
00465             }
00466         
00467         if(RowNum == 4){
00468             if(ColNum == 1){
00469                 letter = "1";
00470                 }
00471             if(ColNum == 2){
00472                 letter = "2";
00473                 }
00474             if(ColNum == 3){
00475                 letter = "3";
00476                 }
00477             if(ColNum == 4){
00478                 letter = "!";
00479                 }
00480             if(ColNum == 5){
00481                 letter = "?";
00482                 }
00483             if(ColNum == 6){
00484                 letter = ".";
00485                 }
00486             if(ColNum == 7){
00487                 letter = "+";
00488                 }
00489             if(ColNum == 8){
00490                 letter = "-";
00491                 }
00492             if(ColNum == 9){
00493                 letter = "=";
00494                 }
00495             }
00496         }
00497         
00498                 
00499         if(!RButton){
00500             pc.printf("%s",letter);
00501             wait(0.3);
00502             }
00503                 
00504         wait(0.001);
00505         }
00506  
00507 }