Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 SDFileSystem mbed
Joystick.h
00001 /** 00002 @file Joystick.h 00003 @brief Header file containing functions prototypes, defines and global variables for constructing joystick. 00004 @brief Example code of how to read a joystick with slightly changed for the game 00005 @https://www.sparkfun.com/products/9032 00006 @author Craig A. Evans 00007 @date 7 March 2015 00008 */ 00009 00010 #ifndef JOYSTICK_H 00011 #define JOYSTICK_H 00012 00013 #define DIRECTION_TOLERANCE 0.25 00014 00015 /** 00016 Joystick vertical direction 00017 @brief Analog input for joystick to send vertical directions data 00018 */ 00019 AnalogIn yPot(PTB2); 00020 /** 00021 Joystick horizontal direction 00022 @brief Analog input for joystick to send horizontal direction data 00023 */ 00024 AnalogIn xPot(PTB3); 00025 00026 //! create enumerated type (0,1,2,3 etc. for direction) 00027 //! could be extended for diagonals etc. 00028 enum DirectionName { 00029 UP, 00030 DOWN, 00031 LEFT, 00032 RIGHT, 00033 CENTRE, 00034 UNKNOWN 00035 }; 00036 //! struct for Joystick 00037 typedef struct JoyStick Joystick; 00038 struct JoyStick { 00039 double x; // current x value 00040 double x0; // 'centred' x value 00041 double y; // current y value 00042 double y0; // 'centred' y value 00043 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) 00044 DirectionName direction; // current direction 00045 }; 00046 //! create struct variable 00047 Joystick joystick; 00048 /** 00049 Read default positions of the joystick to calibrate later readings 00050 */ 00051 void calibrateJoystick(); 00052 /** 00053 Update the current position of joystick 00054 */ 00055 void updateJoystick(); 00056 00057 ///////////////////////////////////////////////// 00058 /// Function Definitions 00059 ///////////////////////////////////////////////// 00060 00061 // read default positions of the joystick to calibrate later readings 00062 void calibrateJoystick() 00063 { 00064 // must not move during calibration 00065 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) 00066 joystick.y0 = yPot; 00067 } 00068 00069 void updateJoystick() 00070 { 00071 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) 00072 joystick.x = xPot - joystick.x0; 00073 joystick.y = yPot - joystick.y0; 00074 00075 // calculate direction depending on x,y values 00076 // tolerance allows a little lee-way in case joystick not exactly in the stated direction 00077 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { 00078 joystick.direction = CENTRE; 00079 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { 00080 joystick.direction = UP; 00081 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { 00082 joystick.direction = DOWN; 00083 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { 00084 joystick.direction = RIGHT; 00085 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { 00086 joystick.direction = LEFT; 00087 } else { 00088 joystick.direction = UNKNOWN; 00089 } 00090 } 00091 00092 #endif
Generated on Mon Sep 12 2022 12:25:09 by
1.7.2