From 14779cb187dac9ed4a0b6e7645e76e78587ad024 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 2 Jun 2022 17:22:48 +0200 Subject: ping working --- client/commands.h | 2 ++ client/i18n/en_us.h | 3 +++ client/main.c | 19 ++++++++++++++++++- client/main.h | 1 + client/serial.c | 11 +++++++++-- client/setup.c | 7 +++---- client/ui.c | 6 ++++++ client/ui_dirc.c | 2 +- 8 files changed, 43 insertions(+), 8 deletions(-) (limited to 'client') diff --git a/client/commands.h b/client/commands.h index 5a12424..02ae313 100644 --- a/client/commands.h +++ b/client/commands.h @@ -4,6 +4,8 @@ #include "../shared/modes.h" #include "serial.h" +void w2_send_bin(w2_s_bin *data); + void w2_send_info(); void w2_send_ping(); void w2_send_mode(w2_e_mode mode); diff --git a/client/i18n/en_us.h b/client/i18n/en_us.h index 206e689..5547db5 100644 --- a/client/i18n/en_us.h +++ b/client/i18n/en_us.h @@ -1,5 +1,8 @@ #pragma once +#define W2_UI_CLI_USAGE "usage: %s \n" +#define W2_UI_CLI_SERPORT_ERROR "serial port open fout\n" +#define W2_UI_CLI_INITSCR_FAIL "ncurses initscr() failed\n" #define W2_UI_CONN_STAT_CONNECTED "connected" #define W2_UI_CONN_STAT_DISCONNECTED "disconnected" #define W2_UI_CONN_STAT_PING "ping" diff --git a/client/main.c b/client/main.c index 8fe6d8e..00f686a 100644 --- a/client/main.c +++ b/client/main.c @@ -1,10 +1,15 @@ #include "main.h" #include "../shared/errcatch.h" +#include "../shared/consts.h" #include "serial.h" #include "setup.h" #include "ui.h" +#include "time.h" +#include "commands.h" -w2_s_client_state g_w2_state; +w2_s_client_state g_w2_state = { + .ping_received = true +}; int main(int argc, char **argv) { w2_client_setup(argc, argv); @@ -13,5 +18,17 @@ int main(int argc, char **argv) { w2_serial_main(); w2_errcatch_main(); w2_ui_main(); + + if (!g_w2_state.ping_received && w2_timer_end(W2_TIMER_PING) > W2_PING_TIMEOUT) { + g_w2_state.ping_timeout = true; + g_w2_state.connected = false; + w2_errcatch_throw(W2_E_WARN_PING_TIMEOUT); + } + + if ((g_w2_state.ping_received && w2_timer_end(W2_TIMER_PING) > W2_PING_FREQUENCY) || g_w2_state.ping_timeout) { + g_w2_state.ping_timeout = false; + g_w2_state.ping_received = false; + w2_send_ping(); + } } } diff --git a/client/main.h b/client/main.h index e581b3c..cc4c728 100644 --- a/client/main.h +++ b/client/main.h @@ -6,6 +6,7 @@ typedef struct { unsigned int ping; uint8_t ping_id; bool ping_received; + bool ping_timeout; bool connected; uint8_t battery_level; diff --git a/client/serial.c b/client/serial.c index e42bc0b..743fe76 100644 --- a/client/serial.c +++ b/client/serial.c @@ -5,19 +5,27 @@ #include "main.h" #include "serial.h" #include "time.h" +#include "commands.h" void w2_serial_main() { int temp; while ((temp = w2_serial_read()) != -1) w2_serial_parse(temp); } -void w2_cmd_ping_tx(w2_s_bin *data) { +void w2_cmd_ping_rx(w2_s_bin *data) { W2_CAST_BIN(w2_s_cmd_ping_tx, data, cast); if (g_w2_state.ping_received) return; if (g_w2_state.ping_id != cast->id) return; + g_w2_state.ping = w2_timer_end(W2_TIMER_PING); g_w2_state.ping_received = true; + g_w2_state.ping_timeout = false; + g_w2_state.connected = true; +} + +void w2_cmd_ping_tx(w2_s_bin *data) { + w2_send_bin(data); } void w2_cmd_expt_tx(w2_s_bin *data) {} @@ -33,7 +41,6 @@ void w2_cmd_info_tx(w2_s_bin *data) { memcpy(&g_w2_state.info, data->data, sizeof(w2_s_cmd_info_tx)); } -void w2_cmd_ping_rx(w2_s_bin *data) { return; } void w2_cmd_mode_rx(w2_s_bin *data) { return; } void w2_cmd_sped_rx(w2_s_bin *data) { return; } void w2_cmd_dirc_rx(w2_s_bin *data) { return; } diff --git a/client/setup.c b/client/setup.c index 50395e0..fd37c13 100644 --- a/client/setup.c +++ b/client/setup.c @@ -17,17 +17,17 @@ uint8_t g_w2_endianness; void w2_client_setup(int argc, char **argv) { if (argc < 2) { - printf("usage: %s \n", argv[0]); + printf(W2_UI_CLI_USAGE, argv[0]); exit(1); } if (w2_serial_open(argv[1]) == 0) { - printf("serial port open fout\n"); + printf(W2_UI_CLI_SERPORT_ERROR); exit(1); } if ((g_w2_ui_win = initscr()) == NULL) { - printf("ncurses initscr() failed\n"); + printf(W2_UI_CLI_INITSCR_FAIL); exit(1); } noecho(); @@ -38,7 +38,6 @@ void w2_client_setup(int argc, char **argv) { w2_cmd_setup_handlers(); w2_send_info(); - w2_send_ping(); // check endianness g_w2_endianness = *_ptest; diff --git a/client/ui.c b/client/ui.c index e6e73f0..1cde52f 100644 --- a/client/ui.c +++ b/client/ui.c @@ -35,6 +35,12 @@ void w2_ui_paint() { void w2_ui_paint_statusbar() { char temp[g_w2_ui_width]; + + for (unsigned int i = 0; i < g_w2_ui_width; i++) temp[i] = ' '; + mvaddnstr(0, 0, temp, g_w2_ui_width); + mvaddnstr(1, 0, temp, g_w2_ui_width); + mvaddnstr(2, 0, temp, g_w2_ui_width); + sprintf(temp, "%s, %ims %s", g_w2_state.connected ? W2_UI_CONN_STAT_CONNECTED : W2_UI_CONN_STAT_DISCONNECTED, g_w2_state.ping, W2_UI_CONN_STAT_PING); diff --git a/client/ui_dirc.c b/client/ui_dirc.c index 2965169..675913a 100644 --- a/client/ui_dirc.c +++ b/client/ui_dirc.c @@ -104,6 +104,6 @@ void w2_ui_dirc(bool first) { drive_l += drive_r * W2_DIRC_STP; drive_r += drive_l * W2_DIRC_STP; - w2_send_dirc(drive_l, drive_r); + // w2_send_dirc(drive_l, drive_r); w2_ui_dirc_paint(drive_l, drive_r); } -- cgit v1.2.3