From 5bb9ccbdc58f2fad08692d4060a8c2b4bd41e834 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Tue, 18 Nov 2025 13:10:54 -0500 Subject: [PATCH] adding oled support --- CMakeLists.txt | 8 ++-- hello_usb.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ pico-ssd1306 | 1 + 3 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 hello_usb.cpp create mode 160000 pico-ssd1306 diff --git a/CMakeLists.txt b/CMakeLists.txt index 704a560..19ced5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,12 +36,14 @@ pico_sdk_init() # Add executable. Default name is the project name, version 0.1 if (TARGET tinyusb_device) - add_executable(hello_usb - hello_usb.c + add_executable(hello_usb + hello_usb.cpp ) + add_subdirectory(pico-ssd1306) + # pull in common dependencies - target_link_libraries(hello_usb pico_stdlib) + target_link_libraries(hello_usb pico_stdlib pico_ssd1306 hardware_i2c) # enable usb output, disable uart output pico_enable_stdio_usb(hello_usb 1) diff --git a/hello_usb.cpp b/hello_usb.cpp new file mode 100644 index 0000000..9aa44c6 --- /dev/null +++ b/hello_usb.cpp @@ -0,0 +1,113 @@ +/* + * Simple USB Serial Echo Program with Timeout, Reconnect, and Backspace Logic. + * Converted to C++ so the project can use the `pico-ssd1306` C++ library. + */ + +#include "pico/stdlib.h" +#include "pico/time.h" // Needed for time_us_64() +#include +#include + +#include "hardware/i2c.h" +#include "hardware/gpio.h" +#include "pico-ssd1306/ssd1306.h" + +using namespace pico_ssd1306; + +// 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 + +// Helper function to handle the echoing and resetting of the buffer +void echo_and_reset(char* buffer, int* index_ptr) { + if (*index_ptr > 0) { + buffer[*index_ptr] = '\0'; + printf("\nEchoed (ALL CAPS): "); + for (int i = 0; buffer[i] != '\0'; i++) { + printf("%c", toupper(buffer[i])); + } + printf("\n"); + } + *index_ptr = 0; + printf("> "); +} + +void wait_for_usb_connection() { + printf("Waiting for USB host to connect...\n"); + while (!stdio_usb_connected()) { + sleep_ms(100); + } + printf("\nConnection Established! Starting Echo Session...\n"); +} + +void run_echo_session() { + char input_buffer[MAX_INPUT_LEN]; + int buffer_index = 0; + uint64_t last_char_time = time_us_64(); + + printf("--- Pico USB String Echo Program Started ---\n"); + printf("Type a sentence (up to %d chars). It will echo on Enter/Timeout.\n", MAX_INPUT_LEN - 2); + printf("--------------------------------------------\n"); + printf("> "); + + while (stdio_usb_connected()) { + int c = getchar_timeout_us(0); + if (c != PICO_ERROR_TIMEOUT) { + last_char_time = time_us_64(); + char input_char = (char)c; + if (input_char == ASCII_BACKSPACE || input_char == 127) { + if (buffer_index > 0) { + buffer_index--; + printf("\b \b"); + } + } else if (input_char == '\r' || input_char == '\n') { + echo_and_reset(input_buffer, &buffer_index); + } else if (buffer_index < (MAX_INPUT_LEN - 1) && isprint(input_char)) { + printf("%c", input_char); + input_buffer[buffer_index] = input_char; + buffer_index++; + } + } + + if (buffer_index > 0 && (time_us_64() - last_char_time) > TIMEOUT_US) { + printf("\n--- Timeout Reached (5.0s silence) ---\n"); + echo_and_reset(input_buffer, &buffer_index); + last_char_time = time_us_64(); + } + + sleep_us(100); + } + + printf("\nHost disconnected. Ending Echo Session.\n"); +} + +int main() { + stdio_init_all(); + + // Initialize I2C for the SSD1306 display (common pins: SDA=GPIO4, SCL=GPIO5) + i2c_init(i2c0, 400 * 1000); + gpio_set_function(4, GPIO_FUNC_I2C); + gpio_set_function(5, GPIO_FUNC_I2C); + gpio_pull_up(4); + gpio_pull_up(5); + + // Create display object (address 0x3C is common). Adjust size/address if needed. + SSD1306 display(i2c0, 0x3C, Size::W128xH64); + display.clear(); + display.sendBuffer(); + for (int y = 0; y < 64; y++){ + display.setPixel(64, y); + } + display.sendBuffer(); //Send buffer to device and show on screen + + + while (true) { + wait_for_usb_connection(); + run_echo_session(); + } + + return 0; +} diff --git a/pico-ssd1306 b/pico-ssd1306 new file mode 160000 index 0000000..2cec467 --- /dev/null +++ b/pico-ssd1306 @@ -0,0 +1 @@ +Subproject commit 2cec467a06bb8cd89363e12091bfd2318f7feab6