361 lines
8.6 KiB
Go
361 lines
8.6 KiB
Go
// Package ssd1306 implements SSD1306 (monochrome) OLED displays.
|
|
package ssd1306
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
|
|
"periph.io/x/conn/v3/gpio"
|
|
|
|
"go.maze.io/harmon/config"
|
|
"go.maze.io/harmon/hardware"
|
|
"go.maze.io/harmon/hardware/display"
|
|
"go.maze.io/harmon/internal/pixel"
|
|
)
|
|
|
|
const (
|
|
defaultWidth = 128
|
|
defaultHeight = 64
|
|
)
|
|
|
|
func init() {
|
|
display.Register("ssd1306", newDriver)
|
|
}
|
|
|
|
type Display struct {
|
|
conn display.Conn
|
|
rect image.Rectangle
|
|
rotate display.Rotation
|
|
bl gpio.PinOut
|
|
buffer []byte
|
|
next *pixel.VerticalLSB
|
|
startPage, endPage int
|
|
startCol, endCol int
|
|
scrolled bool
|
|
halted bool
|
|
}
|
|
|
|
func (Display) Type() string { return "ssd1306" }
|
|
|
|
func newDriver(config config.DisplayConfig) (hardware.Display, error) {
|
|
var (
|
|
opts *display.Options
|
|
conn display.Conn
|
|
err error
|
|
)
|
|
|
|
if opts, err = display.ParseOptions(config, defaultWidth, defaultHeight); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if opts.Rotate == display.Rotate180 {
|
|
// Let the hardware handle this rotation.
|
|
opts.Flip = true
|
|
opts.Mirror = true
|
|
}
|
|
|
|
switch config.Mode {
|
|
case "", "i2c":
|
|
conn, err = display.OpenI2C(config.Bus, config.Address)
|
|
case "spi":
|
|
conn, err = display.OpenSPI(config.Port, config.Reset, config.DC)
|
|
default:
|
|
return nil, display.InvalidMode(config.Mode)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return newDisplay(conn, opts)
|
|
}
|
|
|
|
func newDisplay(conn display.Conn, opts *display.Options) (*Display, error) {
|
|
if opts.W < 8 || opts.W > 128 || opts.W&7 != 0 {
|
|
return nil, fmt.Errorf("invalid width %d", opts.W)
|
|
}
|
|
if opts.H < 8 || opts.H > 64 || opts.H&7 != 0 {
|
|
return nil, fmt.Errorf("invalid height %d", opts.H)
|
|
}
|
|
|
|
nbPages := opts.H / 8
|
|
pageSize := opts.W
|
|
d := &Display{
|
|
conn: conn,
|
|
bl: opts.Backlight,
|
|
rect: image.Rect(0, 0, opts.W, opts.H),
|
|
rotate: opts.Rotate,
|
|
buffer: make([]byte, nbPages*pageSize),
|
|
startPage: 0,
|
|
endPage: nbPages,
|
|
startCol: 0,
|
|
endCol: opts.W,
|
|
scrolled: true, // Signal that the screen must be redrawn on first draw().
|
|
|
|
}
|
|
|
|
if err := d.sendCommand(getInitCmd(opts)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
func (d *Display) sendData(data []byte) error {
|
|
if d.halted {
|
|
// Transparently enable the display.
|
|
if err := d.sendCommand(nil); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return d.conn.Tx(data, false)
|
|
}
|
|
|
|
func (d *Display) sendCommand(data []byte) error {
|
|
if d.halted {
|
|
// Transparently enable the display.
|
|
data = append([]byte{0xAF}, data...)
|
|
d.halted = false
|
|
}
|
|
return d.conn.Tx(data, true)
|
|
}
|
|
|
|
func (d *Display) calculateSubset(next []byte) (int, int, int, int, bool) {
|
|
w := d.rect.Dx()
|
|
h := d.rect.Dy()
|
|
startPage := 0
|
|
endPage := h / 8
|
|
startCol := 0
|
|
endCol := w
|
|
if d.scrolled {
|
|
// Painting disable scrolling but if scrolling was enabled, this requires a
|
|
// full screen redraw.
|
|
d.scrolled = false
|
|
} else {
|
|
// Calculate the smallest square that need to be sent.
|
|
pageSize := w
|
|
|
|
// Top.
|
|
for ; startPage < endPage; startPage++ {
|
|
x := pageSize * startPage
|
|
y := pageSize * (startPage + 1)
|
|
if !bytes.Equal(d.buffer[x:y], next[x:y]) {
|
|
break
|
|
}
|
|
}
|
|
// Bottom.
|
|
for ; endPage > startPage; endPage-- {
|
|
x := pageSize * (endPage - 1)
|
|
y := pageSize * endPage
|
|
if !bytes.Equal(d.buffer[x:y], next[x:y]) {
|
|
break
|
|
}
|
|
}
|
|
if startPage == endPage {
|
|
// Early exit, the image is exactly the same.
|
|
return 0, 0, 0, 0, true
|
|
}
|
|
|
|
// Left.
|
|
for ; startCol < endCol; startCol++ {
|
|
for i := startPage; i < endPage; i++ {
|
|
x := i*pageSize + startCol
|
|
if d.buffer[x] != next[x] {
|
|
goto breakLeft
|
|
}
|
|
}
|
|
}
|
|
breakLeft:
|
|
|
|
// Right.
|
|
for ; endCol > startCol; endCol-- {
|
|
for i := startPage; i < endPage; i++ {
|
|
x := i*pageSize + endCol - 1
|
|
if d.buffer[x] != next[x] {
|
|
goto breakRight
|
|
}
|
|
}
|
|
}
|
|
breakRight:
|
|
}
|
|
return startPage, endPage, startCol, endCol, false
|
|
}
|
|
|
|
// drawInternal sends image data to the controller.
|
|
func (d *Display) drawInternal(next []byte) error {
|
|
startPage, endPage, startCol, endCol, skip := d.calculateSubset(next)
|
|
if skip {
|
|
return nil
|
|
}
|
|
copy(d.buffer, next)
|
|
|
|
if d.startPage != startPage || d.endPage != endPage || d.startCol != startCol || d.endCol != endCol {
|
|
d.startPage = startPage
|
|
d.endPage = endPage
|
|
d.startCol = startCol
|
|
d.endCol = endCol
|
|
}
|
|
|
|
pageSize := d.rect.Dx()
|
|
for page := d.startPage; page < d.endPage; page++ {
|
|
err := d.sendCommand([]byte{
|
|
pageAddr | byte(page),
|
|
setLowColumn | (byte(d.startCol) & 0x0F),
|
|
setHighColumn | (byte(d.startCol) >> 4),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pageStart := page * pageSize
|
|
err = d.sendData(d.buffer[pageStart+d.startCol : pageStart+d.endCol])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Halt turns off the display.
|
|
//
|
|
// Sending any other command afterward reenables the display.
|
|
func (d *Display) Halt() error {
|
|
d.halted = false
|
|
err := d.sendCommand([]byte{0xAE})
|
|
if err == nil {
|
|
d.halted = true
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Size of the display.
|
|
func (d *Display) Size() image.Point {
|
|
if d.rotate == display.Rotate90 || d.rotate == display.Rotate270 {
|
|
return image.Pt(d.rect.Max.Y, d.rect.Max.X)
|
|
}
|
|
return d.rect.Max
|
|
}
|
|
|
|
// Draw implements display.Drawer.
|
|
//
|
|
// It draws synchronously, once this function returns, the display is updated.
|
|
// It means that on slow bus (I²C), it may be preferable to defer Draw() calls
|
|
// to a background goroutine.
|
|
func (d *Display) Draw(r image.Rectangle, src image.Image, sp image.Point) error {
|
|
var next []byte
|
|
if img, ok := src.(*pixel.VerticalLSB); ok && r.Eq(d.rect) && img.Rect.Eq(d.rect) && sp.X == 0 && sp.Y == 0 {
|
|
// Exact size, full frame, image1bit encoding: fast path!
|
|
next = img.Pix
|
|
} else {
|
|
// Double buffering.
|
|
if d.next == nil {
|
|
d.next = pixel.NewVerticalLSB(d.rect)
|
|
}
|
|
next = d.next.Pix
|
|
draw.Src.Draw(d.next, r, src, sp)
|
|
}
|
|
return d.drawInternal(next)
|
|
}
|
|
|
|
func getInitCmd(opts *display.Options) []byte {
|
|
// Set COM output scan direction; C0 means normal; C8 means reversed
|
|
comScan := byte(0xC8)
|
|
// See page 40.
|
|
columnAddr := byte(0xA1)
|
|
if opts.Rotate == display.Rotate180 {
|
|
// Change order both horizontally and vertically.
|
|
comScan = 0xC0
|
|
columnAddr = byte(0xA0)
|
|
}
|
|
if opts.Flip {
|
|
comScan = byte(0xC0)
|
|
}
|
|
if opts.Mirror {
|
|
columnAddr = byte(0xA0)
|
|
}
|
|
// See page 40.
|
|
hwLayout := byte(0x02)
|
|
if !opts.Sequential {
|
|
hwLayout |= 0x10
|
|
}
|
|
if opts.SwapTopBottom {
|
|
hwLayout |= 0x20
|
|
}
|
|
// Set the max frequency. The problem with I²C is that it creates visible
|
|
// tear down. On SPI at high speed this is not visible. Page 23 pictures how
|
|
// to avoid tear down. For now default to max frequency.
|
|
freq := byte(0xF0)
|
|
|
|
// Initialize the device by fully resetting all values.
|
|
// Page 64 has the full recommended flow.
|
|
// Page 28 lists all the commands.
|
|
return []byte{
|
|
0xAE, // Display off
|
|
0xD3, 0x00, // Set display offset; 0
|
|
0x40, // Start display start line; 0
|
|
columnAddr, // Set segment remap; RESET is column 127.
|
|
comScan, //
|
|
0xDA, hwLayout, // Set COM pins hardware configuration; see page 40
|
|
0x81, 0xFF, // Set max contrast
|
|
0xA4, // Set display to use GDDRAM content
|
|
0xA6, // Set normal display (0xA7 for inverted 0=lit, 1=dark)
|
|
0xD5, freq, // Set osc frequency and divide ratio; power on reset value is 0x80.
|
|
0x8D, 0x14, // Enable charge pump regulator; page 62
|
|
0xD9, 0xF1, // Set pre-charge period; from adafruit driver
|
|
0xDB, 0x40, // Set Vcomh deselect level; page 32
|
|
0x2E, // Deactivate scroll
|
|
0xA8, byte(opts.H - 1), // Set multiplex ratio (number of lines to display)
|
|
0x20, 0x00, // Set memory addressing mode to horizontal
|
|
0x21, 0, uint8(opts.W - 1), // Set column address (Width)
|
|
0x22, 0, uint8(opts.H/8 - 1), // Set page address (Pages)
|
|
0xAF, // Display on
|
|
}
|
|
}
|
|
|
|
func (d *Display) Invert(on bool) error {
|
|
if on {
|
|
return d.sendCommand([]byte{0xA7})
|
|
}
|
|
return d.sendCommand([]byte{0xA6})
|
|
}
|
|
|
|
/* hardware.Display interface methods */
|
|
|
|
func (d *Display) Fill(c color.Color) error {
|
|
//return d.Draw(d.rect, &image.Uniform{c}, image.Point{})
|
|
return d.sendCommand([]byte{0xa7}) // invert :D
|
|
}
|
|
|
|
func (d *Display) Refresh() error {
|
|
if d.next == nil {
|
|
return nil
|
|
}
|
|
return d.drawInternal(d.next.Pix)
|
|
}
|
|
|
|
/* draw.Image interface methods */
|
|
|
|
func (d *Display) At(x, y int) color.Color {
|
|
if d.next == nil {
|
|
return pixel.Off
|
|
}
|
|
return d.next.At(x, y)
|
|
}
|
|
|
|
func (d *Display) Bounds() image.Rectangle {
|
|
return d.rect
|
|
}
|
|
|
|
func (d *Display) ColorModel() color.Model {
|
|
return pixel.MonochromeModel
|
|
}
|
|
|
|
func (d *Display) Set(x, y int, c color.Color) {
|
|
if d.next == nil {
|
|
d.next = pixel.NewVerticalLSB(d.rect)
|
|
}
|
|
d.next.Set(x, y, c)
|
|
}
|