From 8382d1caecc689f4dc287cda6fb5f28e2efb4c85 Mon Sep 17 00:00:00 2001 From: Priec Date: Thu, 11 Dec 2025 11:30:56 +0100 Subject: [PATCH] button press and blinking led on a button press --- semestralka1/src/hardware/button.cpp | 16 +++++++++++++++- semestralka1/src/hardware/button.h | 4 +++- semestralka1/src/render/loop.cpp | 3 +-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/semestralka1/src/hardware/button.cpp b/semestralka1/src/hardware/button.cpp index 90b20c3..8932b9f 100644 --- a/semestralka1/src/hardware/button.cpp +++ b/semestralka1/src/hardware/button.cpp @@ -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; } diff --git a/semestralka1/src/hardware/button.h b/semestralka1/src/hardware/button.h index 60e4080..1748d8f 100644 --- a/semestralka1/src/hardware/button.h +++ b/semestralka1/src/hardware/button.h @@ -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(); }; diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index 4a38189..8822978 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -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;