Compare commits

...

2 Commits

Author SHA1 Message Date
81e2a7a90b add tens and amp 2026-04-27 00:55:05 +02:00
cf82ec5071 add uart support 2026-04-26 20:31:43 +02:00
2 changed files with 88 additions and 44 deletions

View File

@ -8,9 +8,7 @@
- power (W)
- rpm (tr/min)
- bat (%)
- torque (Nm)
- tens (V)
- amp (A)
- eff (Wh/Km)
- bat_temp (°C)
- var_temp (°C)
- mot_temp (°C)
- warn (message)

View File

@ -11,6 +11,7 @@
#include <sys/socket.h>
#include <net/if.h>
#include <fcntl.h>
#include <termios.h>
const int digit_bitmaps[10][5][3] = {
{{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}}, // 0
@ -60,16 +61,19 @@ typedef struct{
char title[30];
} StStr;
typedef struct {
char linebuf[128];
int linepos;
} SerialParser;
typedef struct {
StInt speed;
StInt rpm;
StFlt tq;
StInt power;
StFlt eff;
StInt bat;
StFlt bat_temp;
StFlt var_temp;
StFlt mot_temp;
StFlt tens;
StFlt amp;
StStr message;
} Tel;
@ -96,12 +100,10 @@ void get_fake_data(Tel *t) {
t->speed.data = t->speed.ldata + rand() % (t->speed.hdata - t->speed.ldata + 1);
t->power.data = t->power.ldata + rand() % (t->power.hdata - t->power.ldata + 1);
t->bat.data = t->bat.ldata + rand() % (t->bat.hdata - t->bat.ldata + 1);
t->tq.data = t->tq.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->tq.hdata - t->tq.ldata);
t->rpm.data = t->rpm.ldata + rand() % (t->rpm.hdata - t->rpm.ldata + 1);
t->tens.data = t->tens.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->tens.hdata - t->tens.ldata);
t->amp.data = t->amp.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->amp.hdata - t->amp.ldata);
t->eff.data = t->eff.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->eff.hdata - t->eff.ldata);
t->bat_temp.data = t->bat_temp.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->bat_temp.hdata - t->bat_temp.ldata);
t->var_temp.data = t->var_temp.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->var_temp.hdata - t->var_temp.ldata);
t->mot_temp.data = t->mot_temp.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->mot_temp.hdata - t->mot_temp.ldata);
}
void read_can(Tel *t, int soc) {
@ -124,6 +126,31 @@ void read_can(Tel *t, int soc) {
}
}
void read_uart(Tel *t, int fd, SerialParser *uart_str) {
char buf[128];
int n = read(fd, buf, sizeof(buf));
if (n > 0) {
for (int i = 0; i < n; i++) {
char c = buf[i];
if (c == '\n') {
uart_str->linebuf[uart_str->linepos] = '\0';
float tmp_v, tmp_a;
if (sscanf(uart_str->linebuf, "%f,%f", &tmp_v, &tmp_a) == 2) {
t->tens.data = tmp_v;
t->amp.data = tmp_a;
}
uart_str->linepos = 0;
}
if (uart_str->linepos < (int)sizeof(uart_str->linebuf) - 1) {
uart_str->linebuf[uart_str->linepos++] = c;
} else {
uart_str->linepos = 0; // drop line to prevent overflow
}
}
}
}
long now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
@ -361,11 +388,16 @@ int main(int argc, char **argv) {
int fake_data = 0;
int use_can = 0;
int soc = 0;
int use_uart = 0;
int uart_fd = 0;
struct sockaddr_can addr;
struct ifreq ifr;
struct termios uart_str;
SerialParser uart_parser = {0};
while ((option = getopt(argc, argv, "d:hfc")) !=-1) {
while ((option = getopt(argc, argv, "d:hfcu")) !=-1) {
switch (option) {
case 'd' :
delay = atoi(optarg);
@ -379,8 +411,11 @@ int main(int argc, char **argv) {
case 'c' :
use_can = 1;
break;
case 'u' :
use_uart = 1;
break;
case 'h' :
printf("Usage: ./cocomobile-tui [-d int <ms multiplier>] [-h <help>] [-f <random data generation>] [-c <enable can>]\n");
printf("Usage: ./cocomobile-tui [-d int <ms multiplier>] [-h <help>] [-f <random data generation>] [-c <enable can>] [-u <enable uart connection>]\n");
return(0);
}
}
@ -407,6 +442,25 @@ int main(int argc, char **argv) {
}
}
if (use_uart) {
uart_fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (uart_fd < 0) {
perror("uart open error");
return 1;
}
tcgetattr(uart_fd, &uart_str);
uart_str.c_cflag &= ~CSIZE; // clear bit size
uart_str.c_cflag |= (CLOCAL | CREAD | CS8 | B115200);
uart_str.c_lflag = 0; // raw mode
uart_str.c_iflag = 0;
uart_str.c_oflag = 0;
tcsetattr(uart_fd, TCSAFLUSH, &uart_str); // aplly settings
}
initscr();
noecho();
cbreak();
@ -437,37 +491,35 @@ int main(int argc, char **argv) {
int y2 = y / 2;
int y2r = y - y2;
int y4 = y2 / 2;
int y4 = y / 4;
int y4r = y2 - y4;
int y2r4 = y2r / 2;
int y2r4r = y2r - y2r4;
int y6 = y2r / 3;
int y6r = y2r - 2 * y6;
int y6r = y - (y2 + 2* y6);
Tel tel;
tel.speed = (StInt){0 , 0 , 200 , 3, 1, y2 , x3 , 0 , 0 , NULL, "speed (km/h)" };
tel.rpm = (StInt){0 , 0 , 6000 , 4, 1, y2 , x3 , 0 , x3 , NULL, "rpm (tr/min)" };
tel.tq = (StFlt){0.0f, 1.8f, 3.5f , 3, 1, y4r, x3r , y4 , x-x3r , NULL, "torque (N/m)" };
tel.power = (StInt){0 , 0 , 1300 , 4, 1, y6 , 2*x3, y2 , 0 , NULL, "power (W)" };
tel.eff = (StFlt){0.0f, 0.0f, 300.0f, 5, 1, y6 , x3r , y2 , 2*x3 , NULL, "efficiency (Wh/Km)" };
tel.power = (StInt){0 , 0 , 1300 , 4, 1, 2*y6 , 2*x3, y2 , 0 , NULL, "power (W)" };
tel.bat = (StInt){0 , 0 , 100 , 3, 0, y4 , x3r , 0 , x-x3r , NULL, "batteries (%)" };
tel.bat_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, 1, y6 , x3 , y2+y6, 0 , NULL, "batteries temperature (deg C)" };
tel.var_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, 1, y6 , x3 , y2+y6, x3 , NULL, "variator temperature (deg C)" };
tel.mot_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, 1, y6 , x3r , y2+y6, x-x3r , NULL, "motor temperature (deg C)" };
tel.message = (StStr){ y6r, x , y-y6r, 0 , NULL, "warnings" };
tel.eff = (StFlt){0.0f, 0.0f, 300.0f, 5, 0, y4r , x3r , y4 , x-x3r , NULL, "efficiency (Wh/Km)" };
tel.tens = (StFlt){0.0f, 0.0f, 55.0f , 4, 1, y2r4 , x3r , y2 , x-x3r , NULL, "tension (V)" };
tel.amp = (StFlt){0.0f, 0.0f, 55.0f , 4, 1, y2r4r, x3r , y-y2r4r, x-x3r , NULL, "intensity (A)" };
tel.message = (StStr){ y6r , 2*x3, y-y6r , 0 , NULL, "warnings" };
win_init_int(&tel.speed);
win_init_int(&tel.rpm);
win_init_flt(&tel.tq);
win_init_int(&tel.power);
win_init_flt(&tel.eff);
win_init_int(&tel.bat);
win_init_flt(&tel.bat_temp);
win_init_flt(&tel.var_temp);
win_init_flt(&tel.mot_temp);
win_init_flt(&tel.tens);
win_init_flt(&tel.amp);
win_init_str(&tel.message);
bar_mark(tel.power.lwin);
long t100 = 0, t1000 = 0;
long t100 = 0;
int ch = ERR;
while(1) {
@ -481,6 +533,7 @@ int main(int argc, char **argv) {
get_fake_data(&tel);
} else {
if (use_can) read_can(&tel, soc);
if (use_uart) read_uart(&tel, uart_fd, &uart_parser);
}
break;
}
@ -488,17 +541,12 @@ int main(int argc, char **argv) {
if (now - t100 >= 100 * delay) {
win_int(&tel.speed, use_color);
win_int(&tel.rpm, use_color);
win_float(&tel.tq, use_color);
win_bar(&tel.power, use_color);
win_float(&tel.eff, use_color);
t100 = now;
}
if (now - t1000 >= 1000 * delay) {
win_float(&tel.tens, use_color);
win_float(&tel.amp, use_color);
win_int(&tel.bat, use_color);
win_float(&tel.bat_temp, use_color);
win_float(&tel.var_temp, use_color);
win_float(&tel.mot_temp, use_color);
t1000 = now;
t100 = now;
}
//message;
napms(10);
@ -506,13 +554,11 @@ int main(int argc, char **argv) {
end:
delwin(tel.speed.lwin);
delwin(tel.rpm.lwin);
delwin(tel.tq.lwin);
delwin(tel.power.lwin);
delwin(tel.eff.lwin);
delwin(tel.bat.lwin);
delwin(tel.bat_temp.lwin);
delwin(tel.var_temp.lwin);
delwin(tel.mot_temp.lwin);
delwin(tel.tens.lwin);
delwin(tel.amp.lwin);
delwin(tel.message.lwin);
endwin();
if (use_can) close(soc);