Checkpoint
This commit is contained in:
54 files changed
+6855
-526
No files matched your search
@@ -0,0 +1,317 @@
|
||||
// Package ssd1322 implements SSD1322 (monochrome) OLED displays.
|
||||
package ssd1322
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/config"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"go.maze.io/harmon/hardware/display"
|
||||
"go.maze.io/harmon/internal/pixel"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultWidth = 256
|
||||
defaultHeight = 64
|
||||
)
|
||||
|
||||
func init() {
|
||||
display.Register("ssd1322", newDriver)
|
||||
}
|
||||
|
||||
type ResetValue [2]byte
|
||||
|
||||
type Display struct {
|
||||
conn display.Conn
|
||||
buffer []byte
|
||||
width int16
|
||||
height int16
|
||||
bufferSize int16
|
||||
vccState VCCMode
|
||||
canReset bool
|
||||
resetCol ResetValue
|
||||
resetPage ResetValue
|
||||
rotation display.Rotation
|
||||
scrolled bool
|
||||
halted bool
|
||||
}
|
||||
|
||||
func (Display) Type() string { return "ssd1322" }
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"type": "ssd1322",
|
||||
"size": fmt.Sprintf("%dx%d", opts.W, opts.H),
|
||||
"mode": config.Mode,
|
||||
"rotate": opts.Rotate,
|
||||
})
|
||||
log.Debug("new driver")
|
||||
|
||||
var isSPI bool
|
||||
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)
|
||||
isSPI = true
|
||||
default:
|
||||
return nil, display.InvalidMode(config.Mode)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newDisplay(conn, opts, isSPI)
|
||||
}
|
||||
|
||||
func newDisplay(conn display.Conn, opts *display.Options, isSPI bool) (d *Display, err error) {
|
||||
if opts.W < 8 || opts.W > 256 || 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)
|
||||
}
|
||||
|
||||
d = &Display{
|
||||
conn: conn,
|
||||
width: int16(opts.W),
|
||||
height: int16(opts.H),
|
||||
}
|
||||
|
||||
//var zeroReset ResetValue
|
||||
d.vccState = SwitchCapVCC
|
||||
d.resetCol = ResetValue{0, uint8(d.width - 1)}
|
||||
d.resetPage = ResetValue{0, uint8(d.height/8) - 1}
|
||||
d.bufferSize = d.width * d.height / 8
|
||||
d.buffer = make([]byte, d.bufferSize)
|
||||
d.canReset = !isSPI || d.width != 128 || d.height != 64 // I2C or not 128x64
|
||||
logrus.Infof("%d bytes buffer for %d pixels", d.bufferSize, d.width*d.height)
|
||||
|
||||
time.Sleep(100 * time.Nanosecond)
|
||||
|
||||
muxr := byte(d.height - 1)
|
||||
if err = d.sendCommand([]byte{
|
||||
0xFD, 0x12, // set command lock: unlock
|
||||
0xA4, // display off
|
||||
0xB3, 0x91, // set front clock divider to 80fps (default is 0xD0)
|
||||
0xCA, muxr, // multiplex ratio (height - 1)
|
||||
0xA2, 0x00, // display offset 0
|
||||
0xA1, 0x00, // display start line 0
|
||||
0xA0, 0x14, 0x11, // set remap and dual COM line mode
|
||||
0xB5, 0x00, // set GPIO input disabled
|
||||
0xAB, 0x01, // enable internal VDD regulator
|
||||
0xB4, 0xA0, 0xB5, // display enhancement A: enable external VSL, normal GS display quality
|
||||
0xC1, 0x7F, // set contrast current
|
||||
0xC7, 0x0f, // set master contrast current control
|
||||
0xB8, 0x00, 0x02, 0x08, 0x0d, 0x14, 0x1a, 0x20, 0x28, 0x30, 0x38, 0x40, 0x48, 0x50, 0x60, 0x80, 0x80, // custom gray scale table
|
||||
0xB1, 0xE2, // set phase length (period 1: 5 DCLKs, period 2: 14 DCLKs)
|
||||
0xD1, 0xA2, 0x20, // display enhancement B: normal, as-is
|
||||
0xBB, 0x1F, // set pre-charge voltage 0.60*VCC
|
||||
0xB6, 0x08, // set second pre-charge period (8 DCLKs)
|
||||
0xBE, 0x07, // set VCOMH: 0x07 = 0.86*VCC
|
||||
0xA6, // set display mode normal
|
||||
0xA9, // exit partial display
|
||||
0xAF, // display on
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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) PowerSave(on bool) error {
|
||||
var cmd []byte
|
||||
if on {
|
||||
cmd = []byte{
|
||||
0xAB, 0x01, // enable internal VDD regulator
|
||||
0xAF, // display on
|
||||
}
|
||||
} else {
|
||||
cmd = []byte{
|
||||
0xAB, 0x00, // disable internal VDD regulator
|
||||
0xAE, // display off
|
||||
}
|
||||
}
|
||||
return d.sendCommand(cmd)
|
||||
}
|
||||
|
||||
// Halt turns off the display.
|
||||
//
|
||||
// Sending any other command afterward reenables the display.
|
||||
func (d *Display) Halt() error {
|
||||
d.halted = false
|
||||
err := d.PowerSave(true)
|
||||
if err == nil {
|
||||
d.halted = true
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Size of the display.
|
||||
func (d *Display) Size() image.Point {
|
||||
return image.Pt(int(d.width), int(d.height))
|
||||
}
|
||||
|
||||
// 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 {
|
||||
logrus.Debug("draw image")
|
||||
draw.Draw(d, r, src, sp, draw.Src)
|
||||
return d.Refresh()
|
||||
}
|
||||
|
||||
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) Clear() {
|
||||
for i := range d.buffer {
|
||||
d.buffer[i] = 0x00
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Display) Fill(c color.Color) error {
|
||||
return d.Draw(d.Bounds(), &image.Uniform{c}, image.Point{})
|
||||
}
|
||||
|
||||
func (d *Display) Refresh() error {
|
||||
var (
|
||||
offset = (480 - d.width) / 2
|
||||
colStart = offset / 4
|
||||
colEnd = colStart + d.width/4 - 1
|
||||
)
|
||||
if err := d.sendCommand([]byte{
|
||||
0x15, byte(colStart), byte(colEnd),
|
||||
0x75, 0, byte(d.height - 1),
|
||||
0x5c,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return d.sendData(d.buffer)
|
||||
}
|
||||
|
||||
// SetRotation changes the rotation of the device (clock-wise).
|
||||
func (d *Display) SetRotation(rotation display.Rotation) error {
|
||||
d.rotation = rotation
|
||||
|
||||
var command []byte
|
||||
switch d.rotation {
|
||||
case display.NoRotation:
|
||||
command = []byte{
|
||||
segRemap | 0x1, // Reverse horizontal mapping
|
||||
comScanDec, // Reverse vertical mapping
|
||||
}
|
||||
case display.Rotate180:
|
||||
command = []byte{
|
||||
segRemap, // Normal horizontal mapping
|
||||
comScanInc, // Normal vertical mapping
|
||||
}
|
||||
// nothing to do
|
||||
default:
|
||||
command = []byte{
|
||||
segRemap | 0x1, // Reverse horizontal mapping
|
||||
comScanDec, // Reverse vertical mapping
|
||||
}
|
||||
}
|
||||
return d.sendCommand(command)
|
||||
}
|
||||
|
||||
/* draw.Image interface methods */
|
||||
|
||||
func (d *Display) rotate(x, y int) (int, int) {
|
||||
switch d.rotation {
|
||||
case display.Rotate90:
|
||||
x, y = y, x
|
||||
x = int(d.width) - x - 1
|
||||
case display.Rotate180:
|
||||
x = int(d.width) - x - 1
|
||||
y = int(d.height) - y - 1
|
||||
case display.Rotate270:
|
||||
x, y = y, x
|
||||
y = int(d.height) - y - 1
|
||||
}
|
||||
return x, y
|
||||
}
|
||||
|
||||
func (d *Display) At(x, y int) color.Color {
|
||||
x, y = d.rotate(x, y)
|
||||
if x < 0 || int16(x) >= d.width || y < 0 || int16(y) >= d.height {
|
||||
return pixel.Off
|
||||
}
|
||||
|
||||
byteIndex := int16(x) + int16(y/8)*d.width
|
||||
if d.buffer[byteIndex]>>uint8(y%8)&0x1 == 1 {
|
||||
return pixel.On
|
||||
}
|
||||
return pixel.Off
|
||||
}
|
||||
|
||||
func (d *Display) Bounds() image.Rectangle {
|
||||
return image.Rect(0, 0, int(d.width), int(d.height))
|
||||
}
|
||||
|
||||
func (d *Display) ColorModel() color.Model {
|
||||
return pixel.MonochromeModel
|
||||
}
|
||||
|
||||
func (d *Display) Set(x, y int, c color.Color) {
|
||||
x, y = d.rotate(x, y)
|
||||
if x < 0 || int16(x) >= d.width || y < 0 || int16(y) >= d.height {
|
||||
return
|
||||
}
|
||||
byteIndex := int16(x) + int16(y>>3)*d.width
|
||||
if pixel.IsOn(c) {
|
||||
d.buffer[byteIndex] |= 1 << uint8(y%8)
|
||||
} else {
|
||||
d.buffer[byteIndex] &^= 1 << uint8(y%8)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user