7 Commits

Author SHA1 Message Date
Priec
761b86585a removed legacy LED blinking 2025-11-14 21:03:06 +01:00
Priec
528e38b428 another movement done 2025-11-14 20:39:58 +01:00
Priec
a59192cf05 pitch perfect movement 2025-11-14 20:07:38 +01:00
Priec
fa7e18e4d5 BIG MOVES 2025-11-14 19:59:04 +01:00
Priec
4ed4452fae some renames, nothing much 2025-11-14 19:39:19 +01:00
Priec
b9ca8df59b small syntax change 2025-11-14 19:25:25 +01:00
Priec
8fa8bf392b usage of enums for frames 2025-11-14 19:20:14 +01:00
16 changed files with 302 additions and 206 deletions

View File

@@ -1,4 +1,4 @@
// background_dark_inverted.h // src/assets/background_frame.h
#pragma once #pragma once
static const char *BACKGROUND_MASK[] = { static const char *BACKGROUND_MASK[] = {

View File

@@ -0,0 +1,54 @@
// src/assets/character_frames.h
#pragma once
// Player frame 1
static const char *CHARACTER_MASK_FRAME_1[] = {
".....a@$..",
".....7PF..",
"..yaM@@__y",
"..@.y@FFF~",
"..._$M@_..",
"yaa@~.`@y.",
"........@r"
};
// Player frame 2
static const char *CHARACTER_MASK_FRAME_2[] = {
".g@$",
".7PF",
"y@$.",
"0@F.",
"4@$.",
"y$@.",
"u@.."
};
// Player frame 3 (triggered)
static const char *CHARACTER_MASK_FRAME_3[] = {
".._$@.",
".$By`.",
"4@$@@a",
"aa@W@."
};
enum class CharacterFrame {
Walk1 = 0,
Walk2,
Crawl1,
COUNT // number of frames, must stay last
};
// Combine frames for easy access
static const char **PLAYER_FRAMES[] = {
CHARACTER_MASK_FRAME_1, // Walk1
CHARACTER_MASK_FRAME_2, // Walk1
CHARACTER_MASK_FRAME_3 // Crawl1
};
static const int CHARACTER_FRAME_COUNT = static_cast<int>(CharacterFrame::COUNT);
// Height per frame
static const int CHARACTER_HEIGHT_1 = sizeof(CHARACTER_MASK_FRAME_1) / sizeof(CHARACTER_MASK_FRAME_1[0]);
static const int CHARACTER_HEIGHT_2 = sizeof(CHARACTER_MASK_FRAME_2) / sizeof(CHARACTER_MASK_FRAME_2[0]);
static const int CHARACTER_HEIGHT_3 = sizeof(CHARACTER_MASK_FRAME_3) / sizeof(CHARACTER_MASK_FRAME_3[0]);

View File

@@ -0,0 +1,16 @@
// src/game/animation.cpp
#include "animation.h"
AnimationController::AnimationController() {
anim_timer.start();
}
bool AnimationController::tick(int speed) {
if (anim_timer.elapsed_time() >= ANIMATION_TICK) {
// speed determines scroll steps per tick
shift += speed;
anim_timer.reset();
return true;
}
return false;
}

View File

@@ -0,0 +1,20 @@
// src/game/animation.h
#pragma once
#include "mbed.h"
constexpr auto ANIMATION_TICK = 170ms;
class AnimationController {
private:
Timer anim_timer;
int shift = 0;
public:
AnimationController();
// Update animation, returns true if redraw needed
bool tick(int speed);
// Getters
int get_shift() const { return shift; }
};

View File

@@ -0,0 +1,38 @@
// src/game/state.cpp
#include "state.h"
void WalkingState::update() {
// stop crawling after duration
if (current_state == PlayerState::Crawl1 &&
state_timer.elapsed_time() >= CRAWL_DURATION) {
current_state = PlayerState::Walk1;
state_timer.stop();
}
}
void WalkingState::start_crawl() {
current_state = PlayerState::Crawl1;
state_timer.reset();
state_timer.start();
}
void WalkingState::toggle_walk_frame() {
if (current_state == PlayerState::Walk1) {
current_state = PlayerState::Walk2;
} else if (current_state == PlayerState::Walk2) {
current_state = PlayerState::Walk1;
}
}
CharacterFrame WalkingState::get_character_frame() const {
switch (current_state) {
case PlayerState::Walk1:
return CharacterFrame::Walk1;
case PlayerState::Walk2:
return CharacterFrame::Walk2;
case PlayerState::Crawl1:
return CharacterFrame::Crawl1;
default:
return CharacterFrame::Walk1;
}
}

View File

@@ -0,0 +1,27 @@
// src/game/state.h
#pragma once
#include "mbed.h"
#include "../assets/character_frames.h"
constexpr auto CRAWL_DURATION = 300ms;
enum class PlayerState { Walk1, Walk2, Crawl1 };
class WalkingState {
private:
PlayerState current_state = PlayerState::Walk1;
Timer state_timer;
public:
// MOVED FROM loop.cpp
// Update state (handles crawl timeout)
void update();
// State transitions
void start_crawl();
void toggle_walk_frame();
// Getters
PlayerState get_state() const { return current_state; }
CharacterFrame get_character_frame() const;
};

View File

@@ -0,0 +1,49 @@
// 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;
}

View File

@@ -0,0 +1,35 @@
// 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; }
};

