aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-25 15:36:28 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-25 15:36:28 +0200
commita81cbaf61183fe2c471a48042b37f28ce3a70297 (patch)
treebc6fe0f42ec3d18832b76006d5bb7942e0fa2608
parentf3e72c6e339eedc4ffac3a2f32cf6cbae7305ff0 (diff)
implement reset/skip commands
-rw-r--r--client/CMakeLists.txt3
-rw-r--r--client/cmd.cpp34
-rw-r--r--client/cmd.h10
-rw-r--r--client/mod.c9
-rw-r--r--client/pbc.14
-rw-r--r--lib/pbdrv/pb.h3
6 files changed, 44 insertions, 19 deletions
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index 989b837..8c0dcf3 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -23,12 +23,13 @@ add_executable(pbc
parse.cpp
xxd.c
i2c.cpp
+ mod.c
)
target_link_libraries(pbc
i2ctcp
mpack
readline # this is such a common library that I did not bother adding it as a submodule
- pbdrv
+ pbdrv-mod
)
diff --git a/client/cmd.cpp b/client/cmd.cpp
index 062fefa..e365a59 100644
--- a/client/cmd.cpp
+++ b/client/cmd.cpp
@@ -4,13 +4,13 @@
#include <string.h>
#include "cmd.h"
-// #include "pb/types.h"
+#include "pb-types.h"
+#include "pb-buf.h"
+#include "pb-send.h"
#include "rl.h"
#include "i2c.h"
#include "parse.h"
-// #include "pb/bus.h"
-
char* consume_token(char* input, const char* ifs) {
strtok(input, ifs);
return strtok(NULL, "\0");
@@ -43,8 +43,18 @@ void cmd_help(char*) {
}
void cmd_send(char * addr_str) {
+ if (addr_str == NULL) {
+ printf("error: no address\n");
+ return;
+ }
+
char* data_str = consume_token(addr_str, IFS);
+ if (data_str == NULL) {
+ printf("error: no data\n");
+ return;
+ }
+
char* end;
uint16_t addr = strtol(addr_str, &end, 0);
if (addr_str + strlen(addr_str) != end) {
@@ -67,13 +77,25 @@ void cmd_send(char * addr_str) {
free(data);
}
-void cmd_reset(char*) {
+static void cmd_set_state(char * line, pb_global_state_t state) {
+ if (line == NULL) {
+ printf("error: no address\n");
+ return;
+ }
+
+ i2c_addr_t addr = strtol(line, NULL, 0);
+
+ pb_buf_t buf = pb_send_state_set(state);
+ i2c_send(addr, buf.data, buf.size);
+ pb_buf_free(&buf);
}
-void cmd_skip(char*) {
+void cmd_reset(char * line) {
+ cmd_set_state(line, PB_GS_IDLE);
}
-void cmd_ls(char*) {
+void cmd_skip(char * line) {
+ cmd_set_state(line, PB_GS_SOLVED);
}
extern bool i2c_dump_send;
diff --git a/client/cmd.h b/client/cmd.h
index 4f77d50..e2c412a 100644
--- a/client/cmd.h
+++ b/client/cmd.h
@@ -49,7 +49,6 @@ cmd_handle_t cmd_exit;
cmd_handle_t cmd_test;
cmd_handle_t cmd_help;
cmd_handle_t cmd_reset;
-cmd_handle_t cmd_ls;
cmd_handle_t cmd_send;
cmd_handle_t cmd_skip;
cmd_handle_t cmd_dump;
@@ -70,17 +69,12 @@ static const cmd_t cmds[] = {
{
.handle = cmd_reset,
.name = "reset",
- .info = "set game state to 'idle' for one or more puzzle modules",
+ .info = "set a puzzle module's game state to 'idle'",
},
{
.handle = cmd_skip,
.name = "skip",
- .info = "set game state to 'solved' for one or more puzzle modules",
- },
- {
- .handle = cmd_ls,
- .name = "ls",
- .info = "list connected puzzle modules and their state",
+ .info = "set a puzzle module's game state to 'solved'",
},
#ifdef DEBUG
{
diff --git a/client/mod.c b/client/mod.c
new file mode 100644
index 0000000..b67a61d
--- /dev/null
+++ b/client/mod.c
@@ -0,0 +1,9 @@
+#include "pb.h"
+#include "pb-mod.h"
+
+const char * PB_MOD_NAME = "client";
+const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_MAIN;
+
+void pb_i2c_recv(const uint8_t * buf, size_t sz) {}
+void pb_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz) {}
+
diff --git a/client/pbc.1 b/client/pbc.1
index f5a2198..a85b03a 100644
--- a/client/pbc.1
+++ b/client/pbc.1
@@ -22,10 +22,6 @@ help
Print a list of available commands with descriptions. This command takes no
arguments.
.TP
-ls
-List all puzzle modules, their state, and the combined state of all puzzle
-modules (global state of the main controller).
-.TP
reset [mod ...]
Set the main controller or specific puzzle module's global state to \fIidle\fP.
If no modules are specified, the main controller's state is updated. One or
diff --git a/lib/pbdrv/pb.h b/lib/pbdrv/pb.h
index c73f8d9..e9c803d 100644
--- a/lib/pbdrv/pb.h
+++ b/lib/pbdrv/pb.h
@@ -15,6 +15,9 @@
//! I2C bus timeout delay in microseconds
#define PB_TIMEOUT_US (1e3 * PB_TIMEOUT_MS)
+//! Null address (used by client)
+#define PB_ADDR_NULL 0x00
+
//! Adafruit NeoTrellis module 1 I2C address
#define PB_ADDR_ADA_NEO_1 0x2E
//! Adafruit NeoTrellis module 2 I2C address