inky: Minor cleanups: (#431)

* Use maxTxSize from conn.Limits
* Show an error instead of crashing on invalid pin names
pull/1/head
David Sansome 6 years ago committed by M-A
parent 189a8fc87a
commit 1f4a49516e

@ -41,7 +41,7 @@ func (c *Color) String() string {
case White: case White:
return "white" return "white"
default: default:
return "Unknown" return "unknown"
} }
} }
@ -49,13 +49,13 @@ func (c *Color) String() string {
func (c *Color) Set(s string) error { func (c *Color) Set(s string) error {
switch s { switch s {
case "black": case "black":
*c = (Color)(Black) *c = Black
case "red": case "red":
*c = (Color)(Red) *c = Red
case "yellow": case "yellow":
*c = (Color)(Yellow) *c = Yellow
case "white": case "white":
*c = (Color)(White) *c = White
default: default:
return fmt.Errorf("Unknown color %q: expected either black, red, yellow or white", s) return fmt.Errorf("Unknown color %q: expected either black, red, yellow or white", s)
} }
@ -86,9 +86,9 @@ func (m *Model) String() string {
func (m *Model) Set(s string) error { func (m *Model) Set(s string) error {
switch s { switch s {
case "PHAT": case "PHAT":
*m = (Model)(PHAT) *m = PHAT
case "WHAT": case "WHAT":
*m = (Model)(WHAT) *m = WHAT
default: default:
return fmt.Errorf("Unknown model %q: expected either PHAT or WHAT", s) return fmt.Errorf("Unknown model %q: expected either PHAT or WHAT", s)
} }
@ -106,8 +106,6 @@ type Opts struct {
BorderColor Color BorderColor Color
} }
const spiChunkSize = 4096
var borderColor = map[Color]byte{ var borderColor = map[Color]byte{
Black: 0x00, Black: 0x00,
Red: 0x73, Red: 0x73,
@ -126,13 +124,24 @@ func New(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinIn, o *Opts
return nil, fmt.Errorf("failed to connect to inky over spi: %v", err) return nil, fmt.Errorf("failed to connect to inky over spi: %v", err)
} }
// Get the maxTxSize from the conn if it implements the conn.Limits interface,
// otherwise use 4096 bytes.
maxTxSize := 0
if limits, ok := c.(conn.Limits); ok {
maxTxSize = limits.MaxTxSize()
}
if maxTxSize == 0 {
maxTxSize = 4096 // Use a conservative default.
}
d := &Dev{ d := &Dev{
c: c, c: c,
dc: dc, maxTxSize: maxTxSize,
r: reset, dc: dc,
busy: busy, r: reset,
color: o.ModelColor, busy: busy,
border: o.BorderColor, color: o.ModelColor,
border: o.BorderColor,
} }
switch o.Model { switch o.Model {
@ -149,6 +158,8 @@ func New(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinIn, o *Opts
// Dev is a handle to an Inky. // Dev is a handle to an Inky.
type Dev struct { type Dev struct {
c conn.Conn c conn.Conn
// Maximum number of bytes allowed to be sent as a single I/O on c.
maxTxSize int
// Low when sending a command, high when sending data. // Low when sending a command, high when sending data.
dc gpio.PinOut dc gpio.PinOut
// Reset pin, active low. // Reset pin, active low.
@ -383,8 +394,8 @@ func (d *Dev) sendData(data []byte) error {
} }
for len(data) != 0 { for len(data) != 0 {
var chunk []byte var chunk []byte
if len(data) > spiChunkSize { if len(data) > d.maxTxSize {
chunk, data = data[:spiChunkSize], data[spiChunkSize:] chunk, data = data[:d.maxTxSize], data[d.maxTxSize:]
} else { } else {
chunk, data = data, nil chunk, data = data, nil
} }

Loading…
Cancel
Save