From cab3c32c632727d83f01202b011ececf87767002 Mon Sep 17 00:00:00 2001 From: Drath Date: Mon, 4 May 2026 23:04:43 +0200 Subject: [PATCH] cooking continues --- src/lcd.c | 31 +++++++++++++------------------ src/main.c | 26 +++++++++++++------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/lcd.c b/src/lcd.c index f5277b6..f1221aa 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -60,7 +60,7 @@ void lcd_set_cursor(struct lcd_t* lcd, const uint8_t row, const uint8_t col) { /* reconsider so im not writing every time. prepend custom chars to the write buffer? */ void lcd_set_custom_char(struct lcd_t* lcd, const uint8_t index, const lcd_glyph_t* glyph) { uint8_t cmdbuf[] = {254, 78, index, 0, 0, 0, 0, 0, 0, 0, 0}; - memcpy(&cmdbuf[3], &glyph, sizeof(glyph)); + memcpy(&cmdbuf[3], &glyph, sizeof(lcd_glyph_t)); lcd_write(lcd, cmdbuf, sizeof(cmdbuf)); } @@ -83,32 +83,29 @@ void lcd_write_buf(struct lcd_t* lcd, const uint8_t row, const uint8_t col, cons len = MIN(len, sizeof(lcd->writebuf) - col); uint8_t pos = 4 + col + (LCD_COLS * (row % 2)); // skip 4 bytes to leave room for set cursor command if (row >= 2) pos += 2 * LCD_COLS + 4; // if we're writing to disp2, skip another 4 bytes for set cursor command and 2*40 bytes of disp 1 - memcpy(&lcd->writebuf[pos], str, len); + 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, ...) { +/* hyperoptimized function to write a formatted string to lcd buffer. */ +/* we cant just snprintf to the buffer directly bc it null terminates */ +inline void lcd_writef(struct lcd_t *lcd, const uint8_t x, const uint8_t y, const char *fmt, ...) { char tmp[LCD_COLS + 1]; va_list ap; va_start(ap, fmt); - int len = vsnprintf(tmp, sizeof(tmp), fmt, ap); + uint8_t len = vsnprintf(tmp, sizeof(tmp), fmt, ap); // print fmt and ap to a temporary buffer 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; + len = MIN(len, LCD_COLS - x); + uint8_t pos = 4 + x + (LCD_COLS * (y % 2)); // skip 4 bytes to leave room for set cursor command + if (y >= 2) pos += 2 * LCD_COLS + 4; // if we're writing to disp2, skip another 4 bytes for set cursor command and 2*40 bytes of disp 1 + memcpy(lcd->writebuf + pos, tmp, len); // copy to lcd writebuffer address + whatever pos is } -void lcd_clear_buf(struct lcd_t* lcd) { +inline void lcd_clear_buf(struct lcd_t* lcd) { memset(lcd->writebuf, ' ', sizeof(lcd->writebuf)); } /* flushes writebuffer to lcd, hyperoptmized, flushes buffer and commands in a single write operation */ -void lcd_flush_buf(struct lcd_t* lcd) { +inline void lcd_flush_buf(struct lcd_t* lcd) { constexpr uint8_t set_disp[] = {254, 71, 1, 1}; // set cursor to char 1, line 1 memcpy(&lcd->writebuf[0], set_disp, 4); memcpy(&lcd->writebuf[2 * LCD_COLS + 4], set_disp, 4); @@ -118,7 +115,7 @@ void lcd_flush_buf(struct lcd_t* lcd) { // i have a whole flushbuf func to avoid too many writes, but im writing on every bar, need to rethink this. // we could have a separate pre-char buffer and put all the setCustomChar commands to that? -char lcd_bar(struct lcd_t* lcd, const uint8_t char_id, const double value) { +inline char lcd_bar(struct lcd_t* lcd, const uint8_t char_id, const double value) { uint8_t b[8]; memset(b, 0b01110, sizeof(b)); for (uint8_t i = 0; i < (1.0 - value) * 8; i++) @@ -134,8 +131,6 @@ int lcd_init(struct lcd_t* lcd) { return -1; } - applog(LOG_INF, "ret %d, fd %d", ret, lcd->fd); - struct termios term = {}; term.c_cflag = lcd->baud | CS8 | CLOCAL | CREAD; diff --git a/src/main.c b/src/main.c index eec0a49..2ebff32 100644 --- a/src/main.c +++ b/src/main.c @@ -4,9 +4,9 @@ #include "log.h" #include #include -#include #include #include "lcd.h" +#include "sys.h" static void timespec_add_ms(struct timespec *ts, const uint16_t ms) { ts->tv_nsec += ms * 1000000L; @@ -26,7 +26,7 @@ struct control_t { } control; struct sensors_t { - // sys::TimeDate td; + struct time_date_t td; double loadavg[3]; // sys::Memory ram; // sys::Disk home; @@ -62,22 +62,20 @@ 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); + pthread_mutex_lock(&control.sensors_mtx); // lock sensor struct + pthread_cond_timedwait(&control.sensors_ready, &control.sensors_mtx, &ts); // wait until data ready + + lcd_clear_buf(&lcd); + lcd_writef(&lcd, 0, 0, "%s %s", sensors->td.date, sensors->td.time); + lcd_writef(&lcd, 26, 0, "%.2f %.2f %.2f", sensors->loadavg[0], sensors->loadavg[1], sensors->loadavg[2]); - lcd_writef(&lcd, 44, "write %6d", i); lcd_flush_buf(&lcd); - pthread_mutex_unlock(&control.sensors_mtx); - applog(LOG_WRN, "disp tock"); + pthread_mutex_unlock(&control.sensors_mtx); // unlock sensor struct } lcd_close(&lcd); @@ -95,12 +93,14 @@ static void *sensors_thread(void *arg) { while (g_running) { // NOLINT timespec_add_ms(&next, refresh_interval_ms); pthread_mutex_lock(&control.sensors_mtx); - sensors->hass_state = 2.3; + + sys_get_load_avg(sensors->loadavg); + sys_get_time(&sensors->td); + 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"); }