Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:28:51 +01:00
1 parent 7b6db17870
commit 0295eb1ad2
125 files changed
+59376

No files matched your search

+411
View File
@@ -0,0 +1,411 @@
package ssd1306
import (
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"time"
"git.maze.io/go/harmon/config"
"git.maze.io/go/harmon/display"
"git.maze.io/go/harmon/display/oled"
"git.maze.io/go/harmon/internal/pixel"
)
func init() {
display.Register("ssd1306", newDevice)
}
var (
errBufferSize = errors.New("invalid size buffer")
errOutOfRange = errors.New("out of screen range")
)
type ResetValue [2]byte
// Device wraps I2C or SPI connection.
type Device struct {
bus oled.Bus
buffer pixel.Image[pixel.Monochrome]
width int16
height int16
bufferSize int16
vccState VccMode
canReset bool
resetCol ResetValue
resetPage ResetValue
rotation display.Rotation
}
// Config is the configuration for the display
type Config struct {
Width int16
Height int16
VccState VccMode
//Address uint16
// ResetCol and ResetPage are used to reset the screen to 0x0
// This is useful for some screens that have a different size than 128x64
// For example, the Thumby's screen is 72x40
// The default values are normally set automatically based on the size.
// If you're using a different size, you might need to set these values manually.
ResetCol ResetValue
ResetPage ResetValue
Rotation display.Rotation
}
type VccMode uint8
func newDevice(config *config.Display) (display.Display, error) {
width, height, err := display.ParseSize(config.Size, 128, 0)
if err != nil || height == 0 {
return nil, fmt.Errorf("invalid size %q: %w", config.Size, err)
}
rotation, err := display.ParseRotation(config.Rotate)
if err != nil {
return nil, err
}
bus, err := oled.NewBus(config)
if err != nil {
return nil, err
}
d := &Device{
bus: bus,
}
if err = d.Configure(Config{
Width: int16(width),
Height: int16(height),
Rotation: rotation,
}); err != nil {
return nil, err
}
return d, nil
}
// Configure initializes the display with default configuration
func (d *Device) Configure(cfg Config) error {
var zeroReset ResetValue
if cfg.Width != 0 {
d.width = cfg.Width
} else {
d.width = 128
}
if cfg.Height != 0 {
d.height = cfg.Height
} else {
d.height = 64
}
if cfg.VccState != 0 {
d.vccState = cfg.VccState
} else {
d.vccState = SWITCHCAPVCC
}
if cfg.ResetCol != zeroReset {
d.resetCol = cfg.ResetCol
} else {
d.resetCol = ResetValue{0, uint8(d.width - 1)}
}
if cfg.ResetPage != zeroReset {
d.resetPage = cfg.ResetPage
} else {
d.resetPage = ResetValue{0, uint8(d.height/8) - 1}
}
d.bufferSize = d.width * d.height / 8
d.buffer = pixel.NewImage[pixel.Monochrome](int(d.width), int(d.height))
//d.buffer = make([]byte, d.bufferSize)
//d.canReset = cfg.Address != 0 || d.width != 128 || d.height != 64 // I2C or not 128x64
time.Sleep(100 * time.Nanosecond)
d.Command(DISPLAYOFF)
d.Command(SETDISPLAYCLOCKDIV)
d.Command(0x80)
d.Command(SETMULTIPLEX)
d.Command(uint8(d.height - 1))
d.Command(SETDISPLAYOFFSET)
d.Command(0x0)
d.Command(SETSTARTLINE | 0x0)
d.Command(CHARGEPUMP)
if d.vccState == EXTERNALVCC {
d.Command(0x10)
} else {
d.Command(0x14)
}
d.Command(MEMORYMODE)
d.Command(0x00)
d.SetRotation(cfg.Rotation)
if (d.width == 128 && d.height == 64) || (d.width == 64 && d.height == 48) { // 128x64 or 64x48
d.Command(SETCOMPINS)
d.Command(0x12)
d.Command(SETCONTRAST)
if d.vccState == EXTERNALVCC {
d.Command(0x9F)
} else {
d.Command(0xCF)
}
} else if d.width == 128 && d.height == 32 { // 128x32
d.Command(SETCOMPINS)
d.Command(0x02)
d.Command(SETCONTRAST)
d.Command(0x8F)
} else if d.width == 96 && d.height == 16 { // 96x16
d.Command(SETCOMPINS)
d.Command(0x2)
d.Command(SETCONTRAST)
if d.vccState == EXTERNALVCC {
d.Command(0x10)
} else {
d.Command(0xAF)
}
} else {
// fail silently, it might work
println("there's no configuration for this display's size")
}
d.Command(SETPRECHARGE)
if d.vccState == EXTERNALVCC {
d.Command(0x22)
} else {
d.Command(0xF1)
}
d.Command(SETVCOMDETECT)
d.Command(0x40)
d.Command(DISPLAYALLON_RESUME)
d.Command(NORMALDISPLAY)
d.Command(DEACTIVATE_SCROLL)
return d.Command(DISPLAYON)
}
// ClearBuffer clears the image buffer
func (d *Device) ClearBuffer() {
/*
for i := int16(0); i < d.bufferSize; i++ {
d.buffer[i] = 0
}
*/
d.buffer.Clear()
}
// ClearDisplay clears the image buffer and clear the display
func (d *Device) ClearDisplay() error {
d.ClearBuffer()
return d.Refresh()
}
// Refresh sends the whole buffer to the screen
func (d *Device) Refresh() error {
// Reset the screen to 0x0
// This works fine with I2C
// In the 128x64 (SPI) screen resetting to 0x0 after 128 times corrupt the buffer
// Since we're printing the whole buffer, avoid resetting it in this case
if d.canReset {
d.Command(COLUMNADDR)
d.Command(d.resetCol[0])
d.Command(d.resetCol[1])
d.Command(PAGEADDR)
d.Command(d.resetPage[0])
d.Command(d.resetPage[1])
}
return d.Tx(d.buffer.RawBuffer(), false)
}
// SetPixel enables or disables a pixel in the buffer
// color.RGBA{0, 0, 0, 255} is consider transparent, anything else
// with enable a pixel on the screen
func (d *Device) SetPixel(x int16, y int16, c pixel.Monochrome) {
if d.rotation == display.Rotate90 || d.rotation == display.Rotate270 {
x, y = y, x
}
/*
if x < 0 || x >= d.width || y < 0 || y >= d.height {
return
}
byteIndex := x + (y/8)*d.width
if c == pixel.Off {
d.buffer[byteIndex] &^= 1 << uint8(y%8)
} else {
d.buffer[byteIndex] |= 1 << uint8(y%8)
}
*/
d.buffer.SetPixel(int(x), int(y), c)
}
// GetPixel returns if the specified pixel is on (true) or off (false)
func (d *Device) GetPixel(x int16, y int16) pixel.Monochrome {
if d.rotation == display.Rotate90 || d.rotation == display.Rotate270 {
x, y = y, x
}
/*
if x < 0 || x >= d.width || y < 0 || y >= d.height {
return false
}
byteIndex := x + (y/8)*d.width
return (d.buffer[byteIndex] >> uint8(y%8) & 0x1) == 1
*/
return d.buffer.GetPixel(int(x), int(y))
}
/*
// SetBuffer changes the whole buffer at once
func (d *Device) SetBuffer(buffer []byte) error {
if int16(len(buffer)) != d.bufferSize {
return errBufferSize
}
for i := int16(0); i < d.bufferSize; i++ {
d.buffer[i] = buffer[i]
}
return nil
}
*/
// GetBuffer returns the whole buffer
func (d *Device) GetBuffer() []byte {
return d.buffer.RawBuffer()
}
// Command sends a command to the display
func (d *Device) Command(command uint8) error {
return d.bus.Tx([]byte{command}, true)
}
// Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) error {
return d.bus.Tx(data, isCommand)
}
// Size returns the current size of the display.
func (d *Device) Size16() (w, h int16) {
return d.width, d.height
}
// DrawBitmap copies the bitmap to the screen at the given coordinates.
func (d *Device) DrawBitmap(x, y int16, bitmap pixel.Image[pixel.Monochrome]) error {
width, height := bitmap.Size()
if x < 0 || x+int16(width) > d.width || y < 0 || y+int16(height) > d.height {
return errOutOfRange
}
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
d.SetPixel(x+int16(i), y+int16(j), bitmap.GetPixel(i, j))
}
}
return nil
}
// Rotation returns the currently configured rotation.
func (d *Device) Rotation() display.Rotation {
return d.rotation
}
// SetRotation changes the rotation of the device (clock-wise).
func (d *Device) SetRotation(rotation display.Rotation) error {
d.rotation = rotation
switch d.rotation {
case display.NoRotation:
d.Command(SEGREMAP | 0x1) // Reverse horizontal mapping
d.Command(COMSCANDEC) // Reverse vertical mapping
case display.Rotate180:
d.Command(SEGREMAP) // Normal horizontal mapping
d.Command(COMSCANINC) // Normal vertical mapping
// nothing to do
default:
d.Command(SEGREMAP | 0x1) // Reverse horizontal mapping
d.Command(COMSCANDEC) // Reverse vertical mapping
}
return nil
}
// Set the sleep mode for this display. When sleeping, the panel uses a lot
// less power. The display won't show an image anymore, but the memory contents
// should be kept.
func (d *Device) Sleep(sleepEnabled bool) error {
if sleepEnabled {
d.Command(DISPLAYOFF)
} else {
d.Command(DISPLAYON)
}
return nil
}
// FillRectangle fills a rectangle at a given coordinates with a color
func (d *Device) FillRectangle(x, y, width, height int16, c pixel.Monochrome) error {
dw, dh := d.Size16()
if x < 0 || y < 0 || width <= 0 || height <= 0 ||
x >= d.width || (x+width) > dw || y >= dh || (y+height) > dh {
return errOutOfRange
}
if x+width == dw && y+height == dh && c == pixel.Off {
d.ClearDisplay()
return nil
}
for i := x; i < x+width; i++ {
for j := y; j < y+height; j++ {
d.SetPixel(i, j, c)
}
}
return nil
}
// SetScroll sets the vertical scrolling for the display, which is a NOP for this display.
func (d *Device) SetScroll(line int16) {
return
}
/* draw.Image interface */
func (d *Device) At(x, y int) color.Color {
return d.GetPixel(int16(x), int16(y))
}
func (d *Device) Bounds() image.Rectangle {
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
return image.Rect(0, 0, int(d.width), int(d.height))
}
return image.Rect(0, 0, int(d.height), int(d.width))
}
func (d *Device) ColorModel() color.Model {
return pixel.MonochromeModel
}
func (d *Device) Set(x, y int, c color.Color) {
c = pixel.MonochromeModel.Convert(c)
d.SetPixel(int16(x), int16(y), c.(pixel.Monochrome))
}
/* display.Image interface */
func (d *Device) Halt() error {
return d.bus.Halt()
}
func (d *Device) Size() image.Point {
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
return image.Pt(int(d.width), int(d.height))
}
return image.Pt(int(d.height), int(d.width))
}
func (d *Device) String() string {
return fmt.Sprintf("ssd1306:%dx%d", d.width, d.height)
}
/* display.imageDrawer interface */
func (d *Device) DrawImage(img image.Image) error {
draw.Draw(d.buffer, d.buffer.Bounds(), img, image.Point{}, draw.Src)
return d.Refresh()
}