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:
- 15:afeefa3ceb61
- Parent:
- 13:59e17cab320a
- Child:
- 16:3f84f2d7b910
--- a/Input/Input.cpp Mon Mar 18 18:26:11 2019 +0000
+++ b/Input/Input.cpp Tue Mar 19 10:36:51 2019 +0000
@@ -8,6 +8,11 @@
Gamepad * gamepad;
const int NUMBER_OF_BUTTONS = 4;
State states[4]; // NUM_OF_BS
+ int frame_hit[4] = {-99}; // NUM_OF_BS
+ int frame = 0;
+ const int bounce_frames = 3;
+ void updateButtonState(Button button, Gamepad::GamepadEvent event);
+ bool canBounce(Button button);
void init() {
gamepad = new Gamepad();
@@ -23,29 +28,23 @@
}
void update() {
- // update existing states
+ frame++;
+
+ // update based on 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;
+ states[i] = RELEASED_FRAME;
}
}
- // 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;
- }
+ // update state to reflect events
+ updateButtonState(LEFT, Gamepad::A_PRESSED);
+ updateButtonState(RIGHT, Gamepad::Y_PRESSED);
+ updateButtonState(UP, Gamepad::X_PRESSED);
+ updateButtonState(DOWN, Gamepad::B_PRESSED);
}
bool buttonHit(Button button) {
@@ -56,4 +55,23 @@
return states[button] == HIT_FRAME
|| states[button] == HELD;
}
+
+ void updateButtonState(Button button, Gamepad::GamepadEvent event) {
+ if (gamepad->check_event(event)) {
+ if (canBounce(button)) {
+ states[button] = HIT_FRAME;
+ frame_hit[button] = frame;
+ } else {
+ states[button] = HELD;
+ }
+ }
+ }
+
+ bool canBounce(Button button) {
+ if (frame > frame_hit[button] + bounce_frames) {
+ return true;
+ } else {
+ return false;
+ }
+ }
}
