aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/i2c.cpp21
-rw-r--r--client/rl.cpp31
-rw-r--r--client/rl.h5
-rw-r--r--client/sock.cpp2
-rw-r--r--lib/pbdrv/drv/arduino/mod.cpp2
-rw-r--r--lib/pbdrv/drv/rp2040/mod.c14
-rw-r--r--lib/pbdrv/pb-mod.c11
-rw-r--r--lib/pbdrv/pb-mod.h4
-rw-r--r--main/sock.c16
9 files changed, 59 insertions, 47 deletions
diff --git a/client/i2c.cpp b/client/i2c.cpp
index 3655191..c8b2dc8 100644
--- a/client/i2c.cpp
+++ b/client/i2c.cpp
@@ -2,14 +2,13 @@
#include <stdlib.h>
#include "i2ctcpv1.h"
+#include "rl.h"
#include "sock.h"
#include "xxd.h"
#include "pb.h"
#include "pb-types.h"
-// #include "pb/mod/main.h"
-
bool i2c_dump_send = true;
bool i2c_dump_recv = true;
@@ -33,26 +32,12 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) {
free(packed);
}
-static void i2c_handle_cmd_read(uint16_t, const char *, size_t);
-
void i2c_recv(const char * data, size_t data_size) {
if (i2c_dump_recv) {
+ _rl_printf_start();
printf("[%s] data(0x%02lx):\n", __FUNCTION__, data_size);
xxd(data, data_size);
+ _rl_printf_stop();
}
}
-// 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/client/rl.cpp b/client/rl.cpp
index 2e74e5f..493f753 100644
--- a/client/rl.cpp
+++ b/client/rl.cpp
@@ -10,24 +10,22 @@
#include "cmd.h"
#include "parse.h"
-void rl_printf(const char *fmt, ...) {
+static char* saved_line;
+static int saved_point, saved_end;
+
+void _rl_printf_start() {
// save line
- char* saved_line = rl_copy_text(0, rl_end);
- int saved_point = rl_point;
- int saved_end = rl_end;
+ saved_line = rl_copy_text(0, rl_end);
+ saved_point = rl_point;
+ saved_end = rl_end;
// clear line
rl_save_prompt();
rl_replace_line("", 0);
rl_redisplay();
+}
- // printf
- va_list args;
- va_start(args, fmt);
- vprintf(fmt, args);
- va_end(args);
-
- // restore line
+void _rl_printf_stop() {
rl_restore_prompt();
rl_replace_line(saved_line, 0);
rl_point = saved_point;
@@ -37,6 +35,17 @@ void rl_printf(const char *fmt, ...) {
free(saved_line);
}
+void rl_printf(const char *fmt, ...) {
+ _rl_printf_start();
+
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+
+ _rl_printf_stop();
+}
+
static void cli_cmd(char* cmd) {
cmd += strspn(cmd, IFS); // skip leading whitespace
char* line = consume_token(cmd, IFS);
diff --git a/client/rl.h b/client/rl.h
index d2f8612..4a5b2c1 100644
--- a/client/rl.h
+++ b/client/rl.h
@@ -27,6 +27,11 @@ int cli_main();
* completes.
*/
void rl_printf(const char * fmt, ...);
+//! Store line for printing without disrupting readline prompt
+void _rl_printf_start();
+//! Restore line stored by \c _rl_printf_start()
+void _rl_printf_stop();
+
/**
* \brief Get the index of the word currently under the cursor
*
diff --git a/client/sock.cpp b/client/sock.cpp
index e33a3dc..c292d6a 100644
--- a/client/sock.cpp
+++ b/client/sock.cpp
@@ -79,8 +79,6 @@ void PBSocket::sock_task() {
char buf[80];
ssize_t bytes = read(_fd, buf, sizeof(buf));
- rl_printf("%.*s", bytes, buf);
- continue;
if (bytes == -1) {
rl_printf("error: %s (%d)\n", strerror(errno), errno);
break;
diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp
index c381077..581b80a 100644
--- a/lib/pbdrv/drv/arduino/mod.cpp
+++ b/lib/pbdrv/drv/arduino/mod.cpp
@@ -48,6 +48,8 @@ static void pb_setup() {
* RP2040.
*/
__weak void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
+ if (pb_hook_i2c_send(addr, buf, sz)) return;
+
vTaskDelay(10 / portTICK_PERIOD_MS); // prevent bus collisions
Wire.beginTransmission((int) addr);
Wire.write(buf, sz);
diff --git a/lib/pbdrv/drv/rp2040/mod.c b/lib/pbdrv/drv/rp2040/mod.c
index bc20927..bca38d0 100644
--- a/lib/pbdrv/drv/rp2040/mod.c
+++ b/lib/pbdrv/drv/rp2040/mod.c
@@ -1,10 +1,8 @@
-#include "pb.h"
-
-#include "pb.h"
-#include "pb-types.h"
-#include "pb-mod.h"
-#include "pb-send.h"
-#include "pb-buf.h"
+#include "../../pb.h"
+#include "../../pb-types.h"
+#include "../../pb-mod.h"
+#include "../../pb-send.h"
+#include "../../pb-buf.h"
#include <hardware/i2c.h>
#include <hardware/gpio.h>
@@ -43,7 +41,7 @@ void pb_setup() {
}
__weak void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
- pb_hook_i2c_send(addr, buf, sz);
+ if (pb_hook_i2c_send(addr, buf, sz)) return;
// false to write stop condition to i2c bus
i2c_write_timeout_us(PB_I2C_M, addr, buf, sz, false, PB_TIMEOUT_US);
diff --git a/lib/pbdrv/pb-mod.c b/lib/pbdrv/pb-mod.c
index 6b23b04..0342391 100644
--- a/lib/pbdrv/pb-mod.c
+++ b/lib/pbdrv/pb-mod.c
@@ -18,9 +18,8 @@ __weak void pb_hook_mod_state_write(pb_global_state_t state) {
_global_state = state;
}
-__weak void pb_hook_i2c_recv(const uint8_t * data, size_t sz) { }
__weak void pb_i2c_recv(const uint8_t * data, size_t sz) {
- pb_hook_i2c_recv(data, sz);
+ if (pb_hook_i2c_recv(data, sz)) return;
pb_buf_t buf = {
.data = (char *) data,
@@ -35,5 +34,11 @@ __weak void pb_i2c_recv(const uint8_t * data, size_t sz) {
pb_msg_free(msg);
}
-__weak void pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * data, size_t sz) { }
+__weak bool pb_hook_i2c_recv(const uint8_t * data, size_t sz) {
+ return false;
+}
+
+__weak bool pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * data, size_t sz) {
+ return false;
+}
diff --git a/lib/pbdrv/pb-mod.h b/lib/pbdrv/pb-mod.h
index e7b7832..7069b32 100644
--- a/lib/pbdrv/pb-mod.h
+++ b/lib/pbdrv/pb-mod.h
@@ -101,8 +101,8 @@ void pb_hook_mod_state_write(pb_global_state_t state);
* \{
*/
-void pb_hook_i2c_recv(const uint8_t * buf, size_t sz);
-void pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz);
+bool pb_hook_i2c_recv(const uint8_t * buf, size_t sz);
+bool pb_hook_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz);
/// \}
diff --git a/main/sock.c b/main/sock.c
index 07db6af..c97ad04 100644
--- a/main/sock.c
+++ b/main/sock.c
@@ -14,7 +14,7 @@
struct netconn* current_connection = NULL;
i2ctcp_msg_t recv_msg;
-void pb_hook_i2c_send(i2c_addr_t addr, const uint8_t * data, size_t data_size) {
+static void sock_dump_msg(i2c_addr_t addr, const uint8_t * data, size_t data_size) {
if (current_connection == NULL) return;
i2ctcp_msg_t send_msg = {
@@ -35,7 +35,17 @@ void pb_hook_i2c_send(i2c_addr_t addr, const uint8_t * data, size_t data_size) {
free(buf);
}
-static void i2c_recv_fwd(i2c_addr_t addr, const uint8_t * data, size_t data_size) {
+bool pb_hook_i2c_send(i2c_addr_t addr, const uint8_t * data, size_t data_size) {
+ sock_dump_msg(addr, data, data_size);
+ return addr == PB_MOD_ADDR; // stop processing message if it is sent to myself
+}
+
+bool pb_hook_i2c_recv(const uint8_t * data, size_t data_size) {
+ sock_dump_msg(0x00, data, data_size);
+ return false;
+}
+
+static void sock_fwd_msg(i2c_addr_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);
@@ -57,7 +67,7 @@ void recv_handler(struct netconn* conn, struct netbuf* buf) {
if (i2ctcp_read(&recv_msg, data, len) != 0) continue;
// forward received message to puzzle bus
- i2c_recv_fwd(recv_msg.addr, (uint8_t *) recv_msg.data, recv_msg.length);
+ sock_fwd_msg(recv_msg.addr, (uint8_t *) recv_msg.data, recv_msg.length);
free(recv_msg.data);
} while (netbuf_next(buf) >= 0);