(DA) Internet of Things and Smart Electronics- ELE3006M2122 / Mbed 2 deprecated Final_Project_V16_DLeaming_25574043

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Joystick.h Source File

Joystick.h

00001 #ifndef JOYSTICK_H
00002 #define JOYSTICK_H
00003 
00004 #include "mbed.h"
00005 
00006 // this value can be tuned to alter tolerance of joystick movement
00007 #define TOL 0.1f
00008 #define RAD2DEG 57.2957795131f
00009 
00010 enum Direction {
00011     CENTRE,  // 0
00012     N,       // 1
00013     NE,      // 2
00014     E,       // 3
00015     SE,      // 4
00016     S,       // 5
00017     SW,      // 6
00018     W,       // 7
00019     NW      // 8
00020 };
00021 
00022 struct Vector2D {
00023   float x;
00024   float y;  
00025 };
00026 
00027 struct Polar {
00028     float mag;
00029     float angle;
00030 };
00031 
00032 /** Joystick Class
00033 @brief Acknowledgements to Dr Craig A. Evans, University of Leeds
00034 @brief  Library for interfacing with analogue joystick
00035 @upgraded Dr Edmond Nurellari, University of Lincoln
00036 
00037 Example:
00038 
00039 @code
00040 
00041 #include "mbed.h"
00042 #include "Joystick.h"
00043 
00044 //                  y     x     button
00045 Joystick joystick(PTB10,PTB11,PTC16);
00046 
00047 int main() {
00048     
00049     joystick.init();
00050     
00051     while(1) {
00052     
00053         Vector2D coord = joystick.get_coord();
00054         printf("Coord = %f,%f\n",coord.x,coord.y);
00055         
00056         Vector2D mapped_coord = joystick.get_mapped_coord(); 
00057         printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); 
00058         
00059         float mag = joystick.get_mag();
00060         float angle = joystick.get_angle();
00061         printf("Mag = %f Angle = %f\n",mag,angle);
00062         
00063         Direction d = joystick.get_direction();
00064         printf("Direction = %i\n",d);
00065         
00066         if (joystick.button_pressed() ) {
00067             printf("Button Pressed\n");  
00068         }
00069           
00070         wait(0.5);
00071     }
00072     
00073     
00074 }
00075 
00076 * @endcode
00077 */
00078 class Joystick
00079 {
00080 public:
00081     
00082     //              y-pot              x-pot            button
00083     Joystick(PinName vertPin,PinName horizPin,PinName clickPin);
00084     
00085     void init();  // needs to be called at start with joystick centred
00086     float get_mag();              // polar
00087     float get_angle();            // polar
00088     Vector2D get_coord();         // cartesian co-ordinates x,y
00089     Vector2D get_mapped_coord();  // x,y mapped to circle
00090     Direction get_direction();    // N,NE,E,SE etc.
00091     Polar get_polar();            // mag and angle in struct form
00092     bool button_pressed();        // read button flag set in ISR when button pressed
00093     
00094 private:
00095 
00096     AnalogIn *vert;
00097     AnalogIn *horiz;
00098     InterruptIn *click;
00099     
00100     int _click_flag;    // flag set in ISR
00101     void click_isr();   // ISR on button press
00102        
00103     // centred x,y values    
00104     float _x0;
00105     float _y0;
00106     
00107 };
00108 
00109 #endif