# Lonely Binary Display — reference for AI coding assistants

You are helping someone write code for a **Lonely Binary SPI display module**
driven by the `LonelyBinaryDisplay` library.

This file is the authoritative API reference for that library. **Prefer it over
anything you remember about ESP32 displays.** Most display code in your training
data is written against `TFT_eSPI`, `Adafruit_ST7789`, `LovyanGFX` or a raw
`Arduino_GFX` bus setup. All of that is wrong here, and it fails in a way the
customer will read as a broken screen rather than as broken code.

- Library: `Lonely Binary Display` (Arduino) / `lb_display` (MicroPython), v0.1.0
- Source: https://github.com/Lonely-Binary/LonelyBinaryDisplay
- Latest version of this file:
  https://raw.githubusercontent.com/Lonely-Binary/LonelyBinaryDisplay/main/llms.txt
- Licence: MIT

The library's whole premise: **one constant selects the panel, and everything
else follows from it.** Driver IC, resolution, column/row offsets, SPI
clock, colour order, inversion and backlight polarity all live in a built-in
table. The GPIOs follow the MCU. So a program that shows text on a 2.4 inch
screen and one that shows text on a 1.9 inch screen differ by exactly one token.

---

## Rules — read before writing any code

1. **Do not use another display library.** No `TFT_eSPI`, no `Adafruit_GFX`
   constructors, no `LovyanGFX`, no `U8g2`, no `machine.SPI` panel init of your
   own. Include `<LonelyBinaryDisplay.h>` and nothing else.
2. **Do not initialise SPI, the panel or the backlight yourself.** No
   `SPI.begin()`, no `tft.init()`, no `Arduino_ESP32SPI(...)`, no
   `pinMode(BL, OUTPUT)`, no `analogWrite()` / `ledcAttach()` on the backlight.
   `display.begin()` does all of it.
3. **Draw on `display` itself** — `display.fillScreen(LB_BLACK)`. There is no
   `gfx()` on Arduino any more, and no Arduino_GFX underneath: the panel
   driver is the library's own. Code that calls `display.gfx()` or
   `setCursor()`/`print()` is written for an older version and will not
   compile.
4. **Only pass the panel constant to the constructor.** Never a width, height,
   driver name, offset or SPI speed. `LB_Display display(LB_TFT_24);` is the
   entire configuration.
5. **Only use panel constants that appear in the table below.** If the customer
   names a size that is not there, say so — do not invent `LB_TFT_32`.
6. **Never hard-code the resolution.** Use `display.width()` and
   `display.height()`. Several panels ship rotated, so their usable size is the
   transpose of the glass size, and it changes again with `setRotation()`.
7. **Custom wiring starts from the default**: `LB_Wiring pins = LB_WIRING;`,
   then change only what differs. Never fill an `LB_Wiring` from scratch — the
   `spiHost` field is a bus number whose meaning differs per chip.
8. **`setWiring()` and `setSpiHz()` must be called before `begin()`.**
   Everything else works at any time.
9. **ESP32 and ESP32-S3 only.** This library does not build for AVR (Uno, Nano),
   RP2040, STM32 or ESP8266. If the customer names one of those, stop and say
   so rather than producing code that cannot compile.
10. **Do not mix the two language APIs.** Arduino is C++ (`display.backlight(255)`,
    `display.fillScreen(LB_BLACK)`); MicroPython is Python (`d.backlight(255)`,
    `tft.fill(BLACK)`). Decide which one the customer is on before writing a
    line — ask if it is not obvious.

### Mapping what the customer says to a constant

Customers describe their screen by the size printed on it: "my 2.8 inch screen",
"the 1.9 inch one". Map that straight to the constant in the table below. There
is exactly one entry per size, so the mapping is never ambiguous, and the driver
IC never needs to be discussed. If a customer volunteers a controller name
("it's an ST7789"), that is not enough to pick a panel — four different products
use ST7789. Ask for the size.

---

## Hardware

- **MCU:** ESP32-S3 or classic ESP32. In the Arduino IDE the customer picks the
  board under **Tools ▸ Board**; the GPIOs follow that choice with no edits.
