Add USB Keyboard support

This commit is contained in:
Adolfo Reyna
2025-11-29 22:55:07 -05:00
parent d2587410e5
commit 2cb23b70d3
4 changed files with 284 additions and 229 deletions

View File

@@ -1,12 +1,11 @@
/*
* Simple USB Serial Echo Program with Timeout, Reconnect, and Backspace Logic.
* Converted to C++ so the project can use the `pico-ssd1306` C++ library.
* Integrated with e-ink display support via Pico_ePaper library.
* USB Host Keyboard Example with Display Support
*/
#include "pico/stdlib.h"
#include "pico/time.h" // Needed for time_us_64()
#include "pico/time.h"
#include "pico/multicore.h"
#include "tusb.h"
#include <stdio.h>
#include <ctype.h>
#include <cstring>
@@ -65,8 +64,8 @@ void init_epaper_display() {
printf("EPD_7IN5B_V2_Init()\r\n");
EPD_7IN5B_V2_Init_Fast();
printf("EPD_7IN5B_V2_Clear()\r\n");
EPD_7IN5B_V2_Clear();
DEV_Delay_ms(500);
// EPD_7IN5B_V2_Clear();
// DEV_Delay_ms(500);
// Create image buffers for black and red content
// 7.5" display: 800x480, 8 pixels per byte, so (800/8) * 480 = 48000 bytes each
@@ -93,27 +92,20 @@ void init_epaper_display() {
// 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);
Paint_SelectImage(g_epd_red);
printf("Drawing header\r\n");
Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, RED);
// Display the image with both black and red buffers
// printf("display text\r\n");
EPD_7IN5B_V2_Display_Partial(g_epd_red, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
// printf("delay\r\n");
// DEV_Delay_ms(1000);
printf("e-Paper display ready!\r\n");
// EPD_7IN5B_V2_Sleep();
}
// Define the maximum size for the input string buffer
#define MAX_INPUT_LEN 64
// Define the timeout period: 5 seconds in microseconds
#define TIMEOUT_US 5000000
// ASCII code for Backspace (often sent as 0x08)
#define ASCII_BACKSPACE 8
/**
* Add an entry to the display list and send update to core 1
*/
@@ -207,12 +199,6 @@ void core1_display_init() {
// Draw all entries starting below header
UWORD y_pos = 0;
/*for (int i = 0; i < msg->entries.count; i++) {
if (y_pos + 20 < 480) { // Don't draw beyond screen
Paint_DrawString_EN(20, y_pos, msg->entries.entries[i], &Font16, WHITE, BLACK);
y_pos += 25; // Space between entries
}
}*/
// paint only the last 3 entries
int start_index = msg->entries.count > 2 ? msg->entries.count - 2 : 0;
for (int i = start_index; i < msg->entries.count; i++) {
@@ -224,21 +210,8 @@ void core1_display_init() {
}
}
// Use partial or full refresh
//if (msg->use_partial) {
printf("[Core 1] Using partial refresh\n");
EPD_7IN5B_V2_Display_Partial(g_epd_image, msg->xstart, msg->ystart, msg->xend, msg->yend);
/*} else {
printf("[Core 1] Using full refresh\n");
// EPD_7IN5B_V2_Init();
// EPD_7IN5B_V2_Clear();
// DEV_Delay_ms(500);
// Re-initialize partial refresh mode after full refresh
printf("[Core 1] Reinitializing partial refresh mode\n");
EPD_7IN5B_V2_Init_Part();
Paint_SelectImage(g_epd_image);
EPD_7IN5B_V2_Display_Partial(g_epd_image, msg->xstart, msg->ystart, msg->xend, msg->yend);
}*/
printf("[Core 1] Using partial refresh\n");
EPD_7IN5B_V2_Display_Partial(g_epd_image, msg->xstart, msg->ystart, msg->xend, msg->yend);
}
// Free the message (it was allocated by core 0)
@@ -252,78 +225,248 @@ void core1_display_init() {
}
}
void wait_for_usb_connection(DisplayManager &display) {
printf("Waiting for USB host to connect...\n");
while (!stdio_usb_connected()) {
sleep_ms(100);
// Global DisplayManager pointer
static DisplayManager* g_display_manager = nullptr;
// Keyboard buffer
#define MAX_INPUT_LEN 64
static char g_input_buffer[MAX_INPUT_LEN];
static int g_buffer_index = 0;
// ASCII code for Backspace (often sent as 0x08)
#define ASCII_BACKSPACE 8
static const uint8_t keycode2ascii[128][2] = {
{0, 0}, /* 0x00 */
{0, 0}, /* 0x01 */
{0, 0}, /* 0x02 */
{0, 0}, /* 0x03 */
{'a', 'A'}, /* 0x04 */
{'b', 'B'}, /* 0x05 */
{'c', 'C'}, /* 0x06 */
{'d', 'D'}, /* 0x07 */
{'e', 'E'}, /* 0x08 */
{'f', 'F'}, /* 0x09 */
{'g', 'G'}, /* 0x0a */
{'h', 'H'}, /* 0x0b */
{'i', 'I'}, /* 0x0c */
{'j', 'J'}, /* 0x0d */
{'k', 'K'}, /* 0x0e */
{'l', 'L'}, /* 0x0f */
{'m', 'M'}, /* 0x10 */
{'n', 'N'}, /* 0x11 */
{'o', 'O'}, /* 0x12 */
{'p', 'P'}, /* 0x13 */
{'q', 'Q'}, /* 0x14 */
{'r', 'R'}, /* 0x15 */
{'s', 'S'}, /* 0x16 */
{'t', 'T'}, /* 0x17 */
{'u', 'U'}, /* 0x18 */
{'v', 'V'}, /* 0x19 */
{'w', 'W'}, /* 0x1a */
{'x', 'X'}, /* 0x1b */
{'y', 'Y'}, /* 0x1c */
{'z', 'Z'}, /* 0x1d */
{'1', '!'}, /* 0x1e */
{'2', '@'}, /* 0x1f */
{'3', '#'}, /* 0x20 */
{'4', '$'}, /* 0x21 */
{'5', '%'}, /* 0x22 */
{'6', '^'}, /* 0x23 */
{'7', '&'}, /* 0x24 */
{'8', '*'}, /* 0x25 */
{'9', '('}, /* 0x26 */
{'0', ')'}, /* 0x27 */
{'\r', '\r'}, /* 0x28 ENTER */
{'\x1b', '\x1b'}, /* 0x29 ESCAPE */
{'\b', '\b'}, /* 0x2a BACKSPACE */
{'\t', '\t'}, /* 0x2b TAB */
{' ', ' '}, /* 0x2c SPACE */
{'-', '_'}, /* 0x2d MINUS */
{'=', '+'}, /* 0x2e EQUAL */
{'[', '{'}, /* 0x2f BRACKET_LEFT */
{']', '}'}, /* 0x30 BRACKET_RIGHT */
{'\\', '|'}, /* 0x31 BACKSLASH */
{'#', '~'}, /* 0x32 EUROPE_1 */
{';', ':'}, /* 0x33 SEMICOLON */
{'\'', '\"'}, /* 0x34 APOSTROPHE */
{'`', '~'}, /* 0x35 GRAVE */
{',', '<'}, /* 0x36 COMMA */
{'.', '>'}, /* 0x37 PERIOD */
{'/', '?'}, /* 0x38 SLASH */
{0, 0}, /* 0x39 CAPS_LOCK */
{0, 0}, /* 0x3a F1 */
{0, 0}, /* 0x3b F2 */
{0, 0}, /* 0x3c F3 */
{0, 0}, /* 0x3d F4 */
{0, 0}, /* 0x3e F5 */
{0, 0}, /* 0x3f F6 */
{0, 0}, /* 0x40 F7 */
{0, 0}, /* 0x41 F8 */
{0, 0}, /* 0x42 F9 */
{0, 0}, /* 0x43 F10 */
{0, 0}, /* 0x44 F11 */
{0, 0}, /* 0x45 F12 */
{0, 0}, /* 0x46 PRINT_SCREEN */
{0, 0}, /* 0x47 SCROLL_LOCK */
{0, 0}, /* 0x48 PAUSE */
{0, 0}, /* 0x49 INSERT */
{0, 0}, /* 0x4a HOME */
{0, 0}, /* 0x4b PAGE_UP */
{0, 0}, /* 0x4c DELETE */
{0, 0}, /* 0x4d END */
{0, 0}, /* 0x4e PAGE_DOWN */
{0, 0}, /* 0x4f RIGHT_ARROW */
{0, 0}, /* 0x50 LEFT_ARROW */
{0, 0}, /* 0x51 DOWN_ARROW */
{0, 0}, /* 0x52 UP_ARROW */
{0, 0}, /* 0x53 NUM_LOCK */
{'/', '/'}, /* 0x54 KP_DIVIDE */
{'*', '*'}, /* 0x55 KP_MULTIPLY */
{'-', '-'}, /* 0x56 KP_MINUS */
{'+', '+'}, /* 0x57 KP_PLUS */
{'\r', '\r'}, /* 0x58 KP_ENTER */
{'1', '1'}, /* 0x59 KP_1 */
{'2', '2'}, /* 0x5a KP_2 */
{'3', '3'}, /* 0x5b KP_3 */
{'4', '4'}, /* 0x5c KP_4 */
{'5', '5'}, /* 0x5d KP_5 */
{'6', '6'}, /* 0x5e KP_6 */
{'7', '7'}, /* 0x5f KP_7 */
{'8', '8'}, /* 0x60 KP_8 */
{'9', '9'}, /* 0x61 KP_9 */
{'0', '0'}, /* 0x62 KP_0 */
{'.', '.'}, /* 0x63 KP_DECIMAL */
};
static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8_t keycode) {
for(uint8_t i=0; i<6; i++) {
if (report->keycode[i] == keycode) return true;
}
printf("\nConnection Established! Starting Echo Session...\n");
display.refresh(">", nullptr);
return false;
}
void run_echo_session(DisplayManager &display) {
char input_buffer[MAX_INPUT_LEN];
int buffer_index = 0;
static void process_kbd_report(hid_keyboard_report_t const *report) {
static hid_keyboard_report_t prev_report = { 0, 0, {0} };
printf("--- Welcome User! ---\n");
printf("Type a command (or write help):\n");
printf("--------------------------------------------\n");
printf("> ");
while (stdio_usb_connected()) {
int c = getchar_timeout_us(0);
if (c != PICO_ERROR_TIMEOUT) {
char input_char = (char)c;
if (input_char == ASCII_BACKSPACE || input_char == 127) {
if (buffer_index > 0) {
buffer_index--;
printf("\b \b");
// update displays to reflect removed char
input_buffer[buffer_index] = '\0';
display.refresh(input_buffer, g_last_echo);
// send_display_update(input_buffer, g_last_echo);
for(uint8_t i=0; i<6; i++) {
if (report->keycode[i]) {
if (find_key_in_report(&prev_report, report->keycode[i])) {
// exist in previous report means the current key is holding
} else {
// not existed in previous report means the current key is pressed
bool const is_shift = report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
uint8_t ch = keycode2ascii[report->keycode[i]][is_shift ? 1 : 0];
if (ch) {
printf("%c", ch);
if (ch == '\r' || ch == '\n') {
printf("\n");
// Process command
g_input_buffer[g_buffer_index] = '\0';
// Echo logic
echo_and_reset(g_input_buffer, &g_buffer_index);
// Save last echoed (all caps) for display
size_t k = 0;
for (; g_input_buffer[k] != '\0' && k < sizeof(g_last_echo) - 1; ++k) {
g_last_echo[k] = g_input_buffer[k];
}
g_last_echo[k] = '\0';
// Update displays
if (g_display_manager) {
g_display_manager->set_last_echo(g_last_echo);
g_display_manager->refresh("", g_last_echo);
}
send_display_update(g_last_echo);
// Reset buffer
g_buffer_index = 0;
g_input_buffer[0] = '\0';
} else if (ch == '\b' || ch == 127) {
if (g_buffer_index > 0) {
g_buffer_index--;
g_input_buffer[g_buffer_index] = '\0';
if (g_display_manager) {
g_display_manager->refresh(g_input_buffer, g_last_echo);
}
}
} else if (g_buffer_index < MAX_INPUT_LEN - 1) {
g_input_buffer[g_buffer_index] = ch;
g_buffer_index++;
g_input_buffer[g_buffer_index] = '\0';
if (g_display_manager) {
g_display_manager->refresh(g_input_buffer, g_last_echo);
}
}
}
} else if (input_char == '\r' || input_char == '\n') {
echo_and_reset(input_buffer, &buffer_index);
// Save last echoed (all caps) for display
size_t i = 0;
for (; input_buffer[i] != '\0' && i < sizeof(g_last_echo) - 1; ++i) {
g_last_echo[i] = input_buffer[i];
}
g_last_echo[i] = '\0';
// Update both displays to show cleared input and last echo
display.set_last_echo(g_last_echo);
display.refresh("", g_last_echo);
send_display_update(g_last_echo);
} else if (buffer_index < (MAX_INPUT_LEN - 1) && isprint(input_char)) {
printf("%c", input_char);
input_buffer[buffer_index] = input_char;
buffer_index++;
// update both displays with current input
input_buffer[buffer_index] = '\0';
display.refresh(input_buffer, g_last_echo);
// send_display_update(input_buffer, g_last_echo);
}
}
sleep_us(100);
}
printf("\nHost disconnected. Ending Echo Session.\n");
prev_report = *report;
}
//--------------------------------------------------------------------+
// TinyUSB Callbacks
//--------------------------------------------------------------------+
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) {
printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD) {
printf("Keyboard mounted\r\n");
if (g_display_manager) {
g_display_manager->refresh("Keyboard Connected", nullptr);
}
if (!tuh_hid_receive_report(dev_addr, instance)) {
printf("Error: cannot request to receive report\r\n");
}
}
}
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
if (g_display_manager) {
g_display_manager->refresh("Keyboard Disconnected", nullptr);
}
}
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) {
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD) {
process_kbd_report((hid_keyboard_report_t const*) report);
}
// continue to request to receive report
if (!tuh_hid_receive_report(dev_addr, instance)) {
printf("Error: cannot request to receive report\r\n");
}
}
int main() {
stdio_init_all();
// Launch display initialization on core 1
printf("Launching e-Paper display init on core 1...\n");
multicore_launch_core1(core1_display_init);
DisplayManager display;
display.init();
display.refresh("Waiting for USB/Serial", nullptr);
g_display_manager = &display;
display.refresh("Waiting for Keyboard", nullptr);
printf("Initializing TinyUSB Host...\n");
tuh_init(BOARD_TUH_RHPORT);
while (true) {
wait_for_usb_connection(display);
run_echo_session(display);
tuh_task();
}
return 0;