From 28541d3222f672a6fab7d332bffae09d5f8d5f35 Mon Sep 17 00:00:00 2001 From: fastium Date: Sat, 6 Jun 2026 11:18:58 +0200 Subject: [PATCH] feat(MP/daemon): display on oled --- src/06-mini-project/daemon/oled/oled.c | 92 +++++++++++++++++++++----- src/06-mini-project/daemon/oled/oled.h | 22 ++++-- 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/src/06-mini-project/daemon/oled/oled.c b/src/06-mini-project/daemon/oled/oled.c index e3f4d7a..f4be528 100644 --- a/src/06-mini-project/daemon/oled/oled.c +++ b/src/06-mini-project/daemon/oled/oled.c @@ -1,28 +1,84 @@ -// -// Created by fastium on 6/5/26. -// - #include "oled.h" #include "ssd1306.h" -int display() -{ - ssd1306_init(); +#include "oled.h" +#include "ssd1306.h" +#include +#include +#include - ssd1306_set_position (0,0); +/* Internal reference to the registered callbacks */ +static struct oled_callbacks_t* oled_cbs = NULL; + +/* Thread handle for the background update loop */ +static pthread_t oled_thread; + +/** + * display() - Formats and sends dynamic data to the OLED screen. + * + * This function uses snprintf to convert numerical values into strings + * before sending them to the SSD1306 controller. + */ +static void display(int mode, float temp, uint32_t period_ms) { + char buffer[20]; + + /* Static header rows */ + ssd1306_set_position(0, 0); ssd1306_puts("CSEL1a - SP.07"); - ssd1306_set_position (0,1); - ssd1306_puts(" Demo - SW"); - ssd1306_set_position (0,2); + ssd1306_set_position(0, 1); + ssd1306_puts(" WATCHDOG CPU "); + ssd1306_set_position(0, 2); ssd1306_puts("--------------"); - ssd1306_set_position (0,3); - ssd1306_puts("Temp: 35'C"); - ssd1306_set_position (0,4); - ssd1306_puts("Freq: 1Hz"); - ssd1306_set_position (0,5); - ssd1306_puts("Duty: 50%"); + /* Mode display: switches between AUTO and MANU strings */ + ssd1306_set_position(0, 3); + snprintf(buffer, sizeof(buffer), "Mode: %s", mode ? "AUTO" : "MANU"); + ssd1306_puts(buffer); - return 0; + /* Temperature display: formatted with one decimal precision */ + ssd1306_set_position(0, 4); + snprintf(buffer, sizeof(buffer), "Temp: %.1f'C", temp); + ssd1306_puts(buffer); + + /* Period/Frequency display: shows the value in milliseconds */ + ssd1306_set_position(0, 5); + float freq = 1.0f / (period_ms / 1000.0f); + snprintf(buffer, sizeof(buffer), "Freq: %.1fHz", freq); + ssd1306_puts(buffer); +} + +/** + * update_oled_thread() - Continuous loop for screen refreshing. + * + * Runs at a 250ms interval to provide a responsive display without + * overloading the I2C bus. + */ +static void *update_oled_thread(void* arg) { + while (1) { + if (oled_cbs != NULL) { + /* Fetch fresh data using callbacks and update screen */ + display( + oled_cbs->get_mode(), + oled_cbs->get_temperature(), + oled_cbs->get_period() + ); + } + /* Sleep to control refresh rate and save CPU cycles */ + usleep(250000); + } + return NULL; +} + +void init_oled(struct oled_callbacks_t* cbs) { + if (cbs != NULL) { + oled_cbs = cbs; + } + + /* Initialize the hardware driver */ + ssd1306_init(); + + /* Start the background thread for automatic updates */ + pthread_create(&oled_thread, NULL, update_oled_thread, NULL); + pthread_detach(oled_thread); } diff --git a/src/06-mini-project/daemon/oled/oled.h b/src/06-mini-project/daemon/oled/oled.h index 238fb81..dc1962b 100644 --- a/src/06-mini-project/daemon/oled/oled.h +++ b/src/06-mini-project/daemon/oled/oled.h @@ -1,8 +1,20 @@ -// -// Created by fastium on 6/5/26. -// - #ifndef MSE_MA_CSEL_OLED_H #define MSE_MA_CSEL_OLED_H -#endif //MSE_MA_CSEL_OLED_H \ No newline at end of file + +#include + +/* Callback structure to decouple the OLED module from the application logic */ +struct oled_callbacks_t { + float (*get_temperature)(void); + int (*get_mode)(void); + uint32_t (*get_period)(void); +}; + +/* + * Initialize the SSD1306 display and start the background refresh thread. + * The cbs parameter provides the functions needed to fetch real-time data. + */ +void init_oled(struct oled_callbacks_t* cbs); + +#endif //MSE_MA_CSEL_OLED_H