# ssd1306.lua

Reusable pure-Lua SSD1306 OLED driver for I2C-connected SSD1306 panels.

## Require

```lua
local ssd1306 = require("ssd1306")
```

## Dependencies

- `i2c` module
- An opened I2C device handle for the OLED address, usually `0x3C`

## Constructor

```lua
local oled = ssd1306.new(dev, opts)
```

`dev` must be an I2C device handle with a `write` method.

`opts`:
- `width`: `128` by default
- `height`: `64` by default
- `addr`: metadata only, `0x3C` by default
- `external_vcc`: `false` by default
- `segment_remap`: `true` by default
- `com_scan_dec`: `true` by default

Supported sizes are `128x64` and `128x32`.

## Methods

- `oled:init()`: initialize the panel.
- `oled:clear(color)`: clear the framebuffer to lit or unlit pixels.
- `oled:pixel(x, y, color)`: set one pixel.
- `oled:fill_rect(x, y, w, h, color)`: fill a rectangle.
- `oled:draw_char(x, y, ch, color)`: draw one 5x7 ASCII character.
- `oled:draw_text(x, y, text, color)`: draw ASCII text.
- `oled:invert(enable)`: enable or disable display inversion.
- `oled:contrast(value)`: set contrast from `0` to `255`.
- `oled:show()`: flush the framebuffer to the panel.
- `oled:close()`: mark the display closed.

`color` is truthy for lit pixels and `false` or `nil` for cleared pixels.

## Example

```lua
local i2c = require("i2c")
local ssd1306 = require("ssd1306")

local bus = i2c.new(0, 14, 13, 400000)
local dev = bus:device(0x3C)
local oled = ssd1306.new(dev, { width = 128, height = 64 })

oled:init()
oled:clear(false)
oled:draw_text(10, 10, "SSD1306 OK", true)
oled:show()

oled:close()
dev:close()
bus:close()
```
