ts dont run bri
This commit is contained in:
53
src/main.c
53
src/main.c
@@ -4,15 +4,25 @@
|
||||
#include "log.h"
|
||||
#include <unistd.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/signalfd.h>
|
||||
#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) {
|
||||
|
||||
Reference in New Issue
Block a user