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@16:3f84f2d7b910, 2019-03-26 (annotated)
- Committer:
- el17ajf
- Date:
- Tue Mar 26 10:18:17 2019 +0000
- Revision:
- 16:3f84f2d7b910
- Parent:
- 15:afeefa3ceb61
- Child:
- 22:0a474f074553
lab updates
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 | 12:beb0d7632531 | 4 | |
| el17ajf | 13:59e17cab320a | 5 | namespace Input { |
| el17ajf | 13:59e17cab320a | 6 | |
| el17ajf | 13:59e17cab320a | 7 | enum State {RELEASED, RELEASED_FRAME, HIT_FRAME, HELD}; |
| el17ajf | 13:59e17cab320a | 8 | Gamepad * gamepad; |
| el17ajf | 13:59e17cab320a | 9 | const int NUMBER_OF_BUTTONS = 4; |
| el17ajf | 13:59e17cab320a | 10 | State states[4]; // NUM_OF_BS |
| el17ajf | 15:afeefa3ceb61 | 11 | int frame_hit[4] = {-99}; // NUM_OF_BS |
| el17ajf | 15:afeefa3ceb61 | 12 | int frame = 0; |
| el17ajf | 16:3f84f2d7b910 | 13 | const int bounce_frames = 0; |
| el17ajf | 15:afeefa3ceb61 | 14 | void updateButtonState(Button button, Gamepad::GamepadEvent event); |
| el17ajf | 15:afeefa3ceb61 | 15 | bool canBounce(Button button); |
| el17ajf | 13:59e17cab320a | 16 | |
| el17ajf | 13:59e17cab320a | 17 | void init() { |
| el17ajf | 13:59e17cab320a | 18 | gamepad = new Gamepad(); |
| el17ajf | 13:59e17cab320a | 19 | gamepad->init(); |
| el17ajf | 13:59e17cab320a | 20 | |
| el17ajf | 13:59e17cab320a | 21 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 13:59e17cab320a | 22 | states[i] = RELEASED; |
| el17ajf | 9:3a7776a29a11 | 23 | } |
| el17ajf | 9:3a7776a29a11 | 24 | } |
| el17ajf | 5:3efbdcb3efaf | 25 | |
| el17ajf | 13:59e17cab320a | 26 | void deinit() { |
| el17ajf | 13:59e17cab320a | 27 | delete gamepad; |
| el17ajf | 13:59e17cab320a | 28 | } |
| el17ajf | 13:59e17cab320a | 29 | |
| el17ajf | 13:59e17cab320a | 30 | void update() { |
| el17ajf | 15:afeefa3ceb61 | 31 | frame++; |
| el17ajf | 15:afeefa3ceb61 | 32 | |
| el17ajf | 15:afeefa3ceb61 | 33 | // update based on existing states |
| el17ajf | 13:59e17cab320a | 34 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 13:59e17cab320a | 35 | if (states[i] == RELEASED_FRAME) { |
| el17ajf | 13:59e17cab320a | 36 | states[i] = RELEASED; |
| el17ajf | 13:59e17cab320a | 37 | } |
| el17ajf | 13:59e17cab320a | 38 | if (states[i] == HIT_FRAME) { |
| el17ajf | 15:afeefa3ceb61 | 39 | states[i] = RELEASED_FRAME; |
| el17ajf | 13:59e17cab320a | 40 | } |
| el17ajf | 13:59e17cab320a | 41 | } |
| el17ajf | 13:59e17cab320a | 42 | |
| el17ajf | 15:afeefa3ceb61 | 43 | // update state to reflect events |
| el17ajf | 15:afeefa3ceb61 | 44 | updateButtonState(LEFT, Gamepad::A_PRESSED); |
| el17ajf | 15:afeefa3ceb61 | 45 | updateButtonState(RIGHT, Gamepad::Y_PRESSED); |
| el17ajf | 15:afeefa3ceb61 | 46 | updateButtonState(UP, Gamepad::X_PRESSED); |
| el17ajf | 15:afeefa3ceb61 | 47 | updateButtonState(DOWN, Gamepad::B_PRESSED); |
| el17ajf | 13:59e17cab320a | 48 | } |
| el17ajf | 13:59e17cab320a | 49 | |
| el17ajf | 13:59e17cab320a | 50 | bool buttonHit(Button button) { |
| el17ajf | 13:59e17cab320a | 51 | return states[button] == HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 52 | } |
| el17ajf | 13:59e17cab320a | 53 | |
| el17ajf | 13:59e17cab320a | 54 | bool buttonHeld(Button button) { |
| el17ajf | 13:59e17cab320a | 55 | return states[button] == HIT_FRAME |
| el17ajf | 13:59e17cab320a | 56 | || states[button] == HELD; |
| el17ajf | 12:beb0d7632531 | 57 | } |
| el17ajf | 15:afeefa3ceb61 | 58 | |
| el17ajf | 15:afeefa3ceb61 | 59 | void updateButtonState(Button button, Gamepad::GamepadEvent event) { |
| el17ajf | 15:afeefa3ceb61 | 60 | if (gamepad->check_event(event)) { |
| el17ajf | 15:afeefa3ceb61 | 61 | if (canBounce(button)) { |
| el17ajf | 15:afeefa3ceb61 | 62 | states[button] = HIT_FRAME; |
| el17ajf | 15:afeefa3ceb61 | 63 | frame_hit[button] = frame; |
| el17ajf | 15:afeefa3ceb61 | 64 | } else { |
| el17ajf | 15:afeefa3ceb61 | 65 | states[button] = HELD; |
| el17ajf | 15:afeefa3ceb61 | 66 | } |
| el17ajf | 15:afeefa3ceb61 | 67 | } |
| el17ajf | 15:afeefa3ceb61 | 68 | } |
| el17ajf | 15:afeefa3ceb61 | 69 | |
| el17ajf | 15:afeefa3ceb61 | 70 | bool canBounce(Button button) { |
| el17ajf | 15:afeefa3ceb61 | 71 | if (frame > frame_hit[button] + bounce_frames) { |
| el17ajf | 15:afeefa3ceb61 | 72 | return true; |
| el17ajf | 15:afeefa3ceb61 | 73 | } else { |
| el17ajf | 15:afeefa3ceb61 | 74 | return false; |
| el17ajf | 15:afeefa3ceb61 | 75 | } |
| el17ajf | 15:afeefa3ceb61 | 76 | } |
| el17ajf | 9:3a7776a29a11 | 77 | } |
