From b728a15887fa28cdd98d74017c4882d632d6e069 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 30 May 2022 13:31:47 +0200 Subject: status bar almost done --- client/i18n/en_us.h | 8 ++++++++ client/setup.c | 4 +++- client/strings.c | 16 ++++++++++++++++ client/strings.h | 10 ++++++++++ client/ui.c | 17 +++++++---------- client/ui.h | 3 +-- protocol.md | 3 ++- robot/mode_lcal.c | 2 ++ robot/modes.c | 32 +++++++++++++++++++++++++------- robot/modes.h | 35 ++++++++--------------------------- robot/sercomm.c | 1 + robot/setup.c | 3 +++ shared/modes.h | 16 ++++++++++++++++ shared/protocol.h | 1 + 14 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 client/strings.c create mode 100644 client/strings.h create mode 100644 shared/modes.h diff --git a/client/i18n/en_us.h b/client/i18n/en_us.h index 80b9f09..206e689 100644 --- a/client/i18n/en_us.h +++ b/client/i18n/en_us.h @@ -6,3 +6,11 @@ #define W2_UI_BATT_STAT_BATTERY "battery" #define W2_UI_EXPT_STAT_WARNINGS "warning(s)" #define W2_UI_EXPT_STAT_ERRORS "error(s)" +#define W2_UI_MODE_CHRG "charging station" +#define W2_UI_MODE_DIRC "direct control" +#define W2_UI_MODE_GRID "grid" +#define W2_UI_MODE_HALT "emergency stop" +#define W2_UI_MODE_LCAL "line calibration" +#define W2_UI_MODE_MAZE "maze" +#define W2_UI_MODE_SCAL "sensor calibration" +#define W2_UI_MODE_SPIN "wet floor simulation" diff --git a/client/setup.c b/client/setup.c index 59e43d1..a7db057 100644 --- a/client/setup.c +++ b/client/setup.c @@ -1,12 +1,13 @@ +#include #include #include -#include #include "../shared/bin.h" #include "../shared/protocol.h" #include "commands.h" #include "serial.h" #include "setup.h" +#include "strings.h" #include "ui.h" // pointers for endianness check @@ -31,6 +32,7 @@ void w2_client_setup(int argc, char **argv) { } noecho(); + w2_strings_init(); w2_cmd_setup_handlers(); w2_send_info(); diff --git a/client/strings.c b/client/strings.c new file mode 100644 index 0000000..b97d4b2 --- /dev/null +++ b/client/strings.c @@ -0,0 +1,16 @@ +#include "strings.h" + +char *g_w2_mode_strings[W2_MODE_COUNT]; + +void w2_strings_modes_init() { + g_w2_mode_strings[W2_M_CHRG] = W2_UI_MODE_CHRG; + g_w2_mode_strings[W2_M_DIRC] = W2_UI_MODE_DIRC; + g_w2_mode_strings[W2_M_GRID] = W2_UI_MODE_GRID; + g_w2_mode_strings[W2_M_HALT] = W2_UI_MODE_HALT; + g_w2_mode_strings[W2_M_LCAL] = W2_UI_MODE_LCAL; + g_w2_mode_strings[W2_M_MAZE] = W2_UI_MODE_MAZE; + g_w2_mode_strings[W2_M_SCAL] = W2_UI_MODE_SCAL; + g_w2_mode_strings[W2_M_SPIN] = W2_UI_MODE_SPIN; +} + +void w2_strings_init() { w2_strings_modes_init(); } diff --git a/client/strings.h b/client/strings.h new file mode 100644 index 0000000..0085228 --- /dev/null +++ b/client/strings.h @@ -0,0 +1,10 @@ +#pragma once + +#include "../shared/modes.h" +#include "i18n.h" + +#define W2_STRINGS_MODE_MAP_BUFFER_SIZE 32 + +extern char *g_w2_mode_strings[W2_MODE_COUNT]; + +void w2_strings_init(); diff --git a/client/ui.c b/client/ui.c index c47e1fb..869a071 100644 --- a/client/ui.c +++ b/client/ui.c @@ -1,19 +1,20 @@ -#include #include +#include #include "../shared/bin.h" #include "../shared/util.h" #include "i18n.h" #include "main.h" +#include "strings.h" #include "term.h" #include "ui.h" WINDOW *g_w2_ui_win; -unsigned int g_w2_ui_width = 0; +unsigned int g_w2_ui_width = 0; unsigned int g_w2_ui_height = 0; void w2_ui_main() { - g_w2_ui_width = getmaxx(g_w2_ui_win); + g_w2_ui_width = getmaxx(g_w2_ui_win); g_w2_ui_height = getmaxy(g_w2_ui_win); w2_ui_paint(); @@ -28,8 +29,7 @@ void w2_ui_paint_statusbar() { char temp[g_w2_ui_width + 1]; 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); + g_w2_state.ping, W2_UI_CONN_STAT_PING); mvaddstr(0, 0, temp); sprintf(temp, "(%s)", g_w2_state.info.build_str); @@ -38,12 +38,10 @@ void w2_ui_paint_statusbar() { sprintf(temp, "%s %i%%", W2_UI_BATT_STAT_BATTERY, g_w2_state.battery_level); mvaddstr(0, g_w2_ui_width - strlen(temp), temp); - sprintf(temp, "[mode 0x%02x]", g_w2_state.mode); + sprintf(temp, "[%s]", g_w2_mode_strings[g_w2_state.mode]); mvaddstr(1, 0, temp); - sprintf(temp, "%i %s, %i %s", - 0, W2_UI_EXPT_STAT_WARNINGS, - 0, W2_UI_EXPT_STAT_ERRORS); + sprintf(temp, "%i %s, %i %s", 0, W2_UI_EXPT_STAT_WARNINGS, 0, W2_UI_EXPT_STAT_ERRORS); mvaddstr(1, g_w2_ui_width - strlen(temp), temp); w2_ui_paint_tabbar(); @@ -58,4 +56,3 @@ void w2_ui_paint_tabbar() { sprintf(temp, "-- tab bar here --"); mvaddstr(2, g_w2_ui_width / 2 - strlen(temp) / 2, temp); } - diff --git a/client/ui.h b/client/ui.h index 06a8cea..cac7f46 100644 --- a/client/ui.h +++ b/client/ui.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include extern WINDOW *g_w2_ui_win; extern unsigned int g_w2_ui_width; @@ -20,4 +20,3 @@ void w2_ui_main(); void w2_ui_paint_statusbar(); /** draw tab bar */ void w2_ui_paint_tabbar(); - diff --git a/protocol.md b/protocol.md index bc6a8c0..95ef835 100644 --- a/protocol.md +++ b/protocol.md @@ -297,7 +297,7 @@ packet. requests robot info -#### robot info response (`r --> c`) (41 bytes) +#### robot info response (`r --> c`) (42 bytes) |type|description| |-:|-| @@ -308,6 +308,7 @@ requests robot info |`uint8_t`|exponential moving average sercomm module cycle time (ms)| |`uint8_t`|exponential moving average modes module cycle time (ms)| |`uint32_t`|total robot uptime (s)| +|`uint8_t`|current mode code| robot info response diff --git a/robot/mode_lcal.c b/robot/mode_lcal.c index 6b4e736..896d0f0 100644 --- a/robot/mode_lcal.c +++ b/robot/mode_lcal.c @@ -1 +1,3 @@ #include "mode_lcal.h" + +void w2_mode_lcal() {} diff --git a/robot/modes.c b/robot/modes.c index 928e2b4..f73a809 100644 --- a/robot/modes.c +++ b/robot/modes.c @@ -4,13 +4,31 @@ #include "../shared/util.h" #include "sercomm.h" -/** function pointer to current mode */ -// static void (*g_w2_current_mode)() = &w2_mode_halt; - -static void (*g_w2_mode_history[W2_MODE_HISTORY_BUFFER_SIZE])(); -static uint8_t g_w2_mode_history_index = 0; +#include "mode_chrg.h" +#include "mode_dirc.h" +#include "mode_grid.h" +#include "mode_halt.h" +#include "mode_lcal.h" +#include "mode_maze.h" +#include "mode_scal.h" +#include "mode_spin.h" + +w2_e_mode g_w2_mode_history[W2_MODE_HISTORY_BUFFER_SIZE]; +uint8_t g_w2_mode_history_index = 0; +void (*g_w2_modes[W2_MODE_COUNT])(); + +void w2_modes_init() { + g_w2_modes[W2_M_CHRG] = &w2_mode_chrg; + g_w2_modes[W2_M_DIRC] = &w2_mode_dirc; + g_w2_modes[W2_M_GRID] = &w2_mode_grid; + g_w2_modes[W2_M_HALT] = &w2_mode_halt; + g_w2_modes[W2_M_LCAL] = &w2_mode_lcal; + g_w2_modes[W2_M_MAZE] = &w2_mode_maze; + g_w2_modes[W2_M_SCAL] = &w2_mode_scal; + g_w2_modes[W2_M_SPIN] = &w2_mode_spin; +} -void w2_modes_main() { (*g_w2_mode_history[g_w2_mode_history_index])(); } +void w2_modes_main() { (*g_w2_modes[g_w2_mode_history[g_w2_mode_history_index]])(); } void w2_modes_switch(w2_e_mode new_mode, bool replace) { int16_t next_history_index = @@ -24,7 +42,7 @@ void w2_modes_switch(w2_e_mode new_mode, bool replace) { g_w2_mode_history_index = next_history_index; } else { g_w2_mode_history_index = next_history_index; - g_w2_mode_history[g_w2_mode_history_index] = W2_MODES[new_mode]; + g_w2_mode_history[g_w2_mode_history_index] = new_mode; } // forward mode change to sercomm diff --git a/robot/modes.h b/robot/modes.h index 122be4a..8a53560 100644 --- a/robot/modes.h +++ b/robot/modes.h @@ -2,16 +2,16 @@ /** @file modes.h */ +#include + #include "../shared/consts.h" +#include "../shared/modes.h" + +extern w2_e_mode g_w2_mode_history[W2_MODE_HISTORY_BUFFER_SIZE]; +extern uint8_t g_w2_mode_history_index; -#include "mode_chrg.h" -#include "mode_dirc.h" -#include "mode_grid.h" -#include "mode_halt.h" -#include "mode_lcal.h" -#include "mode_maze.h" -#include "mode_scal.h" -#include "mode_spin.h" +/** setup g_w2_modes array */ +void w2_modes_init(); /** * mode logic @@ -20,25 +20,6 @@ */ void w2_modes_main(); -/** mode constants */ -typedef enum { - W2_M_PREV = -1, - W2_M_MAZE = 0, - W2_M_GRID = 1, - W2_M_HALT = 2, - W2_M_LCAL = 3, - W2_M_CHRG = 4, - W2_M_DIRC = 5, - W2_M_SPIN = 6, - W2_M_SCAL = 7, -} w2_e_mode; - -/** array that maps w2_e_mode to mode function pointers */ -static const void(*const W2_MODES[]) = { - &w2_mode_maze, &w2_mode_grid, &w2_mode_grid, &w2_mode_halt, - &w2_mode_chrg, &w2_mode_dirc, &w2_mode_spin, &w2_mode_scal, -}; - /** switch current mode (allow switching back to previous mode) */ void w2_modes_call(w2_e_mode mode); /** switch current mode (replace current mode keeping history index) */ diff --git a/robot/sercomm.c b/robot/sercomm.c index 2317d89..deef39f 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -135,6 +135,7 @@ void w2_cmd_info_rx(w2_s_bin *data) { res_msg->sercomm_ms = (uint8_t)g_w2_hypervisor_ema_sercomm_ms; res_msg->mode_ms = (uint8_t)g_w2_hypervisor_ema_mode_ms; res_msg->uptime_s = w2_bin_hton32((uint32_t)(g_w2_hypervisor_uptime_ms / 1e3)); + res_msg->mode = g_w2_mode_history[g_w2_mode_history_index]; w2_sercomm_append_msg(res_bin); free(res_bin); diff --git a/robot/setup.c b/robot/setup.c index 95b201e..4706d64 100644 --- a/robot/setup.c +++ b/robot/setup.c @@ -19,6 +19,9 @@ void w2_setup_main() { // clear lcd clear(); + // modes array + w2_modes_init(); + // start serial i/o w2_cmd_setup_handlers(); serial_set_baud_rate(W2_SERIAL_BAUD); diff --git a/shared/modes.h b/shared/modes.h new file mode 100644 index 0000000..ff939ea --- /dev/null +++ b/shared/modes.h @@ -0,0 +1,16 @@ +#pragma once + +#define W2_MODE_COUNT 8 + +/** mode constants */ +typedef enum { + W2_M_PREV = -1, + W2_M_MAZE = 0, + W2_M_GRID = 1, + W2_M_HALT = 2, + W2_M_LCAL = 3, + W2_M_CHRG = 4, + W2_M_DIRC = 5, + W2_M_SPIN = 6, + W2_M_SCAL = 7, +} w2_e_mode; diff --git a/shared/protocol.h b/shared/protocol.h index 93e53f4..02d5526 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -155,6 +155,7 @@ typedef struct { uint8_t sercomm_ms; uint8_t mode_ms; uint32_t uptime_s; + uint8_t mode; } w2_s_cmd_info_tx; typedef struct { -- cgit v1.2.3