Andreas Garmannslund / Mbed 2 deprecated SimplePlatformGame

Dependencies:   N5110 PinDetect PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Joystick.h Source File

Joystick.h

Go to the documentation of this file.
00001 /**
00002 @file Joystick.h
00003 @brief Class used for controlling the button
00004 @author Andreas Garmannslund
00005 @date April 2015
00006 */
00007 
00008 #ifndef JOYSTICK_H
00009 #define JOYSTICK_H
00010 
00011 #include "mbed.h"
00012 
00013 /** @brief Enum used for the 8-directions of the joystick. */
00014 enum JoystickDirection {CENTER, UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT, UNKNOWN};
00015 
00016 /// Joystick class
00017 class Joystick
00018 {   
00019     public:
00020     
00021         /** Creates a new Joystick object
00022         * @param x Pin connected to the horizontal potentiometer of the joystick
00023         * @param y Pin connected to the vertical potentiometer of the joystick
00024         * @param button Pin connected to the button of the thumb joystick
00025         */
00026         Joystick(PinName x, PinName y, PinName button);
00027         
00028         /** @brief Deconstructor. Frees allocated memory */
00029         ~Joystick();
00030         
00031         /** @brief Updates the current direction and button status of the joystick */
00032         void update();
00033         
00034         /** Calibrates the joystick. The joystick must be centered while this function is called */
00035         void calibrate(); // Calibrates joystick by updating the center positions
00036         
00037         /** Returns the current JoystickDirection based on last update
00038         * @return The current JoystickDirection.
00039         */
00040         int getDirection() {return dir;}
00041         
00042         /** Reads the value of the button
00043         * @return 1 if pressed, 0 otherwise
00044         */
00045         int readButton() {return *btn;};
00046         
00047         /** Square set around the center of the joystick where the input is ignored. The axes are treated seperately. Can be varied from 0 (no dead-zone) to 0.5 (max value for dx and dy). **/
00048         static const float DEAD_ZONE = 0.1; 
00049         
00050     private:
00051         AnalogIn *xPot;
00052         AnalogIn *yPot;
00053         DigitalIn *btn;
00054         float dx, dy;
00055         float centerX, centerY;
00056         JoystickDirection dir;
00057 };
00058 
00059 #endif