display-test/display/ssd1306.go

80 lines
1.4 KiB
Go
Raw Normal View History

2024-08-20 12:14:10 +00:00
package display
import (
2024-09-11 15:55:26 +00:00
"context"
2024-08-20 12:14:10 +00:00
"fmt"
"image"
"log"
"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/devices/v3/ssd1306"
"periph.io/x/host/v3"
)
type displaySsd1306 struct {
logger *log.Logger
bus i2c.BusCloser
dev *ssd1306.Dev
img *image.Gray
2024-08-20 12:14:10 +00:00
}
func newSsd1306(logger *log.Logger) (Display, error) {
if _, err := host.Init(); err != nil {
return nil, fmt.Errorf("drivers init: %w(may be i2c is disabled)", err)
}
// Setup device
bus, err := i2creg.Open("")
if err != nil {
return nil, fmt.Errorf("open i2c bus: %w", err)
}
opts := ssd1306.Opts{
W: 128,
H: 32,
Rotated: false,
Sequential: true,
SwapTopBottom: false,
}
dev, err := ssd1306.NewI2C(bus, &opts)
if err != nil {
return nil, fmt.Errorf("create i2c: %w", err)
}
return &displaySsd1306{
logger: logger,
bus: bus,
dev: dev,
}, nil
}
func (d *displaySsd1306) GetBounds() image.Rectangle {
return d.dev.Bounds()
2024-08-20 12:14:10 +00:00
}
2024-09-11 15:55:26 +00:00
func (d *displaySsd1306) Test(ctx context.Context) error {
return nil
}
2024-09-18 19:32:47 +00:00
func (d *displaySsd1306) Flush(crystal, page byte) {
//return d.dev.Draw(img.Bounds(), d.img, image.Point{})
2024-08-20 12:14:10 +00:00
}
func (d *displaySsd1306) Close() error {
if err := d.bus.Close(); err != nil {
d.logger.Println("ERROR: close i2c bus:", err.Error())
2024-08-20 12:14:10 +00:00
}
return nil
}
2024-09-18 19:32:47 +00:00
func (d *displaySsd1306) GetImg() *image.Gray {
return nil
}
func (d *displaySsd1306) LockImg() {
}
func (d *displaySsd1306) UnlockImg() {
}