Library for ELEC2645 Gamepad PCB. University of Leeds
Dependents: Project_MaZe1_copy Labirint Game_Controller_Project 200943373MAZE ... more
Gamepad.cpp@16:3ea3d9714b0c, 2017-03-03 (annotated)
- Committer:
- valavanisalex
- Date:
- Fri Mar 03 13:19:19 2017 +0000
- Revision:
- 16:3ea3d9714b0c
- Parent:
- 14:688bdcbdd3a5
- Child:
- 17:cf1e1ffcf773
Move Gamepad events enum into class. Too class-specific to be defined externally.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:a6288c29b936 | 1 | #include "Gamepad.h" |
eencae | 0:a6288c29b936 | 2 | |
valavanisalex | 13:ef5fc9f58805 | 3 | #include "mbed.h" |
valavanisalex | 13:ef5fc9f58805 | 4 | |
eencae | 1:6d25cd49059b | 5 | //////////// constructor/destructor //////////// |
eencae | 0:a6288c29b936 | 6 | Gamepad::Gamepad() |
valavanisalex | 14:688bdcbdd3a5 | 7 | : |
valavanisalex | 14:688bdcbdd3a5 | 8 | _led1(new PwmOut(PTA1)), |
valavanisalex | 14:688bdcbdd3a5 | 9 | _led2(new PwmOut(PTA2)), |
valavanisalex | 14:688bdcbdd3a5 | 10 | _led3(new PwmOut(PTC2)), |
valavanisalex | 14:688bdcbdd3a5 | 11 | _led4(new PwmOut(PTC3)), |
valavanisalex | 14:688bdcbdd3a5 | 12 | _led5(new PwmOut(PTC4)), |
valavanisalex | 14:688bdcbdd3a5 | 13 | _led6(new PwmOut(PTD3)), |
eencae | 3:964a6d95acdd | 14 | |
valavanisalex | 14:688bdcbdd3a5 | 15 | _button_A(new InterruptIn(PTB9)), |
valavanisalex | 14:688bdcbdd3a5 | 16 | _button_B(new InterruptIn(PTD0)), |
valavanisalex | 14:688bdcbdd3a5 | 17 | _button_X(new InterruptIn(PTC17)), |
valavanisalex | 14:688bdcbdd3a5 | 18 | _button_Y(new InterruptIn(PTC12)), |
valavanisalex | 14:688bdcbdd3a5 | 19 | _button_L(new InterruptIn(PTB18)), |
valavanisalex | 14:688bdcbdd3a5 | 20 | _button_R(new InterruptIn(PTB3)), |
valavanisalex | 14:688bdcbdd3a5 | 21 | _button_back(new InterruptIn(PTB19)), |
valavanisalex | 14:688bdcbdd3a5 | 22 | _button_start(new InterruptIn(PTC5)), |
valavanisalex | 14:688bdcbdd3a5 | 23 | _button_joystick(new InterruptIn(PTC16)), |
valavanisalex | 14:688bdcbdd3a5 | 24 | |
valavanisalex | 14:688bdcbdd3a5 | 25 | _vert(new AnalogIn(PTB10)), |
valavanisalex | 14:688bdcbdd3a5 | 26 | _horiz(new AnalogIn(PTB11)), |
valavanisalex | 14:688bdcbdd3a5 | 27 | |
valavanisalex | 14:688bdcbdd3a5 | 28 | _buzzer(new PwmOut(PTC10)), |
valavanisalex | 14:688bdcbdd3a5 | 29 | _pot(new AnalogIn(PTB2)), |
valavanisalex | 14:688bdcbdd3a5 | 30 | |
valavanisalex | 14:688bdcbdd3a5 | 31 | _timeout(new Timeout()), |
valavanisalex | 14:688bdcbdd3a5 | 32 | |
valavanisalex | 14:688bdcbdd3a5 | 33 | _event_state(0), |
valavanisalex | 14:688bdcbdd3a5 | 34 | |
valavanisalex | 14:688bdcbdd3a5 | 35 | _x0(0), |
valavanisalex | 14:688bdcbdd3a5 | 36 | _y0(0) |
valavanisalex | 14:688bdcbdd3a5 | 37 | {} |
eencae | 0:a6288c29b936 | 38 | |
eencae | 1:6d25cd49059b | 39 | Gamepad::~Gamepad() |
eencae | 1:6d25cd49059b | 40 | { |
eencae | 10:a13d2f9d8a14 | 41 | delete _led1,_led2,_led3,_led4,_led5,_led6; |
valavanisalex | 11:ff86b2ffce01 | 42 | delete _button_A,_button_B,_button_joystick,_vert,_horiz; |
valavanisalex | 11:ff86b2ffce01 | 43 | delete _button_X,_button_Y,_button_back,_button_start; |
valavanisalex | 11:ff86b2ffce01 | 44 | delete _button_L,_button_R, _buzzer, _pot, _timeout; |
eencae | 1:6d25cd49059b | 45 | } |
eencae | 1:6d25cd49059b | 46 | |
eencae | 1:6d25cd49059b | 47 | ///////////////// public methods ///////////////// |
eencae | 1:6d25cd49059b | 48 | |
eencae | 0:a6288c29b936 | 49 | void Gamepad::init() |
eencae | 0:a6288c29b936 | 50 | { |
eencae | 1:6d25cd49059b | 51 | leds_off(); |
eencae | 7:019671f7bd83 | 52 | init_buttons(); |
valavanisalex | 14:688bdcbdd3a5 | 53 | |
eencae | 3:964a6d95acdd | 54 | // read centred values of joystick |
valavanisalex | 11:ff86b2ffce01 | 55 | _x0 = _horiz->read(); |
valavanisalex | 11:ff86b2ffce01 | 56 | _y0 = _vert->read(); |
eencae | 1:6d25cd49059b | 57 | |
eencae | 1:6d25cd49059b | 58 | // clear all flags |
valavanisalex | 12:1b0b6355da4f | 59 | _event_state = 0; |
eencae | 0:a6288c29b936 | 60 | } |
eencae | 0:a6288c29b936 | 61 | |
eencae | 0:a6288c29b936 | 62 | void Gamepad::leds_off() |
eencae | 0:a6288c29b936 | 63 | { |
eencae | 10:a13d2f9d8a14 | 64 | leds(0.0); |
eencae | 0:a6288c29b936 | 65 | } |
eencae | 0:a6288c29b936 | 66 | |
eencae | 0:a6288c29b936 | 67 | void Gamepad::leds_on() |
eencae | 0:a6288c29b936 | 68 | { |
eencae | 10:a13d2f9d8a14 | 69 | leds(1.0); |
eencae | 1:6d25cd49059b | 70 | } |
eencae | 1:6d25cd49059b | 71 | |
valavanisalex | 11:ff86b2ffce01 | 72 | void Gamepad::leds(float val) const |
eencae | 1:6d25cd49059b | 73 | { |
eencae | 1:6d25cd49059b | 74 | if (val < 0.0f) { |
eencae | 1:6d25cd49059b | 75 | val = 0.0f; |
eencae | 1:6d25cd49059b | 76 | } |
eencae | 1:6d25cd49059b | 77 | if (val > 1.0f) { |
eencae | 1:6d25cd49059b | 78 | val = 1.0f; |
eencae | 1:6d25cd49059b | 79 | } |
eencae | 1:6d25cd49059b | 80 | |
eencae | 1:6d25cd49059b | 81 | // leds are active-low, so subtract from 1.0 |
eencae | 1:6d25cd49059b | 82 | // 0.0 corresponds to fully-off, 1.0 to fully-on |
eencae | 1:6d25cd49059b | 83 | val = 1.0f - val; |
eencae | 1:6d25cd49059b | 84 | |
eencae | 10:a13d2f9d8a14 | 85 | _led1->write(val); |
eencae | 10:a13d2f9d8a14 | 86 | _led2->write(val); |
eencae | 10:a13d2f9d8a14 | 87 | _led3->write(val); |
eencae | 10:a13d2f9d8a14 | 88 | _led4->write(val); |
eencae | 10:a13d2f9d8a14 | 89 | _led5->write(val); |
eencae | 10:a13d2f9d8a14 | 90 | _led6->write(val); |
eencae | 9:893189072e89 | 91 | } |
eencae | 9:893189072e89 | 92 | |
valavanisalex | 14:688bdcbdd3a5 | 93 | void Gamepad::led1(float val) |
valavanisalex | 14:688bdcbdd3a5 | 94 | { |
eencae | 10:a13d2f9d8a14 | 95 | _led1->write(1.0f-val); // active-low so subtract from 1 |
eencae | 9:893189072e89 | 96 | } |
valavanisalex | 14:688bdcbdd3a5 | 97 | void Gamepad::led2(float val) |
valavanisalex | 14:688bdcbdd3a5 | 98 | { |
eencae | 10:a13d2f9d8a14 | 99 | _led2->write(1.0f-val); // active-low so subtract from 1 |
eencae | 9:893189072e89 | 100 | } |
valavanisalex | 14:688bdcbdd3a5 | 101 | void Gamepad::led3(float val) |
valavanisalex | 14:688bdcbdd3a5 | 102 | { |
eencae | 10:a13d2f9d8a14 | 103 | _led3->write(1.0f-val); // active-low so subtract from 1 |
eencae | 9:893189072e89 | 104 | } |
valavanisalex | 14:688bdcbdd3a5 | 105 | void Gamepad::led4(float val) |
valavanisalex | 14:688bdcbdd3a5 | 106 | { |
eencae | 10:a13d2f9d8a14 | 107 | _led4->write(1.0f-val); // active-low so subtract from 1 |
eencae | 9:893189072e89 | 108 | } |
valavanisalex | 14:688bdcbdd3a5 | 109 | void Gamepad::led5(float val) |
valavanisalex | 14:688bdcbdd3a5 | 110 | { |
eencae | 10:a13d2f9d8a14 | 111 | _led5->write(1.0f-val); // active-low so subtract from 1 |
eencae | 9:893189072e89 | 112 | } |
valavanisalex | 14:688bdcbdd3a5 | 113 | void Gamepad::led6(float val) |
valavanisalex | 14:688bdcbdd3a5 | 114 | { |
eencae | 10:a13d2f9d8a14 | 115 | _led6->write(1.0f-val); // active-low so subtract from 1 |
eencae | 1:6d25cd49059b | 116 | } |
eencae | 1:6d25cd49059b | 117 | |
valavanisalex | 11:ff86b2ffce01 | 118 | float Gamepad::read_pot() const |
eencae | 1:6d25cd49059b | 119 | { |
valavanisalex | 11:ff86b2ffce01 | 120 | return _pot->read(); |
eencae | 1:6d25cd49059b | 121 | } |
eencae | 1:6d25cd49059b | 122 | |
eencae | 1:6d25cd49059b | 123 | void Gamepad::tone(float frequency, float duration) |
eencae | 1:6d25cd49059b | 124 | { |
valavanisalex | 11:ff86b2ffce01 | 125 | _buzzer->period(1.0f/frequency); |
valavanisalex | 11:ff86b2ffce01 | 126 | _buzzer->write(0.5); // 50% duty cycle - square wave |
valavanisalex | 11:ff86b2ffce01 | 127 | _timeout->attach(callback(this, &Gamepad::tone_off), duration ); |
eencae | 1:6d25cd49059b | 128 | } |
eencae | 1:6d25cd49059b | 129 | |
valavanisalex | 12:1b0b6355da4f | 130 | bool Gamepad::check_event(GamepadEvent const id) |
eencae | 1:6d25cd49059b | 131 | { |
valavanisalex | 12:1b0b6355da4f | 132 | // Check whether event flag is set |
valavanisalex | 12:1b0b6355da4f | 133 | if (_event_state[id]) { |
valavanisalex | 12:1b0b6355da4f | 134 | _event_state.reset(id); // clear flag |
eencae | 3:964a6d95acdd | 135 | return true; |
eencae | 3:964a6d95acdd | 136 | } else { |
eencae | 3:964a6d95acdd | 137 | return false; |
eencae | 3:964a6d95acdd | 138 | } |
eencae | 3:964a6d95acdd | 139 | } |
eencae | 3:964a6d95acdd | 140 | |
eencae | 3:964a6d95acdd | 141 | // this method gets the magnitude of the joystick movement |
eencae | 3:964a6d95acdd | 142 | float Gamepad::get_mag() |
eencae | 3:964a6d95acdd | 143 | { |
eencae | 3:964a6d95acdd | 144 | Polar p = get_polar(); |
eencae | 3:964a6d95acdd | 145 | return p.mag; |
eencae | 3:964a6d95acdd | 146 | } |
eencae | 3:964a6d95acdd | 147 | |
eencae | 3:964a6d95acdd | 148 | // this method gets the angle of joystick movement (0 to 360, 0 North) |
eencae | 3:964a6d95acdd | 149 | float Gamepad::get_angle() |
eencae | 3:964a6d95acdd | 150 | { |
eencae | 3:964a6d95acdd | 151 | Polar p = get_polar(); |
eencae | 3:964a6d95acdd | 152 | return p.angle; |
eencae | 3:964a6d95acdd | 153 | } |
eencae | 3:964a6d95acdd | 154 | |
eencae | 4:bafb7f483e93 | 155 | Direction Gamepad::get_direction() |
eencae | 3:964a6d95acdd | 156 | { |
eencae | 3:964a6d95acdd | 157 | float angle = get_angle(); // 0 to 360, -1 for centred |
eencae | 3:964a6d95acdd | 158 | |
eencae | 3:964a6d95acdd | 159 | Direction d; |
eencae | 3:964a6d95acdd | 160 | // partition 360 into segments and check which segment the angle is in |
eencae | 3:964a6d95acdd | 161 | if (angle < 0.0f) { |
eencae | 3:964a6d95acdd | 162 | d = CENTRE; // check for -1.0 angle |
eencae | 3:964a6d95acdd | 163 | } else if (angle < 22.5f) { // then keep going in 45 degree increments |
eencae | 3:964a6d95acdd | 164 | d = N; |
eencae | 3:964a6d95acdd | 165 | } else if (angle < 67.5f) { |
eencae | 3:964a6d95acdd | 166 | d = NE; |
eencae | 3:964a6d95acdd | 167 | } else if (angle < 112.5f) { |
eencae | 3:964a6d95acdd | 168 | d = E; |
eencae | 3:964a6d95acdd | 169 | } else if (angle < 157.5f) { |
eencae | 3:964a6d95acdd | 170 | d = SE; |
eencae | 3:964a6d95acdd | 171 | } else if (angle < 202.5f) { |
eencae | 3:964a6d95acdd | 172 | d = S; |
eencae | 3:964a6d95acdd | 173 | } else if (angle < 247.5f) { |
eencae | 3:964a6d95acdd | 174 | d = SW; |
eencae | 3:964a6d95acdd | 175 | } else if (angle < 292.5f) { |
eencae | 3:964a6d95acdd | 176 | d = W; |
eencae | 3:964a6d95acdd | 177 | } else if (angle < 337.5f) { |
eencae | 3:964a6d95acdd | 178 | d = NW; |
eencae | 3:964a6d95acdd | 179 | } else { |
eencae | 3:964a6d95acdd | 180 | d = N; |
eencae | 3:964a6d95acdd | 181 | } |
eencae | 3:964a6d95acdd | 182 | |
eencae | 3:964a6d95acdd | 183 | return d; |
eencae | 3:964a6d95acdd | 184 | } |
eencae | 3:964a6d95acdd | 185 | |
eencae | 1:6d25cd49059b | 186 | ///////////////////// private methods //////////////////////// |
eencae | 1:6d25cd49059b | 187 | |
eencae | 1:6d25cd49059b | 188 | void Gamepad::tone_off() |
eencae | 1:6d25cd49059b | 189 | { |
valavanisalex | 11:ff86b2ffce01 | 190 | _buzzer->write(0.0); |
eencae | 1:6d25cd49059b | 191 | } |
eencae | 1:6d25cd49059b | 192 | |
eencae | 1:6d25cd49059b | 193 | void Gamepad::init_buttons() |
eencae | 1:6d25cd49059b | 194 | { |
eencae | 1:6d25cd49059b | 195 | // turn on pull-downs as other side of button is connected to 3V3 |
eencae | 1:6d25cd49059b | 196 | // button is 0 when not pressed and 1 when pressed |
valavanisalex | 11:ff86b2ffce01 | 197 | _button_A->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 198 | _button_B->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 199 | _button_X->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 200 | _button_Y->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 201 | _button_back->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 202 | _button_start->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 203 | _button_L->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 204 | _button_R->mode(PullDown); |
valavanisalex | 11:ff86b2ffce01 | 205 | _button_joystick->mode(PullDown); |
eencae | 1:6d25cd49059b | 206 | // therefore setup rising edge interrupts |
valavanisalex | 11:ff86b2ffce01 | 207 | _button_A->rise(callback(this,&Gamepad::a_isr)); |
valavanisalex | 11:ff86b2ffce01 | 208 | _button_B->rise(callback(this,&Gamepad::b_isr)); |
valavanisalex | 11:ff86b2ffce01 | 209 | _button_X->rise(callback(this,&Gamepad::x_isr)); |
valavanisalex | 11:ff86b2ffce01 | 210 | _button_Y->rise(callback(this,&Gamepad::y_isr)); |
valavanisalex | 11:ff86b2ffce01 | 211 | _button_L->rise(callback(this,&Gamepad::l_isr)); |
valavanisalex | 11:ff86b2ffce01 | 212 | _button_R->rise(callback(this,&Gamepad::r_isr)); |
valavanisalex | 11:ff86b2ffce01 | 213 | _button_start->rise(callback(this,&Gamepad::start_isr)); |
valavanisalex | 11:ff86b2ffce01 | 214 | _button_back->rise(callback(this,&Gamepad::back_isr)); |
valavanisalex | 11:ff86b2ffce01 | 215 | _button_joystick->rise(callback(this,&Gamepad::joy_isr)); |
eencae | 1:6d25cd49059b | 216 | } |
eencae | 1:6d25cd49059b | 217 | |
eencae | 1:6d25cd49059b | 218 | // button interrupts ISRs |
valavanisalex | 12:1b0b6355da4f | 219 | // Each of these simply sets the appropriate event bit in the _event_state |
valavanisalex | 12:1b0b6355da4f | 220 | // variable |
eencae | 1:6d25cd49059b | 221 | void Gamepad::a_isr() |
eencae | 1:6d25cd49059b | 222 | { |
valavanisalex | 16:3ea3d9714b0c | 223 | _event_state.set(A_PRESSED); |
eencae | 1:6d25cd49059b | 224 | } |
eencae | 1:6d25cd49059b | 225 | void Gamepad::b_isr() |
eencae | 1:6d25cd49059b | 226 | { |
valavanisalex | 16:3ea3d9714b0c | 227 | _event_state.set(B_PRESSED); |
eencae | 1:6d25cd49059b | 228 | } |
eencae | 1:6d25cd49059b | 229 | void Gamepad::x_isr() |
eencae | 1:6d25cd49059b | 230 | { |
valavanisalex | 16:3ea3d9714b0c | 231 | _event_state.set(X_PRESSED); |
eencae | 1:6d25cd49059b | 232 | } |
eencae | 1:6d25cd49059b | 233 | void Gamepad::y_isr() |
eencae | 1:6d25cd49059b | 234 | { |
valavanisalex | 16:3ea3d9714b0c | 235 | _event_state.set(Y_PRESSED); |
eencae | 1:6d25cd49059b | 236 | } |
eencae | 1:6d25cd49059b | 237 | void Gamepad::l_isr() |
eencae | 1:6d25cd49059b | 238 | { |
valavanisalex | 16:3ea3d9714b0c | 239 | _event_state.set(L_PRESSED); |
eencae | 1:6d25cd49059b | 240 | } |
eencae | 1:6d25cd49059b | 241 | void Gamepad::r_isr() |
eencae | 1:6d25cd49059b | 242 | { |
valavanisalex | 16:3ea3d9714b0c | 243 | _event_state.set(R_PRESSED); |
eencae | 1:6d25cd49059b | 244 | } |
eencae | 1:6d25cd49059b | 245 | void Gamepad::back_isr() |
eencae | 1:6d25cd49059b | 246 | { |
valavanisalex | 16:3ea3d9714b0c | 247 | _event_state.set(BACK_PRESSED); |
eencae | 1:6d25cd49059b | 248 | } |
eencae | 1:6d25cd49059b | 249 | void Gamepad::start_isr() |
eencae | 1:6d25cd49059b | 250 | { |
valavanisalex | 16:3ea3d9714b0c | 251 | _event_state.set(START_PRESSED); |
eencae | 3:964a6d95acdd | 252 | } |
eencae | 3:964a6d95acdd | 253 | void Gamepad::joy_isr() |
eencae | 3:964a6d95acdd | 254 | { |
valavanisalex | 16:3ea3d9714b0c | 255 | _event_state.set(JOY_PRESSED); |
eencae | 3:964a6d95acdd | 256 | } |
eencae | 3:964a6d95acdd | 257 | |
eencae | 3:964a6d95acdd | 258 | // get raw joystick coordinate in range -1 to 1 |
eencae | 3:964a6d95acdd | 259 | // Direction (x,y) |
eencae | 3:964a6d95acdd | 260 | // North (0,1) |
eencae | 3:964a6d95acdd | 261 | // East (1,0) |
eencae | 3:964a6d95acdd | 262 | // South (0,-1) |
eencae | 3:964a6d95acdd | 263 | // West (-1,0) |
eencae | 3:964a6d95acdd | 264 | Vector2D Gamepad::get_coord() |
eencae | 3:964a6d95acdd | 265 | { |
eencae | 3:964a6d95acdd | 266 | // read() returns value in range 0.0 to 1.0 so is scaled and centre value |
eencae | 3:964a6d95acdd | 267 | // substracted to get values in the range -1.0 to 1.0 |
valavanisalex | 11:ff86b2ffce01 | 268 | float x = 2.0f*( _horiz->read() - _x0 ); |
valavanisalex | 11:ff86b2ffce01 | 269 | float y = 2.0f*( _vert->read() - _y0 ); |
eencae | 3:964a6d95acdd | 270 | |
eencae | 3:964a6d95acdd | 271 | // Note: the x value here is inverted to ensure the positive x is to the |
eencae | 3:964a6d95acdd | 272 | // right. This is simply due to how the potentiometer on the joystick |
eencae | 3:964a6d95acdd | 273 | // I was using was connected up. It could have been corrected in hardware |
eencae | 3:964a6d95acdd | 274 | // by swapping the power supply pins. Instead it is done in software so may |
eencae | 3:964a6d95acdd | 275 | // need to be changed depending on your wiring setup |
eencae | 3:964a6d95acdd | 276 | |
eencae | 3:964a6d95acdd | 277 | Vector2D coord = {-x,y}; |
eencae | 3:964a6d95acdd | 278 | return coord; |
eencae | 3:964a6d95acdd | 279 | } |
eencae | 3:964a6d95acdd | 280 | |
eencae | 3:964a6d95acdd | 281 | // This maps the raw x,y coord onto a circular grid. |
eencae | 3:964a6d95acdd | 282 | // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html |
eencae | 3:964a6d95acdd | 283 | Vector2D Gamepad::get_mapped_coord() |
eencae | 3:964a6d95acdd | 284 | { |
eencae | 3:964a6d95acdd | 285 | Vector2D coord = get_coord(); |
eencae | 3:964a6d95acdd | 286 | |
eencae | 3:964a6d95acdd | 287 | // do the transformation |
eencae | 3:964a6d95acdd | 288 | float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); |
eencae | 3:964a6d95acdd | 289 | float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); |
eencae | 3:964a6d95acdd | 290 | |
eencae | 3:964a6d95acdd | 291 | Vector2D mapped_coord = {x,y}; |
eencae | 3:964a6d95acdd | 292 | return mapped_coord; |
eencae | 3:964a6d95acdd | 293 | } |
eencae | 3:964a6d95acdd | 294 | |
eencae | 3:964a6d95acdd | 295 | // this function converts the mapped coordinates into polar form |
eencae | 3:964a6d95acdd | 296 | Polar Gamepad::get_polar() |
eencae | 3:964a6d95acdd | 297 | { |
eencae | 3:964a6d95acdd | 298 | // get the mapped coordinate |
eencae | 3:964a6d95acdd | 299 | Vector2D coord = get_mapped_coord(); |
eencae | 3:964a6d95acdd | 300 | |
eencae | 3:964a6d95acdd | 301 | // at this point, 0 degrees (i.e. x-axis) will be defined to the East. |
eencae | 3:964a6d95acdd | 302 | // We want 0 degrees to correspond to North and increase clockwise to 359 |
eencae | 3:964a6d95acdd | 303 | // like a compass heading, so we need to swap the axis and invert y |
eencae | 3:964a6d95acdd | 304 | float x = coord.y; |
eencae | 3:964a6d95acdd | 305 | float y = coord.x; |
eencae | 3:964a6d95acdd | 306 | |
eencae | 3:964a6d95acdd | 307 | float mag = sqrt(x*x+y*y); // pythagoras |
eencae | 3:964a6d95acdd | 308 | float angle = RAD2DEG*atan2(y,x); |
eencae | 3:964a6d95acdd | 309 | // angle will be in range -180 to 180, so add 360 to negative angles to |
eencae | 3:964a6d95acdd | 310 | // move to 0 to 360 range |
eencae | 3:964a6d95acdd | 311 | if (angle < 0.0f) { |
eencae | 3:964a6d95acdd | 312 | angle+=360.0f; |
eencae | 3:964a6d95acdd | 313 | } |
eencae | 3:964a6d95acdd | 314 | |
eencae | 3:964a6d95acdd | 315 | // the noise on the ADC causes the values of x and y to fluctuate slightly |
eencae | 3:964a6d95acdd | 316 | // around the centred values. This causes the random angle values to get |
eencae | 3:964a6d95acdd | 317 | // calculated when the joystick is centred and untouched. This is also when |
eencae | 3:964a6d95acdd | 318 | // the magnitude is very small, so we can check for a small magnitude and then |
eencae | 3:964a6d95acdd | 319 | // set the angle to -1. This will inform us when the angle is invalid and the |
eencae | 3:964a6d95acdd | 320 | // joystick is centred |
eencae | 3:964a6d95acdd | 321 | |
eencae | 3:964a6d95acdd | 322 | if (mag < TOL) { |
eencae | 3:964a6d95acdd | 323 | mag = 0.0f; |
eencae | 3:964a6d95acdd | 324 | angle = -1.0f; |
eencae | 3:964a6d95acdd | 325 | } |
eencae | 3:964a6d95acdd | 326 | |
eencae | 3:964a6d95acdd | 327 | Polar p = {mag,angle}; |
eencae | 3:964a6d95acdd | 328 | return p; |
eencae | 1:6d25cd49059b | 329 | } |