Filip Maksimovic HW2 repository

Dependencies:   MMA8451Q TSI TextLCD USBDevice mbed tsi_sensor

main.cpp

Committer:
fil
Date:
2014-09-21
Revision:
1:e6873017e1d2
Parent:
0:6ee2b447a1c5

File content as of revision 1:e6873017e1d2:

#include "mbed.h"
#include "MMA8451Q.h"
#include "TSISensor.h"
#include "USBKeyboard.h"
#include "TextLCD.h"
#define MMA8451_I2C_ADDRESS (0x1d<<1)
 
int main(void) {
    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
    TSISensor tsi;
    PwmOut rled(LED_RED);
    PwmOut gled(LED_GREEN);
    PwmOut bled(LED_BLUE);
    Serial pc(USBTX, USBRX);
    USBKeyboard keyboard;
    TextLCD lcd(PTD0,PTD5,PTA12,PTD4,PTA2,PTA1,TextLCD::LCD20x4);
    
    //lcd.printf("Hello World!\n");
    
    rled = 1.0; bled = 0.0; gled = 1.0;
     
    // Begin the big fat fatty loop
    while (1) {
        
        gled = 1.0; bled = 0.0; rled = 1.0;
            
        // Instantiate the touch 'buffer'
        int x[2] = {0,0};
        int thenum = 0;
    
        // Get touch pad sensor reading
        int t = tsi.readDistance();
        
        // Test if the touch pad is being groped
        x[0] = tsi.readDistance();
        int activated = 0;
        
        while(t != 0 && activated <= 1) {
            gled = 0.0;
            x[1] = x[0];
            x[0] = tsi.readDistance();
            thenum = x[0];
            
            
            // display crap on the display thingie! same fat if statement from before :)
            if (acc.getAccZ() > 0.8){
                if (thenum <= 8)
                    lcd.printf("s\n\n");
                else if ((thenum>9) && (thenum<=20))
                    lcd.printf("i\n\n");
                else if ((thenum>20) && (thenum<31))
                    lcd.printf("h\n\n");
                else
                    lcd.printf("d\n\n");
            }
            // half z half y
            else if ((acc.getAccZ() > 0.3 && acc.getAccZ() < 0.8) && (acc.getAccX() < 0.3) && (acc.getAccY() < -0.3 && acc.getAccY() > -0.8)) {
                if (thenum <= 10)
                    lcd.printf("e\n\n");
                else if ((thenum>11) && (thenum<=29))
                    lcd.printf("r\n\n");
                else
                    lcd.printf("a\n\n");
            }
            // half z half +x
            else if ((acc.getAccZ() > 0.3 && acc.getAccZ() < 0.8) && (acc.getAccX() > 0.3 && acc.getAccX() < 0.8) && (acc.getAccY() < 0.3)) {
                if (thenum <= 8)
                    lcd.printf("w\n\n");
                else if ((thenum>9) && (thenum<=20))
                    lcd.printf("f\n\n");
                else if ((thenum>20) && (thenum<31))
                    lcd.printf("g\n\n");
                else
                    lcd.printf("y\n\n");
            }
            // half z half -x
            else if ((acc.getAccZ() > 0.3 && acc.getAccZ() < 0.8) && (acc.getAccY() < 0.3) && (acc.getAccX() < -0.3 && acc.getAccX() > -0.8)) {
                if (thenum <= 8)
                    lcd.printf("l\n\n");
                else if ((thenum>9) && (thenum<=20))
                    lcd.printf("u\n\n");
                else if ((thenum>20) && (thenum<31))
                    lcd.printf("c\n\n");
                else
                    lcd.printf("m\n\n");
            }
            // negative y direction
            else if (acc.getAccY() < -0.8){              
                if (thenum <= 10)
                    lcd.printf("t\n\n");
                else if ((thenum>11) && (thenum<=29))
                    lcd.printf("o\n\n");
                else
                    lcd.printf("n\n\n");
            }
            //positive x direction
            else if (acc.getAccX() > 0.8){
                if (thenum <= 8)
                    lcd.printf("j\n\n");
                else if ((thenum>9) && (thenum<=20))
                    lcd.printf("x\n\n");
                else if ((thenum>20) && (thenum<=31))
                    lcd.printf("q\n\n");
                else
                    lcd.printf("z\n\n");
            }
            //negative x direction
            else if (acc.getAccX() < -0.8){
                if (thenum <= 8)
                    lcd.printf("p\n\n");
                else if ((thenum>9) && (thenum<=20))
                    lcd.printf("b\n\n");
                else if ((thenum>21) && (thenum<=31))
                    lcd.printf("v\n\n");
                else
                    lcd.printf("k\n\n");
            }
            
            // test for a left swipe
            if (x[0] > x[1] + 20) {
                // i swiped right
                // key = spacebar
                bled = 0.0; rled = 1.0; gled = 1.0;
                keyboard.keyCode(' ',0);
                activated = 5;
                lcd.printf(" \n\n");
                break;
            }
            
            // from experience, x[1] is the actual output 'value' booya :)
            thenum = x[1]; x[1] = x[0]; x[0] = tsi.readDistance();
            
            if (tsi.readDistance() == 0){
                activated = 1;
                lcd.printf(" \n\n");
                break; // get out of the fucking loop you piece of shit
            }
            
            // built-in loop delay    
            wait(0.1);
        }
        
        // now i get into the nitty gritty - how do i extract the keys themselves
        if (activated == 1){
            // positive z direction
            if (acc.getAccZ() > 0.8){
                if (thenum <= 8)
                    keyboard.printf("s");
                else if ((thenum>9) && (thenum<=20))
                    keyboard.printf("i");
                else if ((thenum>20) && (thenum<31))
                    keyboard.printf("h");
                else
                    keyboard.printf("d");
            }
            // half z half y
            else if ((acc.getAccZ() > 0.3 && acc.getAccZ() < 0.8) && (acc.getAccX() < 0.3) && (acc.getAccY() < -0.3 && acc.getAccY() > -0.8)) {
                if (thenum <= 10)
                    keyboard.printf("e");
                else if ((thenum>11) && (thenum<=29))
                    keyboard.printf("r");
                else
                    keyboard.printf("a");
            }
            // half z half +x
            else if ((acc.getAccZ() > 0.3 && acc.getAccZ() < 0.8) && (acc.getAccX() > 0.3 && acc.getAccX() < 0.8) && (acc.getAccY() < 0.3)) {
                if (thenum <= 8)
                    keyboard.printf("w");
                else if ((thenum>9) && (thenum<=20))
                    keyboard.printf("f");
                else if ((thenum>20) && (thenum<31))
                    keyboard.printf("g");
                else
                    keyboard.printf("y");
            }
            // half z half -x
            else if ((acc.getAccZ() > 0.3 && acc.getAccZ() < 0.8) && (acc.getAccY() < 0.3) && (acc.getAccX() < -0.3 && acc.getAccX() > -0.8)) {
                if (thenum <= 8)
                    keyboard.printf("l");
                else if ((thenum>9) && (thenum<=20))
                    keyboard.printf("u");
                else if ((thenum>20) && (thenum<31))
                    keyboard.printf("c");
                else
                    keyboard.printf("m");
            }
            // negative y direction
            else if (acc.getAccY() < -0.8){              
                if (thenum <= 10)
                    keyboard.printf("t");
                else if ((thenum>11) && (thenum<=29))
                    keyboard.printf("o");
                else
                    keyboard.printf("n");
            }
            //positive x direction
            else if (acc.getAccX() > 0.8){
                if (thenum <= 8)
                    keyboard.printf("j");
                else if ((thenum>9) && (thenum<=20))
                    keyboard.printf("x");
                else if ((thenum>20) && (thenum<=31))
                    keyboard.printf("q");
                else
                    keyboard.printf("z");
            }
            //negative x direction
            else if (acc.getAccX() < -0.8){
                if (thenum <= 8)
                    keyboard.printf("p");
                else if ((thenum>9) && (thenum<=20))
                    keyboard.printf("b");
                else if ((thenum>21) && (thenum<=31))
                    keyboard.printf("v");
                else
                    keyboard.printf("k");
            }        
        }
        activated = activated - 1;
        wait(0.1);
    }
}