Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,264 @@
|
||||
package lcdproc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.maze.io/go/bragi/config"
|
||||
"git.maze.io/go/bragi/display"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultPort = "13666"
|
||||
DefaultAddr = "localhost:" + DefaultPort
|
||||
screenName = "bragi"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Version string
|
||||
ProtocolVersion string
|
||||
|
||||
addr string
|
||||
conn *textproto.Conn
|
||||
width, height int
|
||||
cellWidth, cellHeight int
|
||||
text [][]byte
|
||||
}
|
||||
|
||||
func New(config *config.Display) (display.Text, error) {
|
||||
var (
|
||||
addr, host, port string
|
||||
err error
|
||||
)
|
||||
if host = config.Host; host == "" {
|
||||
host = "localhost"
|
||||
}
|
||||
if port = config.Port; port == "" {
|
||||
port = DefaultPort
|
||||
}
|
||||
addr = net.JoinHostPort(host, port)
|
||||
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"driver": "lcdproc",
|
||||
"addr": addr,
|
||||
})
|
||||
log.Debug("connecting to server")
|
||||
|
||||
conn, err := textproto.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
addr: addr,
|
||||
conn: conn,
|
||||
}
|
||||
if err = c.hello(log); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err = c.setup(log); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.text = make([][]byte, c.height)
|
||||
for i := 0; i < c.height; i++ {
|
||||
c.text[i] = make([]byte, c.width)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Client) hello(log *logrus.Entry) (err error) {
|
||||
log.Trace("sending hello")
|
||||
var id uint
|
||||
if id, err = c.conn.Cmd("hello"); err != nil {
|
||||
return
|
||||
}
|
||||
c.conn.StartResponse(id)
|
||||
defer c.conn.EndResponse(id)
|
||||
|
||||
var line string
|
||||
if line, err = c.conn.ReadLine(); err != nil {
|
||||
return
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"data": line,
|
||||
"id": id,
|
||||
}).Trace("recieved response")
|
||||
|
||||
var parts = strings.Split(line, " ")
|
||||
for i := 0; i < len(parts); i++ {
|
||||
switch part := parts[i]; part {
|
||||
case "connect":
|
||||
i++ // ignore "LCDproc"
|
||||
c.Version = parts[i]
|
||||
i++
|
||||
log.Debugf("received version %s", c.Version)
|
||||
case "protocol":
|
||||
i++
|
||||
c.ProtocolVersion = parts[i]
|
||||
log.Debugf("received protocol version %s", c.ProtocolVersion)
|
||||
case "lcd": // ignore
|
||||
case "wid":
|
||||
i++
|
||||
c.width, _ = strconv.Atoi(parts[i])
|
||||
case "hgt":
|
||||
i++
|
||||
c.height, _ = strconv.Atoi(parts[i])
|
||||
case "cellwid":
|
||||
i++
|
||||
c.cellWidth, _ = strconv.Atoi(parts[i])
|
||||
case "cellhgt":
|
||||
i++
|
||||
c.cellHeight, _ = strconv.Atoi(parts[i])
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("%dx%d display", c.width*c.cellWidth, c.height*c.cellHeight)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) setup(log *logrus.Entry) (err error) {
|
||||
if err = c.sendCommand("client_set -name Bragi"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = c.fireCommand("screen_del %s", screenName); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = c.fireCommand("screen_add %s", screenName); err != nil {
|
||||
return
|
||||
}
|
||||
if err = c.sendCommand("screen_set %s -cursor off -heartbeat off -priority foreground -duration -wid %d -hgt %d", screenName, c.width, c.height); err != nil {
|
||||
return
|
||||
}
|
||||
for y := 0; y < c.height; y++ {
|
||||
if err = c.sendCommand("widget_add %s row%d string", screenName, y); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) log() *logrus.Entry {
|
||||
return logrus.WithFields(logrus.Fields{
|
||||
"driver": "lcdproc",
|
||||
"addr": c.addr,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) sendCommand(format string, args ...interface{}) (err error) {
|
||||
log := c.log().WithField("command", fmt.Sprintf(format, args...))
|
||||
log.Debug("sending command")
|
||||
|
||||
var id uint
|
||||
if id, err = c.conn.Cmd(format, args...); err != nil {
|
||||
return
|
||||
}
|
||||
c.conn.StartResponse(id)
|
||||
defer c.conn.EndResponse(id)
|
||||
|
||||
log = log.WithField("id", id)
|
||||
log.Trace("reading response")
|
||||
for {
|
||||
var line string
|
||||
if line, err = c.conn.ReadLine(); err != nil {
|
||||
log.WithError(err).Error("error reading response")
|
||||
c.conn.EndResponse(id)
|
||||
return
|
||||
}
|
||||
|
||||
log.WithField("data", line).Trace("response")
|
||||
switch {
|
||||
case line == "success":
|
||||
return
|
||||
case strings.HasPrefix(line, "huh? "):
|
||||
return textproto.ProtocolError(line[5:])
|
||||
case strings.HasPrefix(line, "listen "):
|
||||
continue
|
||||
case strings.HasPrefix(line, "ignore "):
|
||||
continue
|
||||
default:
|
||||
return textproto.ProtocolError("unexpected response: " + line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) fireCommand(format string, args ...interface{}) (err error) {
|
||||
log := c.log().WithField("command", fmt.Sprintf(format, args...))
|
||||
log.Debug("sending command")
|
||||
|
||||
var id uint
|
||||
if id, err = c.conn.Cmd(format, args...); err != nil {
|
||||
return
|
||||
}
|
||||
c.conn.StartResponse(id)
|
||||
defer c.conn.EndResponse(id)
|
||||
|
||||
log = log.WithField("id", id)
|
||||
log.Trace("reading response")
|
||||
for {
|
||||
var line string
|
||||
if line, err = c.conn.ReadLine(); err != nil {
|
||||
log.WithError(err).Error("error reading response")
|
||||
c.conn.EndResponse(id)
|
||||
return
|
||||
}
|
||||
|
||||
log.WithField("data", line).Trace("response")
|
||||
switch {
|
||||
case line == "success":
|
||||
return
|
||||
case strings.HasPrefix(line, "huh? "):
|
||||
return
|
||||
case strings.HasPrefix(line, "listen "):
|
||||
continue
|
||||
case strings.HasPrefix(line, "ignore "):
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) String() string {
|
||||
return fmt.Sprintf("lcdproc %s at %s", c.Version, c.addr)
|
||||
}
|
||||
|
||||
func (c *Client) Columns() int {
|
||||
return c.width
|
||||
}
|
||||
|
||||
func (c *Client) Rows() int {
|
||||
return c.height
|
||||
}
|
||||
|
||||
func (c *Client) Halt() error {
|
||||
return c.conn.Close()
|
||||
}
|
||||
|
||||
func (Client) Refresh() error { return nil }
|
||||
|
||||
func (c *Client) Size() (w, h int) {
|
||||
return c.width * c.cellWidth, c.height * c.cellHeight
|
||||
}
|
||||
|
||||
func (c *Client) Text(x, y int, message string) {
|
||||
w := len(message)
|
||||
if x+w > c.width {
|
||||
message = message[:c.width-x]
|
||||
}
|
||||
_ = c.fireCommand("widget_set %s main %d %d %q", screenName, x+1, y+1, message)
|
||||
}
|
||||
|
||||
func init() {
|
||||
display.Register("lcdproc", func(config *config.Display) (display.Display, error) {
|
||||
return New(config)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package sdl
|
||||
|
||||
import "git.maze.io/go/harmon/input"
|
||||
|
||||
type keyboardInput struct {
|
||||
//events chan *sdl.KeyboardEvent
|
||||
events chan input.Event
|
||||
}
|
||||
|
||||
func newKeyboardInput() *keyboardInput {
|
||||
return &keyboardInput{
|
||||
events: make(chan input.Event),
|
||||
}
|
||||
}
|
||||
|
||||
func (keyboardInput) Name() string {
|
||||
return "keyboard"
|
||||
}
|
||||
|
||||
func (k *keyboardInput) Events() (<-chan input.Event, error) {
|
||||
return k.events, nil
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package sdl
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
|
||||
"git.maze.io/go/harmon/input"
|
||||
)
|
||||
|
||||
var (
|
||||
initOnce sync.Once
|
||||
initTime time.Time
|
||||
quitOnce sync.Once
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
window *sdl.Window
|
||||
surface *sdl.Surface
|
||||
keyboardInput *keyboardInput
|
||||
}
|
||||
|
||||
func New() (*Device, error) {
|
||||
var err error
|
||||
initOnce.Do(func() {
|
||||
err = sdl.Init(sdl.INIT_EVERYTHING)
|
||||
if err == nil {
|
||||
initTime = time.Now()
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
b int32 = 32
|
||||
w int32 = 256
|
||||
h int32 = 32
|
||||
)
|
||||
|
||||
window, err := sdl.CreateWindow("Bragi OLED emulator",
|
||||
sdl.WINDOWPOS_UNDEFINED,
|
||||
sdl.WINDOWPOS_UNDEFINED,
|
||||
w+2*b,
|
||||
h+2*b,
|
||||
sdl.WINDOW_SHOWN)
|
||||
if err != nil {
|
||||
sdl.Quit()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
surface, err := window.GetSurface()
|
||||
if err != nil {
|
||||
_ = window.Destroy()
|
||||
sdl.Quit()
|
||||
return nil, err
|
||||
}
|
||||
surface.FillRect(nil, 0)
|
||||
|
||||
surface.FillRect(&sdl.Rect{0, 0, b + b + w, b + b + h}, sdl.MapRGBA(surface.Format, 128, 64, 0, 255))
|
||||
surface.FillRect(&sdl.Rect{b, b, w, h}, sdl.MapRGBA(surface.Format, 255, 128, 0, 255))
|
||||
window.UpdateSurface()
|
||||
|
||||
d := &Device{
|
||||
window: window,
|
||||
surface: surface,
|
||||
keyboardInput: newKeyboardInput(),
|
||||
}
|
||||
|
||||
input.Register(func() ([]input.Source, error) {
|
||||
return []input.Source{
|
||||
d.keyboardInput,
|
||||
}, nil
|
||||
})
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Device) Close() error {
|
||||
err := d.window.Destroy()
|
||||
quitOnce.Do(sdl.Quit)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Device) Run() error {
|
||||
for running := true; running; {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch event := event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
running = false
|
||||
break
|
||||
|
||||
case *sdl.KeyboardEvent:
|
||||
d.handleKeyboardEvent(event)
|
||||
|
||||
case *sdl.MouseMotionEvent:
|
||||
//select {
|
||||
//case d.mouseMotionEvents <- event:
|
||||
//default:
|
||||
//}
|
||||
|
||||
default:
|
||||
log.Printf("SDL event: %T", event)
|
||||
}
|
||||
}
|
||||
}
|
||||
return d.Close()
|
||||
}
|
||||
|
||||
func (d *Device) handleKeyboardEvent(event *sdl.KeyboardEvent) {
|
||||
/*
|
||||
var kind input.EventType
|
||||
switch event.Type {
|
||||
case sdl.KEYDOWN:
|
||||
kind = input.KeyPress
|
||||
case sdl.KEYUP:
|
||||
kind = input.KeyRelease
|
||||
}
|
||||
d.keyboardInput.events <- input.Event{
|
||||
Type: kind,
|
||||
Time: initTime.Add(time.Duration(event.Timestamp) * time.Second),
|
||||
Code: int(event.Keysym.Sym),
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package ssd1306
|
||||
|
||||
import "git.maze.io/go/harmon/display"
|
||||
|
||||
// Registers
|
||||
const (
|
||||
Address = 0x3D
|
||||
Address_128_32 = 0x3C
|
||||
|
||||
SETCONTRAST = 0x81
|
||||
DISPLAYALLON_RESUME = 0xA4
|
||||
DISPLAYALLON = 0xA5
|
||||
NORMALDISPLAY = 0xA6
|
||||
INVERTDISPLAY = 0xA7
|
||||
DISPLAYOFF = 0xAE
|
||||
DISPLAYON = 0xAF
|
||||
SETDISPLAYOFFSET = 0xD3
|
||||
SETCOMPINS = 0xDA
|
||||
SETVCOMDETECT = 0xDB
|
||||
SETDISPLAYCLOCKDIV = 0xD5
|
||||
SETPRECHARGE = 0xD9
|
||||
SETMULTIPLEX = 0xA8
|
||||
SETLOWCOLUMN = 0x00
|
||||
SETHIGHCOLUMN = 0x10
|
||||
SETSTARTLINE = 0x40
|
||||
MEMORYMODE = 0x20
|
||||
COLUMNADDR = 0x21
|
||||
PAGEADDR = 0x22
|
||||
COMSCANINC = 0xC0
|
||||
COMSCANDEC = 0xC8
|
||||
SEGREMAP = 0xA0
|
||||
CHARGEPUMP = 0x8D
|
||||
ACTIVATE_SCROLL = 0x2F
|
||||
DEACTIVATE_SCROLL = 0x2E
|
||||
SET_VERTICAL_SCROLL_AREA = 0xA3
|
||||
RIGHT_HORIZONTAL_SCROLL = 0x26
|
||||
LEFT_HORIZONTAL_SCROLL = 0x27
|
||||
VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29
|
||||
VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A
|
||||
|
||||
EXTERNALVCC VccMode = 0x1
|
||||
SWITCHCAPVCC VccMode = 0x2
|
||||
|
||||
NO_ROTATION = display.NoRotation
|
||||
ROTATION_180 = display.Rotate180
|
||||
)
|
||||
@@ -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()
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package st7789
|
||||
|
||||
type FrameRate uint8
|
||||
|
||||
// Registers
|
||||
const (
|
||||
NOP = 0x00
|
||||
SWRESET = 0x01
|
||||
RDDID = 0x04
|
||||
RDDST = 0x09
|
||||
SLPIN = 0x10
|
||||
SLPOUT = 0x11
|
||||
PTLON = 0x12
|
||||
NORON = 0x13
|
||||
INVOFF = 0x20
|
||||
INVON = 0x21
|
||||
DISPOFF = 0x28
|
||||
DISPON = 0x29
|
||||
CASET = 0x2A
|
||||
RASET = 0x2B
|
||||
RAMWR = 0x2C
|
||||
RAMRD = 0x2E
|
||||
PTLAR = 0x30
|
||||
COLMOD = 0x3A
|
||||
MADCTL = 0x36
|
||||
MADCTL_MY = 0x80
|
||||
MADCTL_MX = 0x40
|
||||
MADCTL_MV = 0x20
|
||||
MADCTL_ML = 0x10
|
||||
MADCTL_RGB = 0x00
|
||||
MADCTL_BGR = 0x08
|
||||
MADCTL_MH = 0x04
|
||||
RDID1 = 0xDA
|
||||
RDID2 = 0xDB
|
||||
RDID3 = 0xDC
|
||||
RDID4 = 0xDD
|
||||
FRMCTR1 = 0xB1
|
||||
RGBCTRL = 0xB1
|
||||
FRMCTR2 = 0xB2
|
||||
PORCTRL = 0xB2
|
||||
FRMCTR3 = 0xB3
|
||||
INVCTR = 0xB4
|
||||
DISSET5 = 0xB6
|
||||
PWCTR1 = 0xC0
|
||||
PWCTR2 = 0xC1
|
||||
PWCTR3 = 0xC2
|
||||
PWCTR4 = 0xC3
|
||||
PWCTR5 = 0xC4
|
||||
VMCTR1 = 0xC5
|
||||
FRCTRL2 = 0xC6
|
||||
PWCTR6 = 0xFC
|
||||
GMCTRP1 = 0xE0
|
||||
GMCTRN1 = 0xE1
|
||||
GSCAN = 0x45
|
||||
|
||||
// Allowable frame rate codes for FRCTRL2 (Identifier is in Hz)
|
||||
FRAMERATE_111 FrameRate = 0x01
|
||||
FRAMERATE_105 FrameRate = 0x02
|
||||
FRAMERATE_99 FrameRate = 0x03
|
||||
FRAMERATE_94 FrameRate = 0x04
|
||||
FRAMERATE_90 FrameRate = 0x05
|
||||
FRAMERATE_86 FrameRate = 0x06
|
||||
FRAMERATE_82 FrameRate = 0x07
|
||||
FRAMERATE_78 FrameRate = 0x08
|
||||
FRAMERATE_75 FrameRate = 0x09
|
||||
FRAMERATE_72 FrameRate = 0x0A
|
||||
FRAMERATE_69 FrameRate = 0x0B
|
||||
FRAMERATE_67 FrameRate = 0x0C
|
||||
FRAMERATE_64 FrameRate = 0x0D
|
||||
FRAMERATE_62 FrameRate = 0x0E
|
||||
FRAMERATE_60 FrameRate = 0x0F // 60 is default
|
||||
FRAMERATE_58 FrameRate = 0x10
|
||||
FRAMERATE_57 FrameRate = 0x11
|
||||
FRAMERATE_55 FrameRate = 0x12
|
||||
FRAMERATE_53 FrameRate = 0x13
|
||||
FRAMERATE_52 FrameRate = 0x14
|
||||
FRAMERATE_50 FrameRate = 0x15
|
||||
FRAMERATE_49 FrameRate = 0x16
|
||||
FRAMERATE_48 FrameRate = 0x17
|
||||
FRAMERATE_46 FrameRate = 0x18
|
||||
FRAMERATE_45 FrameRate = 0x19
|
||||
FRAMERATE_44 FrameRate = 0x1A
|
||||
FRAMERATE_43 FrameRate = 0x1B
|
||||
FRAMERATE_42 FrameRate = 0x1C
|
||||
FRAMERATE_41 FrameRate = 0x1D
|
||||
FRAMERATE_40 FrameRate = 0x1E
|
||||
FRAMERATE_39 FrameRate = 0x1F
|
||||
|
||||
MAX_VSYNC_SCANLINES = 254
|
||||
)
|
||||
@@ -0,0 +1,535 @@
|
||||
package st7789
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
"periph.io/x/conn/v3/physic"
|
||||
"periph.io/x/conn/v3/spi"
|
||||
"periph.io/x/conn/v3/spi/spireg"
|
||||
|
||||
"git.maze.io/go/harmon/config"
|
||||
"git.maze.io/go/harmon/display"
|
||||
"git.maze.io/go/harmon/internal/pixel"
|
||||
)
|
||||
|
||||
func init() {
|
||||
display.Register("st7789", newDevice)
|
||||
}
|
||||
|
||||
type Device struct {
|
||||
conn spi.Conn
|
||||
reset, cs, dc, bl gpio.PinOut
|
||||
width int16
|
||||
height int16
|
||||
columnOffsetCfg int16
|
||||
rowOffsetCfg int16
|
||||
columnOffset int16
|
||||
rowOffset int16
|
||||
rotation display.Rotation
|
||||
frameRate FrameRate
|
||||
batchLength int32
|
||||
isBGR bool
|
||||
vSyncLines int16
|
||||
}
|
||||
|
||||
type Opts struct {
|
||||
Width int16
|
||||
Height int16
|
||||
Rotation display.Rotation
|
||||
RowOffset int16
|
||||
ColumnOffset int16
|
||||
FrameRate FrameRate
|
||||
VSyncLines int16
|
||||
}
|
||||
|
||||
var DefaultOpts = Opts{
|
||||
Width: 240,
|
||||
Height: 320,
|
||||
}
|
||||
|
||||
func newDevice(config *config.Display) (display.Display, error) {
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"type": "st7789",
|
||||
"name": config.Name,
|
||||
})
|
||||
|
||||
log.WithField("port", config.Port).Trace("opening SPI port")
|
||||
port, err := spireg.Open(config.Port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
bits = 8
|
||||
reset gpio.PinOut
|
||||
dc gpio.PinOut
|
||||
cs gpio.PinOut
|
||||
bl gpio.PinOut
|
||||
)
|
||||
if config.DC == "" {
|
||||
bits = 9
|
||||
} else {
|
||||
if dc, err = display.LookupPinOut(config.DC); err != nil {
|
||||
return nil, fmt.Errorf("invalid dc pin: %w", err)
|
||||
}
|
||||
if err = dc.Out(gpio.Low); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if reset, err = display.LookupPinOut(config.Reset); err != nil {
|
||||
return nil, fmt.Errorf("invalid reset pin: %w", err)
|
||||
}
|
||||
if cs, err = display.LookupPinOut(config.CS); err != nil {
|
||||
return nil, fmt.Errorf("invalid cs pin: %w", err)
|
||||
}
|
||||
if bl, err = display.LookupPinOut(config.BL); err != nil {
|
||||
return nil, fmt.Errorf("invalid bl pin: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"mode": spi.Mode0,
|
||||
"bits": bits,
|
||||
"reset": reset.Name(),
|
||||
"dc": dc.Name(),
|
||||
"cs": cs.Name(),
|
||||
"bl": bl.Name(),
|
||||
}).Debug("connecting on SPI port")
|
||||
conn, err := port.Connect(300*physic.KiloHertz, spi.Mode0, bits)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d := &Device{
|
||||
conn: conn,
|
||||
reset: reset,
|
||||
cs: cs,
|
||||
dc: dc,
|
||||
bl: bl,
|
||||
}
|
||||
cfg := DefaultOpts
|
||||
|
||||
if cfg.Width != 0 {
|
||||
d.width = cfg.Width
|
||||
} else {
|
||||
d.width = 240
|
||||
}
|
||||
if cfg.Height != 0 {
|
||||
d.height = cfg.Height
|
||||
} else {
|
||||
d.height = 240
|
||||
}
|
||||
|
||||
d.rotation = cfg.Rotation
|
||||
d.rowOffsetCfg = cfg.RowOffset
|
||||
d.columnOffsetCfg = cfg.ColumnOffset
|
||||
|
||||
if cfg.FrameRate != 0 {
|
||||
d.frameRate = cfg.FrameRate
|
||||
} else {
|
||||
d.frameRate = FRAMERATE_60
|
||||
}
|
||||
|
||||
if cfg.VSyncLines >= 2 && cfg.VSyncLines <= 254 {
|
||||
d.vSyncLines = cfg.VSyncLines
|
||||
} else {
|
||||
d.vSyncLines = 16
|
||||
}
|
||||
|
||||
d.batchLength = int32(d.width)
|
||||
if d.height > d.width {
|
||||
d.batchLength = int32(d.height)
|
||||
}
|
||||
d.batchLength += d.batchLength & 1
|
||||
|
||||
// Reset the device
|
||||
log.Trace("reset device")
|
||||
if err = d.reset.Out(gpio.High); err != nil {
|
||||
_ = port.Close()
|
||||
return nil, err
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if err = d.reset.Out(gpio.Low); err != nil {
|
||||
_ = port.Close()
|
||||
return nil, err
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if err = d.reset.Out(gpio.High); err != nil {
|
||||
_ = port.Close()
|
||||
return nil, err
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
// Common initialization
|
||||
log.Trace("configure device")
|
||||
d.Command(SWRESET) // Soft reset
|
||||
time.Sleep(150 * time.Millisecond) //
|
||||
|
||||
d.Command(SLPOUT) // Exit sleep mode
|
||||
time.Sleep(500 * time.Millisecond) //
|
||||
|
||||
// Memory initialization
|
||||
d.Command(COLMOD) // Set color mode
|
||||
d.Data(0x55) // 16-bit color
|
||||
time.Sleep(10 * time.Millisecond) //
|
||||
|
||||
d.SetRotation(d.rotation) // Memory orientation
|
||||
|
||||
d.setWindow(0, 0, d.width, d.height) // Full draw window
|
||||
d.FillScreen(color.RGBA{0, 0, 0, 255}) // Clear screen
|
||||
|
||||
// Framerate
|
||||
d.Command(FRCTRL2) // Frame rate for normal mode
|
||||
d.Data(uint8(d.frameRate)) // Default is 60Hz
|
||||
|
||||
// Frame vertical sync and "porch"
|
||||
//
|
||||
// Front and back porch controls vertical scanline sync time before and after
|
||||
// a frame, where memory can be safely written without tearing.
|
||||
//
|
||||
fp := uint8(d.vSyncLines / 2) // Split the desired pause half and half
|
||||
bp := uint8(d.vSyncLines - int16(fp)) // between front and back porch.
|
||||
|
||||
d.Command(PORCTRL)
|
||||
d.Data(bp) // Back porch 5bit (0x7F max 0x08 default)
|
||||
d.Data(fp) // Front porch 5bit (0x7F max 0x08 default)
|
||||
d.Data(0x00) // Seprarate porch (TODO: what is this?)
|
||||
d.Data(0x22) // Idle mode porch (4bit-back 4bit-front 0x22 default)
|
||||
d.Data(0x22) // Partial mode porch (4bit-back 4bit-front 0x22 default)
|
||||
|
||||
// Ready to display
|
||||
d.Command(INVON) // Inversion ON
|
||||
time.Sleep(10 * time.Millisecond) //
|
||||
|
||||
d.Command(NORON) // Normal mode ON
|
||||
time.Sleep(10 * time.Millisecond) //
|
||||
|
||||
d.Command(DISPON) // Screen ON
|
||||
time.Sleep(10 * time.Millisecond) //
|
||||
|
||||
// Backlight ON
|
||||
if err = d.bl.Out(gpio.High); err != nil {
|
||||
_ = port.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Device) String() string {
|
||||
return "st7789"
|
||||
}
|
||||
|
||||
func (d *Device) Bounds() image.Rectangle {
|
||||
if d.rotation == display.Rotate90 || d.rotation == display.Rotate270 {
|
||||
return image.Rect(0, 0, int(d.height), int(d.width))
|
||||
}
|
||||
return image.Rect(0, 0, int(d.width), int(d.height))
|
||||
}
|
||||
|
||||
func (d *Device) ColorModel() color.Model {
|
||||
return color.RGBAModel
|
||||
}
|
||||
|
||||
func (d *Device) Halt() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) Refresh() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sync waits for the display to hit the next VSYNC pause
|
||||
func (d *Device) Sync() {
|
||||
d.SyncToScanLine(0)
|
||||
}
|
||||
|
||||
// SyncToScanLine waits for the display to hit a specific scanline
|
||||
//
|
||||
// A scanline value of 0 will forward to the beginning of the next VSYNC,
|
||||
// even if the display is currently in a VSYNC pause.
|
||||
//
|
||||
// Syncline values appear to increment once for every two vertical
|
||||
// lines on the display.
|
||||
//
|
||||
// NOTE: Use GetHighestScanLine and GetLowestScanLine to obtain the highest
|
||||
// and lowest useful values. Values are affected by front and back porch
|
||||
// vsync settings (derived from VSyncLines configuration option).
|
||||
func (d *Device) SyncToScanLine(scanline uint16) {
|
||||
scan := d.GetScanLine()
|
||||
|
||||
// Sometimes GetScanLine returns erroneous 0 on first call after draw, so double check
|
||||
if scan == 0 {
|
||||
scan = d.GetScanLine()
|
||||
}
|
||||
|
||||
if scanline == 0 {
|
||||
// we dont know where we are in an ongoing vsync so go around
|
||||
for scan < 1 {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
scan = d.GetScanLine()
|
||||
}
|
||||
for scan > 0 {
|
||||
scan = d.GetScanLine()
|
||||
}
|
||||
} else {
|
||||
// go around unless we're very close to the target
|
||||
for scan > scanline+4 {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
scan = d.GetScanLine()
|
||||
}
|
||||
for scan < scanline {
|
||||
scan = d.GetScanLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetScanLine reads the current scanline value from the display
|
||||
func (d *Device) GetScanLine() uint16 {
|
||||
data := []uint8{0x00, 0x00}
|
||||
d.Rx(GSCAN, data)
|
||||
return uint16(data[0])<<8 + uint16(data[1])
|
||||
}
|
||||
|
||||
// GetHighestScanLine calculates the last scanline id in the frame before VSYNC pause
|
||||
func (d *Device) GetHighestScanLine() uint16 {
|
||||
// Last scanline id appears to be backporch/2 + 320/2
|
||||
return uint16(math.Ceil(float64(d.vSyncLines)/2)/2) + 160
|
||||
}
|
||||
|
||||
// GetLowestScanLine calculate the first scanline id to appear after VSYNC pause
|
||||
func (d *Device) GetLowestScanLine() uint16 {
|
||||
// First scanline id appears to be backporch/2 + 1
|
||||
return uint16(math.Ceil(float64(d.vSyncLines)/2)/2) + 1
|
||||
}
|
||||
|
||||
// SetPixel sets a pixel in the screen
|
||||
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
|
||||
if x < 0 || y < 0 ||
|
||||
(((d.rotation == display.NoRotation || d.rotation == display.Rotate180) && (x >= d.width || y >= d.height)) ||
|
||||
((d.rotation == display.Rotate90 || d.rotation == display.Rotate270) && (x >= d.height || y >= d.width))) {
|
||||
return
|
||||
}
|
||||
d.FillRectangle(x, y, 1, 1, c)
|
||||
}
|
||||
|
||||
// setWindow prepares the screen to be modified at a given rectangle
|
||||
func (d *Device) setWindow(x, y, w, h int16) {
|
||||
x += d.columnOffset
|
||||
y += d.rowOffset
|
||||
d.Tx([]uint8{CASET}, true)
|
||||
d.Tx([]uint8{uint8(x >> 8), uint8(x), uint8((x + w - 1) >> 8), uint8(x + w - 1)}, false)
|
||||
d.Tx([]uint8{RASET}, true)
|
||||
d.Tx([]uint8{uint8(y >> 8), uint8(y), uint8((y + h - 1) >> 8), uint8(y + h - 1)}, false)
|
||||
d.Command(RAMWR)
|
||||
}
|
||||
|
||||
// FillRectangle fills a rectangle at a given coordinates with a color
|
||||
func (d *Device) FillRectangle(x, y, width, height int16, c color.Color) error {
|
||||
|
||||
k, i := int16(d.Size().X), int16(d.Size().Y)
|
||||
if x < 0 || y < 0 || width <= 0 || height <= 0 ||
|
||||
x >= k || (x+width) > k || y >= i || (y+height) > i {
|
||||
return errors.New("rectangle coordinates outside display area")
|
||||
}
|
||||
d.setWindow(x, y, width, height)
|
||||
c565 := pixel.Color565Model.Convert(c).(pixel.Color565)
|
||||
//c565 := RGBATo565(c)
|
||||
c1 := uint8(c565 >> 8)
|
||||
c2 := uint8(c565)
|
||||
|
||||
data := make([]uint8, d.batchLength*2)
|
||||
for i := int32(0); i < d.batchLength; i++ {
|
||||
data[i*2] = c1
|
||||
data[i*2+1] = c2
|
||||
}
|
||||
j := int32(width) * int32(height)
|
||||
for j > 0 {
|
||||
if j >= d.batchLength {
|
||||
d.Tx(data, false)
|
||||
} else {
|
||||
d.Tx(data[:j*2], false)
|
||||
}
|
||||
j -= d.batchLength
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FillRectangle fills a rectangle at a given coordinates with a buffer
|
||||
func (d *Device) FillRectangleWithBuffer(x, y, width, height int16, buffer []color.RGBA) error {
|
||||
i, j := int16(d.Size().X), int16(d.Size().Y)
|
||||
if x < 0 || y < 0 || width <= 0 || height <= 0 ||
|
||||
x >= i || (x+width) > i || y >= j || (y+height) > j {
|
||||
return errors.New("rectangle coordinates outside display area")
|
||||
}
|
||||
if int32(width)*int32(height) != int32(len(buffer)) {
|
||||
return errors.New("buffer length does not match with rectangle size")
|
||||
}
|
||||
d.setWindow(x, y, width, height)
|
||||
|
||||
k := int32(width) * int32(height)
|
||||
data := make([]uint8, d.batchLength*2)
|
||||
offset := int32(0)
|
||||
for k > 0 {
|
||||
for i := int32(0); i < d.batchLength; i++ {
|
||||
if offset+i < int32(len(buffer)) {
|
||||
c565 := RGBATo565(buffer[offset+i])
|
||||
c1 := uint8(c565 >> 8)
|
||||
c2 := uint8(c565)
|
||||
data[i*2] = c1
|
||||
data[i*2+1] = c2
|
||||
}
|
||||
}
|
||||
if k >= d.batchLength {
|
||||
d.Tx(data, false)
|
||||
} else {
|
||||
d.Tx(data[:k*2], false)
|
||||
}
|
||||
k -= d.batchLength
|
||||
offset += d.batchLength
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DrawFastVLine draws a vertical line faster than using SetPixel
|
||||
func (d *Device) DrawFastVLine(x, y0, y1 int16, c color.RGBA) {
|
||||
if y0 > y1 {
|
||||
y0, y1 = y1, y0
|
||||
}
|
||||
d.FillRectangle(x, y0, 1, y1-y0+1, c)
|
||||
}
|
||||
|
||||
// DrawFastHLine draws a horizontal line faster than using SetPixel
|
||||
func (d *Device) DrawFastHLine(x0, x1, y int16, c color.RGBA) {
|
||||
if x0 > x1 {
|
||||
x0, x1 = x1, x0
|
||||
}
|
||||
d.FillRectangle(x0, y, x1-x0+1, 1, c)
|
||||
}
|
||||
|
||||
// FillScreen fills the screen with a given color
|
||||
func (d *Device) FillScreen(c color.Color) {
|
||||
if d.rotation == display.NoRotation || d.rotation == display.Rotate180 {
|
||||
d.FillRectangle(0, 0, d.width, d.height, c)
|
||||
} else {
|
||||
d.FillRectangle(0, 0, d.height, d.width, c)
|
||||
}
|
||||
}
|
||||
|
||||
// SetRotation changes the rotation of the device (clock-wise)
|
||||
func (d *Device) SetRotation(rotation display.Rotation) {
|
||||
madctl := uint8(0)
|
||||
switch rotation % 4 {
|
||||
case display.NoRotation:
|
||||
madctl = MADCTL_MX | MADCTL_MY
|
||||
d.rowOffset = d.rowOffsetCfg
|
||||
d.columnOffset = d.columnOffsetCfg
|
||||
break
|
||||
case display.Rotate90:
|
||||
madctl = MADCTL_MY | MADCTL_MV
|
||||
d.rowOffset = d.columnOffsetCfg
|
||||
d.columnOffset = d.rowOffsetCfg
|
||||
break
|
||||
case display.Rotate180:
|
||||
d.rowOffset = 0
|
||||
d.columnOffset = 0
|
||||
break
|
||||
case display.Rotate270:
|
||||
madctl = MADCTL_MX | MADCTL_MV
|
||||
d.rowOffset = 0
|
||||
d.columnOffset = 0
|
||||
break
|
||||
}
|
||||
if d.isBGR {
|
||||
madctl |= MADCTL_BGR
|
||||
}
|
||||
d.Command(MADCTL)
|
||||
d.Data(madctl)
|
||||
}
|
||||
|
||||
// Command sends a command to the display
|
||||
func (d *Device) Command(command uint8) {
|
||||
d.Tx([]byte{command}, true)
|
||||
}
|
||||
|
||||
// Command sends a data to the display
|
||||
func (d *Device) Data(data uint8) {
|
||||
d.Tx([]byte{data}, false)
|
||||
}
|
||||
|
||||
// Tx sends data to the display
|
||||
func (d *Device) Tx(data []byte, isCommand bool) {
|
||||
if isCommand {
|
||||
d.dc.Out(gpio.Low)
|
||||
} else {
|
||||
d.dc.Out(gpio.High)
|
||||
}
|
||||
d.cs.Out(gpio.Low)
|
||||
d.conn.Tx(data, nil)
|
||||
d.cs.Out(gpio.High)
|
||||
}
|
||||
|
||||
// Rx reads data from the display
|
||||
func (d *Device) Rx(command uint8, read_bytes []byte) {
|
||||
d.dc.Out(gpio.Low)
|
||||
d.cs.Out(gpio.Low)
|
||||
d.conn.Tx([]byte{command}, nil)
|
||||
d.dc.Out(gpio.High)
|
||||
for i := range read_bytes {
|
||||
d.conn.Tx([]byte{0xFF}, read_bytes[i:i+1])
|
||||
}
|
||||
d.cs.Out(gpio.High)
|
||||
}
|
||||
|
||||
// Size returns the current size of the display.
|
||||
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))
|
||||
}
|
||||
|
||||
// EnableBacklight enables or disables the backlight
|
||||
func (d *Device) EnableBacklight(enable bool) {
|
||||
if enable {
|
||||
d.bl.Out(gpio.High)
|
||||
} else {
|
||||
d.bl.Out(gpio.Low)
|
||||
}
|
||||
}
|
||||
|
||||
// InverColors inverts the colors of the screen
|
||||
func (d *Device) InvertColors(invert bool) {
|
||||
if invert {
|
||||
d.Command(INVON)
|
||||
} else {
|
||||
d.Command(INVOFF)
|
||||
}
|
||||
}
|
||||
|
||||
// IsBGR changes the color mode (RGB/BGR)
|
||||
func (d *Device) IsBGR(bgr bool) {
|
||||
d.isBGR = bgr
|
||||
}
|
||||
|
||||
func (d *Device) At(x, y int) color.Color {
|
||||
return color.Black
|
||||
}
|
||||
|
||||
func (d *Device) Set(x, y int, c color.Color) {
|
||||
d.FillRectangle(int16(x), int16(y), int16(x)+1, int16(y)+1, color.RGBAModel.Convert(c).(color.RGBA))
|
||||
}
|
||||
|
||||
// RGBATo565 converts a color.RGBA to uint16 used in the display
|
||||
func RGBATo565(c color.RGBA) uint16 {
|
||||
r, g, b, _ := c.RGBA()
|
||||
return uint16((r & 0xF800) +
|
||||
((g & 0xFC00) >> 5) +
|
||||
((b & 0xF800) >> 11))
|
||||
}
|
||||
Reference in new issue
Block a user