- **Default wiring** (the Lonely Binary breakout — every panel in the range
  shares one 15-pin FPC, so these depend on the MCU only, never on the panel):

  | | CS | RST | DC | MOSI | SCLK | Backlight | SPI bus |
  |---|---|---|---|---|---|---|---|
  | ESP32-S3 | 10 | 42 | 2 | 11 | 12 | 41 | HSPI / `SPI(1)` |
  | classic ESP32 | 15 | 4 | 2 | 23 | 18 | 32 | VSPI / `SPI(2)` |

  MISO is unused. A framebuffer (`begin(true)`) needs PSRAM, which the S3 board
  has and the classic ESP32 board does not.

---

## Panel constants

<!-- BEGIN GENERATED PANEL TABLE — panels.yaml via tools/gen_panels.py -->

### Panels Lonely Binary sells

Pick by the size printed on the display. That is the whole decision, and
there is never more than one entry for a size. Everything else in this
table follows from the constant — do not pass any of it to the library by
hand.

| Panel | Arduino constant | MicroPython name | Screen size | Driver IC | Bus | Touch | Backlight |
|---|---|---|---|---|---|---|---|
| 0.96 inch | `LB_TFT_096` | `TFT_096` | 80 x 160 | ST7735 | SPI 20 MHz | — | active low |
| 1.8 inch | `LB_TFT_18` | `TFT_18` | 128 x 160 | ST7735 | SPI 20 MHz | — | active high |
| 2.0 inch | `LB_TFT_20` | `TFT_20` | 240 x 320 | ST7789 | SPI 40 MHz | — | active high |
| 2.4 inch | `LB_TFT_24` | `TFT_24` | 240 x 320 | ST7789 | SPI 40 MHz | — | active high |
| 2.8 inch | `LB_TFT_28` | `TFT_28` | 240 x 320 | ST7789 | SPI 40 MHz | — | active high |
| 3.5 inch | `LB_TFT_35` | `TFT_35` | 320 x 480 | ST7796 | SPI 40 MHz | — | active high |
| 1.14 inch | `LB_NARROW_114` | `NARROW_114` | 240 x 135 | ST7789 | SPI 8 MHz | — | active low |
| 1.68 inch | `LB_NARROW_168` | `NARROW_168` | 428 x 142 | NV3007 | SPI 8 MHz | — | active low |
| 1.9 inch | `LB_NARROW_19` | `NARROW_19` | 320 x 170 | ST7789 | SPI 8 MHz | — | active low |
| 2.25 inch | `LB_NARROW_225` | `NARROW_225` | 284 x 76 | ST7789 | SPI 8 MHz | — | active low |
| 2.79 inch | `LB_NARROW_279` | `NARROW_279` | 428 x 142 | NV3007 | SPI 20 MHz | — | active low |
| 3.92 inch square, capacitive touch | `LB_SQUARE_392_CTP` | not supported | 320 x 320 | ILI9488 | 8-bit parallel | GT911 (capacitive) | active high |
| 1.28 inch round, capacitive touch | `LB_TAP_128_CTP` | not supported | 240 x 240 | GC9A01 | SPI 20 MHz | IT7259 (capacitive) | active high |
| 1.5 inch, capacitive touch | `LB_TAP_150_CTP` | not supported | 240 x 320 | GC9306 | SPI 10 MHz | CST816 (capacitive) | active high |
| 1.96 inch, capacitive touch | `LB_TAP_196_CTP` | not supported | 240 x 296 | GC9307 | 8-bit parallel | CST816 (capacitive) | active high |
| 2.13 inch reflective LCD | `LB_RLCD_213` | not supported | 250 x 122 | ST7305 | SPI 20 MHz | — | active high |
| 2.9 inch reflective LCD | `LB_RLCD_290` | not supported | 384 x 168 | ST7305 | SPI 20 MHz | — | active high |
| 4.2 inch reflective LCD | `LB_RLCD_420` | not supported | 400 x 300 | ST7305 | SPI 20 MHz | — | active high |
| 1.8 inch, resistive touch | `LB_RTP_180` | `RTP_180` | 160 x 128 | ST7735 | SPI 20 MHz | NS2009 (resistive) | active low |
| 2.4 inch, resistive touch | `LB_RTP_240` | `RTP_240` | 320 x 240 | ST7789 | SPI 40 MHz | NS2009 (resistive) | active low |
| 2.8 inch, resistive touch | `LB_RTP_280` | `RTP_280` | 320 x 240 | ST7789 | SPI 40 MHz | NS2009 (resistive) | active low |
| 4.3 inch, resistive touch | `LB_RTP_430` | not supported | 480 x 272 | NV3041A | SPI 20 MHz | NS2009 (resistive) | active low |
| 3.5 inch, resistive touch | `LB_RTP_350` | not supported | 480 x 320 | ST7796 | 8-bit parallel | NS2009 (resistive) | active high |
| 0.85 inch square | `LB_SQUARE_085` | `SQUARE_085` | 128 x 128 | ST7735 | SPI 20 MHz | — | active low |
| 1.33 inch square | `LB_SQUARE_133` | `SQUARE_133` | 240 x 240 | ST7789 | SPI 20 MHz | — | active low |
| 1.54 inch square | `LB_SQUARE_154` | `SQUARE_154` | 240 x 240 | ST7789 | SPI 20 MHz | — | active low |
| 2.0 inch square | `LB_SQUARE_200` | not supported | 320 x 240 | ILI9342 | SPI 40 MHz | — | active low |
| 2.31 inch square | `LB_SQUARE_231` | not supported | 320 x 240 | ILI9342 | SPI 40 MHz | — | active low |
| 3.92 inch square | `LB_SQUARE_392` | `SQUARE_392` | 320 x 320 | ST7796 | SPI 40 MHz | — | active high |
| 4.3 inch RGB | `LB_RGB_430_480X272` | not supported | 480 x 272 | RGB | 16-bit RGB (S3 only) | — | active high |
| 7 inch RGB | `LB_RGB_700_1024X600` | not supported | 1024 x 600 | RGB | 16-bit RGB (S3 only) | — | active high |
| 10.1 inch RGB | `LB_RGB_1010_1024X600` | not supported | 1024 x 600 | RGB | 16-bit RGB (S3 only) | — | active high |
| Lonely Binary ESP32-S3 VGA 16-bit | `LB_VGA_16BIT` | not supported | 800 x 600 | RGB | 16-bit RGB (S3 only) | — | active high |
| Lonely Binary ESP32-S3 RGB to HDMI | `LB_HDMI_RGB` | not supported | 800 x 600 | RGB | 16-bit RGB (S3 only) | — | active high |
| Lonely Binary ESP32-S3 VGA to HDMI | `LB_HDMI_VGA` | not supported | 720 x 576 | RGB | 16-bit RGB (S3 only) | — | active high |

