From a81cbaf61183fe2c471a48042b37f28ce3a70297 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 25 Jun 2024 15:36:28 +0200 Subject: implement reset/skip commands --- client/CMakeLists.txt | 3 ++- client/cmd.cpp | 34 ++++++++++++++++++++++++++++------ client/cmd.h | 10 ++-------- client/mod.c | 9 +++++++++ client/pbc.1 | 4 ---- lib/pbdrv/pb.h | 3 +++ 6 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 client/mod.c 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 #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 -- cgit v1.2.3