package display import ( "errors" "fmt" "math" "strconv" "strings" "github.com/sirupsen/logrus" "go.maze.io/harmon/config" "go.maze.io/harmon/hardware" "periph.io/x/conn/v3/gpio" "periph.io/x/conn/v3/gpio/gpioreg" "periph.io/x/conn/v3/i2c" "periph.io/x/conn/v3/i2c/i2creg" "periph.io/x/conn/v3/physic" "periph.io/x/conn/v3/spi" "periph.io/x/conn/v3/spi/spireg" ) // Rotation is how much a display has been rotated. Displays can be rotated, and // sometimes also mirrored. type Rotation uint8 // Clockwise rotation of the screen. const ( NoRotation Rotation = iota Rotate90 Rotate180 Rotate270 ) func (r Rotation) String() string { switch r { case NoRotation: return "none" case Rotate90: return "90°" case Rotate180: return "180°" case Rotate270: return "270°" default: return "invalid" } } 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) } } // Options for most displays. type Options struct { W int // Width H int // Height Rotate Rotation // Rotate screen Flip bool // Flip screen vertically Mirror bool // Mirror screen horizontally Backlight gpio.PinOut // Backlight pin Sequential bool // Sequential mode SwapTopBottom bool // } // Common display options. func ParseOptions(config config.DisplayConfig, defaultWidth, defaultHeight int) (*Options, error) { w, h, err := ParseSize(config.Size, defaultWidth, defaultHeight) if err != nil { return nil, err } rotate, err := ParseRotation(config.Rotate) if err != nil { return nil, err } var bl gpio.PinOut if config.BL != "" { if bl, err = hardware.LookupPin(config.BL); err != nil { return nil, err } } return &Options{ W: w, H: h, Rotate: rotate, Flip: config.Flip, Mirror: config.Mirror, Backlight: bl, }, nil } // ParserAddr parses an I2C address. func ParseAddr(addr string) (uint16, error) { var ( addr64 uint64 err error ) if strings.HasPrefix(strings.ToLower(addr), "0x") { addr64, err = strconv.ParseUint(addr[2:], 16, 16) } else { addr64, err = strconv.ParseUint(addr, 16, 16) } if err != nil { return math.MaxUint16, InvalidAddr(addr, err) } return uint16(addr64), nil } // ParseSize parses a display size. 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("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 } // InvalidAddr indicates an invalid address. func InvalidAddr(addr string, err error) error { if err != nil { return fmt.Errorf("invalid address %q: %w", addr, err) } return fmt.Errorf("invalid address %q", addr) } // InvalidMode indicates an unknown mode. func InvalidMode(mode string) error { return fmt.Errorf("invalid display mode %q", mode) } // Conn is a display hardware connection (backed by I²C, SPI, etc.). type Conn interface { // Tx transfers data over the bus. Tx(data []byte, isCommand bool) error // Rx receives data over the bus. Rx(command byte, data []byte) error } type i2cConn struct { dev *i2c.Dev } func (c i2cConn) Tx(data []byte, isCommand bool) error { if isCommand { return c.dev.Tx(append([]byte{0x00}, data...), nil) } else { return c.dev.Tx(append([]byte{0x40}, data...), nil) } } func (c i2cConn) Rx(command byte, data []byte) error { return errors.New("not implemented") } // OpenI2C opens the named I²C bus and connects to the device at addr. func OpenI2C(name, addr string) (Conn, error) { log := logrus.WithFields(logrus.Fields{ "name": name, "addr": addr, }) log.Debug("configure I²C bus") add, err := ParseAddr(addr) if err != nil { return nil, err } log.Trace("opening I²C bus") bus, err := i2creg.Open(name) if err != nil { return nil, fmt.Errorf("invalid I²C bus %q: %w", name, err) } log.Tracef("opened I²C bus %q", bus) return i2cConn{&i2c.Dev{ Bus: bus, Addr: add, }}, nil } type spiConn struct { conn spi.Conn reset gpio.PinOut dc gpio.PinOut } func (c spiConn) Tx(data []byte, isCommand bool) (err error) { if c.dc == nil { return errors.New("3-wire SPI mode is not implemented") } level := gpio.High if isCommand { level = gpio.Low } if err = c.dc.Out(level); err != nil { return } return c.conn.Tx(data, nil) } func (c spiConn) Rx(command byte, data []byte) (err error) { if c.dc == nil { return errors.New("3-wire SPI mode is not implemented") } if err := c.Tx([]byte{command}, true); err != nil { return err } if err := c.dc.Out(gpio.High); err != nil { return err } return nil } // OpenSPI opens the named SPI port and connects to the device. func OpenSPI(name, resetPin, dcPin string) (Conn, error) { log := logrus.WithFields(logrus.Fields{ "name": name, "pin_reset": resetPin, "pin_dc": dcPin, }) log.Debug("configure SPI bus") var ( reset = gpioreg.ByName(resetPin) dc = gpioreg.ByName(dcPin) bits = 8 ) if !hardware.IsValidPin(reset) { return nil, hardware.PinNotFound{"reset", resetPin} } if dcPin == "" { bits = 9 } else if !hardware.IsValidPin(dc) { return nil, hardware.PinNotFound{"dc", dcPin} } log.Trace("opening SPI port") port, err := spireg.Open(name) if err != nil { return nil, fmt.Errorf("invalid SPI port %q: %w", name, err) } log.Tracef("opened SPI port %q", port) log.Trace("connecting to SPI port") conn, err := port.Connect(100*physic.MegaHertz, spi.Mode0, bits) if err != nil { _ = port.Close() return nil, fmt.Errorf("error connecting to SPI port %q: %w", name, err) } return spiConn{ conn: conn, reset: reset, dc: dc, }, nil }