Code for the space evader game.

Dependencies:   N5110 PowerControl mbed

Committer:
domplatypus
Date:
Sun May 10 16:48:46 2015 +0000
Revision:
0:dd6685f1343e
Child:
1:225522d0dd77
version 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
domplatypus 0:dd6685f1343e 1 /**
domplatypus 0:dd6685f1343e 2 @file joystick.cpp
domplatypus 0:dd6685f1343e 3
domplatypus 0:dd6685f1343e 4 @brief Program implementation
domplatypus 0:dd6685f1343e 5
domplatypus 0:dd6685f1343e 6 */
domplatypus 0:dd6685f1343e 7 #include "mbed.h"
domplatypus 0:dd6685f1343e 8 #include "joystick.h"
domplatypus 0:dd6685f1343e 9 InterruptIn joystickButton(p17);
domplatypus 0:dd6685f1343e 10 AnalogIn xPot(p16);
domplatypus 0:dd6685f1343e 11 AnalogIn yPot(p15);
domplatypus 0:dd6685f1343e 12 Ticker pollJoystick;
domplatypus 0:dd6685f1343e 13 Serial serial(USBTX,USBRX);
domplatypus 0:dd6685f1343e 14 int printFlag = 0;
domplatypus 0:dd6685f1343e 15 Joystick joystick;
domplatypus 0:dd6685f1343e 16
domplatypus 0:dd6685f1343e 17 void calibrateJoystick()
domplatypus 0:dd6685f1343e 18 {
domplatypus 0:dd6685f1343e 19 joystickButton.mode(PullDown);
domplatypus 0:dd6685f1343e 20 // must not move during calibration
domplatypus 0:dd6685f1343e 21 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
domplatypus 0:dd6685f1343e 22 joystick.y0 = yPot;
domplatypus 0:dd6685f1343e 23 }
domplatypus 0:dd6685f1343e 24
domplatypus 0:dd6685f1343e 25 void updateJoystick()
domplatypus 0:dd6685f1343e 26 {
domplatypus 0:dd6685f1343e 27 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
domplatypus 0:dd6685f1343e 28 joystick.x = xPot - joystick.x0;
domplatypus 0:dd6685f1343e 29 joystick.y = yPot - joystick.y0;
domplatypus 0:dd6685f1343e 30 // read button state
domplatypus 0:dd6685f1343e 31 joystick.button = joystickButton;
domplatypus 0:dd6685f1343e 32
domplatypus 0:dd6685f1343e 33 // calculate direction depending on x,y values
domplatypus 0:dd6685f1343e 34 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
domplatypus 0:dd6685f1343e 35 if ( fabs(joystick.y) < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 36 joystick.direction[0] = 0; //middle for y
domplatypus 0:dd6685f1343e 37 } else if ( joystick.y > DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 38 joystick.direction[0] = 1; //up
domplatypus 0:dd6685f1343e 39 } else if ( joystick.y < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 40 joystick.direction[0] = -1; // down
domplatypus 0:dd6685f1343e 41 }
domplatypus 0:dd6685f1343e 42 if (fabs(joystick.x) < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 43 joystick.direction[1] = 0; // centre for x
domplatypus 0:dd6685f1343e 44 }
domplatypus 0:dd6685f1343e 45 else if ( joystick.x > DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 46 joystick.direction[1] = 1; // right
domplatypus 0:dd6685f1343e 47 } else if ( joystick.x < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 48 joystick.direction[1] = -1; //left
domplatypus 0:dd6685f1343e 49 } else {
domplatypus 0:dd6685f1343e 50 joystick.direction[0] = 0;
domplatypus 0:dd6685f1343e 51 joystick.direction[1] = 0;
domplatypus 0:dd6685f1343e 52 }
domplatypus 0:dd6685f1343e 53
domplatypus 0:dd6685f1343e 54 // set flag for printing
domplatypus 0:dd6685f1343e 55 printFlag = 1;
domplatypus 0:dd6685f1343e 56 }