wifi working tested on hardware
This commit is contained in:
@@ -10,12 +10,14 @@
|
||||
#include <ctype.h>
|
||||
#include <cstring>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include "display.h"
|
||||
#include "commands/echo.h"
|
||||
#include "keyboard_input.h"
|
||||
#include "command_processor.h"
|
||||
#include "epaper_manager.h"
|
||||
#include "wifi_manager.h"
|
||||
|
||||
// Holds last echoed line for display
|
||||
static char g_last_echo[128] = "";
|
||||
@@ -28,7 +30,7 @@ static DisplayManager* g_display_manager = nullptr;
|
||||
static char g_input_buffer[MAX_INPUT_LEN];
|
||||
static int g_buffer_index = 0;
|
||||
|
||||
static bool execute_command(CommandAction action) {
|
||||
static bool execute_command(CommandAction action, const char* input) {
|
||||
switch (action) {
|
||||
case CMD_REFRESH:
|
||||
printf("Command: /refresh\n");
|
||||
@@ -38,9 +40,54 @@ static bool execute_command(CommandAction action) {
|
||||
printf("Command: /clear\n");
|
||||
epaper_clear();
|
||||
return true;
|
||||
case CMD_WIFI:
|
||||
printf("Command: /wifisetup (Not implemented)\n");
|
||||
case CMD_SCAN:
|
||||
printf("Command: /scan\n");
|
||||
if (g_display_manager) g_display_manager->refresh("Scanning WiFi...", nullptr);
|
||||
epaper_send_update("Scanning WiFi...", true);
|
||||
wifi_scan();
|
||||
return true;
|
||||
case CMD_CONNECT: {
|
||||
printf("Command: /connect\n");
|
||||
char ssid[33] = {0};
|
||||
char password[64] = {0};
|
||||
// Skip "/connect " (9 chars)
|
||||
const char* args = input + 9;
|
||||
|
||||
// Simple parsing: first word is ssid, rest is password
|
||||
int parsed = sscanf(args, "%32s %63s", ssid, password);
|
||||
if (parsed < 2) {
|
||||
printf("Usage: /connect <ssid> <password>\n");
|
||||
if (g_display_manager) g_display_manager->refresh("Usage: /connect <ssid> <pass>", nullptr);
|
||||
// epaper_send_update("Usage: /connect <ssid> <pass>", true);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (g_display_manager) g_display_manager->refresh("Connecting...", ssid);
|
||||
// epaper_send_update("Connecting...", true);
|
||||
|
||||
if (wifi_connect(ssid, password)) {
|
||||
if (g_display_manager) g_display_manager->refresh("Connected!", ssid);
|
||||
// epaper_send_update("Connected!", true);
|
||||
// wifi_save_credentials(ssid, password);
|
||||
} else {
|
||||
if (g_display_manager) g_display_manager->refresh("Connection Failed", nullptr);
|
||||
// epaper_send_update("Connection Failed", true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case CMD_STATUS: {
|
||||
printf("Command: /status\n");
|
||||
struct mallinfo m = mallinfo();
|
||||
char status_msg[64];
|
||||
// fordblks is the free chunk size in the arena.
|
||||
// Note: This might not account for the total available system RAM if the heap hasn't grown to fill it yet.
|
||||
// But it gives an idea of fragmentation and available malloc-able memory within the current arena.
|
||||
snprintf(status_msg, sizeof(status_msg), "Heap: %d B, IP: %s", m.fordblks, wifi_get_ip());
|
||||
printf("%s\n", status_msg);
|
||||
if (g_display_manager) g_display_manager->refresh(status_msg, nullptr);
|
||||
epaper_send_update(status_msg, true);
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -64,34 +111,35 @@ static void process_kbd_report(hid_keyboard_report_t const *report) {
|
||||
} 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)) {
|
||||
// Command handled, clear buffer but don't commit line
|
||||
g_buffer_index = 0;
|
||||
g_input_buffer[0] = '\0';
|
||||
|
||||
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 {
|
||||
// Update e-Paper (commit line)
|
||||
epaper_send_update(g_input_buffer, true);
|
||||
// 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';
|
||||
|
||||
// Clear buffer
|
||||
g_buffer_index = 0;
|
||||
g_input_buffer[0] = '\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) {
|
||||
@@ -178,7 +226,12 @@ int main() {
|
||||
display.init();
|
||||
g_display_manager = &display;
|
||||
|
||||
display.refresh("Waiting for Keyboard", nullptr);
|
||||
if (wifi_try_auto_connect()) {
|
||||
display.refresh("WiFi Auto-Connected", nullptr);
|
||||
epaper_send_update("WiFi Auto-Connected", true);
|
||||
} else {
|
||||
display.refresh("Waiting for Keyboard", nullptr);
|
||||
}
|
||||
|
||||
printf("Initializing TinyUSB Host...\n");
|
||||
tuh_init(BOARD_TUH_RHPORT);
|
||||
|
||||
Reference in New Issue
Block a user