experiment

This commit is contained in:
2026-08-14 01:50:58 +02:00
parent 1f70f2ead2
commit 78a77164fb
3 changed files with 65 additions and 3 deletions

View File

@@ -8,6 +8,8 @@
#include "sys.h"
#include "utils.h"
static sensor_t sensors[1];
char *sys_get_time_string(char *buf, const size_t bufsz, const time_t t) {
const struct tm *tm = localtime(&t);
snprintf(buf, bufsz, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
@@ -88,13 +90,22 @@ void sys_detect(struct syssensors_t *s, const char *platform) {
return;
}
applog(LOG_INF, "Detected system sensors in %s", s->hwmon);
open_sensor(&sensors[0], s->hwmon, "/temp5_input", &s->t_sensor, 1000, SENSOR_I16);
}
void sys_update_sensors(struct syssensors_t *s) {
if (!s->hwmon[0])
return;
static char buf[8];
static size_t num_sensors = 0;
if (num_sensors == 0)
num_sensors = sizeof(sensors) / sizeof(sensors[0]);
// static char buf[8];
// fast_read_num_u16(s->hwmon, "/fan2_input", buf, &s->fan_speed[0], 1); // cpu fan, aka aio pump now
// fast_read_num_u16(s->hwmon, "/fan7_input", buf, &s->fan_speed[1], 1); // cpu opt
fast_read_num_i16(s->hwmon, "/temp5_input", buf, &s->t_sensor, 1000);
// fast_read_num_i16(s->hwmon, "/temp5_input", buf, &s->t_sensor, 1000);
for (size_t i = 0; i < num_sensors; i++)
fast_read_sensor_i16(&sensors[i]);
}

View File

@@ -96,7 +96,7 @@ inline ssize_t read_file(const char *dir, const char *file, char *buffer, const
return 0;
}
const ssize_t len = read(fd, buffer, n);
if (len >= 0 && buffer)
if (len > 0 && buffer)
buffer[len] = '\0';
close(fd);
return len;
@@ -126,4 +126,36 @@ inline void fast_read_num_f(const char *dir, const char *file, char *buf, float
read_file(dir, file, buf, 32);
const uint64_t val = strtoull(buf, nullptr, 10);
*result = (float)val / (float)divider;
}
void open_sensor(sensor_t* s, const char* dir, const char *file, void* value, const uint32_t divider, const sensor_type_t type) {
char *nt = stpcpy(s->path, dir); // nt is a pointer to the null terminator of the copied string
strcpy(nt, file); // copy to the null terminator of prev copy, append basically
s->divider = divider;
s->value = value;
s->type = type;
s->fd = open(s->path, O_RDONLY | O_CLOEXEC);
}
inline void fast_read_sensor_i16(const sensor_t *s) {
char buf[32];
lseek(s->fd, 0, SEEK_SET);
const ssize_t len = read(s->fd, buf, sizeof(buf));
if (len > 0)
buf[len] = '\0';
// return if read failed
switch (s->type) {
case SENSOR_I16:
*(int16_t*)s->value = (int16_t)(strtol(buf, nullptr, 10) / s->divider);
break;
default:
return;
}
}
void close_sensor(const sensor_t* s) {
close(s->fd);
}