physical button press
This commit is contained in:
19
semestralka1/src/hardware/button.cpp
Normal file
19
semestralka1/src/hardware/button.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
// src/hardware/button.cpp
|
||||
#include "button.h"
|
||||
|
||||
ButtonHandler::ButtonHandler(PinName pin) : button(pin) {
|
||||
// ISR for button press (falling edge)
|
||||
button.fall(callback(this, &ButtonHandler::on_pressed));
|
||||
}
|
||||
|
||||
void ButtonHandler::on_pressed() {
|
||||
pressed_flag = true;
|
||||
}
|
||||
|
||||
ButtonEvent ButtonHandler::poll() {
|
||||
if (pressed_flag) {
|
||||
pressed_flag = false;
|
||||
return ButtonEvent::Pressed;
|
||||
}
|
||||
return ButtonEvent::None;
|
||||
}
|
||||
21
semestralka1/src/hardware/button.h
Normal file
21
semestralka1/src/hardware/button.h
Normal file
@@ -0,0 +1,21 @@
|
||||
// src/hardware/button.h
|
||||
#pragma once
|
||||
#include "mbed.h"
|
||||
|
||||
enum class ButtonEvent {
|
||||
None = 0,
|
||||
Pressed = 1,
|
||||
};
|
||||
|
||||
class ButtonHandler {
|
||||
private:
|
||||
InterruptIn button;
|
||||
volatile bool pressed_flag = false;
|
||||
|
||||
void on_pressed();
|
||||
|
||||
public:
|
||||
ButtonHandler(PinName pin = BUTTON1);
|
||||
|
||||
ButtonEvent poll();
|
||||
};
|
||||
@@ -1,49 +0,0 @@
|
||||
// src/hardware/uart.cpp
|
||||
#include "uart.h"
|
||||
#include <cstring>
|
||||
|
||||
UartReader::UartReader(BufferedSerial &serial) : serial_port(serial) {
|
||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||
memset(message, 0, sizeof(message));
|
||||
}
|
||||
|
||||
UartEvent UartReader::poll() {
|
||||
bool changed = false;
|
||||
bool triggered = false;
|
||||
|
||||
// cita spravu z uartu
|
||||
if (serial_port.readable()) {
|
||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
||||
if (num > 0) {
|
||||
strncpy(message, rx_buffer, sizeof(message) - 1);
|
||||
message_active = true;
|
||||
changed = true;
|
||||
triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// casovac na 1s zobrazenia spravy
|
||||
if (!timer_started) {
|
||||
msg_timer.start();
|
||||
timer_started = true;
|
||||
}
|
||||
|
||||
// po jednu sekundu sa sprava zobrazi
|
||||
if (message_active && msg_timer.elapsed_time() > MESSAGE_DISPLAY_DURATION) {
|
||||
message_active = false;
|
||||
memset(message, 0, sizeof(message));
|
||||
msg_timer.reset();
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (triggered)
|
||||
return UartEvent::Triggered;
|
||||
if (changed)
|
||||
return UartEvent::MessageUpdate;
|
||||
return UartEvent::NoChange;
|
||||
}
|
||||
|
||||
const char* UartReader::get_message() const {
|
||||
return message_active ? message : nullptr;
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
// src/hardware/uart.h
|
||||
#pragma once
|
||||
#include "mbed.h"
|
||||
#include <cstddef>
|
||||
|
||||
constexpr size_t UART_BUFFER_SIZE = 64;
|
||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
||||
|
||||
enum class UartEvent {
|
||||
NoChange = 0, // No new data
|
||||
MessageUpdate = 1, // Message changed (received or expired)
|
||||
Triggered = 2 // New message received (trigger action)
|
||||
};
|
||||
|
||||
class UartReader {
|
||||
private:
|
||||
BufferedSerial &serial_port;
|
||||
|
||||
char rx_buffer[UART_BUFFER_SIZE];
|
||||
char message[UART_BUFFER_SIZE];
|
||||
bool message_active = false;
|
||||
|
||||
Timer msg_timer;
|
||||
bool timer_started = false;
|
||||
|
||||
public:
|
||||
UartReader(BufferedSerial &serial);
|
||||
|
||||
// Poll for UART events
|
||||
UartEvent poll();
|
||||
|
||||
// Get current message (nullptr if no active message)
|
||||
const char* get_message() const;
|
||||
bool has_message() const { return message_active; }
|
||||
};
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
void draw_mask(const char *unused_filename, int shift, const char *text) {
|
||||
void draw_mask(const char *unused_filename, int shift) {
|
||||
// Terminal clear + home
|
||||
printf("\033[2J\033[H");
|
||||
|
||||
@@ -25,9 +25,5 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
if (text && text[0] != '\0') {
|
||||
printf("\r\n[RX] %s\r\n", text);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
@@ -4,4 +4,4 @@
|
||||
constexpr int VIEW_WIDTH = 90;
|
||||
|
||||
// Draws the ASCII art background with optional message
|
||||
void draw_mask(const char *unused_filename, int shift, const char *text = nullptr);
|
||||
void draw_mask(const char *unused_filename, int shift);
|
||||
|
||||
@@ -8,14 +8,13 @@
|
||||
#include "../game/state.h"
|
||||
#include "../game/animation.h"
|
||||
#include "../game/collision.h"
|
||||
#include "../hardware/uart.h"
|
||||
#include "../hardware/button.h"
|
||||
#include "../render/player.h"
|
||||
#include "../render/obstacle.h"
|
||||
#include "../timing/speed_controller.h"
|
||||
#include "../timing/movement_controller.h"
|
||||
#include "../game/obstacle_system.h"
|
||||
|
||||
extern BufferedSerial serial_port;
|
||||
extern DigitalOut led;
|
||||
|
||||
// Constants
|
||||
@@ -23,12 +22,12 @@ extern DigitalOut led;
|
||||
constexpr int PLAYER_X = 29;
|
||||
constexpr int PLAYER_Y = 6;
|
||||
|
||||
void draw_mask(const char *unused_filename, int shift, const char *text);
|
||||
void draw_mask(const char *unused_filename, int shift);
|
||||
|
||||
void render_loop(int speed) {
|
||||
MovementState player_state;
|
||||
AnimationController animation;
|
||||
UartReader uart(serial_port);
|
||||
ButtonHandler button;
|
||||
MovementController mover(PLAYER_X, VIEW_WIDTH);
|
||||
SpeedController timing;
|
||||
timing.set_ground_speed(speed);
|
||||
@@ -69,19 +68,14 @@ void render_loop(int speed) {
|
||||
}
|
||||
|
||||
need_redraw = false;
|
||||
UartEvent uart_event = uart.poll();
|
||||
ButtonEvent button_event = button.poll();
|
||||
|
||||
if (uart_event == UartEvent::MessageUpdate) {
|
||||
need_redraw = true;
|
||||
}
|
||||
|
||||
if (uart_event == UartEvent::Triggered) {
|
||||
if (button_event == ButtonEvent::Pressed) {
|
||||
PlayerState current = player_state.get_state();
|
||||
|
||||
if (current == PlayerState::Walk || current == PlayerState::Run) {
|
||||
player_state.start_crawl(PlayerState::Crawl1);
|
||||
}
|
||||
else if (current == PlayerState::Crawl1) {
|
||||
} else if (current == PlayerState::Crawl1) {
|
||||
player_state.start_crawl(PlayerState::Crawl2);
|
||||
}
|
||||
|
||||
@@ -108,7 +102,7 @@ void render_loop(int speed) {
|
||||
|
||||
if (need_redraw) {
|
||||
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
||||
draw_mask(bg_file, animation.get_shift());
|
||||
FrameSelection frame = player_state.get_frame_selection();
|
||||
|
||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||
|
||||
Reference in New Issue
Block a user