**Screen size is the size after the panel's default rotation**, which is
what `width()` / `height()` return and what you should lay a UI out
against. These ship rotated (landscape), so their size here is the
transpose of the raw glass:

- `LB_NARROW_114` — glass is 135 x 240, ships at rotation 1, so you get 240 x 135
- `LB_NARROW_168` — glass is 142 x 428, ships at rotation 1, so you get 428 x 142
- `LB_NARROW_19` — glass is 170 x 320, ships at rotation 1, so you get 320 x 170
- `LB_NARROW_225` — glass is 76 x 284, ships at rotation 1, so you get 284 x 76
- `LB_NARROW_279` — glass is 142 x 428, ships at rotation 1, so you get 428 x 142
- `LB_RLCD_213` — glass is 122 x 250, ships at rotation 3, so you get 250 x 122
- `LB_RLCD_290` — glass is 168 x 384, ships at rotation 1, so you get 384 x 168
- `LB_RTP_180` — glass is 128 x 160, ships at rotation 1, so you get 160 x 128
- `LB_RTP_240` — glass is 240 x 320, ships at rotation 1, so you get 320 x 240
- `LB_RTP_280` — glass is 240 x 320, ships at rotation 1, so you get 320 x 240
- `LB_RTP_350` — glass is 320 x 480, ships at rotation 1, so you get 480 x 320
- `LB_ILI9327_400X240` — glass is 240 x 400, ships at rotation 1, so you get 400 x 240
- `LB_ILI9486_480X320` — glass is 320 x 480, ships at rotation 3, so you get 480 x 320
- `LB_ILI9341_320X240` — glass is 240 x 320, ships at rotation 1, so you get 320 x 240

### Panels the library can drive but Lonely Binary does not sell

