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
- Committer:
- el17ajf
- Date:
- 2019-03-18
- Revision:
- 13:59e17cab320a
- Parent:
- 12:beb0d7632531
- Child:
- 15:afeefa3ceb61
File content as of revision 13:59e17cab320a:
#include "Input.h"
#include "Gamepad.h"
#include "mbed.h" // TODO DELETE
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;
}
}
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;
}
}
