add battery percentage calculations

This commit is contained in:
smayzy 2026-04-27 02:06:57 +02:00
parent 9ad4579879
commit f1193eba72

View File

@ -77,6 +77,12 @@ typedef struct {
StStr message;
} Tel;
typedef struct {
float wh_used;
float wh_total;
long previous_ms;
} Bat_state;
void win_init_int(StInt *st) {
st->lwin = newwin(st->height, st->width, st->starty, st->startx);
box(st->lwin, 0, 0);
@ -150,15 +156,20 @@ void read_uart(Tel *t, int fd, SerialParser *uart_str) {
}
}
void calc_data(Tel *t) {
void calc_data(Tel *t, Bat_state *b, long now) {
t->power.data = (int)(t->tens.data * t->amp.data);
t->speed.data = (int)(t->rpm.data * 0.017f);
t->bat.data = (int)((b->wh_total - b->wh_used) * 100 / b->wh_total);
if (t->speed.data > 0) {
t->eff.data = (float)t->power.data / (float)t->speed.data;
} else {
t->eff.data = 0.0f;
}
long dt = now - b->previous_ms;
b->previous_ms = now;
b->wh_used += (float)t->power.data * (float)dt / 3600000.0f;
}
long now_ms(void) {
@ -407,6 +418,8 @@ int main(int argc, char **argv) {
struct termios uart_str;
SerialParser uart_parser = {0};
Bat_state bat_state = {0, 1000, 0};
while ((option = getopt(argc, argv, "d:hfcu")) !=-1) {
switch (option) {
case 'd' :
@ -531,6 +544,8 @@ int main(int argc, char **argv) {
long t100 = 0;
int ch = ERR;
long now = now_ms();
bat_state.previous_ms = now;
while(1) {
ch = tolower(getch());
@ -544,11 +559,11 @@ int main(int argc, char **argv) {
} else {
if (use_can) read_can(&tel, soc);
if (use_uart) read_uart(&tel, uart_fd, &uart_parser);
calc_data(&tel);
calc_data(&tel, &bat_state, now);
}
break;
}
long now = now_ms();
now = now_ms();
if (now - t100 >= 100 * delay) {
win_int(&tel.speed, use_color);
win_int(&tel.rpm, use_color);