button press and blinking led on a button press

This commit is contained in:
Priec
2025-12-11 11:30:56 +01:00
parent 165540588f
commit 8382d1caec
3 changed files with 19 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
// src/hardware/button.cpp // src/hardware/button.cpp
#include "button.h" #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) // ISR for button press (falling edge)
button.fall(callback(this, &ButtonHandler::on_pressed)); button.fall(callback(this, &ButtonHandler::on_pressed));
} }
@@ -11,9 +11,23 @@ void ButtonHandler::on_pressed() {
} }
ButtonEvent ButtonHandler::poll() { ButtonEvent ButtonHandler::poll() {
static Timer blink_timer;
static bool blinking = false;
if (pressed_flag) { if (pressed_flag) {
pressed_flag = false; pressed_flag = false;
led = 1;
blink_timer.reset();
blink_timer.start();
blinking = true;
return ButtonEvent::Pressed; return ButtonEvent::Pressed;
} }
if (blinking && blink_timer.elapsed_time() >= 500ms) {
led = 0;
blink_timer.stop();
blinking = false;
}
return ButtonEvent::None; return ButtonEvent::None;
} }

View File

@@ -10,12 +10,14 @@ enum class ButtonEvent {
class ButtonHandler { class ButtonHandler {
private: private:
InterruptIn button; InterruptIn button;
DigitalOut led;
volatile bool pressed_flag = false; volatile bool pressed_flag = false;
volatile bool blink_flag = false;
void on_pressed(); void on_pressed();
public: public:
ButtonHandler(PinName pin = BUTTON1); ButtonHandler(PinName pin = BUTTON1, PinName led_pin = LED1);
ButtonEvent poll(); ButtonEvent poll();
}; };

View File

@@ -5,6 +5,7 @@
#include "background.h" #include "background.h"
#include "mbed.h" #include "mbed.h"
#include "player.h" #include "player.h"
#include "background.h"
#include "../game/state.h" #include "../game/state.h"
#include "../game/animation.h" #include "../game/animation.h"
#include "../game/collision.h" #include "../game/collision.h"
@@ -22,8 +23,6 @@ extern DigitalOut led;
constexpr int PLAYER_X = 29; constexpr int PLAYER_X = 29;
constexpr int PLAYER_Y = 6; constexpr int PLAYER_Y = 6;
void draw_mask(const char *unused_filename, int shift);
void render_loop(int speed) { void render_loop(int speed) {
MovementState player_state; MovementState player_state;
AnimationController animation; AnimationController animation;