91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package display
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/draw"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.maze.io/go/harmon/hardware"
|
|
)
|
|
|
|
// Display base interface for all displays.
|
|
type Display interface {
|
|
hardware.Resource
|
|
|
|
Size() image.Point
|
|
|
|
// Refresh the display.
|
|
Refresh() error
|
|
|
|
// Image allows this display to be used as canvas.
|
|
draw.Image
|
|
}
|
|
|
|
// Rotation of the display.
|
|
type Rotation uint8
|
|
|
|
const (
|
|
NoRotation Rotation = iota
|
|
Rotate90
|
|
Rotate180
|
|
Rotate270
|
|
)
|
|
|
|
func ParseRotation(s string) (Rotation, error) {
|
|
switch strings.ToLower(s) {
|
|
case "", "no", "none":
|
|
return NoRotation, nil
|
|
case "90", "right", "cw":
|
|
return Rotate90, nil
|
|
case "180", "flip":
|
|
return Rotate180, nil
|
|
case "270", "left", "ccw":
|
|
return Rotate270, nil
|
|
default:
|
|
return NoRotation, fmt.Errorf("invalid rotation %q", s)
|
|
}
|
|
}
|
|
|
|
func ParseI2CAddress(addr string) (uint16, error) {
|
|
if strings.HasPrefix(strings.ToLower(addr), "0x") {
|
|
addr = addr[2:]
|
|
}
|
|
|
|
v, err := strconv.ParseUint(addr, 16, 16)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return uint16(v), nil
|
|
}
|
|
|
|
func ParseSize(size string, defaultWidth, defaultHeight int) (width, height int, err error) {
|
|
if size == "" {
|
|
return defaultWidth, defaultHeight, nil
|
|
}
|
|
|
|
i := strings.IndexByte(size, 'x')
|
|
if i == -1 {
|
|
return 0, 0, fmt.Errorf("display: incorrect size specifier %q: expected `widthxheight`", size)
|
|
} else if i == 0 {
|
|
if height, err = strconv.Atoi(size[1:]); err != nil {
|
|
return
|
|
}
|
|
return defaultWidth, height, nil
|
|
} else if i == len(size)-1 {
|
|
if width, err = strconv.Atoi(size[:i]); err != nil {
|
|
return
|
|
}
|
|
return width, defaultHeight, nil
|
|
}
|
|
|
|
if width, err = strconv.Atoi(size[:i]); err != nil {
|
|
return
|
|
}
|
|
if height, err = strconv.Atoi(size[i+1:]); err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|