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@13:59e17cab320a, 2019-03-18 (annotated)
- Committer:
- el17ajf
- Date:
- Mon Mar 18 18:09:57 2019 +0000
- Revision:
- 13:59e17cab320a
- Parent:
- 12:beb0d7632531
- Child:
- 15:afeefa3ceb61
got tetrominos dropping and moving!
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 | 13:59e17cab320a | 11 | |
| el17ajf | 13:59e17cab320a | 12 | void init() { |
| el17ajf | 13:59e17cab320a | 13 | gamepad = new Gamepad(); |
| el17ajf | 13:59e17cab320a | 14 | gamepad->init(); |
| el17ajf | 13:59e17cab320a | 15 | |
| el17ajf | 13:59e17cab320a | 16 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 13:59e17cab320a | 17 | states[i] = RELEASED; |
| el17ajf | 9:3a7776a29a11 | 18 | } |
| el17ajf | 9:3a7776a29a11 | 19 | } |
| el17ajf | 5:3efbdcb3efaf | 20 | |
| el17ajf | 13:59e17cab320a | 21 | void deinit() { |
| el17ajf | 13:59e17cab320a | 22 | delete gamepad; |
| el17ajf | 13:59e17cab320a | 23 | } |
| el17ajf | 13:59e17cab320a | 24 | |
| el17ajf | 13:59e17cab320a | 25 | void update() { |
| el17ajf | 13:59e17cab320a | 26 | // update existing states |
| el17ajf | 13:59e17cab320a | 27 | for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { |
| el17ajf | 13:59e17cab320a | 28 | if (states[i] == RELEASED_FRAME) { |
| el17ajf | 13:59e17cab320a | 29 | states[i] = RELEASED; |
| el17ajf | 13:59e17cab320a | 30 | } |
| el17ajf | 13:59e17cab320a | 31 | if (states[i] == HIT_FRAME) { |
| el17ajf | 13:59e17cab320a | 32 | states[i] = HELD; |
| el17ajf | 13:59e17cab320a | 33 | } |
| el17ajf | 13:59e17cab320a | 34 | } |
| el17ajf | 13:59e17cab320a | 35 | |
| el17ajf | 13:59e17cab320a | 36 | // check inputs |
| el17ajf | 13:59e17cab320a | 37 | if (gamepad->check_event(Gamepad::A_PRESSED)) { |
| el17ajf | 13:59e17cab320a | 38 | states[LEFT] = HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 39 | } |
| el17ajf | 13:59e17cab320a | 40 | if (gamepad->check_event(Gamepad::Y_PRESSED)) { |
| el17ajf | 13:59e17cab320a | 41 | states[RIGHT] = HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 42 | } |
| el17ajf | 13:59e17cab320a | 43 | if (gamepad->check_event(Gamepad::X_PRESSED)) { |
| el17ajf | 13:59e17cab320a | 44 | states[UP] = HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 45 | } |
| el17ajf | 13:59e17cab320a | 46 | if (gamepad->check_event(Gamepad::B_PRESSED)) { |
| el17ajf | 13:59e17cab320a | 47 | states[DOWN] = HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 48 | } |
| el17ajf | 13:59e17cab320a | 49 | } |
| el17ajf | 13:59e17cab320a | 50 | |
| el17ajf | 13:59e17cab320a | 51 | bool buttonHit(Button button) { |
| el17ajf | 13:59e17cab320a | 52 | return states[button] == HIT_FRAME; |
| el17ajf | 13:59e17cab320a | 53 | } |
| el17ajf | 13:59e17cab320a | 54 | |
| el17ajf | 13:59e17cab320a | 55 | bool buttonHeld(Button button) { |
| el17ajf | 13:59e17cab320a | 56 | return states[button] == HIT_FRAME |
| el17ajf | 13:59e17cab320a | 57 | || states[button] == HELD; |
| el17ajf | 12:beb0d7632531 | 58 | } |
| el17ajf | 9:3a7776a29a11 | 59 | } |
