166 lines
3.7 KiB
Go
166 lines
3.7 KiB
Go
// Package ssd1305 provides a driver for the SSD1305 OLED controller.
|
|
package ssd1305
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"time"
|
|
|
|
"periph.io/x/conn/v3/gpio"
|
|
"periph.io/x/conn/v3/i2c"
|
|
"periph.io/x/conn/v3/physic"
|
|
"periph.io/x/conn/v3/spi"
|
|
|
|
"git.maze.io/go/harmon/display"
|
|
"git.maze.io/go/harmon/internal/pixel"
|
|
)
|
|
|
|
var defaultInfo = display.MonochromeDisplayInfo{
|
|
ResetPulseWidth: 100 * time.Millisecond,
|
|
ResetWait: 100 * time.Millisecond,
|
|
PreChipEnableWait: 10 * time.Nanosecond,
|
|
PostChipEnableWait: 20 * time.Nanosecond,
|
|
DataSetupTime: 40 * time.Nanosecond,
|
|
WritePulseWidth: 150 * time.Nanosecond,
|
|
Width: 128,
|
|
Height: 32,
|
|
}
|
|
|
|
const (
|
|
spiMode spi.Mode = spi.Mode0
|
|
spiSpeed physic.Frequency = 4 * 1000 * 1000
|
|
maxPixels = 256 * 32
|
|
)
|
|
|
|
var emptyBuf [maxPixels >> 3]byte
|
|
|
|
type Device struct {
|
|
hal display.MonochromeDisplay
|
|
info display.MonochromeDisplayInfo
|
|
buf pixel.Image[pixel.Monochrome]
|
|
}
|
|
|
|
func NewI2C(bus i2c.Bus, addr uint16, width, height int) (*Device, error) {
|
|
if addr == 0 {
|
|
addr = 0x3c
|
|
}
|
|
|
|
if width <= 0 {
|
|
width = int(defaultInfo.Width)
|
|
}
|
|
if height <= 0 {
|
|
height = int(defaultInfo.Height)
|
|
}
|
|
|
|
var info = new(display.MonochromeDisplayInfo)
|
|
*info = defaultInfo
|
|
info.Width = uint16(width)
|
|
info.Height = uint16(height)
|
|
|
|
c := &i2c.Dev{Bus: bus, Addr: addr}
|
|
d := &Device{
|
|
hal: display.NewSSD13xxDisplay(c, *info),
|
|
info: *info,
|
|
buf: pixel.NewImage[pixel.Monochrome](width, height),
|
|
}
|
|
if err := d.configure(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
func NewSPI(port spi.Port, reset gpio.PinOut, width, height uint8) (*Device, error) {
|
|
var info = new(display.MonochromeDisplayInfo)
|
|
*info = defaultInfo
|
|
info.Reset = reset
|
|
|
|
c, err := port.Connect(spiSpeed, spiMode, 8)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
d := &Device{
|
|
hal: display.NewSSD13xxDisplay(c, *info),
|
|
}
|
|
if err := d.configure(); err != nil {
|
|
return nil, err
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (d *Device) configure() (err error) {
|
|
if err = d.hal.Init(); err != nil {
|
|
return
|
|
}
|
|
|
|
if err = d.hal.StartTransfer(); err != nil {
|
|
return
|
|
}
|
|
|
|
if err = d.hal.SendCommand([]byte{
|
|
0xae, // Set display off
|
|
0xd5, 0x80, // Set display clock divide ratio/oscillator frequency
|
|
0xa8, 0x3f, // Set multiplex ratio
|
|
0xd3, uint8(d.info.Width), // Set display offset
|
|
0x40, // Set display start line to 0
|
|
0x20, 0x00, // Set horizontal addressing mode
|
|
0xa1, // Set segment remap a0/a1
|
|
0xc8, // Set scan direction (c0: normal, c8: reverse,
|
|
0xda, 0x12, // Set com pins hardware configuration
|
|
0x81, 0xcf, // Set contrast control
|
|
0xd9, 0xf1, // Set pre-charge period
|
|
0xdb, 0x40, // Set VCOMH deselect level
|
|
0x2e, // Deactivate scroll
|
|
0xa4, // Set display output RAM to display
|
|
0xa6, // Set normal display mode
|
|
}); err != nil {
|
|
return
|
|
}
|
|
|
|
return d.hal.EndTransfer()
|
|
}
|
|
|
|
func (d *Device) Clear() error {
|
|
d.buf.FillSolidColor(pixel.Off)
|
|
return d.Refresh()
|
|
}
|
|
|
|
func (d *Device) Refresh() (err error) {
|
|
_ = d.hal.SendCommand([]byte{0x15, 0, byte(defaultInfo.Width) - 1}) // Set column address
|
|
_ = d.hal.SendCommand([]byte{0x75, 0, byte(defaultInfo.Height) - 1}) // Set row address
|
|
buf := d.buf.RawBuffer()
|
|
for i := 0; i < len(buf); i += 16 {
|
|
if err = d.hal.SendData(buf[i : i+16]); err != nil {
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (d *Device) Size() (width, height int) {
|
|
return d.buf.Bounds().Dx(), d.buf.Bounds().Dy()
|
|
}
|
|
|
|
func (d *Device) Bounds() image.Rectangle {
|
|
return d.buf.Bounds()
|
|
}
|
|
|
|
func (d *Device) ColorModel() color.Model {
|
|
return pixel.MonochromeModel
|
|
}
|
|
|
|
func (d *Device) At(x, y int) color.Color {
|
|
return d.buf.At(x, y)
|
|
}
|
|
|
|
func (d *Device) Set(x, y int, c color.Color) {
|
|
d.buf.Set(x, y, c)
|
|
}
|
|
|
|
// Interface check
|
|
var (
|
|
// _ display.Display = &Device{}
|
|
// _ display.Image = &Device{}
|
|
)
|