accelerometer trial

Dependencies:   mbed C12832 MMA7660 USBDevice

main.cpp

Committer:
knasp
Date:
2019-10-30
Revision:
26:f1a0d62af7f9
Parent:
25:f8db641d33ab

File content as of revision 26:f1a0d62af7f9:

#include "mbed.h"
#include "USBKeyboard.h"
#include "MMA7660.h"
#include "C12832.h"

MMA7660         Accelerometer (p28,p27);        //define accelerometer
USBKeyboard     Usbkeyboard;                    //define keyboard
C12832          LCD (p5,p7,p6,p8,p11);          //define LCD
DigitalOut led1(p12);                           //leds
DigitalOut led2(p13);
DigitalOut led3(p14);
DigitalOut led4(p15);

DigitalIn  Switch1 (p17);                         //switch 1
DigitalIn  Switch2 (p16);                         //switch 2
DigitalIn  Switch3 (p18);                         //spacebar

int main (void)                                                     //main program
{
    //enum Orientation {Up, Down,Right, Left,Back, Front,Unknown};
    MMA7660::Orientation myOrientation;                         //defining orientation

    while (true) {


        myOrientation = Accelerometer.getOrientation ();    //getting the orientation from accelerometer
        
        if (Switch3){

                  Usbkeyboard.printf (" ");                       //spacebar

                     }
       
        if ((!Switch1) && (Switch2)){                            //when switch 1 is off and 2 is on the letters will be printed
            
            if (myOrientation == MMA7660::Up) {

                LCD.printf("RIGHT");
                Usbkeyboard.printf ("d\0");                     //instead of simulating pressing of the "left arrow", simulates pressing "a key"
                led2=0;
                led1=0;
                led3=0;
                led4=1;
                
            } else if (myOrientation == MMA7660::Down) {

                LCD.printf("LEFT");
                Usbkeyboard.printf ("a\0");
                led3=0;
                led1=0;
                led2=1;
                led4=0;
                
            } else if (myOrientation == MMA7660::Left) {

                LCD.printf("Up");
                Usbkeyboard.printf ("w\0");
                led1=1;
                led3=0;
                led2=0;
                led4=0;
                
            } else if (myOrientation == MMA7660::Right) {

                LCD.printf("Down");
                Usbkeyboard.printf ("s\0");
                led4=0;
                led1=0;
                led3=1;
                led2=0;

            }

            else if (myOrientation == MMA7660::Unknown) {

                LCD.printf("Don't Know");

            }
        }
        
        else if ((!Switch1) && (!Switch2)){                                     //when both switches are off standby mode is on (all leds are on)
        
            led3=1;
            led1=1;
            led2=1;
            led4=1;
        }
        
        else if ((Switch1) && (!Switch2)){                                      //if switch 1 is on and switch 2 is off the numbers are written
             
             if (myOrientation == MMA7660::Up) {

                    LCD.printf("Three");
                    Usbkeyboard.printf ("3\0");                     //instead of simulating pressing of the "left arrow", simulates pressing "numbers"
                    led2=0;
                    led1=0;
                    led3=0;
                    led4=1;
                } else if (myOrientation == MMA7660::Down) {

                    LCD.printf("One");
                    Usbkeyboard.printf ("1\0");
                    led3=0;
                    led1=0;
                    led2=1;
                    led4=0;
                } else if (myOrientation == MMA7660::Left) {

                    LCD.printf("Two");
                    Usbkeyboard.printf ("2\0");
                    led1=1;
                    led3=0;
                    led2=0;
                    led4=0;
                } else if (myOrientation == MMA7660::Right) {

                    LCD.printf("Four");
                    Usbkeyboard.printf ("4\0");
                    led4=0;
                    led1=0;
                    led3=1;
                    led2=0;

                }


                else if (myOrientation == MMA7660::Unknown) {

                    LCD.printf("Don't Know");

                }

        }
        
        else if ((Switch1) && (Switch2)){                           //when both switches are on the arrows are imputed
                
                if (myOrientation == MMA7660::Up) {                 //if the orientation is up

                    LCD.printf("Right");                             //print "left" on LCD (due to orientation of the accelerometer on the limb, the directions are changed)
                    Usbkeyboard.keyCode (RIGHT_ARROW);               //device simulates pressing "left arrow key" on the keyboard
                    led2=0;                                         //led off
                    led1=0;                                         //led off
                    led3=0;                                         //led off
                    led4=1;                                         //led on
                    
                } else if (myOrientation == MMA7660::Down) {

                    LCD.printf("Left");
                    Usbkeyboard.keyCode (LEFT_ARROW);
                    led3=0;
                    led1=0;
                    led2=1;
                    led4=0;
                    
                } else if (myOrientation == MMA7660::Left) {

                    LCD.printf("UP");
                    Usbkeyboard.keyCode (UP_ARROW);
                    led1=1;
                    led3=0;
                    led2=0;
                    led4=0;
                    
                } else if (myOrientation == MMA7660::Right) {

                    LCD.printf("DOWN");
                    Usbkeyboard.keyCode (DOWN_ARROW);
                    led4=0;
                    led1=0;
                    led3=1;
                    led2=0;

                } else if (myOrientation == MMA7660::Unknown) {

                    LCD.printf("Don't Know");

                }
        }
    }


    return false;

}