diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-05-30 16:53:00 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-05-30 16:53:00 +0200 |
commit | 79f2164bb6fe1727be284e5208ae402379341009 (patch) | |
tree | 37c740231891d7e918a7c69c654fb1f813c0073a | |
parent | b728a15887fa28cdd98d74017c4882d632d6e069 (diff) |
terminal drive control concept
-rw-r--r-- | client/setup.c | 2 | ||||
-rw-r--r-- | client/time.h | 5 | ||||
-rw-r--r-- | client/ui.c | 11 | ||||
-rw-r--r-- | client/ui.h | 5 | ||||
-rw-r--r-- | client/ui_dirc.c | 50 |
5 files changed, 69 insertions, 4 deletions
diff --git a/client/setup.c b/client/setup.c index a7db057..50395e0 100644 --- a/client/setup.c +++ b/client/setup.c @@ -31,6 +31,8 @@ void w2_client_setup(int argc, char **argv) { exit(1); } noecho(); + curs_set(false); + nodelay(g_w2_ui_win, true); w2_strings_init(); w2_cmd_setup_handlers(); diff --git a/client/time.h b/client/time.h index 253451d..30c8124 100644 --- a/client/time.h +++ b/client/time.h @@ -3,7 +3,10 @@ /** amount of parallel timers */ #define W2_CLIENT_TIMER_COUNT (4) extern unsigned long g_w2_client_timers[W2_CLIENT_TIMER_COUNT]; -typedef enum { W2_TIMER_PING = 0 } w2_e_client_timers; +typedef enum { + W2_TIMER_PING = 0, + W2_TIMER_UPDATE = 1, +} w2_e_client_timers; void w2_timer_start(w2_e_client_timers label); unsigned long w2_timer_end(w2_e_client_timers label); diff --git a/client/ui.c b/client/ui.c index 869a071..999e4eb 100644 --- a/client/ui.c +++ b/client/ui.c @@ -8,10 +8,12 @@ #include "strings.h" #include "term.h" #include "ui.h" +#include "time.h" WINDOW *g_w2_ui_win; unsigned int g_w2_ui_width = 0; unsigned int g_w2_ui_height = 0; +void (*g_w2_ui_current_tab)() = &w2_ui_dirc; void w2_ui_main() { g_w2_ui_width = getmaxx(g_w2_ui_win); @@ -22,11 +24,15 @@ void w2_ui_main() { void w2_ui_paint() { w2_ui_paint_statusbar(); + if (w2_timer_end(W2_TIMER_UPDATE) >= (1000 / W2_UI_UPDATE_FPS)) { + (*g_w2_ui_current_tab)(); + w2_timer_start(W2_TIMER_UPDATE); + } refresh(); } void w2_ui_paint_statusbar() { - char temp[g_w2_ui_width + 1]; + char 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); @@ -47,8 +53,7 @@ void w2_ui_paint_statusbar() { w2_ui_paint_tabbar(); for (unsigned int i = 0; i < g_w2_ui_width; i++) temp[i] = '-'; - temp[g_w2_ui_width] = 0; - mvaddstr(3, 0, temp); + mvaddnstr(3, 0, temp, g_w2_ui_width); } void w2_ui_paint_tabbar() { diff --git a/client/ui.h b/client/ui.h index cac7f46..f749fa0 100644 --- a/client/ui.h +++ b/client/ui.h @@ -3,9 +3,12 @@ #include <ncurses.h> #include <stdint.h> +#define W2_UI_UPDATE_FPS (60) + extern WINDOW *g_w2_ui_win; extern unsigned int g_w2_ui_width; extern unsigned int g_w2_ui_height; +extern void (*g_w2_ui_current_tab)(); /** update terminal props */ void w2_ui_update(); @@ -20,3 +23,5 @@ void w2_ui_main(); void w2_ui_paint_statusbar(); /** draw tab bar */ void w2_ui_paint_tabbar(); + +void w2_ui_dirc(); diff --git a/client/ui_dirc.c b/client/ui_dirc.c new file mode 100644 index 0000000..cddd2c4 --- /dev/null +++ b/client/ui_dirc.c @@ -0,0 +1,50 @@ +#include "../shared/util.h" +#include "ui.h" + +void w2_ui_bar_graph(unsigned int percent) { + unsigned int width = g_w2_ui_width - 7; + char bar[width]; + for(unsigned int i = 0; i < width - 2; i++) { + bar[i+1] = i > (width - 2) * percent / 100 ? ' ' : '*'; + } + bar[0] = '|'; + bar[width - 1] = '|'; + mvaddnstr(4, 7, bar, width); +} + +#define W2_DIRC_MOD ((double) 0.95) +#define W2_DIRC_ADD ((double) 13.0) +#define W2_DIRC_PAD ((double) 1.10) +#define W2_DIRC_SPL ((unsigned int) 20) + +int w2_avg(int* samples, unsigned int sample_count) { + double total = 0; + for (int i = 0; i < sample_count; i++) { + total += (double) samples[i] / (double) sample_count; + } + return (int) total; +} + +void w2_ui_dirc() { + static unsigned int idx = 0; + int ch = 0; + unsigned int presses = 0; + + static double drive_l = 0.f; + static int drive_l_avg[W2_DIRC_SPL] = {0}; + + + while ((ch = getch()) != -1) { + if (ch == 'a') presses++; + } + + drive_l *= W2_DIRC_MOD; + drive_l += W2_DIRC_ADD * presses; + drive_l = W2_MIN(100, drive_l); + + idx = (idx + 1) % W2_DIRC_SPL; + drive_l_avg[idx] = (int) W2_MIN(100, drive_l * W2_DIRC_PAD); + + mvaddstr(4, 0, "drive: "); + w2_ui_bar_graph(w2_avg(drive_l_avg, W2_DIRC_SPL)); +} |