Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Gamepad.cpp
00001 #include "Gamepad.h" 00002 00003 #include "mbed.h" 00004 00005 //////////// constructor/destructor //////////// 00006 Gamepad::Gamepad() 00007 : 00008 _led1(new PwmOut(PTA2)), 00009 _led2(new PwmOut(PTC2)), 00010 _led3(new PwmOut(PTC3)), 00011 _led4(new PwmOut(PTA1)), 00012 _led5(new PwmOut(PTC10)), 00013 _led6(new PwmOut(PTC11)), 00014 00015 _button_A(new InterruptIn(PTC7)), 00016 _button_B(new InterruptIn(PTC9)), 00017 _button_X(new InterruptIn(PTC5)), 00018 _button_Y(new InterruptIn(PTC0)), 00019 _button_start(new InterruptIn(PTC8)), 00020 00021 _vert(new AnalogIn(PTB11)), 00022 _horiz(new AnalogIn(PTB10)), 00023 00024 _pot1(new AnalogIn(PTB2)), 00025 _pot2(new AnalogIn(PTB3)), 00026 00027 //rca(new PwmOut(PTE25)), 00028 dac(new AnalogOut(DAC0_OUT)), 00029 ticker(new Ticker), 00030 timeout(new Timeout), 00031 note_timeout(new Timeout), 00032 00033 _x0(0), 00034 _y0(0) 00035 {} 00036 00037 00038 ///////////////// public methods ///////////////// 00039 00040 void Gamepad::init() 00041 { 00042 leds_off(); 00043 00044 // read centred values of joystick 00045 _x0 = _horiz->read(); 00046 _y0 = _vert->read(); 00047 00048 // Set all buttons to PullUp 00049 _button_A->mode(PullUp); 00050 _button_B->mode(PullUp); 00051 _button_X->mode(PullUp); 00052 _button_Y->mode(PullUp); 00053 _button_start->mode(PullUp); 00054 00055 // Set up interrupts for the fall of buttons 00056 _button_A->fall(callback(this,&Gamepad::A_fall_interrupt)); 00057 _button_B->fall(callback(this,&Gamepad::B_fall_interrupt)); 00058 _button_X->fall(callback(this,&Gamepad::X_fall_interrupt)); 00059 _button_Y->fall(callback(this,&Gamepad::Y_fall_interrupt)); 00060 _button_start->fall(callback(this,&Gamepad::start_fall_interrupt)); 00061 00062 // initalise button flags 00063 reset_buttons(); 00064 00065 // number of samples 00066 _n = 16; 00067 _sample_array = new float[_n]; 00068 00069 // create sample array for one period between 0.0 and 1.0 00070 for (int i = 0; i < _n ; i++) { 00071 _sample_array[i] = 0.5f + 0.5f*sin(i*2*PI/_n); 00072 //printf("y[%i] = %f\n",i,_sample_array[i]); 00073 } 00074 00075 } 00076 00077 void Gamepad::leds_off() 00078 { 00079 leds(0.0); 00080 } 00081 00082 void Gamepad::leds_on() 00083 { 00084 leds(1.0); 00085 } 00086 00087 void Gamepad::leds(float val) const 00088 { 00089 if (val < 0.0f) { 00090 val = 0.0f; 00091 } 00092 if (val > 1.0f) { 00093 val = 1.0f; 00094 } 00095 00096 // leds are active-low, so subtract from 1.0 00097 // 0.0 corresponds to fully-off, 1.0 to fully-on 00098 val = 1.0f - val; 00099 00100 _led1->write(val); 00101 _led2->write(val); 00102 _led3->write(val); 00103 _led4->write(val); 00104 _led5->write(val); 00105 _led6->write(val); 00106 } 00107 00108 void Gamepad::led(int n,float val) const 00109 { 00110 // ensure they are within valid range 00111 if (val < 0.0f) { 00112 val = 0.0f; 00113 } 00114 if (val > 1.0f) { 00115 val = 1.0f; 00116 } 00117 00118 switch (n) { 00119 00120 // check for valid LED number and set value 00121 00122 case 1: 00123 _led1->write(1.0f-val); // active-low so subtract from 1 00124 break; 00125 case 2: 00126 _led2->write(1.0f-val); // active-low so subtract from 1 00127 break; 00128 case 3: 00129 _led3->write(1.0f-val); // active-low so subtract from 1 00130 break; 00131 case 4: 00132 _led4->write(1.0f-val); // active-low so subtract from 1 00133 break; 00134 case 5: 00135 _led5->write(1.0f-val); // active-low so subtract from 1 00136 break; 00137 case 6: 00138 _led6->write(1.0f-val); // active-low so subtract from 1 00139 break; 00140 00141 } 00142 } 00143 00144 float Gamepad::read_pot1() const 00145 { 00146 return _pot1->read(); 00147 } 00148 00149 float Gamepad::read_pot2() const 00150 { 00151 return _pot2->read(); 00152 } 00153 00154 00155 // this method gets the magnitude of the joystick movement 00156 float Gamepad::get_mag() 00157 { 00158 Polar p = get_polar(); 00159 return p.mag; 00160 } 00161 00162 // this method gets the angle of joystick movement (0 to 360, 0 North) 00163 float Gamepad::get_angle() 00164 { 00165 Polar p = get_polar(); 00166 return p.angle; 00167 } 00168 00169 Direction Gamepad::get_direction() 00170 { 00171 float angle = get_angle(); // 0 to 360, -1 for centred 00172 00173 Direction d; 00174 // partition 360 into segments and check which segment the angle is in 00175 if (angle < 0.0f) { 00176 d = CENTRE; // check for -1.0 angle 00177 } else if (angle < 22.5f) { // then keep going in 45 degree increments 00178 d = N; 00179 } else if (angle < 67.5f) { 00180 d = NE; 00181 } else if (angle < 112.5f) { 00182 d = E; 00183 } else if (angle < 157.5f) { 00184 d = SE; 00185 } else if (angle < 202.5f) { 00186 d = S; 00187 } else if (angle < 247.5f) { 00188 d = SW; 00189 } else if (angle < 292.5f) { 00190 d = W; 00191 } else if (angle < 337.5f) { 00192 d = NW; 00193 } else { 00194 d = N; 00195 } 00196 00197 return d; 00198 } 00199 00200 void Gamepad::reset_buttons() 00201 { 00202 A_fall = B_fall = X_fall = Y_fall = start_fall = false; 00203 } 00204 00205 bool Gamepad::A_pressed() 00206 { 00207 if (A_fall) { 00208 A_fall = false; 00209 return true; 00210 } else { 00211 return false; 00212 } 00213 } 00214 00215 bool Gamepad::B_pressed() 00216 { 00217 if (B_fall) { 00218 B_fall = false; 00219 return true; 00220 } else { 00221 return false; 00222 } 00223 } 00224 00225 bool Gamepad::X_pressed() 00226 { 00227 if (X_fall) { 00228 X_fall = false; 00229 return true; 00230 } else { 00231 return false; 00232 } 00233 } 00234 00235 bool Gamepad::Y_pressed() 00236 { 00237 if (Y_fall) { 00238 Y_fall = false; 00239 return true; 00240 } else { 00241 return false; 00242 } 00243 } 00244 00245 bool Gamepad::start_pressed() 00246 { 00247 if (start_fall) { 00248 start_fall = false; 00249 return true; 00250 } else { 00251 return false; 00252 } 00253 } 00254 00255 bool Gamepad::A_held() 00256 { 00257 // Buttons are configured as PullUp hence the not 00258 return !_button_A->read(); 00259 } 00260 00261 bool Gamepad::B_held() 00262 { 00263 return !_button_B->read(); 00264 } 00265 00266 bool Gamepad::X_held() 00267 { 00268 return !_button_X->read(); 00269 } 00270 00271 bool Gamepad::Y_held() 00272 { 00273 return !_button_Y->read(); 00274 } 00275 00276 bool Gamepad::start_held() 00277 { 00278 return !_button_start->read(); 00279 } 00280 00281 ///////////////////// private methods //////////////////////// 00282 00283 // get raw joystick coordinate in range -1 to 1 00284 // Direction (x,y) 00285 // North (0,1) 00286 // East (1,0) 00287 // South (0,-1) 00288 // West (-1,0) 00289 Vector2D Gamepad::get_coord() 00290 { 00291 // read() returns value in range 0.0 to 1.0 so is scaled and centre value 00292 // substracted to get values in the range -1.0 to 1.0 00293 float x = 2.0f*( _horiz->read() - _x0 ); 00294 float y = 2.0f*( _vert->read() - _y0 ); 00295 00296 // Note: the y value here is inverted to ensure the positive y is up 00297 00298 Vector2D coord = {x,-y}; 00299 return coord; 00300 } 00301 00302 // This maps the raw x,y coord onto a circular grid. 00303 // See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html 00304 Vector2D Gamepad::get_mapped_coord() 00305 { 00306 Vector2D coord = get_coord(); 00307 00308 // do the transformation 00309 float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); 00310 float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); 00311 00312 Vector2D mapped_coord = {x,y}; 00313 return mapped_coord; 00314 } 00315 00316 // this function converts the mapped coordinates into polar form 00317 Polar Gamepad::get_polar() 00318 { 00319 // get the mapped coordinate 00320 Vector2D coord = get_mapped_coord(); 00321 00322 // at this point, 0 degrees (i.e. x-axis) will be defined to the East. 00323 // We want 0 degrees to correspond to North and increase clockwise to 359 00324 // like a compass heading, so we need to swap the axis and invert y 00325 float x = coord.y; 00326 float y = coord.x; 00327 00328 float mag = sqrt(x*x+y*y); // pythagoras 00329 float angle = RAD2DEG*atan2(y,x); 00330 // angle will be in range -180 to 180, so add 360 to negative angles to 00331 // move to 0 to 360 range 00332 if (angle < 0.0f) { 00333 angle+=360.0f; 00334 } 00335 00336 // the noise on the ADC causes the values of x and y to fluctuate slightly 00337 // around the centred values. This causes the random angle values to get 00338 // calculated when the joystick is centred and untouched. This is also when 00339 // the magnitude is very small, so we can check for a small magnitude and then 00340 // set the angle to -1. This will inform us when the angle is invalid and the 00341 // joystick is centred 00342 00343 if (mag < TOL) { 00344 mag = 0.0f; 00345 angle = -1.0f; 00346 } 00347 00348 Polar p = {mag,angle}; 00349 return p; 00350 } 00351 00352 // ISRs for buttons 00353 void Gamepad::A_fall_interrupt() 00354 { 00355 A_fall = true; 00356 } 00357 void Gamepad::B_fall_interrupt() 00358 { 00359 B_fall = true; 00360 } 00361 void Gamepad::X_fall_interrupt() 00362 { 00363 X_fall = true; 00364 } 00365 void Gamepad::Y_fall_interrupt() 00366 { 00367 Y_fall = true; 00368 } 00369 void Gamepad::start_fall_interrupt() 00370 { 00371 start_fall = true; 00372 } 00373 00374 00375 void Gamepad::set_bpm(float bpm) 00376 { 00377 _bpm = bpm; 00378 } 00379 00380 void Gamepad::tone(float frequency,float duration) 00381 { 00382 // calculate time step between samples 00383 float dt = 1.0f/(frequency*_n); 00384 // start from beginning of LUT 00385 _sample = 0; 00386 00387 // setup ticker and timeout to stop ticker 00388 00389 // the ticker repeats every dt to plat each sample in turn 00390 ticker->attach(callback(this, &Gamepad::ticker_isr), dt); 00391 // the timeout stops the ticker after the required duration 00392 timeout->attach(callback(this, &Gamepad::timeout_isr), duration ); 00393 } 00394 00395 void Gamepad::play_melody(int length,const int *notes,const int *durations,float bpm,bool repeat) 00396 { 00397 // copy arguments to member variables 00398 _bpm = bpm; 00399 _notes = notes; // pointer for array 00400 _durations = durations; // pointer for array 00401 _melody_length = length; 00402 _repeat = repeat; 00403 00404 _note = 0; // start from first note 00405 00406 play_next_note(); // play the next note in the melody 00407 } 00408 00409 void Gamepad::write_dac(float val) 00410 { 00411 if (val < 0.0f) { 00412 val = 0.0f; 00413 } else if (val > 1.0f) { 00414 val = 1.0f; 00415 } 00416 dac->write(val); 00417 } 00418 00419 //writes DAC value in uint format 00420 void Gamepad::write_u16(unsigned short val) 00421 { 00422 if (val > 65535) { 00423 #ifdef CSV //used for error detection in CSV file 00424 printf("/nERROR OUTPUT VALUE= %u",val); 00425 #endif 00426 val = 65535; 00427 } 00428 dac->write_u16(val); 00429 } 00430 00431 void Gamepad::play_next_note() 00432 { 00433 // _note is the note index to play 00434 00435 // calculate the duration and frequency of the note 00436 float duration = 60.0f/(_bpm*_durations[_note]); 00437 float frequency = float(_notes[_note]); 00438 //printf("[%i] f = %f d = %f\n",_note,frequency,duration); 00439 00440 // check if the note is not a space and if not then play the note 00441 if (frequency > 0) { 00442 tone(frequency,duration); 00443 } 00444 00445 // the timeout goes to the next note in the melody 00446 // double the duration to leave a bit of a gap in between notes to be better 00447 // able to distinguish them 00448 note_timeout->attach(callback(this, &Gamepad::note_timeout_isr), duration*2.0f ); 00449 } 00450 00451 // called when the next note needs playing 00452 void Gamepad::note_timeout_isr() 00453 { 00454 _note++; // go onto next note 00455 00456 // if in repeat mode then reset the note counter when get to end of melody 00457 if (_repeat && _note == _melody_length) { 00458 _note=0; 00459 } 00460 00461 // check if note is within the melody 00462 if (_note < _melody_length) { 00463 play_next_note(); 00464 } 00465 } 00466 00467 void Gamepad::ticker_isr() 00468 { 00469 dac->write(_sample_array[_sample%_n]); // use modulo to get index to play 00470 _sample++; // increment the sample ready for next time 00471 } 00472 00473 void Gamepad::timeout_isr() 00474 { 00475 // stops the ticker to end the note 00476 ticker->detach(); 00477 }
Generated on Wed Jul 13 2022 21:50:34 by
1.7.2