Space invaders with a nRF2401A wireless joypad

Dependencies:   Gameduino mbed nRF2401A

Fork of Gameduino_Invaders_game by Chris Dick

Gameduino and an nRF2401A hooked up to an mbed on an mbeduino:

/media/uploads/TheChrisyd/2014-03-08_22.53.54.jpg

Committer:
TheChrisyd
Date:
Sun Mar 09 12:27:20 2014 +0000
Revision:
5:3ede9991d8e0
Parent:
4:bb78bedae411
Update to match Library update

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:bb78bedae411 16
TheChrisyd 4:bb78bedae411 17 #define STICK_LEFT_BIT 0x01
TheChrisyd 4:bb78bedae411 18 #define STICK_RIGHT_BIT 0x02
TheChrisyd 4:bb78bedae411 19 #define STICK_UP_BIT 0x04
TheChrisyd 4:bb78bedae411 20 #define STICK_DOWN_BIT 0x08
TheChrisyd 4:bb78bedae411 21 #define ANALOG_STICK_BIT 0x80
TheChrisyd 4:bb78bedae411 22 #define STICK_INFO_MASK (ANALOG_STICK_BIT)
TheChrisyd 4:bb78bedae411 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 // Digital joystick functions
TheChrisyd 2:20a89dc286d5 41 enum button_name {
TheChrisyd 2:20a89dc286d5 42 buttonA = 0x01,
TheChrisyd 2:20a89dc286d5 43 buttonB = 0x02,
TheChrisyd 2:20a89dc286d5 44 buttonC = 0x04,
TheChrisyd 2:20a89dc286d5 45 buttonX = 0x08,
TheChrisyd 2:20a89dc286d5 46 buttonY = 0x10,
TheChrisyd 2:20a89dc286d5 47 buttonZ = 0x20,
TheChrisyd 2:20a89dc286d5 48 buttonStart = 0x40,
TheChrisyd 2:20a89dc286d5 49 buttonSelect = 0x80
TheChrisyd 2:20a89dc286d5 50 };
TheChrisyd 2:20a89dc286d5 51 // Test a named button
TheChrisyd 2:20a89dc286d5 52 // nb. You can combine button names to test multiple buttons at the same time
TheChrisyd 2:20a89dc286d5 53 bool isPressed(byte n) { return (buttons&n)!=0; }
TheChrisyd 2:20a89dc286d5 54
TheChrisyd 2:20a89dc286d5 55 // This tells you if a button changed between the last two calls to "read()"
TheChrisyd 2:20a89dc286d5 56 bool changed(byte n) { return bool((buttons^prev)&n); }
TheChrisyd 2:20a89dc286d5 57
TheChrisyd 2:20a89dc286d5 58 // Joystick up/down/left/right (or analog stick)
TheChrisyd 4:bb78bedae411 59 bool left() { return (dpad&STICK_LEFT_BIT)!=0; }
TheChrisyd 4:bb78bedae411 60 bool right() { return (dpad&STICK_RIGHT_BIT)!=0; }
TheChrisyd 4:bb78bedae411 61 bool up() { return (dpad&STICK_UP_BIT)!=0; }
TheChrisyd 4:bb78bedae411 62 bool down() { return (dpad&STICK_DOWN_BIT)!=0; }
TheChrisyd 2:20a89dc286d5 63
TheChrisyd 2:20a89dc286d5 64
TheChrisyd 2:20a89dc286d5 65 // Analog joystick functions
TheChrisyd 2:20a89dc286d5 66
TheChrisyd 2:20a89dc286d5 67 // Force the analog joystick to recalibrate itself
TheChrisyd 2:20a89dc286d5 68 void recalibrate();
TheChrisyd 2:20a89dc286d5 69
TheChrisyd 2:20a89dc286d5 70 // Current joystick position in range [-128..127]
TheChrisyd 2:20a89dc286d5 71 char analogX() { return stickX; }
TheChrisyd 2:20a89dc286d5 72 char analogY() { return stickY; }
TheChrisyd 2:20a89dc286d5 73
TheChrisyd 2:20a89dc286d5 74 // For debugging - show all state onscreen at (x,y)
TheChrisyd 2:20a89dc286d5 75 void dump(int x, int y);
TheChrisyd 2:20a89dc286d5 76 };
TheChrisyd 2:20a89dc286d5 77
TheChrisyd 2:20a89dc286d5 78
TheChrisyd 2:20a89dc286d5 79 // JOYSTICK_INCLUDED
TheChrisyd 2:20a89dc286d5 80 #endif