button press and blinking led on a button press
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// src/hardware/button.cpp
|
||||
#include "button.h"
|
||||
|
||||
ButtonHandler::ButtonHandler(PinName pin) : button(pin) {
|
||||
ButtonHandler::ButtonHandler(PinName pin, PinName led_pin) : button(pin), led(led_pin, 0) {
|
||||
// ISR for button press (falling edge)
|
||||
button.fall(callback(this, &ButtonHandler::on_pressed));
|
||||
}
|
||||
@@ -11,9 +11,23 @@ void ButtonHandler::on_pressed() {
|
||||
}
|
||||
|
||||
ButtonEvent ButtonHandler::poll() {
|
||||
static Timer blink_timer;
|
||||
static bool blinking = false;
|
||||
|
||||
if (pressed_flag) {
|
||||
pressed_flag = false;
|
||||
led = 1;
|
||||
blink_timer.reset();
|
||||
blink_timer.start();
|
||||
blinking = true;
|
||||
return ButtonEvent::Pressed;
|
||||
}
|
||||
|
||||
if (blinking && blink_timer.elapsed_time() >= 500ms) {
|
||||
led = 0;
|
||||
blink_timer.stop();
|
||||
blinking = false;
|
||||
}
|
||||
|
||||
return ButtonEvent::None;
|
||||
}
|
||||
|
||||
@@ -10,12 +10,14 @@ enum class ButtonEvent {
|
||||
class ButtonHandler {
|
||||
private:
|
||||
InterruptIn button;
|
||||
DigitalOut led;
|
||||
volatile bool pressed_flag = false;
|
||||
volatile bool blink_flag = false;
|
||||
|
||||
void on_pressed();
|
||||
|
||||
public:
|
||||
ButtonHandler(PinName pin = BUTTON1);
|
||||
ButtonHandler(PinName pin = BUTTON1, PinName led_pin = LED1);
|
||||
|
||||
ButtonEvent poll();
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "background.h"
|
||||
#include "mbed.h"
|
||||
#include "player.h"
|
||||
#include "background.h"
|
||||
#include "../game/state.h"
|
||||
#include "../game/animation.h"
|
||||
#include "../game/collision.h"
|
||||
@@ -22,8 +23,6 @@ extern DigitalOut led;
|
||||
constexpr int PLAYER_X = 29;
|
||||
constexpr int PLAYER_Y = 6;
|
||||
|
||||
void draw_mask(const char *unused_filename, int shift);
|
||||
|
||||
void render_loop(int speed) {
|
||||
MovementState player_state;
|
||||
AnimationController animation;
|
||||
|
||||
Reference in New Issue
Block a user