package uncanny import ( "errors" "fmt" "golang.org/x/text/encoding/charmap" ) // min returns the smaller of two integers { func min(x, y int) int { if x < y { return x } return y } // max returns the larger of two integers { func max(x, y int) int { if x > y { return x } return y } // encodeString encodes a Unicode string into a given charmap. // Unsupported characters are replaced by the default replacement symbol. // Mysteriously, this functionality is missing from x/text/encoding. // TODO Implement the custom NEC display encoding. func encodeString(s string) ([]byte) { ret := make([]byte, len(s)) for i, c := range(s) { ret[i], _ = charmap.ISO8859_1.EncodeRune(c) } return ret } func (c *Can) DisplayWrite(line int, a []byte) error { var addr int switch line { case 0: addr = 0x80 case 1: addr = 0xc0 default: return errors.New("Invalid line") } // print in separate chunks, as one frame payload is limited to 7 bytes // the display has only 16 characters per line, so we truncate there for i := 0; i < min(len(a), 16); i += 7 { err := c.bus.Publish(DisplayCommand{addr, a[i:min(i+7,len(a))]}.Encode()) if err != nil { return err } } return nil } func (c *Can) DisplayPrint(line int, a string) error { return c.DisplayWrite(line, encodeString(a)) } func (c *Can) DisplayPrintf(line int, a string, args ...interface{}) error { s := fmt.Sprintf(a, args...) return c.DisplayPrint(line, s) }