Working Menu with selectable fields yet to add comparison with healthy temperature ranges

Dependencies:   TMP102_02

Committer:
ejh23
Date:
Fri Feb 04 19:10:09 2022 +0000
Revision:
10:62da82b9b6de
Parent:
1:e11018cf2c14
Final code for submission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ejh23 1:e11018cf2c14 1 #include "Joystick.h"
ejh23 1:e11018cf2c14 2
ejh23 1:e11018cf2c14 3 Joystick::Joystick(PinName vertPin,PinName horizPin,PinName clickPin)
ejh23 1:e11018cf2c14 4 {
ejh23 1:e11018cf2c14 5 vert = new AnalogIn(vertPin);
ejh23 1:e11018cf2c14 6 horiz = new AnalogIn(horizPin);
ejh23 1:e11018cf2c14 7 click = new InterruptIn(clickPin);
ejh23 1:e11018cf2c14 8 }
ejh23 1:e11018cf2c14 9
ejh23 1:e11018cf2c14 10 void Joystick::init()
ejh23 1:e11018cf2c14 11 {
ejh23 1:e11018cf2c14 12 // read centred values of joystick
ejh23 1:e11018cf2c14 13 _x0 = horiz->read();
ejh23 1:e11018cf2c14 14 _y0 = vert->read();
ejh23 1:e11018cf2c14 15
ejh23 1:e11018cf2c14 16 // this assumes that the joystick is centred when the init function is called
ejh23 1:e11018cf2c14 17 // if perfectly centred, the pots should read 0.5, but this may
ejh23 1:e11018cf2c14 18 // not be the case and x0 and y0 will be used to calibrate readings
ejh23 1:e11018cf2c14 19
ejh23 1:e11018cf2c14 20 // turn on pull-down for button -> this assumes the other side of the button
ejh23 1:e11018cf2c14 21 // is connected to +3V3 so we read 1 when pressed and 0 when not pressed
ejh23 1:e11018cf2c14 22 click->mode(PullDown);
ejh23 1:e11018cf2c14 23 // we therefore need to fire the interrupt on a rising edge
ejh23 1:e11018cf2c14 24 //click->rise(callback(this,&Joystick::click_isr));
ejh23 1:e11018cf2c14 25 // need to use a callback since mbed-os5 - basically tells it to look in this class for the ISR
ejh23 1:e11018cf2c14 26 _click_flag = 0;
ejh23 1:e11018cf2c14 27
ejh23 1:e11018cf2c14 28 }
ejh23 1:e11018cf2c14 29
ejh23 1:e11018cf2c14 30 Direction Joystick::get_direction()
ejh23 1:e11018cf2c14 31 {
ejh23 1:e11018cf2c14 32 float angle = get_angle(); // 0 to 360, -1 for centred
ejh23 1:e11018cf2c14 33
ejh23 1:e11018cf2c14 34 Direction d;
ejh23 1:e11018cf2c14 35 // partition 360 into segments and check which segment the angle is in
ejh23 1:e11018cf2c14 36 if (angle < 0.0f) {
ejh23 1:e11018cf2c14 37 d = CENTRE; // check for -1.0 angle
ejh23 1:e11018cf2c14 38 } else if (angle < 22.5f) { // then keep going in 45 degree increments
ejh23 1:e11018cf2c14 39 d = N;
ejh23 1:e11018cf2c14 40 } else if (angle < 67.5f) {
ejh23 1:e11018cf2c14 41 d = NE;
ejh23 1:e11018cf2c14 42 } else if (angle < 112.5f) {
ejh23 1:e11018cf2c14 43 d = E;
ejh23 1:e11018cf2c14 44 } else if (angle < 157.5f) {
ejh23 1:e11018cf2c14 45 d = SE;
ejh23 1:e11018cf2c14 46 } else if (angle < 202.5f) {
ejh23 1:e11018cf2c14 47 d = S;
ejh23 1:e11018cf2c14 48 } else if (angle < 247.5f) {
ejh23 1:e11018cf2c14 49 d = SW;
ejh23 1:e11018cf2c14 50 } else if (angle < 292.5f) {
ejh23 1:e11018cf2c14 51 d = W;
ejh23 1:e11018cf2c14 52 } else if (angle < 337.5f) {
ejh23 1:e11018cf2c14 53 d = NW;
ejh23 1:e11018cf2c14 54 } else {
ejh23 1:e11018cf2c14 55 d = N;
ejh23 1:e11018cf2c14 56 }
ejh23 1:e11018cf2c14 57
ejh23 1:e11018cf2c14 58 return d;
ejh23 1:e11018cf2c14 59 }
ejh23 1:e11018cf2c14 60
ejh23 1:e11018cf2c14 61 // this method gets the magnitude of the joystick movement
ejh23 1:e11018cf2c14 62 float Joystick::get_mag()
ejh23 1:e11018cf2c14 63 {
ejh23 1:e11018cf2c14 64 Polar p = get_polar();
ejh23 1:e11018cf2c14 65 return p.mag;
ejh23 1:e11018cf2c14 66 }
ejh23 1:e11018cf2c14 67
ejh23 1:e11018cf2c14 68 // this method gets the angle of joystick movement (0 to 360, 0 North)
ejh23 1:e11018cf2c14 69 float Joystick::get_angle()
ejh23 1:e11018cf2c14 70 {
ejh23 1:e11018cf2c14 71 Polar p = get_polar();
ejh23 1:e11018cf2c14 72 return p.angle;
ejh23 1:e11018cf2c14 73 }
ejh23 1:e11018cf2c14 74
ejh23 1:e11018cf2c14 75 // get raw joystick coordinate in range -1 to 1
ejh23 1:e11018cf2c14 76 // Direction (x,y)
ejh23 1:e11018cf2c14 77 // North (0,1)
ejh23 1:e11018cf2c14 78 // East (1,0)
ejh23 1:e11018cf2c14 79 // South (0,-1)
ejh23 1:e11018cf2c14 80 // West (-1,0)
ejh23 1:e11018cf2c14 81 Vector2D Joystick::get_coord()
ejh23 1:e11018cf2c14 82 {
ejh23 1:e11018cf2c14 83 // read() returns value in range 0.0 to 1.0 so is scaled and centre value
ejh23 1:e11018cf2c14 84 // substracted to get values in the range -1.0 to 1.0
ejh23 1:e11018cf2c14 85 float x = 2.0f*( horiz->read() - _x0 );
ejh23 1:e11018cf2c14 86 float y = 2.0f*( vert->read() - _y0 );
ejh23 1:e11018cf2c14 87
ejh23 1:e11018cf2c14 88 // Note: the x value here is inverted to ensure the positive x is to the
ejh23 1:e11018cf2c14 89 // right. This is simply due to how the potentiometer on the joystick
ejh23 1:e11018cf2c14 90 // I was using was connected up. It could have been corrected in hardware
ejh23 1:e11018cf2c14 91 // by swapping the power supply pins. Instead it is done in software so may
ejh23 1:e11018cf2c14 92 // need to be changed depending on your wiring setup
ejh23 1:e11018cf2c14 93
ejh23 1:e11018cf2c14 94 Vector2D coord = {-x,y};
ejh23 1:e11018cf2c14 95 return coord;
ejh23 1:e11018cf2c14 96 }
ejh23 1:e11018cf2c14 97
ejh23 1:e11018cf2c14 98 // This maps the raw x,y coord onto a circular grid.
ejh23 1:e11018cf2c14 99 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html
ejh23 1:e11018cf2c14 100 Vector2D Joystick::get_mapped_coord()
ejh23 1:e11018cf2c14 101 {
ejh23 1:e11018cf2c14 102 Vector2D coord = get_coord();
ejh23 1:e11018cf2c14 103
ejh23 1:e11018cf2c14 104 // do the transformation
ejh23 1:e11018cf2c14 105 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f);
ejh23 1:e11018cf2c14 106 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f);
ejh23 1:e11018cf2c14 107
ejh23 1:e11018cf2c14 108 Vector2D mapped_coord = {x,y};
ejh23 1:e11018cf2c14 109 return mapped_coord;
ejh23 1:e11018cf2c14 110 }
ejh23 1:e11018cf2c14 111
ejh23 1:e11018cf2c14 112 // this function converts the mapped coordinates into polar form
ejh23 1:e11018cf2c14 113 Polar Joystick::get_polar()
ejh23 1:e11018cf2c14 114 {
ejh23 1:e11018cf2c14 115 // get the mapped coordinate
ejh23 1:e11018cf2c14 116 Vector2D coord = get_mapped_coord();
ejh23 1:e11018cf2c14 117
ejh23 1:e11018cf2c14 118 // at this point, 0 degrees (i.e. x-axis) will be defined to the East.
ejh23 1:e11018cf2c14 119 // We want 0 degrees to correspond to North and increase clockwise to 359
ejh23 1:e11018cf2c14 120 // like a compass heading, so we need to swap the axis and invert y
ejh23 1:e11018cf2c14 121 float x = coord.y;
ejh23 1:e11018cf2c14 122 float y = coord.x;
ejh23 1:e11018cf2c14 123
ejh23 1:e11018cf2c14 124 float mag = sqrt(x*x+y*y); // pythagoras
ejh23 1:e11018cf2c14 125 float angle = RAD2DEG*atan2(y,x);
ejh23 1:e11018cf2c14 126 // angle will be in range -180 to 180, so add 360 to negative angles to
ejh23 1:e11018cf2c14 127 // move to 0 to 360 range
ejh23 1:e11018cf2c14 128 if (angle < 0.0f) {
ejh23 1:e11018cf2c14 129 angle+=360.0f;
ejh23 1:e11018cf2c14 130 }
ejh23 1:e11018cf2c14 131
ejh23 1:e11018cf2c14 132 // the noise on the ADC causes the values of x and y to fluctuate slightly
ejh23 1:e11018cf2c14 133 // around the centred values. This causes the random angle values to get
ejh23 1:e11018cf2c14 134 // calculated when the joystick is centred and untouched. This is also when
ejh23 1:e11018cf2c14 135 // the magnitude is very small, so we can check for a small magnitude and then
ejh23 1:e11018cf2c14 136 // set the angle to -1. This will inform us when the angle is invalid and the
ejh23 1:e11018cf2c14 137 // joystick is centred
ejh23 1:e11018cf2c14 138
ejh23 1:e11018cf2c14 139 if (mag < TOL) {
ejh23 1:e11018cf2c14 140 mag = 0.0f;
ejh23 1:e11018cf2c14 141 angle = -1.0f;
ejh23 1:e11018cf2c14 142 }
ejh23 1:e11018cf2c14 143
ejh23 1:e11018cf2c14 144 Polar p = {mag,angle};
ejh23 1:e11018cf2c14 145 return p;
ejh23 1:e11018cf2c14 146 }
ejh23 1:e11018cf2c14 147
ejh23 1:e11018cf2c14 148 bool Joystick::button_pressed()
ejh23 1:e11018cf2c14 149 {
ejh23 1:e11018cf2c14 150 // ISR must have been triggered
ejh23 1:e11018cf2c14 151 if (_click_flag) {
ejh23 1:e11018cf2c14 152 _click_flag = 0; // clear flag
ejh23 1:e11018cf2c14 153 return true;
ejh23 1:e11018cf2c14 154 } else {
ejh23 1:e11018cf2c14 155 return false;
ejh23 1:e11018cf2c14 156 }
ejh23 1:e11018cf2c14 157 }
ejh23 1:e11018cf2c14 158
ejh23 1:e11018cf2c14 159 void Joystick::click_isr()
ejh23 1:e11018cf2c14 160 {
ejh23 1:e11018cf2c14 161 _click_flag = 1;
ejh23 1:e11018cf2c14 162 }