Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,227 @@
|
||||
// Package ssd1326 provides a driver for the SSD1326 OLED controller.
|
||||
package ssd1326
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"periph.io/x/conn/v3"
|
||||
"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/config"
|
||||
"git.maze.io/go/harmon/display"
|
||||
"git.maze.io/go/harmon/display/ssd1xxx"
|
||||
"git.maze.io/go/harmon/internal/pixel"
|
||||
)
|
||||
|
||||
func init() {
|
||||
display.Register("ssd1326", New)
|
||||
}
|
||||
|
||||
var defaultInfo = display.MonochromeDisplayInfo{
|
||||
ResetPulseWidth: 100 * time.Millisecond,
|
||||
ResetWait: 100 * time.Millisecond,
|
||||
PreChipEnableWait: 15 * time.Nanosecond,
|
||||
PostChipEnableWait: 20 * time.Nanosecond,
|
||||
DataSetupTime: 40 * time.Nanosecond,
|
||||
WritePulseWidth: 60 * time.Nanosecond,
|
||||
Width: 256,
|
||||
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 {
|
||||
name string
|
||||
hal display.MonochromeDisplay
|
||||
buf pixel.Image[pixel.Monochrome]
|
||||
info display.MonochromeDisplayInfo
|
||||
config *config.Display
|
||||
}
|
||||
|
||||
func New(config *config.Display) (display.Display, error) {
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"type": "ssd1306",
|
||||
"name": config.Name,
|
||||
})
|
||||
|
||||
bus, err := ssd1xxx.NewBus(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.WithField("connection", conn.String()).Debug("opening display connection")
|
||||
return newDevice(config, conn, log)
|
||||
}
|
||||
|
||||
func newDevice(config *config.Display, conn conn.Conn, log *logrus.Entry) (d *Device, err error) {
|
||||
var info = new(display.MonochromeDisplayInfo)
|
||||
*info = defaultInfo
|
||||
|
||||
d = &Device{
|
||||
hal: display.NewSSD13xxDisplay(conn, *info),
|
||||
info: *info,
|
||||
config: config,
|
||||
}
|
||||
|
||||
switch conn := conn.(type) {
|
||||
case i2c.Bus:
|
||||
d.name = "SSD1306 I²C@" + conn.String()
|
||||
case i2c.BusCloser:
|
||||
d.name = "SSD1306 I²C@" + conn.String()
|
||||
case spi.Conn:
|
||||
d.name = "SSD1306 SPI@" + conn.String()
|
||||
default:
|
||||
d.name = "SSD1306"
|
||||
}
|
||||
|
||||
log.Trace("configuring display")
|
||||
if err := d.configure(); err != nil {
|
||||
return nil, fmt.Errorf("ssd1306: initial configuration failed: %w", err)
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func NewI2C(bus i2c.Bus, addr uint16) (*Device, error) {
|
||||
if addr == 0 {
|
||||
addr = 0x3c
|
||||
}
|
||||
|
||||
c := &i2c.Dev{Bus: bus, Addr: addr}
|
||||
d := &Device{
|
||||
hal: display.NewSSD13xxDisplay(c, defaultInfo),
|
||||
buf: pixel.NewImage[pixel.Monochrome](int(defaultInfo.Width), int(defaultInfo.Height)),
|
||||
}
|
||||
if err := d.configure(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func NewSPI(port spi.Port, reset gpio.PinOut) (*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
|
||||
}
|
||||
|
||||
/*
|
||||
0xa0 command: 0x0a0 ***abcde
|
||||
a: 1: mono mode
|
||||
b: 0: horizontal (1: vertical) address increment
|
||||
c: 1: enable bit remap
|
||||
d: 1: COM remap
|
||||
e: 1: Column remap
|
||||
*/
|
||||
|
||||
if err = d.hal.SendCommand([]byte{
|
||||
0xfd, 0x12, // Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands
|
||||
0xae, // Set display off
|
||||
0xa8, 0x1f, // Set multiplex ratio(1 to 64)
|
||||
0xa1, 0x00, // Set display start line
|
||||
0xa2, 0x00, // Set display offset, shift mapping RAM counter
|
||||
0xad, 0x02, // Set master configuration: disable embedded DC-DC, enable internal VCOMH
|
||||
0xa0, 0x06, // Set remap and data format
|
||||
0x86, 0x00, // Set current range
|
||||
0xb7, // Set default gray scale table
|
||||
0x81, 0x27, // Contrast, brightness 0..128
|
||||
0xb1, 0x71, // Phase length
|
||||
0xb3, 0xf0, // Set display clock divide ratio/oscillator frequency (set clock as 135 frames/sec)
|
||||
0xbb, 0x35, 0xff, // Set pre-charge
|
||||
0xbc, 0x1f, // Pre-charge voltage level
|
||||
0xbe, 0x0f, // VCOMH voltage
|
||||
0xbf, 0x02 | 0x0d, // VSL voltage level
|
||||
0xa4, // Normal display mode
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return d.hal.EndTransfer()
|
||||
}
|
||||
|
||||
func (d *Device) String() string {
|
||||
return d.name
|
||||
}
|
||||
|
||||
func (d *Device) Halt() error {
|
||||
return d.hal.Halt()
|
||||
}
|
||||
|
||||
func (d *Device) Clear() error {
|
||||
d.buf.FillSolidColor(pixel.Off)
|
||||
//copy(d.buf.Pix[:], emptyBuf[:])
|
||||
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() image.Point {
|
||||
return d.buf.Bounds().Size()
|
||||
}
|
||||
|
||||
func (d *Device) Bounds() image.Rectangle {
|
||||
return d.buf.Bounds()
|
||||
}
|
||||
|
||||
func (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{}
|
||||
)
|
||||
Reference in new issue
Block a user