**If the customer bought a display from Lonely Binary, it is in the table
above and this one is irrelevant.** These are named by controller and
resolution rather than by size, precisely so they cannot be mistaken for a
product. Values are measured on one sample; another sample of the same
controller may well need `setColorOrder()` / `setInverted()` /
`setBacklightActiveLow()`.

| Panel | Arduino constant | MicroPython name | Screen size | Driver IC | Bus | Touch | Backlight |
|---|---|---|---|---|---|---|---|
| RGB | `LB_SUNTON_4827S043R` | not supported | 480 x 272 | RGB | 16-bit RGB (S3 only) | XPT2046 (resistive) | active high |
| RGB | `LB_SUNTON_8048S070C` | not supported | 800 x 480 | RGB | 16-bit RGB (S3 only) | GT911 (capacitive) | active high |
| ILI9341 | `LB_ILI9341_240X320` | not supported | 240 x 320 | ILI9341 | SPI 40 MHz | — | active high |
| ILI9327 | `LB_ILI9327_400X240` | not supported | 400 x 240 | ILI9327 | 8-bit parallel | — | active high |
| ILI9486 | `LB_ILI9486_480X320` | not supported | 480 x 320 | ILI9486 | SPI 20 MHz | — | active high |
| ILI9341 | `LB_ILI9341_320X240` | not supported | 320 x 240 | ILI9341 | SPI 40 MHz | — | active high |
| ST7305 | `LB_ST7305_400X300` | not supported | 400 x 300 | ST7305 | SPI 10 MHz | — | active high |

<!-- END GENERATED PANEL TABLE -->

---

## Resolution: the VGA and HDMI boards

`LB_VGA_16BIT`, `LB_HDMI_RGB` and `LB_HDMI_VGA` are boards, not screens of one size. The constant gives a
default size; `display.setResolution(w, h)` BEFORE `begin()` picks another from the ones that board was
measured at, and returns false (listing the valid ones on Serial) for anything else. Never compute
timings or pass a pixel clock: there is no API for that. `width()`/`height()` follow the chosen size,
so lay out with them, never with literal 800/600.

```cpp
LB_Display display(LB_HDMI_RGB);
void setup() {
  display.setResolution(1280, 720);   // optional; must come before begin()
  display.begin();
}
```

`resolutionCount()` / `resolution(i, &w, &h, &hz)` list them (index 0 = default), `printResolutions()`
prints them, `refreshHz()` / `pixelClockHz()` give the timing in use. `setResolution(w, h, hz)` picks
between two timings of one size (`LB_HDMI_RGB` has 800x600 at 50 and 56 Hz).

| Board | `setResolution(...)` accepts (default first) |
|---|---|
| `LB_VGA_16BIT` | 800x600 @50, 640x480 @60 |
| `LB_HDMI_RGB` | 800x600 @50, 640x480 @64.7, 720x480 @60, 720x576 @50, 800x600 @56, 1024x768 @34.6, 1280x720 @30, 1366x768 @27, 1280x800 @27 |
| `LB_HDMI_VGA` | 720x576 @49.4, 720x480 @59, 640x480 @57 |

The sizes above 800x600 on `LB_HDMI_RGB` run at 27-35 Hz and some monitors refuse them ("no signal").
`LB_HDMI_RGB` starts at 800x600 @50. For something that must work on any monitor or TV use 640x480, 720x480 or 720x576.

## HDMI (`LB_HDMI_RGB`, `LB_HDMI_VGA`) - ESP32-S3 boards

`LB_HDMI_RGB`: RGB565 + pixel clock into a SiI9022A HDMI transmitter. `begin()` sets the chip up over
I2C (SDA 5, SCL 4 - a sketch that also uses Wire must use those pins and must not call Wire.begin()
with others) and a background task re-initialises it when the HDMI cable is plugged back in or a TV
powers up. `hdmiConnected()` = a monitor is plugged in and on. Sound: the chip is set up for I2S,
16-bit, 44.1 kHz, stereo; I2S pins from `display.wiringRgb().i2sBclk / i2sWs / i2sDout` (42/41/40),
standard Philips I2S with 32-bit slots, sample in the top 16 bits (see example `Hdmi07_Sound`; for
MP3 the ESP32-audioI2S library works on the same pins). Tools: ESP32S3 Dev Module, PSRAM "OPI PSRAM"
(required), USB CDC On Boot "Enabled" (serial is the native USB port), Huge APP for big sketches.
Examples `Hdmi01_Hello` ... `Hdmi10_LVGL`; the `Vga*` examples also run with the constant changed.

