Invaders game for the Gameduino

Dependencies:   Gameduino mbed

Committer:
TheChrisyd
Date:
Sat Oct 26 22:32:18 2013 +0000
Revision:
4:e82f4a87df9e
Parent:
2:20a89dc286d5
Shields are now destroyed when hit, corrected score displays

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 2:20a89dc286d5 1 #ifndef JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 2 #define JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 3 /*------------------------------------------------------------
TheChrisyd 2:20a89dc286d5 4 Universal joystick driver for Gameduino
TheChrisyd 2:20a89dc286d5 5
TheChrisyd 2:20a89dc286d5 6 http://www.artlum.com/gameduino/gameduino.html#joystick
TheChrisyd 2:20a89dc286d5 7
TheChrisyd 2:20a89dc286d5 8 Important: Read the file "read_me_first.txt" *before*
TheChrisyd 2:20a89dc286d5 9 writing any code. Do it NOW!
TheChrisyd 2:20a89dc286d5 10
TheChrisyd 2:20a89dc286d5 11 Thanks go to 'Guy" for his contributions to this
TheChrisyd 2:20a89dc286d5 12 ------------------------------------------------------------*/
TheChrisyd 2:20a89dc286d5 13
TheChrisyd 2:20a89dc286d5 14 //#include "WProgram.h"
TheChrisyd 2:20a89dc286d5 15 #include "arduino.h"
TheChrisyd 4:e82f4a87df9e 16
TheChrisyd 4:e82f4a87df9e 17 #define STICK_LEFT_BIT 0x01
TheChrisyd 4:e82f4a87df9e 18 #define STICK_RIGHT_BIT 0x02
TheChrisyd 4:e82f4a87df9e 19 #define STICK_UP_BIT 0x04
TheChrisyd 4:e82f4a87df9e 20 #define STICK_DOWN_BIT 0x08
TheChrisyd 4:e82f4a87df9e 21 #define ANALOG_STICK_BIT 0x80
TheChrisyd 4:e82f4a87df9e 22 #define STICK_INFO_MASK (ANALOG_STICK_BIT)
TheChrisyd 4:e82f4a87df9e 23
TheChrisyd 2:20a89dc286d5 24 class Joystick {
TheChrisyd 2:20a89dc286d5 25 byte buttons; // State of buttons, packed into a byte
TheChrisyd 2:20a89dc286d5 26 byte prev; // State of the buttons on previous read
TheChrisyd 2:20a89dc286d5 27 byte dpad; // State of Up/Down/Left/Right buttons plus some control flags
TheChrisyd 2:20a89dc286d5 28 signed char stickX,stickY;// Analogue x/y position
TheChrisyd 2:20a89dc286d5 29 char xCal,yCal; // Calibration
TheChrisyd 2:20a89dc286d5 30 public:
TheChrisyd 2:20a89dc286d5 31 // The constructor sets up the Arduino pins for input
TheChrisyd 2:20a89dc286d5 32 // and calibrates the joystick using the current position
TheChrisyd 2:20a89dc286d5 33 Joystick();
TheChrisyd 2:20a89dc286d5 34
TheChrisyd 2:20a89dc286d5 35 // Read the current state
TheChrisyd 2:20a89dc286d5 36 void read();
TheChrisyd 2:20a89dc286d5 37
TheChrisyd 2:20a89dc286d5 38 // Return "true" if analog X/Y position is available
TheChrisyd 2:20a89dc286d5 39 bool hasAnalogStick();
TheChrisyd 2:20a89dc286d5 40
TheChrisyd 2:20a89dc286d5 41 // Digital joystick functions
TheChrisyd 2:20a89dc286d5 42 enum button_name {
TheChrisyd 2:20a89dc286d5 43 buttonA = 0x01,
TheChrisyd 2:20a89dc286d5 44 buttonB = 0x02,
TheChrisyd 2:20a89dc286d5 45 buttonC = 0x04,
TheChrisyd 2:20a89dc286d5 46 buttonX = 0x08,
TheChrisyd 2:20a89dc286d5 47 buttonY = 0x10,
TheChrisyd 2:20a89dc286d5 48 buttonZ = 0x20,
TheChrisyd 2:20a89dc286d5 49 buttonStart = 0x40,
TheChrisyd 2:20a89dc286d5 50 buttonSelect = 0x80
TheChrisyd 2:20a89dc286d5 51 };
TheChrisyd 2:20a89dc286d5 52 // Test a named button
TheChrisyd 2:20a89dc286d5 53 // nb. You can combine button names to test multiple buttons at the same time
TheChrisyd 2:20a89dc286d5 54 bool isPressed(byte n) { return (buttons&n)!=0; }
TheChrisyd 2:20a89dc286d5 55
TheChrisyd 2:20a89dc286d5 56 // This tells you if a button changed between the last two calls to "read()"
TheChrisyd 2:20a89dc286d5 57 bool changed(byte n) { return bool((buttons^prev)&n); }
TheChrisyd 2:20a89dc286d5 58
TheChrisyd 2:20a89dc286d5 59 // Joystick up/down/left/right (or analog stick)
TheChrisyd 2:20a89dc286d5 60 bool left() { return (dpad&0x01)!=0; }
TheChrisyd 2:20a89dc286d5 61 bool right() { return (dpad&0x02)!=0; }
TheChrisyd 2:20a89dc286d5 62 bool up() { return (dpad&0x04)!=0; }
TheChrisyd 2:20a89dc286d5 63 bool down() { return (dpad&0x08)!=0; }
TheChrisyd 2:20a89dc286d5 64
TheChrisyd 2:20a89dc286d5 65
TheChrisyd 2:20a89dc286d5 66 // Analog joystick functions
TheChrisyd 2:20a89dc286d5 67
TheChrisyd 2:20a89dc286d5 68 // Force the analog joystick to recalibrate itself
TheChrisyd 2:20a89dc286d5 69 void recalibrate();
TheChrisyd 2:20a89dc286d5 70
TheChrisyd 2:20a89dc286d5 71 // Current joystick position in range [-128..127]
TheChrisyd 2:20a89dc286d5 72 char analogX() { return stickX; }
TheChrisyd 2:20a89dc286d5 73 char analogY() { return stickY; }
TheChrisyd 2:20a89dc286d5 74
TheChrisyd 2:20a89dc286d5 75 // For debugging - show all state onscreen at (x,y)
TheChrisyd 2:20a89dc286d5 76 void dump(int x, int y);
TheChrisyd 2:20a89dc286d5 77 };
TheChrisyd 2:20a89dc286d5 78
TheChrisyd 2:20a89dc286d5 79
TheChrisyd 2:20a89dc286d5 80 // JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 81 #endif