Limit slice range
This commit is contained in:
parent
ddb626ac4f
commit
2ca9c28c5c
1 changed files with 18 additions and 2 deletions
|
@ -6,6 +6,22 @@ import (
|
|||
"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.
|
||||
|
@ -30,8 +46,8 @@ func (c *Can) DisplayWrite(line int, a []byte) error {
|
|||
}
|
||||
// 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 < len(a) && i < 16; i += 7 {
|
||||
err := c.bus.Publish(DisplayCommand{addr, a[i:i+7]}.Encode())
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue