feature to send image from tcp connection
This commit is contained in:
@@ -36,8 +36,41 @@ DisplayManager* g_display_manager = nullptr;
|
||||
static char g_cmd_buffer[MAX_INPUT_LEN];
|
||||
static int g_cmd_index = 0;
|
||||
|
||||
// Image reception state
|
||||
static bool g_receiving_image = false;
|
||||
static uint8_t *g_image_buffer = nullptr;
|
||||
static uint32_t g_image_bytes_received = 0;
|
||||
static const uint32_t IMAGE_SIZE = 15000; // 400x300 / 8
|
||||
|
||||
void on_tcp_connection_change(bool connected) {
|
||||
if (!connected) {
|
||||
if (g_receiving_image) {
|
||||
printf("TCP disconnected, resetting image reception\n");
|
||||
g_receiving_image = false;
|
||||
if (g_image_buffer) {
|
||||
free(g_image_buffer);
|
||||
g_image_buffer = nullptr;
|
||||
}
|
||||
}
|
||||
// Also reset command buffer to avoid partial commands
|
||||
g_cmd_index = 0;
|
||||
g_cmd_buffer[0] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
static bool execute_command(CommandAction action, const char* input) {
|
||||
switch (action) {
|
||||
case CMD_IMAGE:
|
||||
printf("Command: /image - Waiting for %d bytes...\n", IMAGE_SIZE);
|
||||
if (g_image_buffer) free(g_image_buffer);
|
||||
g_image_buffer = (uint8_t*)malloc(IMAGE_SIZE);
|
||||
if (!g_image_buffer) {
|
||||
printf("Failed to allocate image buffer!\n");
|
||||
return true;
|
||||
}
|
||||
g_receiving_image = true;
|
||||
g_image_bytes_received = 0;
|
||||
return true;
|
||||
case CMD_REFRESH:
|
||||
printf("Command: /refresh\n");
|
||||
epaper_force_refresh();
|
||||
@@ -82,6 +115,7 @@ static bool execute_command(CommandAction action, const char* input) {
|
||||
} else {
|
||||
epaper_send_update("Connected!", true);
|
||||
tcp_debug_init();
|
||||
tcp_set_connection_callback(on_tcp_connection_change);
|
||||
}
|
||||
} else {
|
||||
if (g_display_manager) g_display_manager->refresh("Connection Failed", nullptr);
|
||||
@@ -175,6 +209,23 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
|
||||
}
|
||||
|
||||
static void process_input_char(char c) {
|
||||
if (g_receiving_image) {
|
||||
if (g_image_buffer) {
|
||||
g_image_buffer[g_image_bytes_received++] = (uint8_t)c;
|
||||
if (g_image_bytes_received >= IMAGE_SIZE) {
|
||||
printf("\nImage received (%d bytes). Displaying...\n", g_image_bytes_received);
|
||||
epaper_display_full_image(g_image_buffer, IMAGE_SIZE);
|
||||
g_receiving_image = false;
|
||||
g_image_buffer = nullptr; // Ownership transferred
|
||||
}
|
||||
} else {
|
||||
g_receiving_image = false; // Error state
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (c == 0) return; // Ignore null characters in text mode
|
||||
|
||||
if (c == '\b' || c == 127) {
|
||||
if (g_cmd_index > 0) {
|
||||
printf("\b \b");
|
||||
@@ -190,12 +241,20 @@ static void process_input_char(char c) {
|
||||
} else if (c == '\r' || c == '\n') {
|
||||
printf("\n");
|
||||
|
||||
// Debug: Print buffer content
|
||||
printf("Processing line: '%s' (len=%d)\n", g_cmd_buffer, g_cmd_index);
|
||||
printf("Hex: ");
|
||||
for(int i=0; i<g_cmd_index; i++) printf("%02X ", (unsigned char)g_cmd_buffer[i]);
|
||||
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);
|
||||
printf("Parsed Action: %d\n", action); // Debug action
|
||||
|
||||
if (execute_command(action, g_cmd_buffer)) {
|
||||
// Command handled
|
||||
if (g_display_manager) {
|
||||
|
||||
Reference in New Issue
Block a user