Compare commits
2 Commits
c09f0a81b0
...
45dffd54ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 45dffd54ad | |||
| 66002a0e1d |
34
src/main.c
34
src/main.c
@ -12,6 +12,7 @@
|
||||
#include <net/if.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <errno.h>
|
||||
|
||||
const int digit_bitmaps[10][5][3] = {
|
||||
{{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}}, // 0
|
||||
@ -172,6 +173,35 @@ void read_uart(Tel *t, int fd, SerialParser *uart_str) {
|
||||
}
|
||||
}
|
||||
|
||||
void write_uart(Tel *t, int fd) {
|
||||
// speed,rpm,power,eff,bat,tens,amp,warn
|
||||
char msg[256];
|
||||
int len = snprintf(msg, sizeof(msg), "%d,%d,%d,%.1f,%d,%.1f,%.1f,%d\n",
|
||||
t->speed.data,
|
||||
t->rpm.data,
|
||||
t->power.data,
|
||||
t->eff.data,
|
||||
t->bat.data,
|
||||
t->tens.data,
|
||||
t->amp.data,
|
||||
t->message.error_level );
|
||||
|
||||
if (len < 0 || len >= (int)sizeof(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
while (total < len) {
|
||||
int n = write(fd, msg + total, len - total);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
if (errno == EAGAIN) continue;
|
||||
return;
|
||||
}
|
||||
total += n;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@ -188,6 +218,7 @@ void calc_data(Tel *t, Bat_state *b, long now) {
|
||||
|
||||
b->previous_ms = now;
|
||||
b->wh_used += (float)t->power.data * (float)dt / 3600000.0f;
|
||||
if (b->wh_used > b->wh_total) b->wh_used = b->wh_total;
|
||||
}
|
||||
|
||||
long now_ms(void) {
|
||||
@ -655,6 +686,9 @@ int main(int argc, char **argv) {
|
||||
win_float(&tel.amp, use_color);
|
||||
win_int(&tel.bat, use_color);
|
||||
win_warn(&tel, use_color, now);
|
||||
|
||||
if (use_uart) write_uart(&tel, uart_fd);
|
||||
|
||||
t100 = now;
|
||||
}
|
||||
napms(10);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user