Invaders game for the Gameduino

Dependencies:   Gameduino mbed

Committer:
TheChrisyd
Date:
Thu Jun 21 19:13:34 2012 +0000
Revision:
0:8a7c58553b44
Child:
1:f44175dd69fd
backup before adding more features

Who changed what in which revision?

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