IDD hw2

Dependencies:   DebounceIn QEI mbed

Fork of idd_hw2_dpcinci_etchatext by Dominic Cincione

main.cpp

Committer:
dpcinci
Date:
2014-09-21
Revision:
0:4005151fa9b1

File content as of revision 0:4005151fa9b1:

#include "QEI.h"
#include "mbed.h"
#include "DebounceIn.h"
 
Serial pc(USBTX, USBRX);
//Use X4 encoding.
//QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
//Use X2 encoding by default.

DebounceIn RButton(D6);
DebounceIn LButton(D5);

DigitalOut cLight1(D7);
DigitalOut cLight2(D8);
DigitalOut cLight3(D9);
DigitalOut cLight4(D10);
DigitalOut cLight5(D11);
DigitalOut cLight6(D12);
DigitalOut cLight7(D13);
DigitalOut cLight8(D14);
DigitalOut cLight9(D15);

DigitalOut rLight1(A0);
DigitalOut rLight2(A1);
DigitalOut rLight3(A2);
DigitalOut rLight4(A3);

 QEI xWheel (D0, D2, NC, 624);
 QEI yWheel (D3, D4, NC, 624);
 
 int yPos;
 int RowNum;
 int RowEps = 3;
 bool Rpress; 
  
 int xPos;
 int ColNum = 5;
 int ColEps = 3;
 bool Lpress;
 
 char *letter;
 
 
