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:
- 25:bf47fe41883a
- Parent:
- 22:0a474f074553
- Child:
- 31:69fedaa9b171
--- a/Input/Input.cpp Thu Apr 04 08:08:01 2019 +0000
+++ b/Input/Input.cpp Thu Apr 04 15:53:06 2019 +0000
@@ -1,125 +1,31 @@
#include "Input.h"
-#include "Gamepad.h"
-#include "mbed.h" // TODO DELETE
-namespace Input {
-
- enum State {NOT_HIT, HIT};
- Gamepad * gamepad;
- const int NUMBER_OF_BUTTONS = 5;
- State states[5]; // NUM_OF_BS
-
- void updateButtonState(Button button, Gamepad::GamepadEvent event);
-
- void init() {
- gamepad = new Gamepad();
- gamepad->init();
-
- for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
- states[i] = NOT_HIT;
- }
- }
-
- void deinit() {
- delete gamepad;
- }
-
- void update() {
- // reset states
- for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
- states[i] = NOT_HIT;
- }
-
- // 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);
- updateButtonState(START, Gamepad::START_PRESSED);
- }
-
- bool buttonHit(Button button) {
- return states[button] == HIT;
- }
-
- void updateButtonState(Button button, Gamepad::GamepadEvent event) {
- if (gamepad->check_event(event)) {
- states[button] = HIT;
- }
- }
-};
-/*
+#include "SampledIn.h"
+
namespace Input {
- enum State {RELEASED, RELEASED_FRAME, HIT_FRAME, HELD};
- Gamepad * gamepad;
- const int NUMBER_OF_BUTTONS = 5;
- State states[5]; // NUM_OF_BS
- int frame_hit[5] = {-99}; // NUM_OF_BS
- int frame = 0;
- const int bounce_frames = 0;
- void updateButtonState(Button button, Gamepad::GamepadEvent event);
- bool canBounce(Button button);
+ SampledIn aButton(PTB9);
+ SampledIn bButton(PTD0);
+ SampledIn xButton(PTC17);
+ SampledIn yButton(PTC12);
+ SampledIn lButton(PTB18);
+ SampledIn rButton(PTB3);
+ SampledIn backButton(PTB19);
+ SampledIn startButton(PTC5);
- void init() {
- gamepad = new Gamepad();
- gamepad->init();
-
- for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
- states[i] = RELEASED;
+ bool buttonHit(Button button) {
+ switch (button) {
+ case LEFT:
+ return aButton.hit_frame();
+ case RIGHT:
+ return yButton.hit_frame();
+ case UP:
+ return xButton.hit_frame();
+ case DOWN:
+ return bButton.hit_frame();
+ case START:
+ return startButton.hit_frame();
+ default:
+ return false;
}
}
-
- void deinit() {
- delete gamepad;
- }
-
- void update() {
- 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] = RELEASED_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);
- updateButtonState(START, Gamepad::START_PRESSED);
- }
-
- bool buttonHit(Button button) {
- return states[button] == HIT_FRAME;
- }
-
- bool buttonHeld(Button button) {
- 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;
- }
- }
-}
-*/
\ No newline at end of file
+};
\ No newline at end of file
