# 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.
