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
Diff: Input/Input.cpp
- Revision:
- 13:59e17cab320a
- Parent:
- 12:beb0d7632531
- Child:
- 15:afeefa3ceb61
--- a/Input/Input.cpp Sun Mar 17 12:14:56 2019 +0000
+++ b/Input/Input.cpp Mon Mar 18 18:09:57 2019 +0000
@@ -1,49 +1,59 @@
#include "Input.h"
+#include "Gamepad.h"
#include "mbed.h" // TODO DELETE
-const int NUMBER_OF_BUTTONS = 4;
-
-Input::State Input::states[NUMBER_OF_BUTTONS];
-Gamepad * Input::gamepad;
-
-void Input::init() {
- gamepad = new Gamepad();
- gamepad->init();
- //printf("%d\n", NUMBER_OF_BUTTONS);
- /*
- for (int i = 0; i < 4; i++) {
- Input::states[i] = RELEASED;
- }
- */
-}
-
-void Input::deinit() {
- delete gamepad;
-}
-
-void Input::update() {
- // update existing states
- for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
- if (Input::states[i] == RELEASED_FRAME) {
- Input::states[i] = RELEASED;
- }
- if (Input::states[i] == HIT_FRAME) {
- Input::states[i] = HELD;
+namespace Input {
+
+ enum State {RELEASED, RELEASED_FRAME, HIT_FRAME, HELD};
+ Gamepad * gamepad;
+ const int NUMBER_OF_BUTTONS = 4;
+ State states[4]; // NUM_OF_BS
+
+ void init() {
+ gamepad = new Gamepad();
+ gamepad->init();
+
+ for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
+ states[i] = RELEASED;
}
}
- // check inputs
- if (gamepad->check_event(Gamepad::Y_PRESSED)) {
-// states[RIGHT] = HIT_FRAME;
- printf("RIGHT\n");
+ void deinit() {
+ delete gamepad;
+ }
+
+ void update() {
+ // update existing states
+ for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
+ if (states[i] == RELEASED_FRAME) {
+ states[i] = RELEASED;
+ }
+ if (states[i] == HIT_FRAME) {
+ states[i] = HELD;
+ }
+ }
+
+ // check inputs
+ if (gamepad->check_event(Gamepad::A_PRESSED)) {
+ states[LEFT] = HIT_FRAME;
+ }
+ if (gamepad->check_event(Gamepad::Y_PRESSED)) {
+ states[RIGHT] = HIT_FRAME;
+ }
+ if (gamepad->check_event(Gamepad::X_PRESSED)) {
+ states[UP] = HIT_FRAME;
+ }
+ if (gamepad->check_event(Gamepad::B_PRESSED)) {
+ states[DOWN] = HIT_FRAME;
+ }
+ }
+
+ bool buttonHit(Button button) {
+ return states[button] == HIT_FRAME;
+ }
+
+ bool buttonHeld(Button button) {
+ return states[button] == HIT_FRAME
+ || states[button] == HELD;
}
}
-
-bool Input::buttonHit(Button button) {
- return Input::states[button] == HIT_FRAME;
-}
-
-bool Input::buttonHeld(Button button) {
- return Input::states[button] == HIT_FRAME
- || Input::states[button] == HELD;
-}
\ No newline at end of file
