diff --git a/semestralka1/CMakeLists.txt b/semestralka1/CMakeLists.txt index db75bad..c74ea2f 100644 --- a/semestralka1/CMakeLists.txt +++ b/semestralka1/CMakeLists.txt @@ -15,9 +15,18 @@ add_subdirectory(${MBED_PATH}) add_executable(${APP_TARGET}) +# Recursively collect all .cpp files under src/ +file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS "src/*.cpp") + target_sources(${APP_TARGET} PRIVATE - main.cpp + ${SRC_FILES} +) + +# Make includes of headers work +target_include_directories(${APP_TARGET} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src ) target_link_libraries(${APP_TARGET} diff --git a/semestralka1/compile_commands.json b/semestralka1/compile_commands.json index f567068..03e42e8 100644 --- a/semestralka1/compile_commands.json +++ b/semestralka1/compile_commands.json @@ -451,6 +451,7 @@ "-D__DSP_PRESENT=1U", "-D__FPU_PRESENT=1U", "-D__MBED__=1", + "-I/home/filip/Documents/programming/uni/pvs/semestralka1/src", "-I/home/filip/Documents/programming/uni/pvs/semestralka1/mbed-os/cmsis/CMSIS_5/CMSIS/RTOS2/RTX/Config", "-I/home/filip/Documents/programming/uni/pvs/semestralka1/mbed-os/cmsis/CMSIS_5/CMSIS/RTOS2/RTX/Include", "-I/home/filip/Documents/programming/uni/pvs/semestralka1/mbed-os/cmsis/CMSIS_5/CMSIS/RTOS2/RTX/Include1", @@ -512,11 +513,11 @@ "-Os", "-c", "-o", - "CMakeFiles/mbed-os-example-blinky.dir/main.cpp.obj", - "/home/filip/Documents/programming/uni/pvs/semestralka1/main.cpp" + "CMakeFiles/mbed-os-example-blinky.dir/src/render/background.cpp.obj", + "/home/filip/Documents/programming/uni/pvs/semestralka1/src/render/background.cpp" ], "directory": "/home/filip/Documents/programming/uni/pvs/semestralka1/cmake_build/NUCLEO_U575ZI_Q/develop/GCC_ARM", - "file": "/home/filip/Documents/programming/uni/pvs/semestralka1/main.cpp", - "output": "/home/filip/Documents/programming/uni/pvs/semestralka1/cmake_build/NUCLEO_U575ZI_Q/develop/GCC_ARM/CMakeFiles/mbed-os-example-blinky.dir/main.cpp.obj" + "file": "/home/filip/Documents/programming/uni/pvs/semestralka1/src/render/background.cpp", + "output": "/home/filip/Documents/programming/uni/pvs/semestralka1/cmake_build/NUCLEO_U575ZI_Q/develop/GCC_ARM/CMakeFiles/mbed-os-example-blinky.dir/src/render/background.cpp.obj" } ] diff --git a/semestralka1/main.cpp b/semestralka1/main.cpp deleted file mode 100644 index 8ddf374..0000000 --- a/semestralka1/main.cpp +++ /dev/null @@ -1,114 +0,0 @@ -// main.cpp - -#include "mbed.h" -#include "background_dark_inverted.h" -#include -#include - -#define TARGET_TX_PIN USBTX -#define TARGET_RX_PIN USBRX -// #define BAUD_RATE 1843200 -// #define BAUD_RATE 921600 -#define BAUD_RATE 460800 -// #define BAUD_RATE 115200 - -static BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, BAUD_RATE); - -FileHandle *mbed::mbed_override_console(int fd) -{ - return &serial_port; -} - -DigitalOut led(LED1); - -#define BUFFER_SIZE 64 -static char rx_buffer[BUFFER_SIZE]; - -// ASCII fart background -void draw_mask(const char *unused_filename, int shift, const char *text = nullptr) { - const int view_width = 50; // visible width - const int view_height = 19; - - // Terminal clear + home - printf("\033[2J\033[H"); - - for (int i = 0; i < view_height && i < BACKGROUND_DARK_INVERTED_LINES; i++) { - const char *row = BACKGROUND_DARK_INVERTED[i]; - int width = strlen(row); - if (width == 0) { - printf("\r\n"); - continue; - } - - int start = shift % width; - - // Print visible window (wrap around) - for (int j = 0; j < view_width; j++) { - char c = row[(start + j) % width]; - printf("%c", c); - } - printf("\r\n"); - } - - if (text && text[0] != '\0') - printf("\r\n[RX] %s\r\n", text); - - fflush(stdout); -} - -int main(void) { - serial_port.set_format(8, BufferedSerial::None, 1); - printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE); - - int shift = 0; - char message[BUFFER_SIZE] = {0}; - bool message_active = false; - Timer msg_timer; - Timer anim_timer; - msg_timer.start(); - anim_timer.start(); - - const char *bg_file = "background_dark_inverted.txt"; - - bool need_redraw = false; - - while (true) { - 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; - - strncpy(message, rx_buffer, sizeof(message) - 1); - message_active = true; - msg_timer.reset(); - - need_redraw = true; - } - } - - if (message_active && msg_timer.elapsed_time() > 1s) { - message_active = false; - memset(message, 0, sizeof(message)); - - need_redraw = true; - } - - // if (message_active && msg_timer.elapsed_time() > 100ms) { - // shift = (shift + 1) % 8; - - // } - if (anim_timer.elapsed_time() >= 80ms) { - shift++; - anim_timer.reset(); - - need_redraw = true; - } - if (need_redraw) { - draw_mask(bg_file, shift, message_active ? message : nullptr); - need_redraw = false; - ThisThread::sleep_for(60ms); - } - ThisThread::sleep_for(20ms); - } -} diff --git a/semestralka1/background_dark_inverted.h b/semestralka1/src/background_dark_inverted.h similarity index 100% rename from semestralka1/background_dark_inverted.h rename to semestralka1/src/background_dark_inverted.h diff --git a/semestralka1/src/main.cpp b/semestralka1/src/main.cpp new file mode 100644 index 0000000..2582055 --- /dev/null +++ b/semestralka1/src/main.cpp @@ -0,0 +1,26 @@ +// main.cpp + +#include "mbed.h" +#include "background_dark_inverted.h" +#include "render/loop.h" + +#define TARGET_TX_PIN USBTX +#define TARGET_RX_PIN USBRX +#define BAUD_RATE 460800 + +BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, BAUD_RATE); + +FileHandle *mbed::mbed_override_console(int fd) +{ + return &serial_port; +} + +DigitalOut led(LED1); + +int main(void) { + serial_port.set_format(8, BufferedSerial::None, 1); + printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE); + + // Just call into render feature + render_loop(); +} diff --git a/semestralka1/src/render/background.cpp b/semestralka1/src/render/background.cpp new file mode 100644 index 0000000..738a5bc --- /dev/null +++ b/semestralka1/src/render/background.cpp @@ -0,0 +1,35 @@ +// src/render/background.cpp + +#include "../background_dark_inverted.h" +#include +#include + +void draw_mask(const char *unused_filename, int shift, const char *text) { + const int view_width = 50; + const int view_height = 19; + + // Terminal clear + home + printf("\033[2J\033[H"); + + for (int i = 0; i < view_height && i < BACKGROUND_DARK_INVERTED_LINES; i++) { + const char *row = BACKGROUND_DARK_INVERTED[i]; + int width = strlen(row); + if (width == 0) { + printf("\r\n"); + continue; + } + + int start = shift % width; + + for (int j = 0; j < view_width; j++) { + printf("%c", row[(start + j) % width]); + } + printf("\r\n"); + } + + if (text && text[0] != '\0') { + printf("\r\n[RX] %s\r\n", text); + } + + fflush(stdout); +} diff --git a/semestralka1/src/render/background.h b/semestralka1/src/render/background.h new file mode 100644 index 0000000..097655b --- /dev/null +++ b/semestralka1/src/render/background.h @@ -0,0 +1,6 @@ +// src/render/background.h + +#pragma once + +// Draws the ASCII art background with optional message +void draw_mask(const char *unused_filename, int shift, const char *text = nullptr); diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp new file mode 100644 index 0000000..56abebe --- /dev/null +++ b/semestralka1/src/render/loop.cpp @@ -0,0 +1,67 @@ +// src/render/loop.c + +#include "loop.h" +#include "mbed.h" +#include "../background_dark_inverted.h" + +extern BufferedSerial serial_port; +extern DigitalOut led; + +#define BUFFER_SIZE 64 + +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 = nullptr); + +void render_loop() { + Timer msg_timer; + Timer anim_timer; + msg_timer.start(); + anim_timer.start(); + + int shift = 0; + const char *bg_file = "background_dark_inverted.txt"; + bool need_redraw = false; + + while (true) { + // Read from UART if available + 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; + + strncpy(message, rx_buffer, sizeof(message) - 1); + message_active = true; + msg_timer.reset(); + + need_redraw = true; + } + } + + // Message lifetime + if (message_active && msg_timer.elapsed_time() > 1s) { + message_active = false; + memset(message, 0, sizeof(message)); + need_redraw = true; + } + + // Animation tick + if (anim_timer.elapsed_time() >= 80ms) { + shift++; + anim_timer.reset(); + need_redraw = true; + } + + // Draw + if (need_redraw) { + draw_mask(bg_file, shift, message_active ? message : nullptr); + need_redraw = false; + ThisThread::sleep_for(60ms); + } + + ThisThread::sleep_for(20ms); + } +} diff --git a/semestralka1/src/render/loop.h b/semestralka1/src/render/loop.h new file mode 100644 index 0000000..48868ab --- /dev/null +++ b/semestralka1/src/render/loop.h @@ -0,0 +1,6 @@ +// src/render/loop.h + +#pragma once + +// Runs the render loop: reads UART, animates, and draws output +void render_loop();