ssd1306: improvements needed for Thumby SPI display

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2024-02-21 01:02:23 +01:00
committed by BCG
parent 308763f500
commit c344e5d879
2 changed files with 76 additions and 4 deletions
+26 -4
View File
@@ -13,6 +13,8 @@ import (
"tinygo.org/x/drivers/internal/legacy"
)
type ResetValue [2]byte
// Device wraps I2C or SPI connection.
type Device struct {
bus Buser
@@ -22,6 +24,8 @@ type Device struct {
bufferSize int16
vccState VccMode
canReset bool
resetCol ResetValue
resetPage ResetValue
}
// Config is the configuration for the display
@@ -30,6 +34,13 @@ type Config struct {
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
}
type I2CBus struct {
@@ -79,6 +90,7 @@ func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) Device {
// Configure initializes the display with default configuration
func (d *Device) Configure(cfg Config) {
var zeroReset ResetValue
if cfg.Width != 0 {
d.width = cfg.Width
} else {
@@ -97,6 +109,16 @@ func (d *Device) Configure(cfg Config) {
} 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 = make([]byte, d.bufferSize)
d.canReset = cfg.Address != 0 || d.width != 128 || d.height != 64 // I2C or not 128x64
@@ -186,11 +208,11 @@ func (d *Device) Display() error {
// Since we're printing the whole buffer, avoid resetting it in this case
if d.canReset {
d.Command(COLUMNADDR)
d.Command(0)
d.Command(uint8(d.width - 1))
d.Command(d.resetCol[0])
d.Command(d.resetCol[1])
d.Command(PAGEADDR)
d.Command(0)
d.Command(uint8(d.height/8) - 1)
d.Command(d.resetPage[0])
d.Command(d.resetPage[1])
}
return d.Tx(d.buffer, false)