coffee/barista/overheat.c
2025-10-15 21:14:19 +02:00

53 lines
892 B
C

/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "overheat.h"
#include <string.h>
void
overheat_init(struct overheat *o)
{
memset(o, 0, sizeof(struct overheat));
}
void
overheat_input(struct overheat *o, unsigned long t_ms, float temp)
{
unsigned long delta = t_ms - o->last_time;
if (delta < OVH_INTERVAL)
return;
o->temp[o->next++] = temp;
if (o->next >= OVH_NSAMPLES)
o->next = 0;
if (o->n < OVH_NSAMPLES)
o->n++;
/* Recompute state */
float tmin = 10000.0, tmax = 0.0;
for (int i = 0; i < o->n; i++) {
if (o->temp[i] < tmin)
tmin = o->temp[i];
if (o->temp[i] > tmax)
tmax = o->temp[i];
}
o->delta = tmax - tmin;
}
float
overheat_delta(struct overheat *o)
{
return o->delta;
}
int
overheat_panic(struct overheat *o)
{
if (o->delta > OVH_THRESHOLD)
return 1;
else
return 0;
}