summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-06-01 10:02:07 +0200
committerlonkaars <loek@pipeframe.xyz>2022-06-01 10:02:07 +0200
commit35cdecc0134b82d69b120533b091b8e5b58415e1 (patch)
tree6a66f2995f787a109224e3daf8b19ecbbfb3c03b /client
parent139651d45a72d57c5147e2854647d95cb87c9e4e (diff)
direct control toy working
Diffstat (limited to 'client')
-rw-r--r--client/commands.c37
-rw-r--r--client/commands.h3
-rw-r--r--client/ui.c6
-rw-r--r--client/ui.h4
-rw-r--r--client/ui_dirc.c11
5 files changed, 46 insertions, 15 deletions
diff --git a/client/commands.c b/client/commands.c
index 1ab79fb..0d47fcd 100644
--- a/client/commands.c
+++ b/client/commands.c
@@ -2,6 +2,7 @@
#include "../shared/bin.h"
#include "../shared/protocol.h"
+#include "../shared/modes.h"
#include "commands.h"
#include "main.h"
#include "time.h"
@@ -14,26 +15,42 @@ void w2_send_bin(w2_s_bin *data) {
}
}
+w2_s_bin *w2_send_mode(w2_e_mode mode) {
+ W2_CREATE_MSG_BIN(w2_s_cmd_mode_rx, msg, msg_bin);
+
+ msg->opcode = W2_CMD_MODE | W2_CMDDIR_RX;
+ msg->mode = mode;
+
+ w2_send_bin(msg_bin);
+ free(msg_bin);
+ return NULL;
+}
+
+w2_s_bin *w2_send_dirc(uint16_t left, uint16_t right) {
+ W2_CREATE_MSG_BIN(w2_s_cmd_dirc_rx, msg, msg_bin);
+ msg->opcode = W2_CMD_DIRC | W2_CMDDIR_RX;
+ msg->left = w2_bin_hton16(left);
+ msg->right = w2_bin_hton16(right);
+ w2_send_bin(msg_bin);
+ free(msg_bin);
+ return NULL;
+}
+
w2_s_bin *w2_send_info() {
- size_t msg_size = sizeof(w2_s_cmd_info_rx);
- w2_s_cmd_info_rx *msg = malloc(msg_size);
- msg->opcode = W2_CMD_INFO | W2_CMDDIR_RX;
- w2_s_bin *msg_bin = w2_bin_s_alloc(msg_size, (uint8_t *)msg);
+ W2_CREATE_MSG_BIN(w2_s_cmd_info_rx, msg, msg_bin);
+ msg->opcode = W2_CMD_INFO | W2_CMDDIR_RX;
w2_send_bin(msg_bin);
- free(msg);
free(msg_bin);
+ return NULL;
}
w2_s_bin *w2_send_ping() {
- g_w2_state.ping_id = (uint8_t)rand();
- size_t msg_size = sizeof(w2_s_cmd_ping_rx);
- w2_s_cmd_ping_rx *msg = malloc(msg_size);
+ W2_CREATE_MSG_BIN(w2_s_cmd_ping_rx, msg, msg_bin);
msg->opcode = W2_CMD_PING | W2_CMDDIR_RX;
msg->id = g_w2_state.ping_id;
- w2_s_bin *msg_bin = w2_bin_s_alloc(msg_size, (uint8_t *)msg);
w2_send_bin(msg_bin);
- free(msg);
free(msg_bin);
w2_timer_start(W2_TIMER_PING);
+ return NULL;
}
diff --git a/client/commands.h b/client/commands.h
index 949360e..cb01189 100644
--- a/client/commands.h
+++ b/client/commands.h
@@ -1,7 +1,10 @@
#pragma once
#include "../shared/bin.h"
+#include "../shared/modes.h"
#include "serial.h"
+w2_s_bin *w2_send_mode(w2_e_mode mode);
+w2_s_bin *w2_send_dirc(uint16_t left, uint16_t right);
w2_s_bin *w2_send_info();
w2_s_bin *w2_send_ping();
diff --git a/client/ui.c b/client/ui.c
index ca96efb..26e2116 100644
--- a/client/ui.c
+++ b/client/ui.c
@@ -13,7 +13,8 @@
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 (*g_w2_ui_current_tab)(bool first) = &w2_ui_dirc;
+void (*g_w2_ui_last_tab)(bool first) = NULL;
void w2_ui_main() {
g_w2_ui_width = getmaxx(g_w2_ui_win);
@@ -25,7 +26,8 @@ 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)();
+ (*g_w2_ui_current_tab)(g_w2_ui_last_tab != g_w2_ui_current_tab);
+ g_w2_ui_last_tab = g_w2_ui_current_tab;
w2_timer_start(W2_TIMER_UPDATE);
}
refresh();
diff --git a/client/ui.h b/client/ui.h
index f749fa0..0d375de 100644
--- a/client/ui.h
+++ b/client/ui.h
@@ -8,7 +8,7 @@
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)();
+extern void (*g_w2_ui_current_tab)(bool first);
/** update terminal props */
void w2_ui_update();
@@ -24,4 +24,4 @@ void w2_ui_paint_statusbar();
/** draw tab bar */
void w2_ui_paint_tabbar();
-void w2_ui_dirc();
+void w2_ui_dirc(bool first);
diff --git a/client/ui_dirc.c b/client/ui_dirc.c
index 118a3ea..13b2d10 100644
--- a/client/ui_dirc.c
+++ b/client/ui_dirc.c
@@ -1,4 +1,6 @@
#include "../shared/util.h"
+#include "../shared/protocol.h"
+#include "commands.h"
#include "ui.h"
void w2_ui_bar_graph(unsigned int percent) {
@@ -45,7 +47,12 @@ int w2_avg(int *samples, unsigned int sample_count) {
W2_DIRC_MOTOR_DRIVER(l);
W2_DIRC_MOTOR_DRIVER(r);
-void w2_ui_dirc() {
+void w2_ui_dirc_init() {
+ w2_send_mode(W2_M_DIRC);
+}
+
+void w2_ui_dirc(bool first) {
+ if (first) w2_ui_dirc_init();
int ch = 0;
unsigned int lb = 0;
unsigned int lf = 0;
@@ -64,4 +71,6 @@ void w2_ui_dirc() {
char temp[32] = {0};
sprintf(temp, "l: %04i, r: %04i", drive_l, drive_r);
mvaddstr(4, 0, temp);
+
+ w2_send_dirc(drive_l, drive_r);
}