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
Fork of el17ajf by
Input/Input.cpp@22:0a474f074553, 2019-04-04 (annotated)
- Committer:
- el17ajf
- Date:
- Thu Apr 04 07:08:39 2019 +0000
- Revision:
- 22:0a474f074553
- Parent:
- 16:3f84f2d7b910
- Child:
- 25:bf47fe41883a
Added pause menu
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17ajf | 5:3efbdcb3efaf | 1 | #include "Input.h" |
| el17ajf | 13:59e17cab320a | 2 | #include "Gamepad.h" |
| el17ajf | 12:beb0d7632531 | 3 | #include "mbed.h" // TODO DELETE |
| el17ajf | 22:0a474f074553 | 4 | namespace Input { |
| el17ajf | 22:0a474f074553 | 5 | |
| el17ajf | 22:0a474f074553 | 6 | enum State {NOT_HIT, HIT}; |
| el17ajf | 22:0a474f074553 | 7 | Gamepad * gamepad; |
| el17ajf | 22:0a474f074553 | 8 | const int NUMBER_OF_BUTTONS = 5; |
| el17ajf | 22:0a474f074553 | 9 | State states[5]; // NUM_OF_BS |
| el17ajf | 22:0a474f074553 | 10 | |
| el17ajf | 22:0a474f074553 | 11 | void updateButtonState(Button button, Gamepad::GamepadEvent event); |
| el17ajf | 22:0a474f074553 | 12 | |
| el17ajf | 22:0a474f074553 | 13 | void init() { |
| el17ajf | 22:0a474f074553 | 14 | gamepad = new Gamepad(); |
| el17ajf | 22:0a474f074553 | 15 | gamepad->init(); |
| el17ajf | 22:0a474f074553 | 16 | |
| el17ajf | 22:0a474f074553 | 17 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 22:0a474f074553 | 18 | states[i] = NOT_HIT; |
| el17ajf | 22:0a474f074553 | 19 | } |
| el17ajf | 22:0a474f074553 | 20 | } |
| el17ajf | 22:0a474f074553 | 21 | |
| el17ajf | 22:0a474f074553 | 22 | void deinit() { |
| el17ajf | 22:0a474f074553 | 23 | delete gamepad; |
| el17ajf | 22:0a474f074553 | 24 | } |
| el17ajf | 22:0a474f074553 | 25 | |
| el17ajf | 22:0a474f074553 | 26 | void update() { |
| el17ajf | 22:0a474f074553 | 27 | // reset states |
| el17ajf | 22:0a474f074553 | 28 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 22:0a474f074553 | 29 | states[i] = NOT_HIT; |
| el17ajf | 22:0a474f074553 | 30 | } |
| el17ajf | 22:0a474f074553 | 31 | |
| el17ajf | 22:0a474f074553 | 32 | // update state to reflect events |
| el17ajf | 22:0a474f074553 | 33 | updateButtonState(LEFT, Gamepad::A_PRESSED); |
| el17ajf | 22:0a474f074553 | 34 | updateButtonState(RIGHT, Gamepad::Y_PRESSED); |
| el17ajf | 22:0a474f074553 | 35 | updateButtonState(UP, Gamepad::X_PRESSED); |
| el17ajf | 22:0a474f074553 | 36 | updateButtonState(DOWN, Gamepad::B_PRESSED); |
| el17ajf | 22:0a474f074553 | 37 | updateButtonState(START, Gamepad::START_PRESSED); |
| el17ajf | 22:0a474f074553 | 38 | } |
| el17ajf | 22:0a474f074553 | 39 | |
| el17ajf | 22:0a474f074553 | 40 | bool buttonHit(Button button) { |
| el17ajf | 22:0a474f074553 | 41 | return states[button] == HIT; |
| el17ajf | 22:0a474f074553 | 42 | } |
| el17ajf | 22:0a474f074553 | 43 | |
| el17ajf | 22:0a474f074553 | 44 | void updateButtonState(Button button, Gamepad::GamepadEvent event) { |
| el17ajf | 22:0a474f074553 | 45 | if (gamepad->check_event(event)) { |
| el17ajf | 22:0a474f074553 | 46 | states[button] = HIT; |
| el17ajf | 22:0a474f074553 | 47 | } |
| el17ajf | 22:0a474f074553 | 48 | } |
| el17ajf | 22:0a474f074553 | 49 | }; |
| el17ajf | 22:0a474f074553 | 50 | /* |
| el17ajf | 13:59e17cab320a | 51 | namespace Input { |
| el17ajf | 13:59e17cab320a | 52 | |
| el17ajf | 13:59e17cab320a | 53 | enum State {RELEASED, RELEASED_FRAME, HIT_FRAME, HELD}; |
| el17ajf | 13:59e17cab320a | 54 | Gamepad * gamepad; |
| el17ajf | 22:0a474f074553 | 55 | const int NUMBER_OF_BUTTONS = 5; |
| el17ajf | 22:0a474f074553 | 56 | State states[5]; // NUM_OF_BS |
| el17ajf | 22:0a474f074553 | 57 | int frame_hit[5] = {-99}; // NUM_OF_BS |
| el17ajf | 15:afeefa3ceb61 | 58 | int frame = 0; |
| el17ajf | 16:3f84f2d7b910 | 59 | const int bounce_frames = 0; |
| el17ajf | 15:afeefa3ceb61 | 60 | void updateButtonState(Button button, Gamepad::GamepadEvent event); |
| el17ajf | 15:afeefa3ceb61 | 61 | bool canBounce(Button button); |
| el17ajf | 13:59e17cab320a | 62 | |
| el17ajf | 13:59e17cab320a | 63 | void init() { |
| el17ajf | 13:59e17cab320a | 64 | gamepad = new Gamepad(); |
| el17ajf | 13:59e17cab320a | 65 | gamepad->init(); |
| el17ajf | 13:59e17cab320a | 66 | |
| el17ajf | 13:59e17cab320a | 67 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 13:59e17cab320a | 68 | states[i] = RELEASED; |
| el17ajf | 9:3a7776a29a11 | 69 | } |
| el17ajf | 9:3a7776a29a11 | 70 | } |
| el17ajf | 5:3efbdcb3efaf | 71 | |
| el17ajf | 13:59e17cab320a | 72 | void deinit() { |
| el17ajf | 13:59e17cab320a | 73 | delete gamepad; |
| el17ajf | 13:59e17cab320a | 74 | } |
| el17ajf | 13:59e17cab320a | 75 | |
| el17ajf | 13:59e17cab320a | 76 | void update() { |
| el17ajf | 15:afeefa3ceb61 | 77 | frame++; |
| el17ajf | 15:afeefa3ceb61 | 78 | |
| el17ajf | 15:afeefa3ceb61 | 79 | // update based on existing states |
| el17ajf | 13:59e17cab320a | 80 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 13:59e17cab320a | 81 | if (states[i] == RELEASED_FRAME) { |
| el17ajf | 13:59e17cab320a | 82 | states[i] = RELEASED; |
| el17ajf | 13:59e17cab320a | 83 | } |
| el17ajf | 13:59e17cab320a | 84 | if (states[i] == HIT_FRAME) { |
| el17ajf | 15:afeefa3ceb61 | 85 | states[i] = RELEASED_FRAME; |
| el17ajf | 13:59e17cab320a | 86 | } |
| el17ajf | 13:59e17cab320a | 87 | } |
| el17ajf | 13:59e17cab320a | 88 | |
| el17ajf | 15:afeefa3ceb61 | 89 | // update state to reflect events |
| el17ajf | 15:afeefa3ceb61 | 90 | updateButtonState(LEFT, Gamepad::A_PRESSED); |
| el17ajf | 15:afeefa3ceb61 | 91 | updateButtonState(RIGHT, Gamepad::Y_PRESSED); |
| el17ajf | 15:afeefa3ceb61 | 92 | updateButtonState(UP, Gamepad::X_PRESSED); |
| el17ajf | 15:afeefa3ceb61 | 93 | updateButtonState(DOWN, Gamepad::B_PRESSED); |
| el17ajf | 22:0a474f074553 | 94 | updateButtonState(START, Gamepad::START_PRESSED); |
| el17ajf | 13:59e17cab320a | 95 | } |
| el17ajf | 13:59e17cab320a | 96 | |
| el17ajf | 13:59e17cab320a | 97 | bool buttonHit(Button button) { |
| el17ajf | 13:59e17cab320a | 98 | return states[button] == HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 99 | } |
| el17ajf | 13:59e17cab320a | 100 | |
| el17ajf | 13:59e17cab320a | 101 | bool buttonHeld(Button button) { |
| el17ajf | 13:59e17cab320a | 102 | return states[button] == HIT_FRAME |
| el17ajf | 13:59e17cab320a | 103 | || states[button] == HELD; |
| el17ajf | 12:beb0d7632531 | 104 | } |
| el17ajf | 15:afeefa3ceb61 | 105 | |
| el17ajf | 15:afeefa3ceb61 | 106 | void updateButtonState(Button button, Gamepad::GamepadEvent event) { |
| el17ajf | 15:afeefa3ceb61 | 107 | if (gamepad->check_event(event)) { |
| el17ajf | 15:afeefa3ceb61 | 108 | if (canBounce(button)) { |
| el17ajf | 15:afeefa3ceb61 | 109 | states[button] = HIT_FRAME; |
| el17ajf | 15:afeefa3ceb61 | 110 | frame_hit[button] = frame; |
| el17ajf | 15:afeefa3ceb61 | 111 | } else { |
| el17ajf | 15:afeefa3ceb61 | 112 | states[button] = HELD; |
| el17ajf | 15:afeefa3ceb61 | 113 | } |
| el17ajf | 15:afeefa3ceb61 | 114 | } |
| el17ajf | 15:afeefa3ceb61 | 115 | } |
| el17ajf | 15:afeefa3ceb61 | 116 | |
| el17ajf | 15:afeefa3ceb61 | 117 | bool canBounce(Button button) { |
| el17ajf | 15:afeefa3ceb61 | 118 | if (frame > frame_hit[button] + bounce_frames) { |
| el17ajf | 15:afeefa3ceb61 | 119 | return true; |
| el17ajf | 15:afeefa3ceb61 | 120 | } else { |
| el17ajf | 15:afeefa3ceb61 | 121 | return false; |
| el17ajf | 15:afeefa3ceb61 | 122 | } |
| el17ajf | 15:afeefa3ceb61 | 123 | } |
| el17ajf | 9:3a7776a29a11 | 124 | } |
| el17ajf | 22:0a474f074553 | 125 | */ |
