Library for ELEC2645 Gamepad PCB. University of Leeds Version 2 January 2020
Dependents: ELEC2645_Ticker_WAV ELEC2645_Project_username ELEC2645_Project_el18vgt ELEC2645_Project_el17oc11 ... more
Diff: Gamepad.h
- Revision:
- 21:da0b4e14c867
- Parent:
- 20:c0959710291b
- Child:
- 22:72357412608d
diff -r c0959710291b -r da0b4e14c867 Gamepad.h --- a/Gamepad.h Wed Apr 26 13:58:10 2017 +0000 +++ b/Gamepad.h Mon Jan 27 12:31:15 2020 +0000 @@ -11,11 +11,15 @@ class AnalogIn; class InterruptIn; class PwmOut; +class AnalogOut; +class Ticker; class Timeout; } #define TOL 0.1f #define RAD2DEG 57.2957795131f +#define PI 3.14159265359 + /** Enum for direction */ enum Direction { @@ -49,22 +53,6 @@ */ class Gamepad { -public: -/** Gamepad events - * @brief List of events that can be registered on the gamepad - */ -enum GamepadEvent { - A_PRESSED, ///< Button A has been pressed - B_PRESSED, ///< Button B has been pressed - X_PRESSED, ///< Button X has been pressed - Y_PRESSED, ///< Button Y has been pressed - L_PRESSED, ///< Button L has been pressed - R_PRESSED, ///< Button R has been pressed - BACK_PRESSED, ///< Button "Back" has been pressed - START_PRESSED, ///< Button "Start" has been pressed - JOY_PRESSED, ///< Joystick button has been pressed - N_EVENTS ///< A dummy flag that marks the end of the list -}; private: mbed::PwmOut *_led1; @@ -78,33 +66,39 @@ mbed::InterruptIn *_button_B; mbed::InterruptIn *_button_X; mbed::InterruptIn *_button_Y; - mbed::InterruptIn *_button_L; - mbed::InterruptIn *_button_R; - mbed::InterruptIn *_button_back; mbed::InterruptIn *_button_start; - mbed::InterruptIn *_button_joystick; mbed::AnalogIn *_vert; mbed::AnalogIn *_horiz; - mbed::PwmOut *_buzzer; - mbed::AnalogIn *_pot; - - mbed::Timeout *_timeout; - - std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed + mbed::AnalogIn *_pot1; + mbed::AnalogIn *_pot2; + + mbed::AnalogOut *dac; + mbed::Ticker *ticker; + mbed::Timeout *timeout; + mbed::Timeout *note_timeout; // centred x,y values float _x0; float _y0; + + float *_sample_array; + const int *_notes; + const int *_durations; + + int _n; + int _melody_length; + volatile unsigned int _sample; + volatile unsigned int _note; + float _bpm; + bool _repeat; + public: /** Constructor */ Gamepad(); - /** Destructor */ - ~Gamepad(); - /** Initialise all peripherals and configure interrupts */ void init(); @@ -125,37 +119,15 @@ */ void led(int n,float val) const; - /** Read potentiometer + /** Read potentiometer 1 value *@returns potentiometer value in range 0.0 to 1.0 */ - float read_pot() const; - - /** Play tone on piezo - * @param frequency in Hz - * @param duration of tone in seconds - */ - void tone(float frequency, float duration); + float read_pot1() const; - /** - * @brief Check whether an event flag has been set and clear it - * @param id[in] The ID of the event to test - * @return true if the event occurred - */ - bool check_event(GamepadEvent const id); - - /** - * @brief Get the raw binary event state - * @return The event state as a binary code - * @details The check_event() function is likely to be more useful than - * this, for testing whether a given event has occurred. It can be - * useful for debugging via the terminal, however, for example: - * @code - * std::cout << gamepad.get_raw_event_state() << std::endl; - * @endcode - */ - inline std::bitset<N_EVENTS> get_raw_event_state() const { - return _event_state; - } + /** Read potentiometer 2 value + *@returns potentiometer value in range 0.0 to 1.0 + */ + float read_pot2() const; /** Get magnitude of joystick movement * @returns value in range 0.0 to 1.0 @@ -187,19 +159,200 @@ */ Polar get_polar(); // mag and angle in struct form -private: - void init_buttons(); - void tone_off(); + + /** Resets all button states. Useful for calling inbetween scenes + * where you do not want button presses from the previous scene effecting + * the current scene + */ + void reset_buttons(); + + /** Returns true if A has been pressed + * @returns a bool corresponding to A being pressed + */ + + bool A_pressed(); + + /** Returns true if A is held + * @returns a bool corresponding to A being held + * + */ + bool A_held(); + + + /** Returns true if B has been pressed + * @returns a bool corresponding to B being pressed + */ + bool B_pressed(); + + /** Returns true if B is held + * @returns a bool corresponding to B being held + * + */ + bool B_held(); + + /** Returns true if B has been pressed + * @returns a bool corresponding to B being pressed + */ + bool X_pressed(); + + /** Returns true if X is held + * @returns a bool corresponding to X being held + * + */ + bool X_held(); + + /** Returns true if Y has been pressed + * @returns a bool corresponding to Y being pressed + */ + bool Y_pressed(); - void a_isr(); - void b_isr(); - void x_isr(); - void y_isr(); - void l_isr(); - void r_isr(); - void back_isr(); - void start_isr(); - void joy_isr(); + /** Returns true if Y is held + * @returns a bool corresponding to Y being held + * + */ + bool Y_held(); + + + /** Returns true if start has been pressed + * @returns a bool corresponding to start being pressed + */ + bool start_pressed(); + + /** Returns true if start is held + * @returns a bool corresponding to start being held + * + */ + bool start_held(); + + + // play single note of specific duration + void tone(const float frequency,const float duration); + + // play array of notes and duration, must send length and beats per minute + void play_melody(int length,const int *notes,const int *durations,float bpm,bool repeat); + + // change the BPM + void set_bpm(float bpm); + + + + +private: + + volatile bool A_fall; + void A_fall_interrupt(); + + volatile bool B_fall; + void B_fall_interrupt(); + + volatile bool X_fall; + void X_fall_interrupt(); + + volatile bool Y_fall; + void Y_fall_interrupt(); + + volatile bool start_fall; + void start_fall_interrupt(); + + // Tone functions + void ticker_isr(); + void timeout_isr(); + void note_timeout_isr(); + void play_next_note(); + + + }; +// Note definitions from Arduino.cc +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 + #endif \ No newline at end of file