Coursework

Committer:
sesa514652
Date:
Thu Dec 16 14:03:11 2021 +0000
Revision:
0:1f799c7cce2b
Screen and US combined code

Who changed what in which revision?

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