From 81d154ebfa8abe5ed3f7d5f64d00618677cea61a Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Wed, 5 May 2021 22:34:25 +0200 Subject: [PATCH] Added display printing util function --- uncanny/display.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++ uncanny/go.mod | 5 ++++- uncanny/go.sum | 3 +++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 uncanny/display.go diff --git a/uncanny/display.go b/uncanny/display.go new file mode 100644 index 0000000..42e0cfe --- /dev/null +++ b/uncanny/display.go @@ -0,0 +1,52 @@ +package uncanny + +import ( + "errors" + "fmt" + "golang.org/x/text/encoding/charmap" +) + +// 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 3 chunks, as one frame is limited to 8 bytes + // the display has only 16 characters per line, so we truncate there + if len(a) > 0 { + return c.bus.Publish(DisplayCommand{addr, a[:7]}.Encode()) + } + if len(a) > 7 { + return c.bus.Publish(DisplayCommand{addr, a[7:14]}.Encode()) + } + if len(a) > 14 { + return c.bus.Publish(DisplayCommand{addr, a[14:16]}.Encode()) + } + 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) +} diff --git a/uncanny/go.mod b/uncanny/go.mod index a708f20..af21f0c 100644 --- a/uncanny/go.mod +++ b/uncanny/go.mod @@ -2,4 +2,7 @@ module git.kabelsalat.ch/ccc-basel/matemat go 1.11 -require github.com/brutella/can v0.0.1 +require ( + github.com/brutella/can v0.0.1 + golang.org/x/text v0.3.6 +) diff --git a/uncanny/go.sum b/uncanny/go.sum index 9cde019..7854855 100644 --- a/uncanny/go.sum +++ b/uncanny/go.sum @@ -2,3 +2,6 @@ github.com/brutella/can v0.0.1 h1:Rz+2Zuje3NT79daon8wPN9+VphH3/kl1DP8Dhf/k1NI= github.com/brutella/can v0.0.1/go.mod h1:NYDxbQito3w4+4DcjWs/fpQ3xyaFdpXw/KYqtZFU98k= golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 h1:0oC8rFnE+74kEmuHZ46F6KHsMr5Gx2gUQPuNz28iQZM= golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=