`LB_HDMI_VGA`: an R-2R ladder (VGA) into an MS9288C converter; picture only, no setup, no sound yet.
Not yet verified through this library.

Everything in the VGA section below about drawing speed, `flush()`, `scanStats()` and Wi-Fi applies
to both HDMI boards too.

## VGA 16-bit (`LB_VGA_16BIT`) - on sale

Renamed from `LB_VGA_800X600` in 0.2.0 (the old name still compiles, deprecated, same 800x600 default).
A Lonely Binary VGA board with an ESP32-S3 (N16R8). 800 x 600 @ 50 Hz by default (32 MHz pixel clock), RGB565,
scanned out of a PSRAM framebuffer; no controller, no touch, S3 only, rotation 0
only, MicroPython not supported. Same `LB_Display` API as every other panel.

- Tools > Board "ESP32S3 Dev Module", PSRAM "OPI PSRAM", USB CDC On Boot
  "Disabled", Partition Scheme "Huge APP" for large sketches.
- `begin()` always gives a framebuffer (`begin(true)` is not needed). Drawing
  reaches the monitor only after `flush()`.
- Drawing into the screen is slow because the scan-out uses most of the PSRAM
  bandwidth: 2-5 million pixels a second, a full-screen fill ~0.23 s, a
  full-screen `drawJpg` ~1.3 s. Animate by erasing and redrawing small areas;
  never repaint the screen per frame.
- Keep Wi-Fi/Bluetooth off while the picture matters; the radio makes it shake.
- Wait for a frame with `scanStats(&frames, &late, &maxUs)`: `frames` increases
  once per frame (50 per second), `late` must stay 0.
- Never name a constant `PS`, `OK`, `WARN` or `CRIT` in a sketch: ESP32 headers
  define them as macros.
- Examples `Vga01_Hello` ... `Vga12_LVGL` (File > Examples > Lonely Binary
  Display) go from hello-world to a game and an LVGL dashboard; copy the one
  closest to the task.

## Arduino / C++

### Install

Library Manager ▸ **Lonely Binary Display**. It depends on **GFX Library for
Arduino** (moononournation), which Library Manager pulls in automatically.
Examples land under **File ▸ Examples ▸ Lonely Binary Display**.

### Minimal program

```cpp
#include <LonelyBinaryDisplay.h>

LB_Display display(LB_TFT_24);      // the only line that changes per panel

void setup() {
  Serial.begin(115200);

  if (!display.begin()) {
    Serial.println("Display did not start — check the ribbon cable and DC.");
    return;
  }
  // begin() has already turned the backlight on at full.

  display.fillScreen(LB_BLACK);
  display.setTextColor(LB_WHITE);
  display.setTextSize(2);
  display.drawString("Hello", 10, 10);
  display.flush();                  // no-op without a framebuffer; always call it
}

void loop() {}
```

### One API for every Lonely Binary display

`LB_Display` is an `LB_Canvas` — the drawing API from the *Lonely Binary GFX*
library, which Lonely Binary VGA and e-paper use too. A function written
against `LB_Canvas &` runs unchanged on all of them:

```cpp
void drawGauge(LB_Canvas &c, float value);   // TFT, VGA and e-paper
drawGauge(display, 42.0f);
```

### API

