Code for the space evader game.

Dependencies:   N5110 PowerControl mbed

Committer:
domplatypus
Date:
Mon May 11 11:44:51 2015 +0000
Revision:
1:225522d0dd77
Parent:
0:dd6685f1343e
Version 1.0

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 1:225522d0dd77 5 @brief Original code https://developer.mbed.org/users/eencae/code/Joystick/
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 1:225522d0dd77 35 //Direction matrix modified here depending on the direction tolerence
domplatypus 1:225522d0dd77 36 // +1 is down/right, 0 is centre and -1 is up/left
domplatypus 1:225522d0dd77 37 //modified by Dominic Platt
domplatypus 0:dd6685f1343e 38 if ( fabs(joystick.y) < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 39 joystick.direction[0] = 0; //middle for y
domplatypus 0:dd6685f1343e 40 } else if ( joystick.y > DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 41 joystick.direction[0] = 1; //up
domplatypus 0:dd6685f1343e 42 } else if ( joystick.y < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 43 joystick.direction[0] = -1; // down
domplatypus 0:dd6685f1343e 44 }
domplatypus 0:dd6685f1343e 45 if (fabs(joystick.x) < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 46 joystick.direction[1] = 0; // centre for x
domplatypus 0:dd6685f1343e 47 }
domplatypus 0:dd6685f1343e 48 else if ( joystick.x > DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 49 joystick.direction[1] = 1; // right
domplatypus 0:dd6685f1343e 50 } else if ( joystick.x < DIRECTION_TOLERANCE) {
domplatypus 0:dd6685f1343e 51 joystick.direction[1] = -1; //left
domplatypus 0:dd6685f1343e 52 } else {
domplatypus 0:dd6685f1343e 53 joystick.direction[0] = 0;
domplatypus 0:dd6685f1343e 54 joystick.direction[1] = 0;
domplatypus 0:dd6685f1343e 55 }
domplatypus 0:dd6685f1343e 56
domplatypus 0:dd6685f1343e 57 // set flag for printing
domplatypus 0:dd6685f1343e 58 printFlag = 1;
domplatypus 0:dd6685f1343e 59 }