From 1ce6e1adab00fd2fa87f370a51a861ff267e5376 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Fri, 9 Jan 2026 10:20:54 -0500 Subject: [PATCH] screen auto off in 30s --- README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ eink_api.c | 13 +++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ff74e4 --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# eInk API for Raspberry Pi Pico + +This firmware drives a Waveshare 4.2" e-Paper display from a Raspberry Pi Pico / Pico W. It launches the e-paper driver on core 1 while core 0 acts as a simple serial command processor, giving you an interactive API for drawing primitives, text, and editor-mode logs over USB. + +## Features + +- Lightweight C implementation built on the official Pico SDK and Waveshare e-Paper drivers. +- Core 1 renders directly to a framebuffer and updates the display with both full and partial refreshes. +- Serial command queue exposes operations such as clearing, drawing geometry, writing text, and entering an editor mode. +- Automatic display shutdown after 30 seconds of inactivity to preserve the panel. + +## Requirements + +- Raspberry Pi Pico or Pico W (project sets `PICO_BOARD` to `pico_w`). +- Waveshare 4.2" e-Paper V2 display, wired to SPI + compatible GPIOs defined in `DEV_Config.h`. +- Pico SDK (2.2.0) installed and available via `PICO_SDK_PATH`. +- `cmake`, `ninja`, and `picotool` (or the Pico toolchain) for flashing. + +## Building + +```sh +# configure the build +cmake -S . -B build + +# build the UF2 image +cmake --build build +``` + +After building, flash the firmware to your Pico (e.g. by copying `build/eink_api.uf2` to the Pico's mass storage device or using `picotool load build/eink_api.uf2`). + +## Usage + +1. Open a serial terminal (115200 baud) over the Pico's USB CDC interface. +2. Type commands followed by `Enter`. The firmware echoes the command buffer and executes the operation on core 1. +3. Most commands require the display to be initialized (`init`). The device automatically turns off the screen after ~30 seconds of inactivity, so send another command to revive it. +4. If `editor` mode is enabled, keystrokes are batched and rendered directly on the display until inactivity ends the mode. + +### Supported Commands + +| Command | Description | +| --- | --- | +| `init` | Initializes the e-Paper panel and allocates the framebuffer. | +| `clear` | Clears the current framebuffer to white (requires display on). | +| `display` | Pushes the framebuffer to the panel (requires display on). | +| `draw_test` | Draws a hard-coded Waveshare demo pattern. | +| `draw_text x y text` | Writes `text` at `(x,y)` using the current font. | +| `draw_point x y` | Draws a black point at the given coordinates. | +| `draw_line x1 y1 x2 y2` | Draws a black line between two points. | +| `draw_rectangle x1 y1 x2 y2` | Draws an empty rectangle. | +| `draw_circle x y r` | Draws an empty circle with radius `r`. | +| `draw_num x y num` | Draws `num` using the default numeric font. | +| `set_pixel x y color` | Sets a single pixel to `color` (0=white, 1=black). | +| `close` | Sends the panel to deep sleep and frees the framebuffer. | +| `editor` | Enables editor mode; text typed is rendered live on-screen until inactivity. | + +## Notes + +- The project imports the Waveshare e-Paper libraries from `Pico_ePaper_Code/c/lib/*` for configuration, fonts, GUI primitives, and the panel driver. +- USB serial I/O is enabled (`pico_enable_stdio_usb`), while UART stdio is disabled to avoid conflicts (`pico_enable_stdio_uart` set to `0`). +- The firmware allocates the framebuffer via `malloc` and frees it when the display goes to sleep; avoid running out of heap on severely constrained Pico builds. +- Editor-mode inactivity is 10 seconds before automatically exiting back to command mode. diff --git a/eink_api.c b/eink_api.c index e6acb03..80ec37c 100644 --- a/eink_api.c +++ b/eink_api.c @@ -21,6 +21,7 @@ void process_command(char *cmd); void core1_entry(); bool screen_on = false; +uint32_t screen_last_ts = 0; int epaper_init() { @@ -47,7 +48,7 @@ int epaper_init() } bool editor_mode = false; -int editor_last_ts = 0; +uint32_t editor_last_ts = 0; char screen_log[2000]; int screen_log_i = 0; sFONT *current_font = &Font16; @@ -211,6 +212,8 @@ void core1_entry() { if(!screen_on){ epaper_init(); } + absolute_time_t time = get_absolute_time(); + screen_last_ts = to_ms_since_boot(time); if(editor_mode){ bool overflow = false; Paint_Clear(WHITE); @@ -243,6 +246,14 @@ void core1_entry() { } } + if(screen_on){ + absolute_time_t time = get_absolute_time(); + uint32_t ms = to_ms_since_boot(time); + if(screen_last_ts + 30000 < ms){ + printf("Turning off screen due to inactivity (30s)"); + epaper_close(); + } + } } }