1
0

feat(MP/daemon): display on oled

This commit is contained in:
2026-06-06 11:18:58 +02:00
committed by Fastium
parent 83532f3848
commit 28541d3222
2 changed files with 91 additions and 23 deletions

View File

@@ -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 <pthread.h>
#include <stdio.h>
#include <unistd.h>
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);
}

View File

@@ -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
#include <stdint.h>
/* 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