| Call | Notes |
|---|---|
| `LB_Display display(LB_TFT_24)` | The panel constant is the entire configuration. |
| `begin(bool useCanvas = false)` | Brings up SPI, the panel and the backlight PWM, and turns the backlight on. Returns `false` only if the panel driver refuses to start — almost always a wiring fault, DC first. |
| `pushImage(x, y, w, h, px)` | A block of raw RGB565 pixels (`uint16_t`). Into the framebuffer if there is one, else straight to the panel. |
| `begun()` | Whether `begin()` has brought the panel up. |
| `backlight(0..255)` | PWM on every panel. Active-high vs active-low comes from the panel table; the sketch never needs to know which. |
| `flush()` | Pushes the framebuffer. No-op without a canvas, so always safe to call. |
| `hasCanvas()` | Whether `begin(true)` actually got a framebuffer. |
| `framebuffer()` | The raw RGB565 buffer, or `nullptr`. LVGL renders straight into this. |
| `setRotation(0..3)` | Applies the correct offset pair for portrait vs landscape. With a framebuffer only 0 ↔ 2 and 1 ↔ 3 are allowed. |
| `rotation()`, `width()`, `height()` | Current state, rotation included. |
| `setWiring(pins)` | Your own GPIOs. **Before `begin()`.** Takes `LB_Wiring` (SPI panels) or `LB_WiringPar8` (the square series, 8-bit parallel); start from `LB_WIRING` / `LB_WIRING_PAR8` and change only what differs. |
| `touch()` | The touch controller of a `_CTP` / `_RTP` panel, or `nullptr`. Needs `#include <LB_TouchGT911.h>` in the sketch (`<LB_TouchCST816.h>` for `LB_TAP_150_CTP`, `<LB_TouchIT7259.h>` for `LB_TAP_128_CTP`, which also has `takeGesture()` for tap / double tap / flick) (the LVGL library includes it itself). `touch()->getTouch(&x, &y)` returns `true` while a finger is down, in screen coordinates that follow `setRotation()`. `touch()->read(points, max)` gives every finger. |
| `setSpiHz(hz)` | Override the panel's SPI clock. **Before `begin()`**; `0` restores the default. |
| `setColorOrder(order)` | `LB_Display::COLOR_AUTO` / `COLOR_RGB` / `COLOR_BGR`. Any time, before or after `begin()`. |
| `setInverted(bool)` | Set the panel's inversion. Any time, before or after `begin()`. |
| `setBacklightActiveLow(bool)` | Flip the backlight polarity. Any time. |
| `setPowerPin(pin, activeLow, settleMs)` | A GPIO that switches the panel's supply on (a load switch on your board). **Before `begin()`**, which drives it on and waits `settleMs` (default 120). |
| `setVcom(uint8_t)` | The LCD's VCOM, 0x00-0x3F, for a still picture whose mid tones shimmer as if the backlight were unsteady. ST7796 (0.5 V + 0.025 V x value) and ST7789 (0.1 V + 0.025 V x value) only; returns `false` on any other controller. Any time, before or after `begin()`. The panel table already sets it where it has been measured (`LB_TFT_35`: 0x05); the `VcomTune` example finds the value for a panel by eye. |
| `vcom()`, `vcomVolts()` | The VCOM value in use (`-1` where not adjustable) and its voltage. |
| `panelDef()` | The panel table entry — `->name`, `->id`, `->width`, `->height`. |
| `selfTest()` | Colour bars, backlight sweep, panel identity. The "does my wiring work" check. |
| `printInfo()` | The same details to Serial. Ask for this output when diagnosing. |

Drawing, all on `display`: `fillScreen`, `drawPixel`, `drawFastHLine`,
`drawFastVLine`, `drawLine`, `drawRect`, `fillRect`, `drawRoundRect`,
`fillRoundRect`, `drawCircle`, `fillCircle`, `drawTriangle`, `fillTriangle`,
`setFont`, `setTextColor(fg)` / `setTextColor(fg, bg)`, `setTextSize`,
`drawString(str, x, y)`, `textWidth`, `fontHeight`, `drawRGB565`, `drawJpg`.
There is no `setCursor`/`print`/`printf`: format with `snprintf` into a buffer
and pass it to `drawString`. Text is UTF-8.

### Colours

Drawing calls take `LB_BLACK`, `LB_WHITE`, `LB_RED`, `LB_GREEN`, `LB_BLUE`,
`LB_YELLOW`, `LB_MAGENTA`, `LB_CYAN`, … (type `lb_color_t`, from Lonely Binary
GFX).

The bare names `BLACK WHITE RED GREEN BLUE YELLOW MAGENTA CYAN ORANGE LIME`
are raw RGB565 `uint16_t` values, only for `pushImage()` and `framebuffer()`.
**Never pass a bare name to a drawing call, or an `LB_` name into a pixel
buffer** — both compile and both give wrong colours. Define
`LB_NO_LEGACY_COLORS` before the include to suppress the bare names.

### Recipe: a value that updates without flicker

