diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-05 14:49:34 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-05 14:49:34 +0200 |
commit | 36a8f66aeee73e82f28b040ca304e55034f05644 (patch) | |
tree | cbd29649bf24c39415fca55eb4175fbf288f6f37 | |
parent | a8b794c02574e96150d55852fa5db1ce7529503d (diff) |
arduino specific puzzle bus driver test
-rw-r--r-- | client/CMakeLists.txt | 2 | ||||
-rw-r--r-- | client/cmd.cpp | 22 | ||||
-rw-r--r-- | client/i2c.cpp | 37 | ||||
-rw-r--r-- | puzzle/dummy/CMakeLists.txt | 9 | ||||
-rw-r--r-- | puzzle/dummy/main.cpp | 20 | ||||
-rw-r--r-- | shared/include.cmake | 5 | ||||
-rw-r--r-- | shared/pb.cmake | 20 | ||||
-rw-r--r-- | shared/pb/drv/arduino/mod.cpp | 33 | ||||
-rw-r--r-- | shared/pb/drv/arduino/mod.h | 19 | ||||
-rw-r--r-- | shared/pb/moddrv.c | 108 | ||||
-rw-r--r-- | shared/pb/moddrv.h | 38 | ||||
-rw-r--r-- | test/CMakeLists.txt | 2 |
12 files changed, 121 insertions, 194 deletions
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index d838266..b1cfbaf 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -11,7 +11,7 @@ add_compile_definitions(DEBUG) project(puzzlebox_client C CXX) include(../i2ctcp/include.cmake) -include(../shared/include.cmake) +include(../shared/pb.cmake) add_executable(pbc main.cpp diff --git a/client/cmd.cpp b/client/cmd.cpp index 5ac2ff3..e7d42d0 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -4,12 +4,12 @@ #include <string.h> #include "cmd.h" -#include "pb/types.h" +// #include "pb/types.h" #include "rl.h" #include "i2c.h" #include "parse.h" -#include "pb/bus.h" +// #include "pb/bus.h" char* consume_token(char* input, const char* ifs) { strtok(input, ifs); @@ -68,30 +68,12 @@ void cmd_send(char * addr_str) { } void cmd_reset(char*) { - const char msg[] = { - PB_CMD_WRITE, - 0x00, - PB_GS_IDLE, - }; - i2c_send(BUSADDR_MAIN, msg, sizeof(msg)); } void cmd_skip(char*) { - const char msg[] = { - PB_CMD_WRITE, - 0x00, - PB_GS_SOLVED, - }; - i2c_send(BUSADDR_MAIN, msg, sizeof(msg)); } void cmd_ls(char*) { - return; - const char msg[] = { - PB_CMD_READ, - // TODO: which address is this? - }; - i2c_send(BUSADDR_MAIN, msg, sizeof(msg)); } extern bool i2c_dump_send; diff --git a/client/i2c.cpp b/client/i2c.cpp index ee57e20..951f654 100644 --- a/client/i2c.cpp +++ b/client/i2c.cpp @@ -8,7 +8,7 @@ #include "pb/bus.h" #include "pb/types.h" -#include "pb/mod/main.h" +// #include "pb/mod/main.h" bool i2c_dump_send = false; bool i2c_dump_recv = true; @@ -40,28 +40,19 @@ void i2c_recv(uint16_t addr, const char * data, size_t data_size) { printf("[%s] addr(0x%02x) data(0x%02lx):\n", __FUNCTION__, addr, data_size); xxd(data, data_size); } - - if (data_size == 0) return; - enum pb_cmd cmd = (enum pb_cmd) data[0]; - data++; data_size--; - - switch (cmd) { - case PB_CMD_READ: return i2c_handle_cmd_read(addr, data, data_size); - default: return; - } } -static void i2c_handle_cmd_read(uint16_t i2c_addr, const char * buf, size_t sz) { - if (sz < 2) return; // require data address + 1 byte of data - pb_cmd_read_t * cmd = (pb_cmd_read_t *) buf; - sz--; // sz now represents size of cmd->data - - if (i2c_addr == BUSADDR_MAIN && cmd->address == 0x01) { - if (sz % 2 != 0) return; // invalid data - for (size_t offset = 0; offset < sz; offset += sizeof(pb_mod_main_mod_t)) { - pb_mod_main_mod_t * mod = (pb_mod_main_mod_t *) (cmd->data + offset); - printf("module at addr 0x%02x with state %d\n", mod->addr, mod->state); - } - } -} +// static void i2c_handle_cmd_read(uint16_t i2c_addr, const char * buf, size_t sz) { +// if (sz < 2) return; // require data address + 1 byte of data +// pb_cmd_read_t * cmd = (pb_cmd_read_t *) buf; +// sz--; // sz now represents size of cmd->data +// +// if (i2c_addr == BUSADDR_MAIN && cmd->address == 0x01) { +// if (sz % 2 != 0) return; // invalid data +// for (size_t offset = 0; offset < sz; offset += sizeof(pb_mod_main_mod_t)) { +// pb_mod_main_mod_t * mod = (pb_mod_main_mod_t *) (cmd->data + offset); +// printf("module at addr 0x%02x with state %d\n", mod->addr, mod->state); +// } +// } +// } diff --git a/puzzle/dummy/CMakeLists.txt b/puzzle/dummy/CMakeLists.txt index 026864b..acc567c 100644 --- a/puzzle/dummy/CMakeLists.txt +++ b/puzzle/dummy/CMakeLists.txt @@ -14,16 +14,15 @@ set(ARDUINO_BOARD "Arduino Uno [avr.uno]") project(pb_mod_dummy C CXX) -include(../../shared/include.cmake) +include(../../shared/pb.cmake) add_executable(main main.cpp ) -# target_link_libraries(main -# puzbus -# # pbdrv-mod-arduino -# ) +target_link_libraries(main + pbdrv-mod + ) target_link_arduino_libraries(main core Wire diff --git a/puzzle/dummy/main.cpp b/puzzle/dummy/main.cpp index 4374066..edcc587 100644 --- a/puzzle/dummy/main.cpp +++ b/puzzle/dummy/main.cpp @@ -1,23 +1,17 @@ #include <Arduino.h> #include <Wire.h> -void receiveEvent(int howMany){ - while (Wire.available()){ - char c = Wire.read(); - Serial.print(c, HEX); - } - Serial.println(); -} +#include "pb/drv/arduino/mod.h" + +const char * PBDRV_MOD_NAME = "dummy"; +const i2c_addr_t PBDRV_MOD_ADDR = 0x20; void setup() { - Wire.begin(0x00); - Wire.onReceive(receiveEvent); + pbdrv_setup(); } void loop() { - const uint8_t data[] = {0xff, 0x00, 0xde, 0xad, 0xbe, 0xef}; - Wire.beginTransmission(0x69); - Wire.write(data, sizeof(data)); - Wire.endTransmission(); + pbdrv_i2c_send(0x00, (uint8_t *) "hoi", 3); + delay(100); } diff --git a/shared/include.cmake b/shared/include.cmake deleted file mode 100644 index c4b01c2..0000000 --- a/shared/include.cmake +++ /dev/null @@ -1,5 +0,0 @@ -include_directories(${CMAKE_CURRENT_LIST_DIR}) -add_library(puzbus STATIC - ${CMAKE_CURRENT_LIST_DIR}/pb/moddrv.c - ) - diff --git a/shared/pb.cmake b/shared/pb.cmake new file mode 100644 index 0000000..71a28cb --- /dev/null +++ b/shared/pb.cmake @@ -0,0 +1,20 @@ +if(DEFINED ARDUINO) + set(PBDRV_ARDUINO true) +endif() + +include_directories(${CMAKE_CURRENT_LIST_DIR}) + +list(APPEND PBDRV_SRCS "${CMAKE_CURRENT_LIST_DIR}/pb/moddrv.c") + +if(PBDRV_ARDUINO) + list(APPEND PBDRV_SRCS "${CMAKE_CURRENT_LIST_DIR}/pb/drv/arduino/mod.cpp") +endif() + +add_library(pbdrv-mod STATIC ${PBDRV_SRCS}) + +if(PBDRV_ARDUINO) + target_link_arduino_libraries(pbdrv-mod + core + Wire + ) +endif() diff --git a/shared/pb/drv/arduino/mod.cpp b/shared/pb/drv/arduino/mod.cpp new file mode 100644 index 0000000..c7bbe45 --- /dev/null +++ b/shared/pb/drv/arduino/mod.cpp @@ -0,0 +1,33 @@ +#ifndef ARDUINO +#error This driver only works on the Arduino platform! +#endif + +#include <Arduino.h> +#include <Wire.h> + +#include <stdlib.h> +#include <stdint.h> + +#include "mod.h" + +static void recv_event(int bytes) { + uint8_t * data = (uint8_t *) malloc(bytes); + size_t size = 0; + while (Wire.available()) { + data[size++] = Wire.read(); + } + + pbdrv_i2c_recv(data, size); +} + +void pbdrv_setup() { + Wire.begin((int) PBDRV_MOD_ADDR); + Wire.onReceive(recv_event); +} + +__weak void pbdrv_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) { + Wire.beginTransmission((int) addr); + Wire.write(buf, sz); + Wire.endTransmission(); +} + diff --git a/shared/pb/drv/arduino/mod.h b/shared/pb/drv/arduino/mod.h new file mode 100644 index 0000000..e2e3b6d --- /dev/null +++ b/shared/pb/drv/arduino/mod.h @@ -0,0 +1,19 @@ +#pragma once + +#include "../../moddrv.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief puzzle bus driver setup + * + * This function should be called from the Arduino \c setup() function. + */ +void pbdrv_setup(); + +#ifdef __cplusplus +} +#endif + diff --git a/shared/pb/moddrv.c b/shared/pb/moddrv.c index 9677341..b17b7ac 100644 --- a/shared/pb/moddrv.c +++ b/shared/pb/moddrv.c @@ -1,101 +1,25 @@ -#include <memory.h> - #include "types.h" #include "moddrv.h" -/** \brief [private] placeholder global state variable */ -static enum pb_state _global_state = PB_GS_NOINIT; - -/** \brief [private] main controller global state */ -static enum pb_state _main_state = PB_GS_NOINIT; +//! fallback module name +__weak const char * PBDRV_MOD_NAME = "???"; -__weak enum pb_state pbdrv_hook_mod_state_read() { - return _global_state; -} +//! [private] placeholder global state variable +static pb_global_state_t _global_state = PB_GS_NOINIT; -__weak void pbdrv_hook_mod_state_write(enum pb_state state) { - _global_state = state; -} - -__weak void pbdrv_i2c_recv(const char * buf, size_t sz) { - return; -} +//! [private] main controller global state +static pb_global_state_t _main_state = PB_GS_NOINIT; -__weak void pbdrv_handle_read(uint16_t i2c_addr, const char * buf, size_t sz) { - if (sz == 0) return; - pb_cmd_read_t * cmd = (pb_cmd_read_t *) buf; +// __weak enum pb_state pbdrv_hook_mod_state_read() { +// return _global_state; +// } - // allow user to addrimplement custom read handlers - if (pbdrv_hook_read(i2c_addr, cmd->address)) - return; - - switch (cmd->address) { - case PB_ADDR_GS: { - char res[] = { - PB_CMD_READ, - PB_ADDR_GS, - pbdrv_hook_mod_state_read(), - }; - return pbdrv_i2c_send(i2c_addr, res, sizeof(res)); - } - default: return; - } -} - -__weak void pbdrv_handle_write(uint16_t i2c_addr, const char * buf, size_t sz) { - if (sz < 2) return; // must have address and at least 1 byte data - pb_cmd_write_t * cmd = (pb_cmd_write_t *) buf; - - // allow user to implement custom read handlers - if (pbdrv_hook_write(i2c_addr, cmd->address, (char *) cmd->data, sz - 1)) - return; - - switch (cmd->address) { - case PB_ADDR_GS: - pbdrv_hook_mod_state_write(cmd->data[0]); - break; - default: return; - } -} +// __weak void pbdrv_hook_mod_state_write(enum pb_state state) { +// _global_state = state; +// } -__weak void pbdrv_handle_magic(uint16_t i2c_addr, const char * buf, size_t sz) { - if (sz != sizeof(pb_magic_msg)) return; - if (memcmp(buf, pb_magic_msg, sizeof(pb_magic_msg)) != 0) return; - - size_t res_size = sizeof(pb_cmd_t) + sizeof(pb_magic_res); - char res[res_size]; - res[0] = PB_CMD_MAGIC; - memcpy(res, pb_magic_res, sizeof(pb_magic_res)); - - pbdrv_i2c_send(i2c_addr, res, res_size); -} - -__weak void pbdrv_handle_sex(uint16_t i2c_addr, const char * buf, size_t sz) { - if (sz == 0) return; - pb_cmd_sex_t * cmd = (pb_cmd_sex_t *) buf; - - // send own state - char res[] = { - PB_CMD_SEX, - pbdrv_hook_mod_state_read(), - }; - pbdrv_i2c_send(i2c_addr, res, sizeof(res)); - - if (cmd->main_state == _main_state) return; - // keep main controller state - _main_state = cmd->main_state; - // call update if main state changed - pbdrv_hook_main_state_update(_main_state); -} - -__weak void pbdrv_hook_main_state_update(enum pb_state state) { } -__weak bool pbdrv_hook_cmd(uint16_t i2c_addr, enum pb_state cmd, const char * buf, size_t sz) { - return false; -} -__weak bool pbdrv_hook_read(uint16_t i2c_addr, uint8_t addr) { - return false; -} -__weak bool pbdrv_hook_write(uint16_t i2c_addr, uint8_t addr, const char * buf, size_t sz) { - return false; +__weak void pbdrv_i2c_recv(const uint8_t * buf, size_t sz) { + return; } - + +__weak void pbdrv_hook_main_state_update(pb_global_state_t state) { } diff --git a/shared/pb/moddrv.h b/shared/pb/moddrv.h index d2e2d97..b48f4db 100644 --- a/shared/pb/moddrv.h +++ b/shared/pb/moddrv.h @@ -16,45 +16,15 @@ #include "types.h" -#ifndef PBDRV_MOD_NAME -#define PBDRV_MOD_NAME "???" -#endif +extern const char * PBDRV_MOD_NAME; +extern const i2c_addr_t PBDRV_MOD_ADDR; #ifdef __cplusplus extern "C" { #endif -void pbdrv_i2c_recv(const char * buf, size_t sz); -void pbdrv_i2c_send(uint16_t i2c_addr, const char * buf, size_t sz); - -enum pb_state pbdrv_hook_mod_state_read(); -void pbdrv_hook_mod_state_write(enum pb_state state); -void pbdrv_hook_main_state_update(enum pb_state state); - -/** - * \name hooks - * - * Implementing this function allows you to use the weak implementation of \c - * pbdrv_i2c_recv() while being able to implement custom command handlers. - * - * \return true if the cmd was recognized, or false to forward the command to - * the default handlers - * - * \{ - */ - -/** \brief cmd receive hook */ -bool pbdrv_hook_cmd(uint16_t i2c_addr, enum pb_state cmd, const char * buf, size_t sz); -/** \brief read cmd hook */ -bool pbdrv_hook_read(uint16_t i2c_addr, uint8_t addr); -/** \brief write cmd hook */ -bool pbdrv_hook_write(uint16_t i2c_addr, uint8_t addr, const char * buf, size_t sz); -//! \} - -void pbdrv_handle_read(uint16_t i2c_addr, const char * buf, size_t sz); -void pbdrv_handle_write(uint16_t i2c_addr, const char * buf, size_t sz); -void pbdrv_handle_magic(uint16_t i2c_addr, const char * buf, size_t sz); -void pbdrv_handle_sex(uint16_t i2c_addr, const char * buf, size_t sz); +void pbdrv_i2c_recv(const uint8_t * buf, size_t sz); +void pbdrv_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz); #ifdef __cplusplus } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 44191cc..e139c34 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) project(pbtest C CXX ASM) include(../i2ctcp/include.cmake) -include(../shared/include.cmake) +include(../shared/pb.cmake) add_executable(test ExampleTest.cpp |