Checkpoint
This commit is contained in:
54 files changed
+6855
-526
No files matched your search
@@ -4,7 +4,7 @@ type VCCMode uint8
|
||||
|
||||
// Registers
|
||||
const (
|
||||
setcontrast = 0x81
|
||||
setContrast = 0x81
|
||||
displayAllOnResume = 0xA4
|
||||
displayAllOn = 0xA5
|
||||
normalDisplay = 0xA6
|
||||
|
||||
+163
-207
@@ -2,14 +2,13 @@
|
||||
package ssd1306
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"time"
|
||||
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/config"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"go.maze.io/harmon/hardware/display"
|
||||
@@ -25,17 +24,21 @@ func init() {
|
||||
display.Register("ssd1306", newDriver)
|
||||
}
|
||||
|
||||
type ResetValue [2]byte
|
||||
|
||||
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
|
||||
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 "ssd1306" }
|
||||
@@ -57,11 +60,21 @@ func newDriver(config config.DisplayConfig) (hardware.Display, error) {
|
||||
opts.Mirror = true
|
||||
}
|
||||
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"type": "ssd1306",
|
||||
"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)
|
||||
}
|
||||
@@ -70,10 +83,10 @@ func newDriver(config config.DisplayConfig) (hardware.Display, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newDisplay(conn, opts)
|
||||
return newDisplay(conn, opts, isSPI)
|
||||
}
|
||||
|
||||
func newDisplay(conn display.Conn, opts *display.Options) (*Display, error) {
|
||||
func newDisplay(conn display.Conn, opts *display.Options, isSPI bool) (d *Display, err error) {
|
||||
if opts.W < 8 || opts.W > 128 || opts.W&7 != 0 {
|
||||
return nil, fmt.Errorf("invalid width %d", opts.W)
|
||||
}
|
||||
@@ -81,27 +94,48 @@ func newDisplay(conn display.Conn, opts *display.Options) (*Display, error) {
|
||||
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().
|
||||
|
||||
d = &Display{
|
||||
conn: conn,
|
||||
width: int16(opts.W),
|
||||
height: int16(opts.H),
|
||||
}
|
||||
|
||||
if err := d.sendCommand(getInitCmd(opts)); err != nil {
|
||||
return nil, err
|
||||
//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{
|
||||
0xAE, // display off
|
||||
0xA8, muxr, // multiplex ratio (height - 1)
|
||||
0xD3, 0x00, // display offset 0
|
||||
0x40, // display start line 0
|
||||
0xA1, // segment remap a0/a1
|
||||
0xC8, // scan dir reverse (C0 normal)
|
||||
0xD5, 0x80, // clock divide ration (0x00=1) and osc. frequency (0x8)
|
||||
0xDA, 0x12, // com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5)
|
||||
0x81, 0xFF, // contrast
|
||||
0xA4, // output RAM to display
|
||||
0xDB, 0x40, // vcom deselect level
|
||||
0x20, 0x02, // set memory addressing mode
|
||||
0x00, 0x10, // set lower column start address for page addressing mode: no offset
|
||||
0x8D, 0x14, // [2] charge pump setting (p62): 0x014 enable, 0x010 disable
|
||||
0xD9, 0x22, // set pre-charge
|
||||
0x2E, // scroll off
|
||||
0xA6, // none inverted normal display mode
|
||||
0xAF, // display on
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return d, nil
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Display) sendData(data []byte) error {
|
||||
@@ -123,99 +157,14 @@ func (d *Display) sendCommand(data []byte) error {
|
||||
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
|
||||
func (d *Display) PowerSave(on bool) error {
|
||||
var cmd byte
|
||||
if on {
|
||||
cmd = 0xAF
|
||||
} 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:
|
||||
cmd = 0xAE
|
||||
}
|
||||
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
|
||||
return d.sendCommand([]byte{cmd})
|
||||
}
|
||||
|
||||
// Halt turns off the display.
|
||||
@@ -223,7 +172,7 @@ func (d *Display) drawInternal(next []byte) error {
|
||||
// Sending any other command afterward reenables the display.
|
||||
func (d *Display) Halt() error {
|
||||
d.halted = false
|
||||
err := d.sendCommand([]byte{0xAE})
|
||||
err := d.PowerSave(true)
|
||||
if err == nil {
|
||||
d.halted = true
|
||||
}
|
||||
@@ -232,10 +181,7 @@ func (d *Display) Halt() error {
|
||||
|
||||
// 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
|
||||
return image.Pt(int(d.width), int(d.height))
|
||||
}
|
||||
|
||||
// Draw implements display.Drawer.
|
||||
@@ -244,74 +190,9 @@ func (d *Display) Size() image.Point {
|
||||
// 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
|
||||
}
|
||||
logrus.Debug("draw image")
|
||||
draw.Draw(d, r, src, sp, draw.Src)
|
||||
return d.Refresh()
|
||||
}
|
||||
|
||||
func (d *Display) Invert(on bool) error {
|
||||
@@ -323,29 +204,98 @@ func (d *Display) Invert(on bool) error {
|
||||
|
||||
/* 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.rect, &image.Uniform{c}, image.Point{})
|
||||
return d.sendCommand([]byte{0xa7}) // invert :D
|
||||
return d.Draw(d.Bounds(), &image.Uniform{c}, image.Point{})
|
||||
}
|
||||
|
||||
func (d *Display) Refresh() error {
|
||||
if d.next == nil {
|
||||
return nil
|
||||
// Displaying pixels on SSD1306 is peculiar, because the pixels are stored in columns
|
||||
// of 8-bits (1 byte). Each column is sent out on a page.
|
||||
for page := 0; page < int(d.height)>>3; page++ {
|
||||
if err := d.sendCommand([]byte{
|
||||
0x40 | 0, // Set display start line
|
||||
0x10 | 0, // Set Higher Column Start Address for Page Addressing Mode
|
||||
0xb0 | byte(page&0x0f), // Define start page address
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
off = page * int(d.width)
|
||||
end = off + int(d.width)
|
||||
)
|
||||
if err := d.sendData(d.buffer[off:end]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return d.drawInternal(d.next.Pix)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if d.next == nil {
|
||||
x, y = d.rotate(x, y)
|
||||
if x < 0 || int16(x) >= d.width || y < 0 || int16(y) >= d.height {
|
||||
return pixel.Off
|
||||
}
|
||||
return d.next.At(x, y)
|
||||
|
||||
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 d.rect
|
||||
return image.Rect(0, 0, int(d.width), int(d.height))
|
||||
}
|
||||
|
||||
func (d *Display) ColorModel() color.Model {
|
||||
@@ -353,8 +303,14 @@ func (d *Display) ColorModel() color.Model {
|
||||
}
|
||||
|
||||
func (d *Display) Set(x, y int, c color.Color) {
|
||||
if d.next == nil {
|
||||
d.next = pixel.NewVerticalLSB(d.rect)
|
||||
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)
|
||||
}
|
||||
d.next.Set(x, y, c)
|
||||
}
|
||||
Reference in new issue
Block a user