aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-18 19:29:05 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-18 19:29:05 +0200
commitcc9b3beb1e82449e34dc3d7052395607fe12b47f (patch)
treef16258fd34abc8bda8ba9c491cd3552381a0fbe8 /main
parent752dca0a41581282498ccb0d3e2aedb15181e4a8 (diff)
WIP puzzle module <-> main controller <-> client communication
Diffstat (limited to 'main')
-rw-r--r--main/i2c.c24
-rw-r--r--main/sock.c23
-rw-r--r--main/sock.h3
3 files changed, 34 insertions, 16 deletions
diff --git a/main/i2c.c b/main/i2c.c
index 612075b..2503560 100644
--- a/main/i2c.c
+++ b/main/i2c.c
@@ -10,13 +10,35 @@
#include "pb-mod.h"
#include "pbdrv.h"
#include "config.h"
+#include "pb-buf.h"
+#include "pb-send.h"
i2c_addr_t modules[CFG_PB_MOD_MAX];
size_t modules_size = 0;
+static void state_exchange() {
+ for (size_t i = 0; i < modules_size; i++) {
+ pb_buf_t buf = pb_send_state_req();
+
+ pb_buf_free(&buf);
+ }
+}
+
void bus_task() {
+ // do a scan of the bus
bus_scan();
- vTaskDelete(NULL);
+
+ // FIXME: this should be removed (see handover: RP2040 I2C limitations)
+ // wait for 5 seconds until all handshake responses are received
+ pb_mod_blocking_delay_ms(5e3);
+
+ while(1) {
+ // send my state to all puzzle modules
+ state_exchange();
+
+ // wait 1 second
+ pb_mod_blocking_delay_ms(1e3);
+ }
}
void pb_route_cmd_magic_res(pb_msg_t * msg) {
diff --git a/main/sock.c b/main/sock.c
index 6a5ff72..112e754 100644
--- a/main/sock.c
+++ b/main/sock.c
@@ -9,6 +9,7 @@
#include "config.h"
#include "i2ctcpv1.h"
#include "sock.h"
+#include "pb-mod.h"
struct netconn* current_connection = NULL;
i2ctcp_msg_t recv_msg;
@@ -34,16 +35,14 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) {
free(buf);
}
-void i2c_recv(uint16_t addr, const char * data, size_t data_size) {
- printf("address: 0x%02x\n", addr);
- printf("data: \"%.*s\"\n", data_size, data);
-
- // send message back
- char reply[] = "Test message back!";
- i2c_send(0x69, reply, strlen(reply));
-
- // TODO: this function should forward the recieved message onto the puzzle
- // bus instead of printing/replying
+static void i2c_recv_fwd(uint16_t addr, const uint8_t * data, size_t data_size) {
+ if (addr == PB_MOD_ADDR) {
+ // addressed to me = act as recieved
+ pb_i2c_recv(data, data_size);
+ } else {
+ // addressed to other puzzle module = forward
+ pb_i2c_send(addr, data, data_size);
+ }
}
void recv_handler(struct netconn* conn, struct netbuf* buf) {
@@ -55,10 +54,10 @@ void recv_handler(struct netconn* conn, struct netbuf* buf) {
netbuf_data(buf, (void**)&data, &len);
// continue early if more data is needed to complete message
- if (!i2ctcp_read(&recv_msg, data, len)) continue;
+ if (i2ctcp_read(&recv_msg, data, len) != 0) continue;
// forward received message to puzzle bus
- i2c_recv(recv_msg.addr, recv_msg.data, recv_msg.length);
+ i2c_recv_fwd(recv_msg.addr, (uint8_t *) recv_msg.data, recv_msg.length);
free(recv_msg.data);
} while (netbuf_next(buf) >= 0);
diff --git a/main/sock.h b/main/sock.h
index 61828fb..38fca01 100644
--- a/main/sock.h
+++ b/main/sock.h
@@ -6,6 +6,3 @@
//! start listening for TCP socket requests
void serve_task();
-void i2c_send(uint16_t addr, const char * data, size_t data_size);
-void i2c_recv(uint16_t addr, const char * data, size_t data_size);
-