From a90a65ba969259fd21d7c6a48dd5dc087010da24 Mon Sep 17 00:00:00 2001 From: Drath Date: Sat, 2 May 2026 20:32:47 +0200 Subject: [PATCH] ts dont run bri --- include/lcd.h | 1 + src/lcd.c | 18 +++++++++++++++++ src/main.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/include/lcd.h b/include/lcd.h index 9a98233..3aac556 100644 --- a/include/lcd.h +++ b/include/lcd.h @@ -24,6 +24,7 @@ void lcd_set_custom_char(struct lcd_t* lcd, uint8_t index, const lcd_glyph_t* gl void lcd_write_str(struct lcd_t* lcd, const char* str, uint8_t len); void lcd_write_str_pos(struct lcd_t* lcd, const char* str, uint8_t len, uint8_t posX, uint8_t posY); void lcd_write_buf(struct lcd_t* lcd, uint8_t row, uint8_t col, const char* str); +int lcd_writef(struct lcd_t *lcd, uint8_t offset, const char *fmt, ...); void lcd_clear_buf(struct lcd_t* lcd); void lcd_flush_buf(struct lcd_t* lcd); char lcd_bar(struct lcd_t* lcd, uint8_t char_id, double value); diff --git a/src/lcd.c b/src/lcd.c index 6b30285..f5277b6 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -5,6 +5,8 @@ #include "log.h" #include "lcd.h" +#include + #define MIN(a,b) ((a) < (b) ? (a) : (b)) int lcd_open(struct lcd_t* lcd, const bool reopen) { @@ -84,6 +86,22 @@ void lcd_write_buf(struct lcd_t* lcd, const uint8_t row, const uint8_t col, cons memcpy(&lcd->writebuf[pos], str, len); } +int lcd_writef(struct lcd_t *lcd, uint8_t offset, const char *fmt, ...) + __attribute__((format(printf, 3, 4))); + +int lcd_writef(struct lcd_t *lcd, uint8_t offset, const char *fmt, ...) { + char tmp[LCD_COLS + 1]; + va_list ap; + va_start(ap, fmt); + int len = vsnprintf(tmp, sizeof(tmp), fmt, ap); + va_end(ap); + + if (len <= 0) return -1; + if (offset + len > LCD_COLS) len = LCD_COLS - offset; /* clamp to buffer */ + + memcpy(lcd->writebuf + offset, tmp, len); + return len; +} void lcd_clear_buf(struct lcd_t* lcd) { memset(lcd->writebuf, ' ', sizeof(lcd->writebuf)); diff --git a/src/main.c b/src/main.c index 0a38131..eec0a49 100644 --- a/src/main.c +++ b/src/main.c @@ -4,15 +4,25 @@ #include "log.h" #include #include +#include #include #include "lcd.h" +static void timespec_add_ms(struct timespec *ts, const uint16_t ms) { + ts->tv_nsec += ms * 1000000L; + if (ts->tv_nsec >= 1000000000L) { + ts->tv_sec++; + ts->tv_nsec -= 1000000000L; + } +} + +static constexpr uint16_t refresh_interval_ms = 500; + atomic_bool g_running; // if using c11, you need atomic_store() and atomic_load(); struct control_t { - pthread_mutex_t sensors_mtx = PTHREAD_MUTEX_INITIALIZER; - pthread_cond_t sensors_ready = PTHREAD_COND_INITIALIZER, - + pthread_mutex_t sensors_mtx; + pthread_cond_t sensors_ready; } control; struct sensors_t { @@ -52,8 +62,22 @@ static void *display_thread(void *arg) { lcd_flush_buf(&lcd); lcd_clear_buf(&lcd); + static int i = 0; + + + while (g_running) { // NOLINT its updated globally bud - ; + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + timespec_add_ms(&ts, (uint16_t)(refresh_interval_ms * 1.25)); + + pthread_mutex_lock(&control.sensors_mtx); + pthread_cond_timedwait(&control.sensors_ready, &control.sensors_mtx, &ts); + + lcd_writef(&lcd, 44, "write %6d", i); + lcd_flush_buf(&lcd); + pthread_mutex_unlock(&control.sensors_mtx); + applog(LOG_WRN, "disp tock"); } lcd_close(&lcd); @@ -64,7 +88,22 @@ static void *display_thread(void *arg) { static void *sensors_thread(void *arg) { applog(LOG_START, "sensors thread started"); struct sensors_t* sensors = arg; - (void)sensors; + + struct timespec next; + clock_gettime(CLOCK_REALTIME, &next); + + while (g_running) { // NOLINT + timespec_add_ms(&next, refresh_interval_ms); + pthread_mutex_lock(&control.sensors_mtx); + sensors->hass_state = 2.3; + pthread_cond_signal(&control.sensors_ready); + pthread_mutex_unlock(&control.sensors_mtx); + + clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next, nullptr); + applog(LOG_WRN, "sensors tick"); + } + + applog(LOG_WRN, "sensors thread stopped"); return nullptr; } @@ -83,6 +122,10 @@ int main(void) { struct signalfd_siginfo info; const int sfd = signalfd(-1, &sigset, SFD_CLOEXEC); + /* init control struct */ + pthread_mutex_init(&control.sensors_mtx, nullptr); + pthread_cond_init(&control.sensors_ready, nullptr); + struct sensors_t *sensors = malloc(sizeof(struct sensors_t)); if (sensors == nullptr) {