int main() {
 
    RButton.mode(PullUp); 
    LButton.mode(PullUp);
 
    while(1){
        yPos = yWheel.getPulses();
        xPos = xWheel.getPulses();
        
        
        Rpress = !RButton.read();
        Lpress = !LButton.read();
        
        //Get Column position
        if(yPos < 2*RowEps){
            RowNum = 1;
            }
        if(yPos > 2*RowEps && yPos < 4*RowEps){
            RowNum = 2;
            }
        if(yPos > 4*RowEps && yPos < 6*RowEps){
            RowNum = 3;
            }
        if(yPos > 6*RowEps){
            RowNum = 4;
            }
            
            
        //Get Column position
        
        if(xPos < -7*ColEps){
            ColNum = 1;
            }
        if(xPos > -7*ColEps && xPos < -5*ColEps){
            ColNum = 2;
            }
        if(xPos > -5*ColEps && xPos < -3*ColEps){
            ColNum = 3;
            }
        if(xPos > -3*ColEps && xPos < -ColEps){
            ColNum = 4;
            }
        if(xPos > -ColEps && xPos < ColEps){
            ColNum = 5;
            }    
        if(xPos > ColEps && xPos < 3*ColEps){
            ColNum = 6;
            }
        if(xPos > 3*ColEps && xPos < 5*ColEps){
            ColNum = 7;
            }    
        if(xPos > 5*ColEps && xPos < 7*ColEps){
            ColNum = 8;
            }    
        if(xPos > 7*ColEps){
            ColNum = 9;
            }
        
        
        
        
        //Set row LEDs
        
        if(RowNum ==1){
            rLight1 = 1;
            rLight2 = 0;
            rLight3 = 0;
            rLight4 = 0;      
            }
        
        if(RowNum ==2){
            rLight1 = 0;
            rLight2 = 1;
            rLight3 = 0;
            rLight4 = 0;      
            }
        
        if(RowNum ==3){
            rLight1 = 0;
            rLight2 = 0;
            rLight3 = 1;
            rLight4 = 0;      
            }
        
        if(RowNum ==4){
            rLight1 = 0;
            rLight2 = 0;
            rLight3 = 0;
            rLight4 = 1;      
            }
        //set column leds
        
        if(ColNum ==1){
            cLight1 = 1;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 0;
            }
        
        if(ColNum ==2){
            cLight1 = 0;
            cLight2 = 1;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 0;
            cLight8 = 0;
            cLight9 = 0;
            }  
              
        if(ColNum ==3){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 1;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 0;
            cLight8 = 0;
            cLight9 = 0;
            }
            
        if(ColNum ==4){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 1;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 0;
            cLight8 = 0;
            cLight9 = 0;
            }
            
        if(ColNum ==5){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 1;
            cLight6 = 0;
            cLight7 = 0;
            cLight8 = 0;
            cLight9 = 0;
            
            }
            
        if(ColNum ==6){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 1;
            cLight7 = 0;
            cLight8 = 0;
            cLight9 = 0;
            
            }
            
        if(ColNum ==7){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 1;
            cLight8 = 0;
            cLight9 = 0;
                        
            } 
            
        if(ColNum ==8){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 0;
            cLight8 = 1;
            cLight9 = 0;
                        
            } 
            
        if(ColNum ==9){
            cLight1 = 0;
            cLight2 = 0;
            cLight3 = 0;
            cLight4 = 0;
            cLight5 = 0;
            cLight6 = 0;
            cLight7 = 0;
            cLight8 = 0;
            cLight9 = 1;
            
            } 
                
                
        // Set letter:
        
        
    if(LButton){
        if(RowNum == 1){
            if(ColNum == 1){
                letter = "q";
                }
            if(ColNum == 2){
                letter = "z";
                }
            if(ColNum == 3){
                letter = "x";
                }
            if(ColNum == 4){
                letter = "c";
                }
            if(ColNum == 5){
                letter = "v";
                }
            if(ColNum == 6){
                letter = "b";
                }
            if(ColNum == 7){
                letter = "n";
                }
            if(ColNum == 8){
                letter = "m";
                }
            if(ColNum == 9){
                letter = " ";
                }
            }
        
        if(RowNum == 2){
            if(ColNum == 1){
                letter = "a";
                }
            if(ColNum == 2){
                letter = "s";
                }
            if(ColNum == 3){
                letter = "d";
                }
            if(ColNum == 4){
                letter = "f";
                }
            if(ColNum == 5){
                letter = "g";
                }
            if(ColNum == 6){
                letter = "h";
                }
            if(ColNum == 7){
                letter = "j";
                }
            if(ColNum == 8){
                letter = "k";
                }
            if(ColNum == 9){
                letter = "l";
                }
            }
        
        
        if(RowNum == 3){
            if(ColNum == 1){
                letter = "w";
                }
            if(ColNum == 2){
                letter = "e";
                }
            if(ColNum == 3){
                letter = "r";
                }
            if(ColNum == 4){
                letter = "t";
                }
            if(ColNum == 5){
                letter = "y";
                }
            if(ColNum == 6){
                letter = "u";
                }
            if(ColNum == 7){
                letter = "i";
                }
            if(ColNum == 8){
                letter = "o";
                }
            if(ColNum == 9){
                letter = "p";
                }
            }
        
        if(RowNum == 4){
            if(ColNum == 1){
                letter = "1";
                }
            if(ColNum == 2){
                letter = "2";
                }
            if(ColNum == 3){
                letter = "3";
                }
            if(ColNum == 4){
                letter = "!";
                }
            if(ColNum == 5){
                letter = "?";
                }
            if(ColNum == 6){
                letter = ".";
                }
            if(ColNum == 7){
                letter = "+";
                }
            if(ColNum == 8){
                letter = "-";
                }
            if(ColNum == 9){
                letter = "=";
                }
            }
        }
        
        
    if(!LButton){
        if(RowNum == 1){
            if(ColNum == 1){
                letter = "Q";
                }
            if(ColNum == 2){
                letter = "Z";
                }
            if(ColNum == 3){
                letter = "X";
                }
            if(ColNum == 4){
                letter = "C";
                }
            if(ColNum == 5){
                letter = "V";
                }
            if(ColNum == 6){
                letter = "B";
                }
            if(ColNum == 7){
                letter = "N";
                }
            if(ColNum == 8){
                letter = "M";
                }
            if(ColNum == 9){
                letter = " ";
                }
            }
        
        if(RowNum == 2){
            if(ColNum == 1){
                letter = "A";
                }
            if(ColNum == 2){
                letter = "S";
                }
            if(ColNum == 3){
                letter = "D";
                }
            if(ColNum == 4){
                letter = "F";
                }
            if(ColNum == 5){
                letter = "G";
                }
            if(ColNum == 6){
                letter = "H";
                }
            if(ColNum == 7){
                letter = "J";
                }
            if(ColNum == 8){
                letter = "K";
                }
            if(ColNum == 9){
                letter = "L";
                }
            }
        
        
        if(RowNum == 3){
            if(ColNum == 1){
                letter = "W";
                }
            if(ColNum == 2){
                letter = "E";
                }
            if(ColNum == 3){
                letter = "R";
                }
            if(ColNum == 4){
                letter = "T";
                }
            if(ColNum == 5){
                letter = "Y";
                }
            if(ColNum == 6){
                letter = "U";
                }
            if(ColNum == 7){
                letter = "I";
                }
            if(ColNum == 8){
                letter = "C";
                }
            if(ColNum == 9){
                letter = "P";
                }
            }
        
        if(RowNum == 4){
            if(ColNum == 1){
                letter = "1";
                }
            if(ColNum == 2){
                letter = "2";
                }
            if(ColNum == 3){
                letter = "3";
                }
            if(ColNum == 4){
                letter = "!";
                }
            if(ColNum == 5){
                letter = "?";
                }
            if(ColNum == 6){
                letter = ".";
                }
            if(ColNum == 7){
                letter = "+";
                }
            if(ColNum == 8){
                letter = "-";
                }
            if(ColNum == 9){
                letter = "=";
                }
            }
        }
        
                
        if(!RButton){
            pc.printf("%s",letter);
            wait(0.3);
            }
                
        wait(0.001);
        }
 
}