The naive loop — clear the screen, redraw the number — flickers badly. Two ways
to avoid it, in order of preference:

```cpp
// 1. Opaque text: a two-argument setTextColor() paints the background with
//    each glyph, so the old digits are overwritten rather than cleared.
//    Pad to a fixed width or "9" will leave the "0" of a previous "10" behind.
void loop() {
  char buf[16];
  snprintf(buf, sizeof(buf), "%5.1f C", readTemperature());
  display.setTextColor(LB_WHITE, LB_BLACK);
  display.setTextSize(3);
  display.drawString(buf, 10, 40);
  display.flush();
  delay(500);
}
```

```cpp
// 2. A framebuffer: draw the whole screen freely, then push it in one go.
//    Needs width x height x 2 bytes: PSRAM on the larger panels, internal RAM
//    is enough up to about 128 x 160. If the allocation fails the library says so on
//    Serial and draws direct instead, so begin(true) is always safe to ask for
//    — check hasCanvas() if the difference matters to you.
void setup() {
  display.begin(true);
  display.backlight(255);
}

void loop() {
  char buf[16];
  snprintf(buf, sizeof(buf), "%.1f", readTemperature());
  display.fillScreen(LB_BLACK);
  display.drawString(buf, 10, 40);
  display.flush();          // nothing appears until this call
  delay(500);
}
```

With a canvas, **`setRotation()` cannot switch between portrait and landscape** —
the framebuffer was allocated the other way round, so the library refuses and
logs to Serial. 0 ↔ 2 and 1 ↔ 3 work. For a quarter turn, use `begin()`
without a canvas.

### Recipe: the customer's own wiring

```cpp
LB_Display display(LB_TFT_24);      // the panel constant does NOT change

void setup() {
  LB_Wiring pins = LB_WIRING;       // start from the kit wiring for this board
  pins.cs        = 5;               // ...change only what differs
  pins.dc        = 16;
  pins.rst       = 17;
  pins.backlight = 4;               // or -1 for no backlight pin

  display.setWiring(pins);          // must precede begin()
  display.begin();
  display.printInfo();              // marks the pins as custom — catches typos
}
```

Driver IC, resolution, offsets, colour order and backlight polarity belong to
the **screen**; only the GPIOs belong to the board. So the panel constant stays.

Long ribbon extensions or jumper wires sometimes will not take the panel's rated
clock — a scrambled or half-drawn image is the symptom:

```cpp
display.setSpiHz(20000000);         // before begin(); 0 restores the default
```

---

## MicroPython

Runs on **stock MicroPython** from micropython.org — no custom firmware build.

```bash
mpremote mip install github:Lonely-Binary/LonelyBinaryDisplay/micropython
```

```python
from lb_display import LBDisplay, BLACK, WHITE
from lb_panels import TFT_24          # the only line that changes per panel

d = LBDisplay(TFT_24)
d.begin()                             # also enables the backlight at full
d.backlight(255)

tft = d.gfx()
tft.fill(BLACK)
tft.text("Hello", 10, 10, WHITE, scale=2)
```

### API

`LBDisplay(panel, wiring=None, board=None)`, then `begin()`, `gfx()`,
`backlight(0..255)`, `set_vcom(0x00..0x3F)` / `vcom()` / `vcom_volts()` (ST7796
and ST7789 only, after `begin()`), `set_rotation(0..3)`, `width()`, `height()`,
`self_test()`, `print_info()`. `detect_board()` returns `"esp32s3"` or `"esp32"`.

Drawing, on the object from `gfx()`:
`fill(color)`, `fill_rect(x, y, w, h, color)`, `rect(...)`, `pixel(x, y, color)`,
`hline(x, y, w, color)`, `vline(x, y, h, color)`, `line(x0, y0, x1, y1, color)`,
`circle(cx, cy, r, color)`, `fill_circle(...)`, `fill_triangle(...)`,
`blit(buf, x, y, w, h)`, and
`text(s, x, y, color, bg=BLACK, scale=1)`.

Note that `text()` **always paints a background** — it defaults to `BLACK`, so
drawing a label on a coloured fill needs the `bg=` argument to match, or it
appears in a black box. There is no cursor and no `print()`; every call takes
explicit coordinates.

