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