Compare commits

...

3 Commits

Author SHA1 Message Date
Adolfo Reyna 03e826653b change to 4.2 inch screen 2025-12-25 09:38:13 -05:00
Adolfo Reyna 9bc57f7ee2 Refactor input to use stdio drivers for Keyboard and TCP 2025-12-23 23:50:58 -05:00
Adolfo Reyna 9f53148583 tcp port send stdio 2025-12-10 14:11:45 -05:00
7 changed files with 376 additions and 104 deletions
+2
View File
@@ -44,6 +44,8 @@ if (TARGET tinyusb_host)
command_processor.cpp
epaper_manager.cpp
wifi_manager.cpp
tcp_debug.cpp
keyboard_stdio.cpp
)
add_subdirectory(pico-ssd1306 commands)
+38 -41
View File
@@ -1,5 +1,6 @@
#include "epaper_manager.h"
#include "wifi_manager.h"
#include "tcp_debug.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/flash.h"
@@ -11,16 +12,15 @@
// e-Paper library includes
extern "C" {
#include "DEV_Config.h"
#include "EPD_7in5b_V2.h"
#include "EPD_4in2_V2.h"
#include "GUI_Paint.h"
}
// e-Paper display buffers
static UBYTE *g_epd_image = NULL; // Black image buffer
static UBYTE *g_epd_red = NULL; // Red image buffer
// Entry list for display
#define MAX_ENTRIES 15
#define MAX_ENTRIES 10
#define ENTRY_LENGTH 64
typedef struct {
char entries[MAX_ENTRIES][ENTRY_LENGTH];
@@ -48,7 +48,7 @@ typedef struct {
} DisplayMessage;
static void init_epaper_display() {
printf("[Core 1] Initializing 7.5\" e-Paper display (B V2)...\r\n");
printf("[Core 1] Initializing 4.2\" e-Paper display (V2)...\r\n");
// Initialize the hardware
if (DEV_Module_Init() != 0) {
@@ -57,55 +57,51 @@ static void init_epaper_display() {
}
// Initialize the display
printf("[Core 1] EPD_7IN5B_V2_Init()\r\n");
EPD_7IN5B_V2_Init_Fast();
printf("[Core 1] EPD_7IN5B_V2_Clear()\r\n");
printf("[Core 1] EPD_4IN2_V2_Init_Fast()\r\n");
EPD_4IN2_V2_Init();
printf("[Core 1] EPD_4IN2_V2_Clear()\r\n");
EPD_4IN2_V2_Clear();
// Create image buffers for black and red content
// 7.5" display: 800x480, 8 pixels per byte, so (800/8) * 480 = 48000 bytes each
UWORD imagesize = (EPD_7IN5B_V2_WIDTH / 8) * EPD_7IN5B_V2_HEIGHT;
// Create image buffers for black content
// 4.2" display: 400x300, 8 pixels per byte
UWORD imagesize = ((EPD_4IN2_V2_WIDTH % 8 == 0)? (EPD_4IN2_V2_WIDTH / 8 ): (EPD_4IN2_V2_WIDTH / 8 + 1)) * EPD_4IN2_V2_HEIGHT;
g_epd_image = (UBYTE *)malloc(imagesize);
g_epd_red = (UBYTE *)malloc(imagesize);
if (g_epd_image == NULL || g_epd_red == NULL) {
if (g_epd_image == NULL) {
printf("[Core 1] Failed to allocate memory for e-Paper image buffers!\r\n");
return;
}
// Initialize red buffer to all white (0xFF = no red pixels)
for (UWORD i = 0; i < imagesize; i++) {
g_epd_red[i] = 0xFF;
}
// Setup paint buffer for black content
printf("[Core 1] White background\r\n");
Paint_NewImage(g_epd_image, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT, 0, WHITE);
Paint_NewImage(g_epd_image, EPD_4IN2_V2_WIDTH, EPD_4IN2_V2_HEIGHT, 0, WHITE);
Paint_SelectImage(g_epd_image);
Paint_Clear(WHITE);
// Draw header
EPD_7IN5B_V2_Init_Part();
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
printf("[Core 1] Trying to connect to wifi\r\n");
bool connected = wifi_try_auto_connect();
printf("[Core 1] Connected: %d\r\n", connected);
if (connected) {
tcp_debug_init();
}
Paint_SelectImage(g_epd_red);
Paint_SelectImage(g_epd_image);
printf("[Core 1] Drawing header\r\n");
if (connected) {
Paint_DrawString_EN(10, 10, "What's new today: ...", &Font24, WHITE, RED);
char header_text[64];
const char* ip = wifi_get_ip();
snprintf(header_text, sizeof(header_text), "IP: %s", ip ? ip : "Unknown");
Paint_DrawString_EN(10, 10, header_text, &Font24, WHITE, BLACK);
} else {
Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, RED);
Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, BLACK);
}
// Display the image with both black and red buffers
EPD_7IN5B_V2_Display_Partial(g_epd_red, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
// Display the image
EPD_4IN2_V2_PartialDisplay(g_epd_image, 0, 0, EPD_4IN2_V2_WIDTH, 40);
// EPD_4IN2_V2_Display_Fast(g_epd_image);
printf("[Core 1] e-Paper display ready!\r\n");
}
@@ -126,7 +122,7 @@ static void core1_display_init() {
printf("[Core 1] Display ready, waiting for update messages...\n");
// Initialize for partial refresh on second call
EPD_7IN5B_V2_Init_Part();
// EPD_4IN2_V2_Init_Fast(Seconds_1S); // Already done in init
// Core 1 main loop: handle display updates via Queue
while (true) {
@@ -137,15 +133,15 @@ static void core1_display_init() {
// Drain Queue to get the latest message
DisplayMessage *next_msg;
while (queue_try_remove(&g_display_queue, &next_msg)) {
printf("[Core 1] Skipping intermediate update\n");
// printf("[Core 1] Skipping intermediate update\n");
free(msg); // Free the stale message
msg = next_msg;
}
printf("[Core 1] Updating display with %d entries\n", msg->entries.count);
// printf("[Core 1] Updating display with %d entries\n", msg->entries.count);
// Update the e-Paper display
if (g_epd_image != NULL && g_epd_red != NULL) {
if (g_epd_image != NULL) {
// For partial refresh, only clear the text area
UWORD y_start = msg->ystart;
UWORD y_end = msg->yend;
@@ -154,13 +150,14 @@ static void core1_display_init() {
Paint_Clear(WHITE);
// Calculate offset for partial refresh
UBYTE *image_ptr = g_epd_image + (msg->ystart * (EPD_7IN5B_V2_WIDTH / 8));
// UBYTE *image_ptr = g_epd_image + (msg->ystart * (EPD_4IN2_V2_WIDTH / 8));
if(!msg->use_partial){
// for loop 5 times display white partial
Paint_Clear(WHITE);
int i = 0;
for(i = 0; i < 1; i++){
EPD_7IN5B_V2_Display_Partial(image_ptr, msg->xstart, msg->ystart, msg->xend, msg->yend);
EPD_4IN2_V2_PartialDisplay(g_epd_image, 0, 0, 400, 300);
}
}
@@ -169,7 +166,7 @@ static void core1_display_init() {
// paint only the last 10 entries
int start_index = msg->entries.count > 10 ? msg->entries.count - 10 : 0;
for (int i = start_index; i < msg->entries.count; i++) {
if (y_pos + 25 < 480) { //Don't draw beyond screen
if (y_pos + 25 < 300) { //Don't draw beyond screen
Paint_DrawString_EN(20, 50 + i*25, msg->entries.entries[i], &Font16, WHITE, BLACK);
y_pos += 25; // Space between entries
} else {
@@ -177,8 +174,8 @@ static void core1_display_init() {
}
}
// Use partial or full refresh
printf("[Core 1] Using partial refresh\n");
EPD_7IN5B_V2_Display_Partial(image_ptr, msg->xstart, msg->ystart, msg->xend, msg->yend);
// printf("[Core 1] Using partial refresh\n");
EPD_4IN2_V2_PartialDisplay(g_epd_image, 0, 0, 400, 300);
}
// Free the message (it was allocated by core 0)
@@ -235,13 +232,13 @@ void epaper_send_update(const char *entry, bool finish_line) {
// Full text area refresh
msg->xstart = 0;
msg->ystart = 50; // Start below the header
msg->xend = 800; // Full width
msg->yend = 480; // Full height from header onwards
msg->xend = EPD_4IN2_V2_WIDTH; // Full width
msg->yend = EPD_4IN2_V2_HEIGHT; // Full height from header onwards
} else {
// Partial refresh of ONLY the current line
msg->xstart = 0;
msg->ystart = 50 + (g_entry_list.count * 25);
msg->xend = 800;
msg->xend = EPD_4IN2_V2_WIDTH;
msg->yend = msg->ystart + 25;
}
+82 -63
View File
@@ -22,13 +22,19 @@
// Holds last echoed line for display
static char g_last_echo[128] = "";
// Global DisplayManager pointer
static DisplayManager* g_display_manager = nullptr;
#include "command_processor.h"
#include "epaper_manager.h"
#include "wifi_manager.h"
#include "tcp_debug.h"
#include "keyboard_stdio.h"
// Keyboard buffer
// Global display manager instance
DisplayManager* g_display_manager = nullptr;
// Command line buffer
#define MAX_INPUT_LEN 64
static char g_input_buffer[MAX_INPUT_LEN];
static int g_buffer_index = 0;
static char g_cmd_buffer[MAX_INPUT_LEN];
static int g_cmd_index = 0;
static bool execute_command(CommandAction action, const char* input) {
switch (action) {
@@ -75,6 +81,7 @@ static bool execute_command(CommandAction action, const char* input) {
epaper_send_update(err_msg, true);
} else {
epaper_send_update("Connected!", true);
tcp_debug_init();
}
} else {
if (g_display_manager) g_display_manager->refresh("Connection Failed", nullptr);
@@ -103,62 +110,8 @@ static bool execute_command(CommandAction action, const char* input) {
static void process_kbd_report(hid_keyboard_report_t const *report) {
KeyEvent event;
if (parse_keyboard_report(report, &event)) {
if (event.is_backspace) {
if (g_buffer_index > 0) {
printf("\b \b");
g_buffer_index--;
g_input_buffer[g_buffer_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_input_buffer, g_last_echo);
// Update e-Paper (in-place)
epaper_send_update(g_input_buffer, false);
}
} else if (event.is_enter) {
printf("\n");
// Always commit the input line to e-Paper so commands are visible
if (g_buffer_index > 0) {
epaper_send_update(g_input_buffer, true);
}
CommandAction action = parse_command(g_input_buffer);
if (execute_command(action, g_input_buffer)) {
// Command handled
if (g_display_manager) {
g_display_manager->set_last_echo("Command Executed");
g_display_manager->refresh("", "Command Executed");
}
} else {
// Not a command, just text (already committed to e-Paper above)
// Save to last echo
strncpy(g_last_echo, g_input_buffer, sizeof(g_last_echo) - 1);
g_last_echo[sizeof(g_last_echo) - 1] = '\0';
// Update OLED
if (g_display_manager) {
g_display_manager->set_last_echo(g_last_echo);
g_display_manager->refresh("", g_last_echo);
}
}
// Clear buffer
g_buffer_index = 0;
g_input_buffer[0] = '\0';
} else if (event.is_printable) {
printf("%c", event.ascii);
if (g_buffer_index < MAX_INPUT_LEN - 1) {
g_input_buffer[g_buffer_index++] = event.ascii;
g_input_buffer[g_buffer_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_input_buffer, g_last_echo);
// Update e-Paper on every keystroke
epaper_send_update(g_input_buffer, false);
}
if (event.ascii) {
keyboard_stdio_push_char(event.ascii);
}
}
}
@@ -200,8 +153,8 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
g_keyboard_mounted = false;
// Reset input buffer
g_buffer_index = 0;
g_input_buffer[0] = '\0';
g_cmd_index = 0;
g_cmd_buffer[0] = '\0';
if (g_display_manager) {
g_display_manager->refresh("Waiting for Keyboard", nullptr);
@@ -221,8 +174,69 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
}
}
static void process_input_char(char c) {
if (c == '\b' || c == 127) {
if (g_cmd_index > 0) {
printf("\b \b");
g_cmd_index--;
g_cmd_buffer[g_cmd_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_cmd_buffer, g_last_echo);
// Update e-Paper (in-place)
epaper_send_update(g_cmd_buffer, false);
}
} else if (c == '\r' || c == '\n') {
printf("\n");
// Always commit the input line to e-Paper so commands are visible
if (g_cmd_index > 0) {
epaper_send_update(g_cmd_buffer, true);
}
CommandAction action = parse_command(g_cmd_buffer);
if (execute_command(action, g_cmd_buffer)) {
// Command handled
if (g_display_manager) {
g_display_manager->set_last_echo("Command Executed");
g_display_manager->refresh("", "Command Executed");
}
} else {
// Not a command, just text (already committed to e-Paper above)
// Save to last echo
strncpy(g_last_echo, g_cmd_buffer, sizeof(g_last_echo) - 1);
g_last_echo[sizeof(g_last_echo) - 1] = '\0';
// Update OLED
if (g_display_manager) {
g_display_manager->set_last_echo(g_last_echo);
g_display_manager->refresh("", g_last_echo);
}
}
// Clear buffer
g_cmd_index = 0;
g_cmd_buffer[0] = '\0';
} else {
printf("%c", c);
if (g_cmd_index < MAX_INPUT_LEN - 1) {
g_cmd_buffer[g_cmd_index++] = c;
g_cmd_buffer[g_cmd_index] = '\0';
// Update OLED
if (g_display_manager) g_display_manager->refresh(g_cmd_buffer, g_last_echo);
// Update e-Paper on every keystroke
epaper_send_update(g_cmd_buffer, false);
}
}
}
int main() {
stdio_init_all();
keyboard_stdio_init();
sleep_ms(3000); // Give time for power to settle and serial to connect
printf("System Booting...\n");
@@ -241,6 +255,11 @@ int main() {
while (true) {
tuh_task();
int c = getchar_timeout_us(0);
if (c != PICO_ERROR_TIMEOUT) {
process_input_char((char)c);
}
uint32_t now = to_ms_since_boot(get_absolute_time());
if (now - last_print > 5000) {
printf("Heartbeat: %u, Mounted: %d\n", now, g_keyboard_mounted);
+46
View File
@@ -0,0 +1,46 @@
#include "keyboard_stdio.h"
#include "pico/stdio/driver.h"
#include "pico/sync.h"
#define KBD_BUF_SIZE 128
// Circular buffer for keyboard input
static char kbd_buf[KBD_BUF_SIZE];
static volatile int write_idx = 0;
static volatile int read_idx = 0;
static critical_section_t kbd_crit;
void keyboard_stdio_push_char(char c) {
critical_section_enter_blocking(&kbd_crit);
int next_write = (write_idx + 1) % KBD_BUF_SIZE;
if (next_write != read_idx) { // Only write if not full
kbd_buf[write_idx] = c;
write_idx = next_write;
}
critical_section_exit(&kbd_crit);
}
static int keyboard_in_chars(char *buf, int len) {
int count = 0;
critical_section_enter_blocking(&kbd_crit);
while (count < len && read_idx != write_idx) {
buf[count++] = kbd_buf[read_idx];
read_idx = (read_idx + 1) % KBD_BUF_SIZE;
}
critical_section_exit(&kbd_crit);
return count > 0 ? count : PICO_ERROR_NO_DATA;
}
static stdio_driver_t kbd_driver = {
.out_chars = NULL,
.out_flush = NULL,
.in_chars = keyboard_in_chars,
#if PICO_STDIO_ENABLE_CRLF_SUPPORT
.crlf_enabled = false
#endif
};
void keyboard_stdio_init(void) {
critical_section_init(&kbd_crit);
stdio_set_driver_enabled(&kbd_driver, true);
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef KEYBOARD_STDIO_H
#define KEYBOARD_STDIO_H
#include <stdbool.h>
/**
* Initializes the keyboard stdio driver.
* This allows standard C input functions (getchar, scanf, etc.) to read from the USB keyboard.
*/
void keyboard_stdio_init(void);
/**
* Pushes a character into the keyboard input buffer.
* This should be called by the USB HID report handler.
*/
void keyboard_stdio_push_char(char c);
#endif
+177
View File
@@ -0,0 +1,177 @@
#include "tcp_debug.h"
#include "pico/cyw43_arch.h"
#include "lwip/tcp.h"
#include "pico/stdio/driver.h"
#include "pico/sync.h"
#include <string.h>
#include <stdio.h>
#define TCP_PORT 4242
#define DEBUG_BUF_SIZE 2048
#define TCP_IN_BUF_SIZE 128
// Circular buffer for debug output
static char debug_buf[DEBUG_BUF_SIZE];
static volatile int write_idx = 0;
static volatile int read_idx = 0;
// Circular buffer for TCP input
static char tcp_in_buf[TCP_IN_BUF_SIZE];
static volatile int in_write_idx = 0;
static volatile int in_read_idx = 0;
static critical_section_t tcp_in_crit;
static struct tcp_pcb *server_pcb = NULL;
static struct tcp_pcb *client_pcb = NULL;
// Helper to write to buffer
static void buffer_write(const char *data, int len) {
for (int i = 0; i < len; i++) {
int next_write = (write_idx + 1) % DEBUG_BUF_SIZE;
if (next_write != read_idx) { // Only write if not full
debug_buf[write_idx] = data[i];
write_idx = next_write;
}
}
}
// stdio driver callback
static void tcp_out_chars(const char *buf, int len) {
// Handle CRLF conversion manually if needed, or just write raw
// The issue is likely that \n is sent without \r, causing "staircase" effect in raw terminals
for (int i = 0; i < len; i++) {
if (buf[i] == '\n') {
char cr = '\r';
buffer_write(&cr, 1);
}
buffer_write(&buf[i], 1);
}
}
static int tcp_in_chars(char *buf, int len) {
int count = 0;
critical_section_enter_blocking(&tcp_in_crit);
while (count < len && in_read_idx != in_write_idx) {
buf[count++] = tcp_in_buf[in_read_idx];
in_read_idx = (in_read_idx + 1) % TCP_IN_BUF_SIZE;
}
critical_section_exit(&tcp_in_crit);
return count > 0 ? count : PICO_ERROR_NO_DATA;
}
static stdio_driver_t tcp_driver = {
.out_chars = tcp_out_chars,
.out_flush = NULL,
.in_chars = tcp_in_chars,
#if PICO_STDIO_ENABLE_CRLF_SUPPORT
.crlf_enabled = false
#endif
};
// TCP callbacks
static err_t tcp_server_poll(void *arg, struct tcp_pcb *tpcb) {
if (tpcb != client_pcb) return ERR_OK;
// Check if there is data in the ring buffer to send
if (write_idx != read_idx) {
int len_to_send = 0;
char send_buf[256]; // Temporary buffer for one TCP write chunk
// Copy from ring buffer to linear buffer
while (write_idx != read_idx && len_to_send < sizeof(send_buf)) {
send_buf[len_to_send++] = debug_buf[read_idx];
read_idx = (read_idx + 1) % DEBUG_BUF_SIZE;
}
if (len_to_send > 0) {
err_t err = tcp_write(tpcb, send_buf, len_to_send, TCP_WRITE_FLAG_COPY);
if (err == ERR_OK) {
tcp_output(tpcb);
} else {
// If write failed (e.g. buffer full), rewind read_idx (simple retry logic)
// Note: This is a simplification. In a real robust system we might drop data or handle partial writes.
// For now, we just lose the data if TCP is full to avoid blocking the system.
}
}
}
return ERR_OK;
}
static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
if (!p) {
tcp_close(tpcb);
client_pcb = NULL;
printf("TCP debug client disconnected\n");
return ERR_OK;
}
// Process received data into input buffer
if (p->tot_len > 0) {
critical_section_enter_blocking(&tcp_in_crit);
struct pbuf *q = p;
while (q != NULL) {
char *data = (char *)q->payload;
for (int i = 0; i < q->len; i++) {
int next_write = (in_write_idx + 1) % TCP_IN_BUF_SIZE;
if (next_write != in_read_idx) {
tcp_in_buf[in_write_idx] = data[i];
in_write_idx = next_write;
}
}
q = q->next;
}
critical_section_exit(&tcp_in_crit);
tcp_recved(tpcb, p->tot_len);
}
pbuf_free(p);
return ERR_OK;
}
static err_t tcp_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
if (client_pcb) {
// Already have a client, reject or close old?
// Let's close the old one and accept the new one (last one wins)
tcp_abort(client_pcb);
}
client_pcb = newpcb;
tcp_recv(newpcb, tcp_server_recv);
tcp_poll(newpcb, tcp_server_poll, 1); // Poll every 500ms (coarse grain) -> actually arg is interval in 500ms steps?
// Wait, poll interval is passed to tcp_poll. 1 means every 500ms.
// We might want faster polling for debug output.
// But we can't poll faster than the lwIP timer.
// However, we can also trigger writes from the main loop if we wanted, but polling is safer for threading.
printf("TCP debug client connected\n");
return ERR_OK;
}
bool tcp_debug_init(void) {
if (server_pcb) return true;
critical_section_init(&tcp_in_crit);
// Register stdio driver
stdio_set_driver_enabled(&tcp_driver, true);
cyw43_arch_lwip_begin();
server_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (!server_pcb) {
cyw43_arch_lwip_end();
return false;
}
err_t err = tcp_bind(server_pcb, IP_ANY_TYPE, TCP_PORT);
if (err != ERR_OK) {
cyw43_arch_lwip_end();
return false;
}
server_pcb = tcp_listen(server_pcb);
tcp_accept(server_pcb, tcp_server_accept);
cyw43_arch_lwip_end();
printf("TCP debug server listening on port %d\n", TCP_PORT);
return true;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef TCP_DEBUG_H
#define TCP_DEBUG_H
#include <stdbool.h>
/**
* Initializes the TCP debug server on port 4242.
* Registers a stdio driver that redirects printf output to connected TCP clients.
* Returns true on success.
*/
bool tcp_debug_init(void);
#endif