idd hw3

Dependencies:   mbed DebounceIn USBDevice PinDetect

main.cpp

Committer:
emilychen55
Date:
2014-09-26
Revision:
2:4b2e3ca87deb
Parent:
1:85b6b5d1528e
Child:
4:c90291fd6cc9

File content as of revision 2:4b2e3ca87deb:

#include "mbed.h"
#include "USBMouseKeyboard.h"
#include "DebounceIn.h"
 
//LED1: NUM_LOCK
//LED2: CAPS_LOCK
//LED3: SCROLL_LOCK
BusOut leds(LED1, LED2, LED3);
DebounceIn accelerate(D7);
DigitalIn powerUp(D13);
DigitalIn breakz(D6);

Serial pc(USBTX, USBRX);

AnalogIn accelZ(A0);
AnalogIn accelY(A1);
AnalogIn accelX(A2);
 
//USBMouseKeyboard
USBMouseKeyboard controller;

//Y: 0.43 - 0.56
//X: 0.39 - 0.57
float CENTER_LEFT = 0.48;
float CENTER_RIGHT = 0.51;
float CENTER_UP = 0.43;
float CENTER_DOWN = 0.53;
 
int main(void) {
    pc.baud(115200);
    while (1) {
        if (!accelerate) {
            controller.keyCode('x');
            if (accelY.read() <= CENTER_LEFT) {
                //controller.move(-1, 0);
                controller.keyCode(LEFT_ARROW);
                controller.keyCode('x');
            }
            if (accelY.read() >= CENTER_RIGHT) {
                //controller.move(1, 0);
                controller.keyCode(RIGHT_ARROW);
                controller.keyCode('x');
            }
        }
        if (!powerUp) {
            controller.keyCode('x');
            controller.keyCode('s');
            controller.keyCode('x');
        }
        if (!breakz) {
            controller.keyCode('z');
        }
        // Navigation in Game Menu 
        if (accelY.read() <= CENTER_LEFT) {
            controller.keyCode(LEFT_ARROW);
        }
        if (accelY.read() >= CENTER_RIGHT) {
            controller.keyCode(RIGHT_ARROW);
        }
        if (accelX.read() <= CENTER_UP) {
            controller.keyCode(UP_ARROW);
            wait(0.1);
        }
        if (accelX.read() >= CENTER_DOWN) {
            controller.keyCode(DOWN_ARROW);
            wait(0.1);
        }
    }
}