aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-05-29 17:41:41 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-05-29 17:41:41 +0200
commita9a38d387f98b29b68f97b8a9c6858d2b399caf0 (patch)
treea981e8d195d1743f2b218c02aef12b8809978912
parent439859f1133fb88e64df31acaa1b2845e36ba348 (diff)
WIP puzzle bus driver rework
-rw-r--r--client/CMakeLists.txt2
-rw-r--r--client/cmd.cpp3
-rw-r--r--shared/include.cmake5
-rw-r--r--shared/pb/bus.h18
-rw-r--r--shared/pb/driver.c31
-rw-r--r--shared/pb/driver.h24
-rw-r--r--shared/pb/types.h52
-rw-r--r--shared/puzbus.h39
8 files changed, 134 insertions, 40 deletions
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index 57a2447..50d3cd7 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -11,6 +11,7 @@ add_compile_definitions(DEBUG)
project(puzzlebox_client C CXX)
include(../i2ctcp/include.cmake)
+include(../shared/include.cmake)
add_executable(pbc
main.cpp
@@ -23,6 +24,7 @@ add_executable(pbc
target_link_libraries(pbc
i2ctcp
+ puzbus
mpack
readline # this is such a common library that I did not bother adding it as a submodule
)
diff --git a/client/cmd.cpp b/client/cmd.cpp
index a6242af..b7adfae 100644
--- a/client/cmd.cpp
+++ b/client/cmd.cpp
@@ -5,11 +5,12 @@
#include "cmd.h"
#include "i2ctcpv1.h"
+#include "pb/types.h"
#include "rl.h"
#include "sock.h"
#include "parse.h"
-#include "../shared/puzbus.h"
+#include "pb/bus.h"
char* consume_token(char* input, const char* ifs) {
strtok(input, ifs);
diff --git a/shared/include.cmake b/shared/include.cmake
new file mode 100644
index 0000000..f07a78b
--- /dev/null
+++ b/shared/include.cmake
@@ -0,0 +1,5 @@
+include_directories(${CMAKE_CURRENT_LIST_DIR})
+add_library(puzbus STATIC
+ ${CMAKE_CURRENT_LIST_DIR}/pb/driver.c
+ )
+
diff --git a/shared/pb/bus.h b/shared/pb/bus.h
new file mode 100644
index 0000000..6f464c3
--- /dev/null
+++ b/shared/pb/bus.h
@@ -0,0 +1,18 @@
+#pragma once
+
+// Adafruit NeoTrellis modules
+#define BUSADDR_ADA_NEO_1 0x2E
+#define BUSADDR_ADA_NEO_2 0x2F
+#define BUSADDR_ADA_NEO_3 0x30
+#define BUSADDR_ADA_NEO_4 0x32
+
+// TODO: ???
+#define BUSADDR_MOD_NEOTRELLIS 0
+#define BUSADDR_MOD_SOFTWARE 0
+#define BUSADDR_MOD_HARDWARE 0
+#define BUSADDR_MOD_VAULT 0
+// #define BUSADDR_MOD_AUTOMATION 0
+
+// main controller
+#define BUSADDR_MAIN 0x00
+
diff --git a/shared/pb/driver.c b/shared/pb/driver.c
new file mode 100644
index 0000000..2552b61
--- /dev/null
+++ b/shared/pb/driver.c
@@ -0,0 +1,31 @@
+#include "types.h"
+#include "driver.h"
+
+__weak bool pbdrv_hook_cmd() {
+ return false;
+}
+
+__weak void pbdrv_i2c_recv(uint16_t addr, const char * buf, size_t sz) {
+ if (sz == 0) return;
+ enum pb_cmd cmd = (enum pb_cmd) buf[0];
+
+ // shift buffer pointer to only contain the puzzle bus message buf
+ buf++;
+ sz--;
+
+ // allow user to override command handler while still using this weak
+ // function
+ if (pbdrv_hook_cmd(cmd, buf, sz)) return;
+
+ switch (cmd) {
+ case PB_CMD_READ: return pbdrv_handle_read(buf, sz);
+ // case PB_CMD_WRITE: return pbdrv_handle_write(buf, sz);
+ // case PB_CMD_MAGIC: return pbdrv_handle_magic(buf, sz);
+ default: return;
+ }
+}
+
+__weak void pbdrv_i2c_send(uint16_t addr, const char * buf, size_t sz) {
+ return;
+}
+
diff --git a/shared/pb/driver.h b/shared/pb/driver.h
new file mode 100644
index 0000000..1c9c3a3
--- /dev/null
+++ b/shared/pb/driver.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void pbdrv_i2c_recv(uint16_t addr, const char * buf, size_t sz);
+void pbdrv_i2c_send(uint16_t addr, const char * buf, size_t sz);
+
+void pbdrv_hook_state(enum pb_global_state * state, bool rw);
+bool pbdrv_hook_cmd(enum pb_cmd cmd, const char * buf, size_t sz);
+
+void pbdrv_handle_read(const char * buf, size_t sz);
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/shared/pb/types.h b/shared/pb/types.h
new file mode 100644
index 0000000..8150194
--- /dev/null
+++ b/shared/pb/types.h
@@ -0,0 +1,52 @@
+#pragma once
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __GNUC__
+#define __packed __attribute__((packed))
+#define __weak __attribute__((weak))
+#endif
+#ifndef __packed
+#error Could not determine packed attribute for current compiler
+#define __packed
+#endif
+#ifndef __weak
+#error Could not determine weak attribute for current compiler
+#define __weak
+#endif
+
+/**
+ * \brief puzzle bus command types
+ *
+ * The first byte of a puzzle bus message's data indicates the command type.
+ */
+enum __packed pb_cmd {
+ PB_CMD_READ, //!< read a puzzle module property
+ PB_CMD_WRITE, //!< write to a puzzle module property
+ // PB_CMD_UPDATE, //!< request an update
+};
+
+/** \brief Puzzle bus global states */
+enum __packed pb_global_state {
+ PB_GS_NOINIT, //!< uninitialized (only used by puzzle modules)
+ PB_GS_IDLE, //!< puzzle not started yet
+ PB_GS_PLAYING, //!< puzzle actively being solved
+ PB_GS_SOLVED, //!< puzzle completed
+};
+
+typedef struct __packed {
+ uint8_t address;
+} pb_cmd_read_t;
+
+typedef struct __packed {
+ uint8_t address;
+ uint8_t data[];
+} pb_cmd_write_t;
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/shared/puzbus.h b/shared/puzbus.h
deleted file mode 100644
index 59a8867..0000000
--- a/shared/puzbus.h
+++ /dev/null
@@ -1,39 +0,0 @@
-#pragma once
-
-/** \file bus address reference */
-
-// Adafruit NeoTrellis modules
-#define BUSADDR_ADA_NEO_1 0x2E
-#define BUSADDR_ADA_NEO_2 0x2F
-#define BUSADDR_ADA_NEO_3 0x30
-#define BUSADDR_ADA_NEO_4 0x32
-
-// TODO: ???
-#define BUSADDR_MOD_NEOTRELLIS 0
-#define BUSADDR_MOD_SOFTWARE 0
-#define BUSADDR_MOD_HARDWARE 0
-#define BUSADDR_MOD_VAULT 0
-#define BUSADDR_MOD_AUTOMATION 0
-
-// main controller
-#define BUSADDR_MAIN 0x00
-
-/**
- * \brief puzzle bus command types
- *
- * The first byte of a puzzle bus message's data indicates the command type.
- */
-enum pb_cmd {
- PB_CMD_READ, //!< read a puzzle module property
- PB_CMD_WRITE, //!< write to a puzzle module property
- // PB_CMD_UPDATE, //!< request an update
-};
-
-/** \brief Puzzle bus global states */
-enum pb_global_state {
- PB_GS_NOINIT, //!< uninitialized (only used by puzzle modules)
- PB_GS_IDLE, //!< puzzle not started yet
- PB_GS_PLAYING, //!< puzzle actively being solved
- PB_GS_SOLVED, //!< puzzle completed
-};
-