Added display printing util function
This commit is contained in:
parent
5e3bb96257
commit
81d154ebfa
3 changed files with 59 additions and 1 deletions
52
uncanny/display.go
Normal file
52
uncanny/display.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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=
|
||||
|
|
Loading…
Reference in a new issue