![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Owen Cavender 201159294
Dependencies: mbed
Gamepad2/Gamepad.cpp@2:ffbfd3f53ee2, 2020-05-30 (annotated)
- Committer:
- el17oc
- Date:
- Sat May 30 06:12:09 2020 +0000
- Revision:
- 2:ffbfd3f53ee2
- Parent:
- 1:897160a1a3ae
Final Submission. I have read and agreed with Statement of Academic Integrity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17oc | 1:897160a1a3ae | 1 | #include "Gamepad.h" |
el17oc | 1:897160a1a3ae | 2 | |
el17oc | 1:897160a1a3ae | 3 | #include "mbed.h" |
el17oc | 1:897160a1a3ae | 4 | |
el17oc | 1:897160a1a3ae | 5 | //////////// constructor/destructor //////////// |
el17oc | 1:897160a1a3ae | 6 | Gamepad::Gamepad() |
el17oc | 1:897160a1a3ae | 7 | : |
el17oc | 1:897160a1a3ae | 8 | _led1(new PwmOut(PTA2)), |
el17oc | 1:897160a1a3ae | 9 | _led2(new PwmOut(PTC2)), |
el17oc | 1:897160a1a3ae | 10 | _led3(new PwmOut(PTC3)), |
el17oc | 1:897160a1a3ae | 11 | _led4(new PwmOut(PTA1)), |
el17oc | 1:897160a1a3ae | 12 | _led5(new PwmOut(PTC10)), |
el17oc | 1:897160a1a3ae | 13 | _led6(new PwmOut(PTC11)), |
el17oc | 1:897160a1a3ae | 14 | |
el17oc | 1:897160a1a3ae | 15 | _button_A(new InterruptIn(PTC7)), |
el17oc | 1:897160a1a3ae | 16 | _button_B(new InterruptIn(PTC9)), |
el17oc | 1:897160a1a3ae | 17 | _button_X(new InterruptIn(PTC5)), |
el17oc | 1:897160a1a3ae | 18 | _button_Y(new InterruptIn(PTC0)), |
el17oc | 1:897160a1a3ae | 19 | _button_start(new InterruptIn(PTC8)), |
el17oc | 1:897160a1a3ae | 20 | |
el17oc | 1:897160a1a3ae | 21 | _vert(new AnalogIn(PTB11)), |
el17oc | 1:897160a1a3ae | 22 | _horiz(new AnalogIn(PTB10)), |
el17oc | 1:897160a1a3ae | 23 | |
el17oc | 1:897160a1a3ae | 24 | _pot1(new AnalogIn(PTB2)), |
el17oc | 1:897160a1a3ae | 25 | _pot2(new AnalogIn(PTB3)), |
el17oc | 1:897160a1a3ae | 26 | |
el17oc | 1:897160a1a3ae | 27 | dac(new AnalogOut(DAC0_OUT)), |
el17oc | 1:897160a1a3ae | 28 | ticker(new Ticker), |
el17oc | 1:897160a1a3ae | 29 | timeout(new Timeout), |
el17oc | 1:897160a1a3ae | 30 | note_timeout(new Timeout), |
el17oc | 1:897160a1a3ae | 31 | |
el17oc | 1:897160a1a3ae | 32 | _x0(0), |
el17oc | 1:897160a1a3ae | 33 | _y0(0) |
el17oc | 1:897160a1a3ae | 34 | {} |
el17oc | 1:897160a1a3ae | 35 | |
el17oc | 1:897160a1a3ae | 36 | |
el17oc | 1:897160a1a3ae | 37 | ///////////////// public methods ///////////////// |
el17oc | 1:897160a1a3ae | 38 | |
el17oc | 1:897160a1a3ae | 39 | void Gamepad::init() |
el17oc | 1:897160a1a3ae | 40 | { |
el17oc | 1:897160a1a3ae | 41 | leds_off(); |
el17oc | 1:897160a1a3ae | 42 | |
el17oc | 1:897160a1a3ae | 43 | // read centred values of joystick |
el17oc | 1:897160a1a3ae | 44 | _x0 = _horiz->read(); |
el17oc | 1:897160a1a3ae | 45 | _y0 = _vert->read(); |
el17oc | 1:897160a1a3ae | 46 | |
el17oc | 1:897160a1a3ae | 47 | // Set all buttons to PullUp |
el17oc | 1:897160a1a3ae | 48 | _button_A->mode(PullUp); |
el17oc | 1:897160a1a3ae | 49 | _button_B->mode(PullUp); |
el17oc | 1:897160a1a3ae | 50 | _button_X->mode(PullUp); |
el17oc | 1:897160a1a3ae | 51 | _button_Y->mode(PullUp); |
el17oc | 1:897160a1a3ae | 52 | _button_start->mode(PullUp); |
el17oc | 1:897160a1a3ae | 53 | |
el17oc | 1:897160a1a3ae | 54 | // Set up interrupts for the fall of buttons |
el17oc | 1:897160a1a3ae | 55 | _button_A->fall(callback(this,&Gamepad::A_fall_interrupt)); |
el17oc | 1:897160a1a3ae | 56 | _button_B->fall(callback(this,&Gamepad::B_fall_interrupt)); |
el17oc | 1:897160a1a3ae | 57 | _button_X->fall(callback(this,&Gamepad::X_fall_interrupt)); |
el17oc | 1:897160a1a3ae | 58 | _button_Y->fall(callback(this,&Gamepad::Y_fall_interrupt)); |
el17oc | 1:897160a1a3ae | 59 | _button_start->fall(callback(this,&Gamepad::start_fall_interrupt)); |
el17oc | 1:897160a1a3ae | 60 | |
el17oc | 1:897160a1a3ae | 61 | // initalise button flags |
el17oc | 1:897160a1a3ae | 62 | reset_buttons(); |
el17oc | 1:897160a1a3ae | 63 | |
el17oc | 1:897160a1a3ae | 64 | // number of samples |
el17oc | 1:897160a1a3ae | 65 | _n = 16; |
el17oc | 1:897160a1a3ae | 66 | _sample_array = new float[_n]; |
el17oc | 1:897160a1a3ae | 67 | |
el17oc | 1:897160a1a3ae | 68 | // create sample array for one period between 0.0 and 1.0 |
el17oc | 1:897160a1a3ae | 69 | for (int i = 0; i < _n ; i++) { |
el17oc | 1:897160a1a3ae | 70 | _sample_array[i] = 0.5f + 0.5f*sin(i*2*PI/_n); |
el17oc | 1:897160a1a3ae | 71 | //printf("y[%i] = %f\n",i,_sample_array[i]); |
el17oc | 1:897160a1a3ae | 72 | } |
el17oc | 1:897160a1a3ae | 73 | |
el17oc | 1:897160a1a3ae | 74 | } |
el17oc | 1:897160a1a3ae | 75 | |
el17oc | 1:897160a1a3ae | 76 | void Gamepad::leds_off() |
el17oc | 1:897160a1a3ae | 77 | { |
el17oc | 1:897160a1a3ae | 78 | leds(0.0); |
el17oc | 1:897160a1a3ae | 79 | } |
el17oc | 1:897160a1a3ae | 80 | |
el17oc | 1:897160a1a3ae | 81 | void Gamepad::leds_on() |
el17oc | 1:897160a1a3ae | 82 | { |
el17oc | 1:897160a1a3ae | 83 | leds(1.0); |
el17oc | 1:897160a1a3ae | 84 | } |
el17oc | 1:897160a1a3ae | 85 | |
el17oc | 1:897160a1a3ae | 86 | void Gamepad::leds(float val) const |
el17oc | 1:897160a1a3ae | 87 | { |
el17oc | 1:897160a1a3ae | 88 | if (val < 0.0f) { |
el17oc | 1:897160a1a3ae | 89 | val = 0.0f; |
el17oc | 1:897160a1a3ae | 90 | } |
el17oc | 1:897160a1a3ae | 91 | if (val > 1.0f) { |
el17oc | 1:897160a1a3ae | 92 | val = 1.0f; |
el17oc | 1:897160a1a3ae | 93 | } |
el17oc | 1:897160a1a3ae | 94 | |
el17oc | 1:897160a1a3ae | 95 | // leds are active-low, so subtract from 1.0 |
el17oc | 1:897160a1a3ae | 96 | // 0.0 corresponds to fully-off, 1.0 to fully-on |
el17oc | 1:897160a1a3ae | 97 | val = 1.0f - val; |
el17oc | 1:897160a1a3ae | 98 | |
el17oc | 1:897160a1a3ae | 99 | _led1->write(val); |
el17oc | 1:897160a1a3ae | 100 | _led2->write(val); |
el17oc | 1:897160a1a3ae | 101 | _led3->write(val); |
el17oc | 1:897160a1a3ae | 102 | _led4->write(val); |
el17oc | 1:897160a1a3ae | 103 | _led5->write(val); |
el17oc | 1:897160a1a3ae | 104 | _led6->write(val); |
el17oc | 1:897160a1a3ae | 105 | } |
el17oc | 1:897160a1a3ae | 106 | |
el17oc | 1:897160a1a3ae | 107 | void Gamepad::led(int n,float val) const |
el17oc | 1:897160a1a3ae | 108 | { |
el17oc | 1:897160a1a3ae | 109 | // ensure they are within valid range |
el17oc | 1:897160a1a3ae | 110 | if (val < 0.0f) { |
el17oc | 1:897160a1a3ae | 111 | val = 0.0f; |
el17oc | 1:897160a1a3ae | 112 | } |
el17oc | 1:897160a1a3ae | 113 | if (val > 1.0f) { |
el17oc | 1:897160a1a3ae | 114 | val = 1.0f; |
el17oc | 1:897160a1a3ae | 115 | } |
el17oc | 1:897160a1a3ae | 116 | |
el17oc | 1:897160a1a3ae | 117 | switch (n) { |
el17oc | 1:897160a1a3ae | 118 | |
el17oc | 1:897160a1a3ae | 119 | // check for valid LED number and set value |
el17oc | 1:897160a1a3ae | 120 | |
el17oc | 1:897160a1a3ae | 121 | case 1: |
el17oc | 1:897160a1a3ae | 122 | _led1->write(1.0f-val); // active-low so subtract from 1 |
el17oc | 1:897160a1a3ae | 123 | break; |
el17oc | 1:897160a1a3ae | 124 | case 2: |
el17oc | 1:897160a1a3ae | 125 | _led2->write(1.0f-val); // active-low so subtract from 1 |
el17oc | 1:897160a1a3ae | 126 | break; |
el17oc | 1:897160a1a3ae | 127 | case 3: |
el17oc | 1:897160a1a3ae | 128 | _led3->write(1.0f-val); // active-low so subtract from 1 |
el17oc | 1:897160a1a3ae | 129 | break; |
el17oc | 1:897160a1a3ae | 130 | case 4: |
el17oc | 1:897160a1a3ae | 131 | _led4->write(1.0f-val); // active-low so subtract from 1 |
el17oc | 1:897160a1a3ae | 132 | break; |
el17oc | 1:897160a1a3ae | 133 | case 5: |
el17oc | 1:897160a1a3ae | 134 | _led5->write(1.0f-val); // active-low so subtract from 1 |
el17oc | 1:897160a1a3ae | 135 | break; |
el17oc | 1:897160a1a3ae | 136 | case 6: |
el17oc | 1:897160a1a3ae | 137 | _led6->write(1.0f-val); // active-low so subtract from 1 |
el17oc | 1:897160a1a3ae | 138 | break; |
el17oc | 1:897160a1a3ae | 139 | |
el17oc | 1:897160a1a3ae | 140 | } |
el17oc | 1:897160a1a3ae | 141 | } |
el17oc | 1:897160a1a3ae | 142 | |
el17oc | 1:897160a1a3ae | 143 | float Gamepad::read_pot1() const |
el17oc | 1:897160a1a3ae | 144 | { |
el17oc | 1:897160a1a3ae | 145 | return _pot1->read(); |
el17oc | 1:897160a1a3ae | 146 | } |
el17oc | 1:897160a1a3ae | 147 | |
el17oc | 1:897160a1a3ae | 148 | float Gamepad::read_pot2() const |
el17oc | 1:897160a1a3ae | 149 | { |
el17oc | 1:897160a1a3ae | 150 | return _pot2->read(); |
el17oc | 1:897160a1a3ae | 151 | } |
el17oc | 1:897160a1a3ae | 152 | |
el17oc | 1:897160a1a3ae | 153 | |
el17oc | 1:897160a1a3ae | 154 | // this method gets the magnitude of the joystick movement |
el17oc | 1:897160a1a3ae | 155 | float Gamepad::get_mag() |
el17oc | 1:897160a1a3ae | 156 | { |
el17oc | 1:897160a1a3ae | 157 | Polar p = get_polar(); |
el17oc | 1:897160a1a3ae | 158 | return p.mag; |
el17oc | 1:897160a1a3ae | 159 | } |
el17oc | 1:897160a1a3ae | 160 | |
el17oc | 1:897160a1a3ae | 161 | // this method gets the angle of joystick movement (0 to 360, 0 North) |
el17oc | 1:897160a1a3ae | 162 | float Gamepad::get_angle() |
el17oc | 1:897160a1a3ae | 163 | { |
el17oc | 1:897160a1a3ae | 164 | Polar p = get_polar(); |
el17oc | 1:897160a1a3ae | 165 | return p.angle; |
el17oc | 1:897160a1a3ae | 166 | } |
el17oc | 1:897160a1a3ae | 167 | |
el17oc | 1:897160a1a3ae | 168 | Direction Gamepad::get_direction() |
el17oc | 1:897160a1a3ae | 169 | { |
el17oc | 1:897160a1a3ae | 170 | float angle = get_angle(); // 0 to 360, -1 for centred |
el17oc | 1:897160a1a3ae | 171 | |
el17oc | 1:897160a1a3ae | 172 | Direction d; |
el17oc | 1:897160a1a3ae | 173 | // partition 360 into segments and check which segment the angle is in |
el17oc | 1:897160a1a3ae | 174 | if (angle < 0.0f) { |
el17oc | 1:897160a1a3ae | 175 | d = CENTRE; // check for -1.0 angle |
el17oc | 1:897160a1a3ae | 176 | } else if (angle < 22.5f) { // then keep going in 45 degree increments |
el17oc | 1:897160a1a3ae | 177 | d = N; |
el17oc | 1:897160a1a3ae | 178 | } else if (angle < 67.5f) { |
el17oc | 1:897160a1a3ae | 179 | d = NE; |
el17oc | 1:897160a1a3ae | 180 | } else if (angle < 112.5f) { |
el17oc | 1:897160a1a3ae | 181 | d = E; |
el17oc | 1:897160a1a3ae | 182 | } else if (angle < 157.5f) { |
el17oc | 1:897160a1a3ae | 183 | d = SE; |
el17oc | 1:897160a1a3ae | 184 | } else if (angle < 202.5f) { |
el17oc | 1:897160a1a3ae | 185 | d = S; |
el17oc | 1:897160a1a3ae | 186 | } else if (angle < 247.5f) { |
el17oc | 1:897160a1a3ae | 187 | d = SW; |
el17oc | 1:897160a1a3ae | 188 | } else if (angle < 292.5f) { |
el17oc | 1:897160a1a3ae | 189 | d = W; |
el17oc | 1:897160a1a3ae | 190 | } else if (angle < 337.5f) { |
el17oc | 1:897160a1a3ae | 191 | d = NW; |
el17oc | 1:897160a1a3ae | 192 | } else { |
el17oc | 1:897160a1a3ae | 193 | d = N; |
el17oc | 1:897160a1a3ae | 194 | } |
el17oc | 1:897160a1a3ae | 195 | |
el17oc | 1:897160a1a3ae | 196 | return d; |
el17oc | 1:897160a1a3ae | 197 | } |
el17oc | 1:897160a1a3ae | 198 | |
el17oc | 1:897160a1a3ae | 199 | void Gamepad::reset_buttons() |
el17oc | 1:897160a1a3ae | 200 | { |
el17oc | 1:897160a1a3ae | 201 | A_fall = B_fall = X_fall = Y_fall = start_fall = false; |
el17oc | 1:897160a1a3ae | 202 | } |
el17oc | 1:897160a1a3ae | 203 | |
el17oc | 1:897160a1a3ae | 204 | bool Gamepad::A_pressed() |
el17oc | 1:897160a1a3ae | 205 | { |
el17oc | 1:897160a1a3ae | 206 | if (A_fall) { |
el17oc | 1:897160a1a3ae | 207 | A_fall = false; |
el17oc | 1:897160a1a3ae | 208 | return true; |
el17oc | 1:897160a1a3ae | 209 | } else { |
el17oc | 1:897160a1a3ae | 210 | return false; |
el17oc | 1:897160a1a3ae | 211 | } |
el17oc | 1:897160a1a3ae | 212 | } |
el17oc | 1:897160a1a3ae | 213 | |
el17oc | 1:897160a1a3ae | 214 | bool Gamepad::B_pressed() |
el17oc | 1:897160a1a3ae | 215 | { |
el17oc | 1:897160a1a3ae | 216 | if (B_fall) { |
el17oc | 1:897160a1a3ae | 217 | B_fall = false; |
el17oc | 1:897160a1a3ae | 218 | return true; |
el17oc | 1:897160a1a3ae | 219 | } else { |
el17oc | 1:897160a1a3ae | 220 | return false; |
el17oc | 1:897160a1a3ae | 221 | } |
el17oc | 1:897160a1a3ae | 222 | } |
el17oc | 1:897160a1a3ae | 223 | |
el17oc | 1:897160a1a3ae | 224 | bool Gamepad::X_pressed() |
el17oc | 1:897160a1a3ae | 225 | { |
el17oc | 1:897160a1a3ae | 226 | if (X_fall) { |
el17oc | 1:897160a1a3ae | 227 | X_fall = false; |
el17oc | 1:897160a1a3ae | 228 | return true; |
el17oc | 1:897160a1a3ae | 229 | } else { |
el17oc | 1:897160a1a3ae | 230 | return false; |
el17oc | 1:897160a1a3ae | 231 | } |
el17oc | 1:897160a1a3ae | 232 | } |
el17oc | 1:897160a1a3ae | 233 | |
el17oc | 1:897160a1a3ae | 234 | bool Gamepad::Y_pressed() |
el17oc | 1:897160a1a3ae | 235 | { |
el17oc | 1:897160a1a3ae | 236 | if (Y_fall) { |
el17oc | 1:897160a1a3ae | 237 | Y_fall = false; |
el17oc | 1:897160a1a3ae | 238 | return true; |
el17oc | 1:897160a1a3ae | 239 | } else { |
el17oc | 1:897160a1a3ae | 240 | return false; |
el17oc | 1:897160a1a3ae | 241 | } |
el17oc | 1:897160a1a3ae | 242 | } |
el17oc | 1:897160a1a3ae | 243 | |
el17oc | 1:897160a1a3ae | 244 | bool Gamepad::start_pressed() |
el17oc | 1:897160a1a3ae | 245 | { |
el17oc | 1:897160a1a3ae | 246 | if (start_fall) { |
el17oc | 1:897160a1a3ae | 247 | start_fall = false; |
el17oc | 1:897160a1a3ae | 248 | return true; |
el17oc | 1:897160a1a3ae | 249 | } else { |
el17oc | 1:897160a1a3ae | 250 | return false; |
el17oc | 1:897160a1a3ae | 251 | } |
el17oc | 1:897160a1a3ae | 252 | } |
el17oc | 1:897160a1a3ae | 253 | |
el17oc | 1:897160a1a3ae | 254 | bool Gamepad::A_held() |
el17oc | 1:897160a1a3ae | 255 | { |
el17oc | 1:897160a1a3ae | 256 | // Buttons are configured as PullUp hence the not |
el17oc | 1:897160a1a3ae | 257 | return !_button_A->read(); |
el17oc | 1:897160a1a3ae | 258 | } |
el17oc | 1:897160a1a3ae | 259 | |
el17oc | 1:897160a1a3ae | 260 | bool Gamepad::B_held() |
el17oc | 1:897160a1a3ae | 261 | { |
el17oc | 1:897160a1a3ae | 262 | return !_button_B->read(); |
el17oc | 1:897160a1a3ae | 263 | } |
el17oc | 1:897160a1a3ae | 264 | |
el17oc | 1:897160a1a3ae | 265 | bool Gamepad::X_held() |
el17oc | 1:897160a1a3ae | 266 | { |
el17oc | 1:897160a1a3ae | 267 | return !_button_X->read(); |
el17oc | 1:897160a1a3ae | 268 | } |
el17oc | 1:897160a1a3ae | 269 | |
el17oc | 1:897160a1a3ae | 270 | bool Gamepad::Y_held() |
el17oc | 1:897160a1a3ae | 271 | { |
el17oc | 1:897160a1a3ae | 272 | return !_button_Y->read(); |
el17oc | 1:897160a1a3ae | 273 | } |
el17oc | 1:897160a1a3ae | 274 | |
el17oc | 1:897160a1a3ae | 275 | bool Gamepad::start_held() |
el17oc | 1:897160a1a3ae | 276 | { |
el17oc | 1:897160a1a3ae | 277 | return !_button_start->read(); |
el17oc | 1:897160a1a3ae | 278 | } |
el17oc | 1:897160a1a3ae | 279 | |
el17oc | 1:897160a1a3ae | 280 | ///////////////////// private methods //////////////////////// |
el17oc | 1:897160a1a3ae | 281 | |
el17oc | 1:897160a1a3ae | 282 | // get raw joystick coordinate in range -1 to 1 |
el17oc | 1:897160a1a3ae | 283 | // Direction (x,y) |
el17oc | 1:897160a1a3ae | 284 | // North (0,1) |
el17oc | 1:897160a1a3ae | 285 | // East (1,0) |
el17oc | 1:897160a1a3ae | 286 | // South (0,-1) |
el17oc | 1:897160a1a3ae | 287 | // West (-1,0) |
el17oc | 1:897160a1a3ae | 288 | Vector2D Gamepad::get_coord() |
el17oc | 1:897160a1a3ae | 289 | { |
el17oc | 1:897160a1a3ae | 290 | // read() returns value in range 0.0 to 1.0 so is scaled and centre value |
el17oc | 1:897160a1a3ae | 291 | // substracted to get values in the range -1.0 to 1.0 |
el17oc | 1:897160a1a3ae | 292 | float x = 2.0f*( _horiz->read() - _x0 ); |
el17oc | 1:897160a1a3ae | 293 | float y = 2.0f*( _vert->read() - _y0 ); |
el17oc | 1:897160a1a3ae | 294 | |
el17oc | 1:897160a1a3ae | 295 | // Note: the y value here is inverted to ensure the positive y is up |
el17oc | 1:897160a1a3ae | 296 | |
el17oc | 1:897160a1a3ae | 297 | Vector2D coord = {x,-y}; |
el17oc | 1:897160a1a3ae | 298 | return coord; |
el17oc | 1:897160a1a3ae | 299 | } |
el17oc | 1:897160a1a3ae | 300 | |
el17oc | 1:897160a1a3ae | 301 | // This maps the raw x,y coord onto a circular grid. |
el17oc | 1:897160a1a3ae | 302 | // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html |
el17oc | 1:897160a1a3ae | 303 | Vector2D Gamepad::get_mapped_coord() |
el17oc | 1:897160a1a3ae | 304 | { |
el17oc | 1:897160a1a3ae | 305 | Vector2D coord = get_coord(); |
el17oc | 1:897160a1a3ae | 306 | |
el17oc | 1:897160a1a3ae | 307 | // do the transformation |
el17oc | 1:897160a1a3ae | 308 | float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); |
el17oc | 1:897160a1a3ae | 309 | float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); |
el17oc | 1:897160a1a3ae | 310 | |
el17oc | 1:897160a1a3ae | 311 | Vector2D mapped_coord = {x,y}; |
el17oc | 1:897160a1a3ae | 312 | return mapped_coord; |
el17oc | 1:897160a1a3ae | 313 | } |
el17oc | 1:897160a1a3ae | 314 | |
el17oc | 1:897160a1a3ae | 315 | // this function converts the mapped coordinates into polar form |
el17oc | 1:897160a1a3ae | 316 | Polar Gamepad::get_polar() |
el17oc | 1:897160a1a3ae | 317 | { |
el17oc | 1:897160a1a3ae | 318 | // get the mapped coordinate |
el17oc | 1:897160a1a3ae | 319 | Vector2D coord = get_mapped_coord(); |
el17oc | 1:897160a1a3ae | 320 | |
el17oc | 1:897160a1a3ae | 321 | // at this point, 0 degrees (i.e. x-axis) will be defined to the East. |
el17oc | 1:897160a1a3ae | 322 | // We want 0 degrees to correspond to North and increase clockwise to 359 |
el17oc | 1:897160a1a3ae | 323 | // like a compass heading, so we need to swap the axis and invert y |
el17oc | 1:897160a1a3ae | 324 | float x = coord.y; |
el17oc | 1:897160a1a3ae | 325 | float y = coord.x; |
el17oc | 1:897160a1a3ae | 326 | |
el17oc | 1:897160a1a3ae | 327 | float mag = sqrt(x*x+y*y); // pythagoras |
el17oc | 1:897160a1a3ae | 328 | float angle = RAD2DEG*atan2(y,x); |
el17oc | 1:897160a1a3ae | 329 | // angle will be in range -180 to 180, so add 360 to negative angles to |
el17oc | 1:897160a1a3ae | 330 | // move to 0 to 360 range |
el17oc | 1:897160a1a3ae | 331 | if (angle < 0.0f) { |
el17oc | 1:897160a1a3ae | 332 | angle+=360.0f; |
el17oc | 1:897160a1a3ae | 333 | } |
el17oc | 1:897160a1a3ae | 334 | |
el17oc | 1:897160a1a3ae | 335 | // the noise on the ADC causes the values of x and y to fluctuate slightly |
el17oc | 1:897160a1a3ae | 336 | // around the centred values. This causes the random angle values to get |
el17oc | 1:897160a1a3ae | 337 | // calculated when the joystick is centred and untouched. This is also when |
el17oc | 1:897160a1a3ae | 338 | // the magnitude is very small, so we can check for a small magnitude and then |
el17oc | 1:897160a1a3ae | 339 | // set the angle to -1. This will inform us when the angle is invalid and the |
el17oc | 1:897160a1a3ae | 340 | // joystick is centred |
el17oc | 1:897160a1a3ae | 341 | |
el17oc | 1:897160a1a3ae | 342 | if (mag < TOL) { |
el17oc | 1:897160a1a3ae | 343 | mag = 0.0f; |
el17oc | 1:897160a1a3ae | 344 | angle = -1.0f; |
el17oc | 1:897160a1a3ae | 345 | } |
el17oc | 1:897160a1a3ae | 346 | |
el17oc | 1:897160a1a3ae | 347 | Polar p = {mag,angle}; |
el17oc | 1:897160a1a3ae | 348 | return p; |
el17oc | 1:897160a1a3ae | 349 | } |
el17oc | 1:897160a1a3ae | 350 | |
el17oc | 1:897160a1a3ae | 351 | // ISRs for buttons |
el17oc | 1:897160a1a3ae | 352 | void Gamepad::A_fall_interrupt() |
el17oc | 1:897160a1a3ae | 353 | { |
el17oc | 1:897160a1a3ae | 354 | A_fall = true; |
el17oc | 1:897160a1a3ae | 355 | } |
el17oc | 1:897160a1a3ae | 356 | void Gamepad::B_fall_interrupt() |
el17oc | 1:897160a1a3ae | 357 | { |
el17oc | 1:897160a1a3ae | 358 | B_fall = true; |
el17oc | 1:897160a1a3ae | 359 | } |
el17oc | 1:897160a1a3ae | 360 | void Gamepad::X_fall_interrupt() |
el17oc | 1:897160a1a3ae | 361 | { |
el17oc | 1:897160a1a3ae | 362 | X_fall = true; |
el17oc | 1:897160a1a3ae | 363 | } |
el17oc | 1:897160a1a3ae | 364 | void Gamepad::Y_fall_interrupt() |
el17oc | 1:897160a1a3ae | 365 | { |
el17oc | 1:897160a1a3ae | 366 | Y_fall = true; |
el17oc | 1:897160a1a3ae | 367 | } |
el17oc | 1:897160a1a3ae | 368 | void Gamepad::start_fall_interrupt() |
el17oc | 1:897160a1a3ae | 369 | { |
el17oc | 1:897160a1a3ae | 370 | start_fall = true; |
el17oc | 1:897160a1a3ae | 371 | } |
el17oc | 1:897160a1a3ae | 372 | |
el17oc | 1:897160a1a3ae | 373 | void Gamepad::set_bpm(float bpm) |
el17oc | 1:897160a1a3ae | 374 | { |
el17oc | 1:897160a1a3ae | 375 | _bpm = bpm; |
el17oc | 1:897160a1a3ae | 376 | } |
el17oc | 1:897160a1a3ae | 377 | |
el17oc | 1:897160a1a3ae | 378 | void Gamepad::tone(float frequency,float duration) |
el17oc | 1:897160a1a3ae | 379 | { |
el17oc | 1:897160a1a3ae | 380 | // calculate time step between samples |
el17oc | 1:897160a1a3ae | 381 | float dt = 1.0f/(frequency*_n); |
el17oc | 1:897160a1a3ae | 382 | // start from beginning of LUT |
el17oc | 1:897160a1a3ae | 383 | _sample = 0; |
el17oc | 1:897160a1a3ae | 384 | |
el17oc | 1:897160a1a3ae | 385 | // setup ticker and timeout to stop ticker |
el17oc | 1:897160a1a3ae | 386 | |
el17oc | 1:897160a1a3ae | 387 | // the ticker repeats every dt to plat each sample in turn |
el17oc | 1:897160a1a3ae | 388 | ticker->attach(callback(this, &Gamepad::ticker_isr), dt); |
el17oc | 1:897160a1a3ae | 389 | // the timeout stops the ticker after the required duration |
el17oc | 1:897160a1a3ae | 390 | timeout->attach(callback(this, &Gamepad::timeout_isr), duration ); |
el17oc | 1:897160a1a3ae | 391 | } |
el17oc | 1:897160a1a3ae | 392 | |
el17oc | 1:897160a1a3ae | 393 | void Gamepad::play_melody(int length,const int *notes,const int *durations,float bpm,bool repeat) |
el17oc | 1:897160a1a3ae | 394 | { |
el17oc | 1:897160a1a3ae | 395 | // copy arguments to member variables |
el17oc | 1:897160a1a3ae | 396 | _bpm = bpm; |
el17oc | 1:897160a1a3ae | 397 | _notes = notes; // pointer for array |
el17oc | 1:897160a1a3ae | 398 | _durations = durations; // pointer for array |
el17oc | 1:897160a1a3ae | 399 | _melody_length = length; |
el17oc | 1:897160a1a3ae | 400 | _repeat = repeat; |
el17oc | 1:897160a1a3ae | 401 | |
el17oc | 1:897160a1a3ae | 402 | _note = 0; // start from first note |
el17oc | 1:897160a1a3ae | 403 | |
el17oc | 1:897160a1a3ae | 404 | play_next_note(); // play the next note in the melody |
el17oc | 1:897160a1a3ae | 405 | } |
el17oc | 1:897160a1a3ae | 406 | |
el17oc | 1:897160a1a3ae | 407 | void Gamepad::write_dac(float val) |
el17oc | 1:897160a1a3ae | 408 | { |
el17oc | 1:897160a1a3ae | 409 | if (val < 0.0f) { |
el17oc | 1:897160a1a3ae | 410 | val = 0.0f; |
el17oc | 1:897160a1a3ae | 411 | } else if (val > 1.0f) { |
el17oc | 1:897160a1a3ae | 412 | val = 1.0f; |
el17oc | 1:897160a1a3ae | 413 | } |
el17oc | 1:897160a1a3ae | 414 | dac->write(val); |
el17oc | 1:897160a1a3ae | 415 | } |
el17oc | 1:897160a1a3ae | 416 | |
el17oc | 1:897160a1a3ae | 417 | |
el17oc | 1:897160a1a3ae | 418 | void Gamepad::play_next_note() |
el17oc | 1:897160a1a3ae | 419 | { |
el17oc | 1:897160a1a3ae | 420 | // _note is the note index to play |
el17oc | 1:897160a1a3ae | 421 | |
el17oc | 1:897160a1a3ae | 422 | // calculate the duration and frequency of the note |
el17oc | 1:897160a1a3ae | 423 | float duration = 60.0f/(_bpm*_durations[_note]); |
el17oc | 1:897160a1a3ae | 424 | float frequency = float(_notes[_note]); |
el17oc | 1:897160a1a3ae | 425 | //printf("[%i] f = %f d = %f\n",_note,frequency,duration); |
el17oc | 1:897160a1a3ae | 426 | |
el17oc | 1:897160a1a3ae | 427 | // check if the note is not a space and if not then play the note |
el17oc | 1:897160a1a3ae | 428 | if (frequency > 0) { |
el17oc | 1:897160a1a3ae | 429 | tone(frequency,duration); |
el17oc | 1:897160a1a3ae | 430 | } |
el17oc | 1:897160a1a3ae | 431 | |
el17oc | 1:897160a1a3ae | 432 | // the timeout goes to the next note in the melody |
el17oc | 1:897160a1a3ae | 433 | // double the duration to leave a bit of a gap in between notes to be better |
el17oc | 1:897160a1a3ae | 434 | // able to distinguish them |
el17oc | 1:897160a1a3ae | 435 | note_timeout->attach(callback(this, &Gamepad::note_timeout_isr), duration*2.0f ); |
el17oc | 1:897160a1a3ae | 436 | } |
el17oc | 1:897160a1a3ae | 437 | |
el17oc | 1:897160a1a3ae | 438 | // called when the next note needs playing |
el17oc | 1:897160a1a3ae | 439 | void Gamepad::note_timeout_isr() |
el17oc | 1:897160a1a3ae | 440 | { |
el17oc | 1:897160a1a3ae | 441 | _note++; // go onto next note |
el17oc | 1:897160a1a3ae | 442 | |
el17oc | 1:897160a1a3ae | 443 | // if in repeat mode then reset the note counter when get to end of melody |
el17oc | 1:897160a1a3ae | 444 | if (_repeat && _note == _melody_length) { |
el17oc | 1:897160a1a3ae | 445 | _note=0; |
el17oc | 1:897160a1a3ae | 446 | } |
el17oc | 1:897160a1a3ae | 447 | |
el17oc | 1:897160a1a3ae | 448 | // check if note is within the melody |
el17oc | 1:897160a1a3ae | 449 | if (_note < _melody_length) { |
el17oc | 1:897160a1a3ae | 450 | play_next_note(); |
el17oc | 1:897160a1a3ae | 451 | } |
el17oc | 1:897160a1a3ae | 452 | } |
el17oc | 1:897160a1a3ae | 453 | |
el17oc | 1:897160a1a3ae | 454 | void Gamepad::ticker_isr() |
el17oc | 1:897160a1a3ae | 455 | { |
el17oc | 1:897160a1a3ae | 456 | dac->write(_sample_array[_sample%_n]); // use modulo to get index to play |
el17oc | 1:897160a1a3ae | 457 | _sample++; // increment the sample ready for next time |
el17oc | 1:897160a1a3ae | 458 | } |
el17oc | 1:897160a1a3ae | 459 | |
el17oc | 1:897160a1a3ae | 460 | void Gamepad::timeout_isr() |
el17oc | 1:897160a1a3ae | 461 | { |
el17oc | 1:897160a1a3ae | 462 | // stops the ticker to end the note |
el17oc | 1:897160a1a3ae | 463 | ticker->detach(); |
el17oc | 1:897160a1a3ae | 464 | } |