Colours are `BLACK WHITE RED GREEN BLUE YELLOW MAGENTA CYAN` — importable from
`lb_display`. There is no `ORANGE`, `LIME` or `GREY` here; use
`st77xx.color565(r, g, b)`.

### Differences from the Arduino side

- **No framebuffer.** There is no `begin(True)`, no `flush()`, no canvas. Avoid
  flicker by redrawing only the region that changed (`fill_rect` over the old
  value, then `text`), not by clearing the screen each frame.
- **No colour-order or inversion setters.** Those live on the panel dict, so
  override them by copying it:

  ```python
  panel = dict(TFT_24)
  panel["bgr"] = True            # red and blue swapped
  panel["invert"] = False        # black coming out white
  d = LBDisplay(panel)
  ```

- **Custom wiring is a dict**, copied from the default for the same reason the
  C++ side copies `LB_WIRING` — the SPI bus id differs between the chips:

  ```python
  import lb_panels
  from lb_display import LBDisplay, detect_board
  from lb_panels import TFT_24

  pins = dict(lb_panels.WIRING[detect_board()])
  pins["cs"] = 5
  pins["backlight"] = -1         # no backlight pin on this board
  d = LBDisplay(TFT_24, wiring=pins)
  d.begin()
  ```

- **`nv3007.py`** is imported only when a NV3007 panel is selected, so the other
  panels do not need that file on the board.
- Panel constants drop the `LB_` prefix: `TFT_24`, `NARROW_19`, …

---

## Troubleshooting

Ask the customer for the output of `printInfo()` / `print_info()` first — it
reports the panel, the pins actually in use, the colour order, the backlight
polarity and whether a framebuffer was allocated, which settles most of these
without guessing.

| Symptom | Cause | Fix |
|---|---|---|
| `begin()` returned false | Wiring — **DC first**, then RST and CS | Check the ribbon seating; `printInfo()` lists the pins in use |
| Nothing on screen, backlight is on | Drawing to a canvas and never pushing it | Call `display.flush()` |
| Screen dark at `backlight(255)`, bright at `backlight(0)` | Backlight polarity is backwards | `setBacklightActiveLow()` — and report it, it belongs in the panel table |
| Red shows blue, blue shows red, black is still black | Colour order | `setColorOrder(LB_Display::COLOR_BGR)` (or `COLOR_RGB`) |
| Photo-negative — black comes out white | Inversion | `setInverted(true)` (or `false`) |
| Red → yellow, green → magenta, blue → cyan | **Both** of the above stacked | Both calls |
| Still picture shimmers on greys / mid tones, like an unsteady backlight; black, white and saturated colours are steady | VCOM does not suit the glass | Run the `VcomTune` example, then `setVcom(value)` after `begin()` (ST7796 / ST7789) |
| Scrambled, torn or half-drawn image | SPI clock too high for the wiring | `setSpiHz(20000000)` before `begin()` |
| Image drawn into a corner or shifted by a few pixels | Offsets — usually a hand-written driver setup, or a non-Lonely-Binary panel | Use the panel constant; for a foreign panel the offsets cannot be guessed |
| UI laid out off the right edge | Hard-coded resolution on a panel that ships rotated | Use `width()` / `height()` |
| `setRotation()` did nothing, message on Serial | Portrait↔landscape with a canvas allocated | Set rotation before `begin(true)` |

For a panel Lonely Binary **sells**, a wrong colour order, inversion or
backlight polarity is a bug in the library's panel table, not something to work
around in the sketch. Please report it — the fix belongs in `panels.yaml`, where
both language bindings pick it up.

---

## What this library does not do

Say so plainly rather than improvising — each of these is a real boundary, not a
gap to paper over:

- **Touch.** No touch controller support of any kind.
- **Images and fonts from an SD card / SPIFFS.** Not provided; JPEG or PNG
  decoding is the sketch's own business.
- **LVGL setup.** The library gets on well with LVGL — `begin(true)` gives you
  the framebuffer LVGL wants, and `framebuffer()` lets its flush callback be a
  no-op — but the LVGL glue itself lives in a separate Lonely Binary library.
- **Non-ESP32 targets.** See rule 9.
- **Panels bought elsewhere.** Only `LB_ILI9341_240X320` is characterised at
  all, on a single sample. Expect to reach for the colour-order, inversion,
  backlight-polarity and SPI-clock overrides.
