This Class allows the joystick, buttons and petiometers to be implemented easily and coherently in other classes
Fork of Gamepad by
Controller.h@28:7c0dbbd90c2e, 2017-05-01 (annotated)
- Committer:
- domkay97
- Date:
- Mon May 01 17:04:29 2017 +0000
- Revision:
- 28:7c0dbbd90c2e
- Parent:
- 26:ccdf299b26c4
- Child:
- 30:7b1e427a20f6
Tidying up ready for publication, tidying DOxygen Commenting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
domkay97 | 21:99ec20695524 | 1 | #ifndef Controller_H |
domkay97 | 21:99ec20695524 | 2 | #define Controller_H |
domkay97 | 21:99ec20695524 | 3 | |
domkay97 | 21:99ec20695524 | 4 | #include <bitset> |
domkay97 | 21:99ec20695524 | 5 | #include "mbed.h" |
domkay97 | 23:34da21dfdfd6 | 6 | |
domkay97 | 21:99ec20695524 | 7 | |
domkay97 | 21:99ec20695524 | 8 | |
domkay97 | 21:99ec20695524 | 9 | // Forward declaration of the classes that we use from the mbed library |
domkay97 | 21:99ec20695524 | 10 | // This avoids the need for us to include the huge mbed.h header inside our |
domkay97 | 21:99ec20695524 | 11 | // own library API |
domkay97 | 21:99ec20695524 | 12 | namespace mbed |
domkay97 | 21:99ec20695524 | 13 | { |
domkay97 | 21:99ec20695524 | 14 | class AnalogIn; |
domkay97 | 21:99ec20695524 | 15 | class InterruptIn; |
domkay97 | 21:99ec20695524 | 16 | class PwmOut; |
domkay97 | 21:99ec20695524 | 17 | class Timeout; |
domkay97 | 21:99ec20695524 | 18 | } |
domkay97 | 21:99ec20695524 | 19 | |
domkay97 | 21:99ec20695524 | 20 | |
domkay97 | 21:99ec20695524 | 21 | /** Controller Class |
domkay97 | 28:7c0dbbd90c2e | 22 | @brief This Class allows interrupts and petiometers to be implimented easily and coherently in other classes |
domkay97 | 28:7c0dbbd90c2e | 23 | @author Dr. Craig Evans, Modified by Dominic Kay |
domkay97 | 24:7b149f5d4bef | 24 | @date April 2017 |
domkay97 | 21:99ec20695524 | 25 | */ |
domkay97 | 21:99ec20695524 | 26 | class Controller |
domkay97 | 21:99ec20695524 | 27 | { |
domkay97 | 21:99ec20695524 | 28 | public: |
domkay97 | 21:99ec20695524 | 29 | /** Controller events |
domkay97 | 24:7b149f5d4bef | 30 | * @brief List of all events which are triggered by an interrupt. |
domkay97 | 21:99ec20695524 | 31 | */ |
domkay97 | 21:99ec20695524 | 32 | enum ControllerEvent { |
domkay97 | 21:99ec20695524 | 33 | A_PRESSED, ///< Button A has been pressed |
domkay97 | 21:99ec20695524 | 34 | B_PRESSED, ///< Button B has been pressed |
domkay97 | 21:99ec20695524 | 35 | X_PRESSED, ///< Button X has been pressed |
domkay97 | 21:99ec20695524 | 36 | Y_PRESSED, ///< Button Y has been pressed |
domkay97 | 21:99ec20695524 | 37 | L_PRESSED, ///< Button L has been pressed |
domkay97 | 21:99ec20695524 | 38 | R_PRESSED, ///< Button R has been pressed |
domkay97 | 21:99ec20695524 | 39 | BACK_PRESSED, ///< Button "Back" has been pressed |
domkay97 | 21:99ec20695524 | 40 | START_PRESSED, ///< Button "Start" has been pressed |
domkay97 | 21:99ec20695524 | 41 | JOY_PRESSED, ///< Joystick button has been pressed |
domkay97 | 21:99ec20695524 | 42 | N_EVENTS ///< A dummy flag that marks the end of the list |
domkay97 | 21:99ec20695524 | 43 | }; |
domkay97 | 21:99ec20695524 | 44 | private: |
domkay97 | 21:99ec20695524 | 45 | mbed::PwmOut *_led1; |
domkay97 | 21:99ec20695524 | 46 | mbed::PwmOut *_led2; |
domkay97 | 21:99ec20695524 | 47 | mbed::PwmOut *_led3; |
domkay97 | 21:99ec20695524 | 48 | mbed::PwmOut *_led4; |
domkay97 | 21:99ec20695524 | 49 | mbed::PwmOut *_led5; |
domkay97 | 21:99ec20695524 | 50 | mbed::PwmOut *_led6; |
domkay97 | 21:99ec20695524 | 51 | |
domkay97 | 21:99ec20695524 | 52 | mbed::InterruptIn *_button_A; |
domkay97 | 21:99ec20695524 | 53 | mbed::InterruptIn *_button_B; |
domkay97 | 21:99ec20695524 | 54 | mbed::InterruptIn *_button_X; |
domkay97 | 21:99ec20695524 | 55 | mbed::InterruptIn *_button_Y; |
domkay97 | 21:99ec20695524 | 56 | mbed::InterruptIn *_button_L; |
domkay97 | 21:99ec20695524 | 57 | mbed::InterruptIn *_button_R; |
domkay97 | 21:99ec20695524 | 58 | mbed::InterruptIn *_button_back; |
domkay97 | 21:99ec20695524 | 59 | mbed::InterruptIn *_button_start; |
domkay97 | 21:99ec20695524 | 60 | mbed::InterruptIn *_button_joystick; |
domkay97 | 21:99ec20695524 | 61 | |
domkay97 | 21:99ec20695524 | 62 | mbed::AnalogIn *_vert; |
domkay97 | 21:99ec20695524 | 63 | mbed::AnalogIn *_horiz; |
domkay97 | 21:99ec20695524 | 64 | |
domkay97 | 21:99ec20695524 | 65 | mbed::PwmOut *_buzzer; |
domkay97 | 21:99ec20695524 | 66 | mbed::AnalogIn *_pot; |
domkay97 | 21:99ec20695524 | 67 | |
domkay97 | 21:99ec20695524 | 68 | mbed::Timeout *_timeout; |
domkay97 | 21:99ec20695524 | 69 | |
domkay97 | 21:99ec20695524 | 70 | std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed |
domkay97 | 26:ccdf299b26c4 | 71 | float _x0; |
domkay97 | 26:ccdf299b26c4 | 72 | |
domkay97 | 21:99ec20695524 | 73 | public: |
domkay97 | 21:99ec20695524 | 74 | |
domkay97 | 21:99ec20695524 | 75 | /** Constructor */ |
domkay97 | 21:99ec20695524 | 76 | Controller(); |
domkay97 | 21:99ec20695524 | 77 | |
domkay97 | 21:99ec20695524 | 78 | /** Destructor */ |
domkay97 | 21:99ec20695524 | 79 | ~Controller(); |
domkay97 | 21:99ec20695524 | 80 | |
domkay97 | 24:7b149f5d4bef | 81 | /** Clear all the interrupt flags and turn off all LEDs */ |
domkay97 | 21:99ec20695524 | 82 | void init(); |
domkay97 | 21:99ec20695524 | 83 | |
domkay97 | 24:7b149f5d4bef | 84 | /** Turn on all the LEDs */ |
domkay97 | 24:7b149f5d4bef | 85 | void ledsON(); |
domkay97 | 21:99ec20695524 | 86 | |
domkay97 | 24:7b149f5d4bef | 87 | /** Turn off all the LEDs */ |
domkay97 | 24:7b149f5d4bef | 88 | void ledsOFF(); |
domkay97 | 21:99ec20695524 | 89 | |
domkay97 | 24:7b149f5d4bef | 90 | /** Set all LEDs to duty-cycle EDIT |
domkay97 | 24:7b149f5d4bef | 91 | *@param float value varing from 0.0 to 1.0 |
domkay97 | 21:99ec20695524 | 92 | */ |
domkay97 | 21:99ec20695524 | 93 | void leds(float val) const; |
domkay97 | 21:99ec20695524 | 94 | |
domkay97 | 24:7b149f5d4bef | 95 | /** Set LED to duty-cycle EDIT |
domkay97 | 21:99ec20695524 | 96 | *@param led number (0 to 5) |
domkay97 | 21:99ec20695524 | 97 | *@param value in range 0.0 to 1.0 |
domkay97 | 21:99ec20695524 | 98 | */ |
domkay97 | 21:99ec20695524 | 99 | void led(int n,float val) const; |
domkay97 | 21:99ec20695524 | 100 | |
domkay97 | 21:99ec20695524 | 101 | /** Read potentiometer |
domkay97 | 24:7b149f5d4bef | 102 | *@returns value from 0.0 to 1.0 to effect other classes |
domkay97 | 21:99ec20695524 | 103 | */ |
domkay97 | 24:7b149f5d4bef | 104 | float pot_value() const; |
domkay97 | 21:99ec20695524 | 105 | |
domkay97 | 21:99ec20695524 | 106 | /** Play tone on piezo |
domkay97 | 24:7b149f5d4bef | 107 | * @param frequency of sound(Hz) |
domkay97 | 24:7b149f5d4bef | 108 | * @param duration of sound(s) |
domkay97 | 21:99ec20695524 | 109 | */ |
domkay97 | 24:7b149f5d4bef | 110 | void sound(float frequency, float duration); |
domkay97 | 21:99ec20695524 | 111 | |
domkay97 | 21:99ec20695524 | 112 | /** |
domkay97 | 24:7b149f5d4bef | 113 | * @brief Checks if a specific flag has been set. |
domkay97 | 24:7b149f5d4bef | 114 | * @param id[in] The specific flag ID |
domkay97 | 24:7b149f5d4bef | 115 | * @return true if the flag occurred |
domkay97 | 21:99ec20695524 | 116 | */ |
domkay97 | 21:99ec20695524 | 117 | bool check_event(ControllerEvent const id); |
domkay97 | 21:99ec20695524 | 118 | |
domkay97 | 24:7b149f5d4bef | 119 | /** |
domkay97 | 26:ccdf299b26c4 | 120 | * @brief Checks if joystick is flicked |
domkay97 | 26:ccdf299b26c4 | 121 | * @return x value |
domkay97 | 26:ccdf299b26c4 | 122 | */ |
domkay97 | 26:ccdf299b26c4 | 123 | |
domkay97 | 26:ccdf299b26c4 | 124 | float get_joy(); |
domkay97 | 26:ccdf299b26c4 | 125 | |
domkay97 | 26:ccdf299b26c4 | 126 | /** |
domkay97 | 25:edb07ff38749 | 127 | * @brief Allows for a specific integer to be returned if a specific flag has been set |
domkay97 | 25:edb07ff38749 | 128 | * @return interger unique to the flag set. |
domkay97 | 24:7b149f5d4bef | 129 | */ |
domkay97 | 21:99ec20695524 | 130 | int check_for_buttons(); |
domkay97 | 21:99ec20695524 | 131 | |
domkay97 | 21:99ec20695524 | 132 | |
domkay97 | 26:ccdf299b26c4 | 133 | |
domkay97 | 21:99ec20695524 | 134 | private: |
domkay97 | 21:99ec20695524 | 135 | void init_buttons(); |
domkay97 | 24:7b149f5d4bef | 136 | void sound_off(); |
domkay97 | 21:99ec20695524 | 137 | |
domkay97 | 21:99ec20695524 | 138 | void a_isr(); |
domkay97 | 21:99ec20695524 | 139 | void b_isr(); |
domkay97 | 21:99ec20695524 | 140 | void x_isr(); |
domkay97 | 21:99ec20695524 | 141 | void y_isr(); |
domkay97 | 21:99ec20695524 | 142 | void l_isr(); |
domkay97 | 21:99ec20695524 | 143 | void r_isr(); |
domkay97 | 21:99ec20695524 | 144 | void back_isr(); |
domkay97 | 21:99ec20695524 | 145 | void start_isr(); |
domkay97 | 21:99ec20695524 | 146 | void joy_isr(); |
domkay97 | 21:99ec20695524 | 147 | }; |
domkay97 | 21:99ec20695524 | 148 | |
domkay97 | 21:99ec20695524 | 149 | #endif |