View File

@@ -1,7 +1,7 @@
// main.cpp // main.cpp
#include "mbed.h" #include "mbed.h"
#include "background_dark_inverted.h" #include "assets/background_frame.h"
#include "render/loop.h" #include "render/loop.h"
#define TARGET_TX_PIN USBTX #define TARGET_TX_PIN USBTX

View File

@@ -1,6 +1,6 @@
// src/render/background.cpp // src/render/background.cpp
#include "../background_dark_inverted.h" #include "../assets/background_frame.h"
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>

View File

@@ -1,169 +1,65 @@
// src/render/loop.cpp // src/render/loop.cpp
#include "loop.h" #include "loop.h"
#include "../background_dark_inverted.h" #include "../assets/background_frame.h"
#include "player_positioning.h" #include "player_positioning.h"
#include "background.h" #include "background.h"
#include "mbed.h" #include "mbed.h"
#include "player.h" #include "player.h"
#include "player_mask.h" #include "../game/state.h"
#include <cstring> #include "../game/animation.h"
#include "../hardware/uart.h"
#include "../assets/character_frames.h"
extern BufferedSerial serial_port; extern BufferedSerial serial_port;
extern DigitalOut led; extern DigitalOut led;
// Constants // Constants
constexpr size_t BUFFER_SIZE = 64;
constexpr auto CRAWL_DURATION = 300ms;
constexpr auto ANIMATION_TICK = 170ms;
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
constexpr int PLAYER_X = 9; constexpr int PLAYER_X = 9;
constexpr int PLAYER_Y = 6; constexpr int PLAYER_Y = 6;
enum class PlayerState { Walk1, Walk2, Crawl1 };
struct WalkingState {
PlayerState current_state = PlayerState::Walk1;
Timer state_timer;
};
static char rx_buffer[BUFFER_SIZE];
static char message[BUFFER_SIZE];
static bool message_active = false;
void draw_mask(const char *unused_filename, int shift, const char *text); void draw_mask(const char *unused_filename, int shift, const char *text);
// Returns:
// 0 = no change - walk1
// 1 = normal redraw - walk2
// 2 = button triggered - crawl1
static int read_uart() {
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) {
led = !led; // toogle ledky
strncpy(message, rx_buffer, sizeof(message) - 1);
message_active = true;
changed = true;
triggered = true;
}
}
// casovac na 1s zobrazenia spravy
static Timer msg_timer;
static bool timer_started = false;
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 2; // LED toggled press
if (changed)
return 1;
return 0;
}
static bool update_animation(Timer &anim_timer, int &shift, int speed) {
if (anim_timer.elapsed_time() >= ANIMATION_TICK) {
// speed determines scroll steps per tick
shift += speed;
anim_timer.reset();
return true;
}
return false;
}
static int get_player_frame(PlayerState state) {
switch (state) {
case PlayerState::Walk1:
return 0;
case PlayerState::Walk2:
return 1;
case PlayerState::Crawl1:
return 2;
default:
return 0;
}
}
static void update_player_state(WalkingState &state) {
// stop crawling
if (state.current_state == PlayerState::Crawl1 &&
state.state_timer.elapsed_time() >= CRAWL_DURATION) {
state.current_state = PlayerState::Walk1;
state.state_timer.stop();
}
}
static void toggle_walk_frame(WalkingState &state) {
if (state.current_state == PlayerState::Walk1) {
state.current_state = PlayerState::Walk2;
} else if (state.current_state == PlayerState::Walk2) {
state.current_state = PlayerState::Walk1;
}
}
static void start_crawl(WalkingState &state) {
state.current_state = PlayerState::Crawl1;
state.state_timer.reset();
state.state_timer.start();
}
void render_loop(int speed) { void render_loop(int speed) {
WalkingState player_state; WalkingState player_state;
AnimationController animation;
UartReader uart(serial_port);
Timer anim_timer; CharacterPosition pos = {PLAYER_X, PLAYER_Y};
anim_timer.start();
int shift = 0;
PlayerPosition pos = {PLAYER_X, PLAYER_Y};
const char *bg_file = "background_dark_inverted.txt"; const char *bg_file = "background_dark_inverted.txt";
bool need_redraw = false; bool need_redraw = false;
while (true) { while (true) {
need_redraw = false; need_redraw = false;
int uart_state = read_uart(); // returns 0/1/2 UartEvent uart_event = uart.poll();
if (uart_state == 1) { if (uart_event == UartEvent::MessageUpdate) {
need_redraw = true; need_redraw = true;
} }
if (uart_state == 2) { if (uart_event == UartEvent::Triggered) {
// start crawl frame for x time // start crawl frame for x time
start_crawl(player_state); player_state.start_crawl();
need_redraw = true; need_redraw = true;
} }
// Check if crawl duration expired // Check if crawl duration expired
update_player_state(player_state); player_state.update();
if (update_animation(anim_timer, shift, speed)) { if (animation.tick(speed)) {
need_redraw = true; need_redraw = true;
} }
if (need_redraw) { if (need_redraw) {
draw_mask(bg_file, shift, message_active ? message : nullptr); draw_mask(bg_file, animation.get_shift(), uart.get_message());
int player_frame = get_player_frame(player_state.current_state); CharacterFrame player_frame = player_state.get_character_frame();
PlayerPosition draw_pos = get_aligned_frame_position(pos, player_frame);
draw_player(draw_pos.x, draw_pos.y, player_frame); CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
draw_character(draw_pos.x, draw_pos.y, player_frame);
// alternate between frame 0 and 1 when not crawling // alternate between frame 0 and 1 when not crawling
if (player_state.current_state != PlayerState::Crawl1) { if (player_state.get_state() != PlayerState::Crawl1) {
toggle_walk_frame(player_state); player_state.toggle_walk_frame();
} }
ThisThread::sleep_for(50ms); ThisThread::sleep_for(50ms);

View File

@@ -1,25 +1,33 @@
// src/render/player.cpp // src/render/player.cpp
#include "player.h" #include "player.h"
#include "player_mask.h" #include "../assets/character_frames.h"
#include <cstdio> #include <cstdio>
void draw_player(int x, int y, int frame_index) { void draw_character(int x, int y, CharacterFrame frame) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT) int frame_index = static_cast<int>(frame);
if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
frame_index = 0; // fallback safety frame_index = 0; // fallback safety
const char **sprite = PLAYER_FRAMES[frame_index]; const char **character = PLAYER_FRAMES[frame_index];
int sprite_height = 0;
if (frame_index == 0)
sprite_height = PLAYER_MASK_LINES_FRAME_1;
else if (frame_index == 1)
sprite_height = PLAYER_MASK_LINES_FRAME_2;
else
sprite_height = PLAYER_MASK_LINES_FRAME_3;
// Draw the sprite starting from (x, y) int character_height = 0;
for (int i = 0; i < sprite_height; i++) { switch (frame) {
printf("\033[%d;%dH%s", y + i + 1, x + 1, sprite[i]); case CharacterFrame::Walk1:
character_height = CHARACTER_HEIGHT_1;
break;
case CharacterFrame::Walk2:
character_height = CHARACTER_HEIGHT_2;
break;
case CharacterFrame::Crawl1:
character_height = CHARACTER_HEIGHT_3;
break;
default:
character_height = CHARACTER_HEIGHT_1;
break;
}
for (int i = 0; i < character_height; i++) {
printf("\033[%d;%dH%s", y + i + 1, x + 1, character[i]);
} }
fflush(stdout); fflush(stdout);
} }

View File

@@ -1,5 +1,6 @@
// src/render/player.h // src/render/player.h
#pragma once #pragma once
#include "../assets/character_frames.h"
// Draw the player object starting at given (x, y) // Draw the player object starting at given (x, y)
void draw_player(int x, int y, int frame_index); void draw_character(int x, int y, CharacterFrame frame);

View File

@@ -1,50 +0,0 @@
// src/render/player_mask.h
#pragma once
// Player frame 1
static const char *PLAYER_MASK_FRAME_1[] = {
".....a@$..",
".....7PF..",
"..yaM@@__y",
"..@.y@FFF~",
"..._$M@_..",
"yaa@~.`@y.",
"........@r"
};
// Player frame 2
static const char *PLAYER_MASK_FRAME_2[] = {
".g@$",
".7PF",
"y@$.",
"0@F.",
"4@$.",
"y$@.",
"u@.."
};
// Player frame 3 (triggered)
static const char *PLAYER_MASK_FRAME_3[] = {
".._$@.",
".$By`.",
"4@$@@a",
"aa@W@."
};
// Combine frames for easy access
static const char **PLAYER_FRAMES[] = {
PLAYER_MASK_FRAME_1, // 0
PLAYER_MASK_FRAME_2, // 1
PLAYER_MASK_FRAME_3 // 2 (triggered)
};
static const int PLAYER_FRAME_COUNT = sizeof(PLAYER_FRAMES) / sizeof(PLAYER_FRAMES[0]);
// Line counts per frame
static const int PLAYER_MASK_LINES_FRAME_1 =
sizeof(PLAYER_MASK_FRAME_1) / sizeof(PLAYER_MASK_FRAME_1[0]);
static const int PLAYER_MASK_LINES_FRAME_2 =
sizeof(PLAYER_MASK_FRAME_2) / sizeof(PLAYER_MASK_FRAME_2[0]);
static const int PLAYER_MASK_LINES_FRAME_3 =
sizeof(PLAYER_MASK_FRAME_3) / sizeof(PLAYER_MASK_FRAME_3[0]);

View File

@@ -1,24 +1,25 @@
// src/render/player_positioning.cpp // src/render/player_positioning.cpp
#include "player_positioning.h" #include "player_positioning.h"
#include "../background_dark_inverted.h" #include "../assets/background_frame.h"
#include <cstdio> #include <cstdio>
// Convert a pivot coordinate (bottom-left) to the top-left draw position // Convert a pivot coordinate (bottom-left) to the top-left draw position
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index) { CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT) int frame_index = static_cast<int>(frame);
if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
frame_index = 0; frame_index = 0;
int sprite_height = 0; int character_height = 0;
if (frame_index == 0) if (frame_index == 0)
sprite_height = PLAYER_MASK_LINES_FRAME_1; character_height = CHARACTER_HEIGHT_1;
else if (frame_index == 1) else if (frame_index == 1)
sprite_height = PLAYER_MASK_LINES_FRAME_2; character_height = CHARACTER_HEIGHT_2;
else else
sprite_height = PLAYER_MASK_LINES_FRAME_3; character_height = CHARACTER_HEIGHT_3;
PlayerPosition draw_pos; CharacterPosition draw_pos;
draw_pos.x = base.x; draw_pos.x = base.x;
draw_pos.y = (VIEW_HEIGHT - base.y) - sprite_height; draw_pos.y = (VIEW_HEIGHT - base.y) - character_height;
return draw_pos; return draw_pos;
} }

View File

@@ -1,11 +1,12 @@
// src/render/player_positioning.h // src/render/player_positioning.h
#pragma once #pragma once
#include "player_mask.h" #include "../assets/background_frame.h"
#include "../assets/character_frames.h"
struct PlayerPosition { struct CharacterPosition {
int x; int x;
int y; int y;
}; };
// Calculates drawing position relative to bottom-left pivot // Calculates drawing position relative to bottom-left pivot
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index); CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame);