Working Menu, additions to be made

Dependencies:   mbed

Committer:
jackmcgarley
Date:
Wed Aug 24 18:48:25 2022 +0000
Revision:
15:61d9a4e63b99
Parent:
0:d4d7e882c87d
Working menu, additions to be made;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
25574069 0:d4d7e882c87d 1 #ifndef JOYSTICK_H
25574069 0:d4d7e882c87d 2 #define JOYSTICK_H
25574069 0:d4d7e882c87d 3
25574069 0:d4d7e882c87d 4 #include "mbed.h"
25574069 0:d4d7e882c87d 5
25574069 0:d4d7e882c87d 6 // this value can be tuned to alter tolerance of joystick movement
25574069 0:d4d7e882c87d 7 #define TOL 0.1f
25574069 0:d4d7e882c87d 8 #define RAD2DEG 57.2957795131f
25574069 0:d4d7e882c87d 9
25574069 0:d4d7e882c87d 10 enum Direction {
25574069 0:d4d7e882c87d 11 CENTRE, // 0
25574069 0:d4d7e882c87d 12 N, // 1
25574069 0:d4d7e882c87d 13 NE, // 2
25574069 0:d4d7e882c87d 14 E, // 3
25574069 0:d4d7e882c87d 15 SE, // 4
25574069 0:d4d7e882c87d 16 S, // 5
25574069 0:d4d7e882c87d 17 SW, // 6
25574069 0:d4d7e882c87d 18 W, // 7
25574069 0:d4d7e882c87d 19 NW // 8
25574069 0:d4d7e882c87d 20 };
25574069 0:d4d7e882c87d 21
25574069 0:d4d7e882c87d 22 struct Vector2D {
25574069 0:d4d7e882c87d 23 float x;
25574069 0:d4d7e882c87d 24 float y;
25574069 0:d4d7e882c87d 25 };
25574069 0:d4d7e882c87d 26
25574069 0:d4d7e882c87d 27 struct Polar {
25574069 0:d4d7e882c87d 28 float mag;
25574069 0:d4d7e882c87d 29 float angle;
25574069 0:d4d7e882c87d 30 };
25574069 0:d4d7e882c87d 31
25574069 0:d4d7e882c87d 32 /** Joystick Class
25574069 0:d4d7e882c87d 33 @brief Acknowledgements to Dr Craig A. Evans, University of Leeds
25574069 0:d4d7e882c87d 34 @brief Library for interfacing with analogue joystick
25574069 0:d4d7e882c87d 35 @upgraded Dr Edmond Nurellari, University of Lincoln
25574069 0:d4d7e882c87d 36
25574069 0:d4d7e882c87d 37 Example:
25574069 0:d4d7e882c87d 38
25574069 0:d4d7e882c87d 39 @code
25574069 0:d4d7e882c87d 40
25574069 0:d4d7e882c87d 41 #include "mbed.h"
25574069 0:d4d7e882c87d 42 #include "Joystick.h"
25574069 0:d4d7e882c87d 43
25574069 0:d4d7e882c87d 44 // y x button
25574069 0:d4d7e882c87d 45 Joystick joystick(PTB10,PTB11,PTC16);
25574069 0:d4d7e882c87d 46
25574069 0:d4d7e882c87d 47 int main() {
25574069 0:d4d7e882c87d 48
25574069 0:d4d7e882c87d 49 joystick.init();
25574069 0:d4d7e882c87d 50
25574069 0:d4d7e882c87d 51 while(1) {
25574069 0:d4d7e882c87d 52
25574069 0:d4d7e882c87d 53 Vector2D coord = joystick.get_coord();
25574069 0:d4d7e882c87d 54 printf("Coord = %f,%f\n",coord.x,coord.y);
25574069 0:d4d7e882c87d 55
25574069 0:d4d7e882c87d 56 Vector2D mapped_coord = joystick.get_mapped_coord();
25574069 0:d4d7e882c87d 57 printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y);
25574069 0:d4d7e882c87d 58
25574069 0:d4d7e882c87d 59 float mag = joystick.get_mag();
25574069 0:d4d7e882c87d 60 float angle = joystick.get_angle();
25574069 0:d4d7e882c87d 61 printf("Mag = %f Angle = %f\n",mag,angle);
25574069 0:d4d7e882c87d 62
25574069 0:d4d7e882c87d 63 Direction d = joystick.get_direction();
25574069 0:d4d7e882c87d 64 printf("Direction = %i\n",d);
25574069 0:d4d7e882c87d 65
25574069 0:d4d7e882c87d 66 if (joystick.button_pressed() ) {
25574069 0:d4d7e882c87d 67 printf("Button Pressed\n");
25574069 0:d4d7e882c87d 68 }
25574069 0:d4d7e882c87d 69
25574069 0:d4d7e882c87d 70 wait(0.5);
25574069 0:d4d7e882c87d 71 }
25574069 0:d4d7e882c87d 72
25574069 0:d4d7e882c87d 73
25574069 0:d4d7e882c87d 74 }
25574069 0:d4d7e882c87d 75
25574069 0:d4d7e882c87d 76 * @endcode
25574069 0:d4d7e882c87d 77 */
25574069 0:d4d7e882c87d 78 class Joystick
25574069 0:d4d7e882c87d 79 {
25574069 0:d4d7e882c87d 80 public:
25574069 0:d4d7e882c87d 81
25574069 0:d4d7e882c87d 82 // y-pot x-pot button
25574069 0:d4d7e882c87d 83 Joystick(PinName vertPin,PinName horizPin,PinName clickPin);
25574069 0:d4d7e882c87d 84
25574069 0:d4d7e882c87d 85 void init(); // needs to be called at start with joystick centred
25574069 0:d4d7e882c87d 86 float get_mag(); // polar
25574069 0:d4d7e882c87d 87 float get_angle(); // polar
25574069 0:d4d7e882c87d 88 Vector2D get_coord(); // cartesian co-ordinates x,y
25574069 0:d4d7e882c87d 89 Vector2D get_mapped_coord(); // x,y mapped to circle
25574069 0:d4d7e882c87d 90 Direction get_direction(); // N,NE,E,SE etc.
25574069 0:d4d7e882c87d 91 Polar get_polar(); // mag and angle in struct form
25574069 0:d4d7e882c87d 92 bool button_pressed(); // read button flag set in ISR when button pressed
25574069 0:d4d7e882c87d 93
25574069 0:d4d7e882c87d 94 private:
25574069 0:d4d7e882c87d 95
25574069 0:d4d7e882c87d 96 AnalogIn *vert;
25574069 0:d4d7e882c87d 97 AnalogIn *horiz;
25574069 0:d4d7e882c87d 98 InterruptIn *click;
25574069 0:d4d7e882c87d 99
25574069 0:d4d7e882c87d 100 int _click_flag; // flag set in ISR
25574069 0:d4d7e882c87d 101 void click_isr(); // ISR on button press
25574069 0:d4d7e882c87d 102
25574069 0:d4d7e882c87d 103 // centred x,y values
25574069 0:d4d7e882c87d 104 float _x0;
25574069 0:d4d7e882c87d 105 float _y0;
25574069 0:d4d7e882c87d 106
25574069 0:d4d7e882c87d 107 };
25574069 0:d4d7e882c87d 108
25574069 0:d4d7e882c87d 109 #endif