Skip to content

Commit 2e37a17

Browse files
authored
Assorted mini fixes (#8)
Device page: cursor is at first device setting on page open Device page: can go back even after config fetch failure Device page: "Addr Led" renamed to "Team Led" Device page: show device description (contains just firmware version as of FW 2.5.0) Device page: wrap cursor up and down Scan page: Ignore invalid device IDs during scan All: UI elemnts positions adjusted a bit
1 parent e885323 commit 2e37a17

File tree

4 files changed

+69
-46
lines changed

4 files changed

+69
-46
lines changed

src/device.go

+35-34
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67
"time"
78
)
89

@@ -17,8 +18,9 @@ var (
1718
var buf []byte = make([]byte, 1000)
1819

1920
type Device struct {
20-
id byte
21-
name string
21+
id byte
22+
name string
23+
description string
2224

2325
settings []Setting
2426

@@ -29,6 +31,7 @@ type Device struct {
2931
cursor int
3032
active bool
3133
changed bool
34+
failure bool
3235
}
3336

3437
func NewDevice() *Device {
@@ -44,12 +47,12 @@ func (d *Device) Open() {
4447

4548
encoder.SetChangeHandler(nil)
4649

47-
d.cursor = 4
50+
d.cursor = 0
4851
encoder.SetClickHandler(d.HandleClick)
4952

5053
display.device.ClearDisplay()
5154

52-
display.Print(10, 10, fmt.Sprintf("%X", d.id))
55+
d.ShowHeader()
5356

5457
display.Print(10, 30, "Refreshing...")
5558

@@ -59,7 +62,7 @@ func (d *Device) Open() {
5962

6063
err := d.Get(true)
6164
if err != nil {
62-
println(err.Error())
65+
d.failure = true
6366
display.Clear(10, 30, "Refreshing...")
6467
display.Print(10, 30, err.Error())
6568
display.device.Display()
@@ -72,27 +75,27 @@ func (d *Device) Open() {
7275

7376
for {
7477
if !d.active {
75-
if d.cursor == len(d.settings)+1 { // save & back
78+
if d.cursor == len(d.settings) { // save & back
7679
if d.changed {
77-
display.Clear(10, 10, fmt.Sprintf("%X !!!", d.id))
78-
display.Print(10, 10, fmt.Sprintf("%X", d.id))
80+
display.Clear(1, 10, fmt.Sprintf("%X !!!", d.id))
81+
display.Print(1, 10, fmt.Sprintf("%X", d.id))
7982
display.device.Display()
8083
err := d.Set()
8184
if err != nil {
82-
display.Print(10, 10, fmt.Sprintf("%X !!!", d.id))
85+
display.Print(1, 10, fmt.Sprintf("%X !!!", d.id))
8386
display.device.Display()
8487
d.active = true
8588
continue
8689
}
8790
}
8891
return
8992
}
90-
if d.cursor == len(d.settings)+2 { // cancel & back
93+
if d.failure || d.cursor == len(d.settings)+1 { // failure to fetch config OR cancel & back
9194
return
9295
}
9396

94-
display.Clear(10, 20+int16(d.cursor-d.scroll)*10, ">")
95-
display.Print(10, 20+int16(d.cursor-d.scroll)*10, "*")
97+
display.Clear(1, 20+int16(d.cursor-d.scroll)*10, ">")
98+
display.Print(1, 20+int16(d.cursor-d.scroll)*10, "*")
9699
display.device.Display()
97100

98101
setting := d.settings[d.cursor]
@@ -106,8 +109,8 @@ func (d *Device) Open() {
106109
}
107110
}
108111

109-
display.Clear(10, 20+int16(d.cursor-d.scroll)*10, "*")
110-
display.Print(10, 20+int16(d.cursor-d.scroll)*10, ">")
112+
display.Clear(1, 20+int16(d.cursor-d.scroll)*10, "*")
113+
display.Print(1, 20+int16(d.cursor-d.scroll)*10, ">")
111114
display.device.Display()
112115

113116
encoder.SetClickHandler(d.HandleClick)
@@ -123,7 +126,7 @@ func (d *Device) Open() {
123126
func (d *Device) Show() {
124127
display.device.ClearDisplay()
125128

126-
display.Print(10, 10, fmt.Sprintf("%X", d.id))
129+
d.ShowHeader()
127130

128131
for i, s := range d.settings {
129132
if i < d.scroll || i > d.scroll+4 {
@@ -132,15 +135,20 @@ func (d *Device) Show() {
132135
s.Show(i - d.scroll)
133136
}
134137

135-
display.Print(10, int16(20+(len(d.settings)-d.scroll)*10), " ----------------")
136-
display.Print(10, int16(20+(len(d.settings)+1-d.scroll)*10), " Save & Back")
137-
display.Print(10, int16(20+(len(d.settings)+2-d.scroll)*10), " Cancel & Back")
138+
display.Line(12, int16(20+(len(d.settings)-1-d.scroll)*10)+2, 126, int16(20+(len(d.settings)-1-d.scroll)*10)+2, WHITE)
139+
display.Print(1, int16(20+(len(d.settings)-d.scroll)*10), " Save & Back")
140+
display.Print(1, int16(20+(len(d.settings)+1-d.scroll)*10), " Cancel & Back")
138141

139-
display.Print(10, 20+int16(d.cursor-d.scroll)*10, ">")
142+
display.Print(1, 20+int16(d.cursor-d.scroll)*10, ">")
140143

141144
display.device.Display()
142145
}
143146

147+
func (d *Device) ShowHeader() {
148+
display.Print(1, 6, fmt.Sprintf("%-10s %10s", d.name, strings.TrimSpace(d.description)))
149+
display.Line(1, 11, 126, 11, WHITE)
150+
}
151+
144152
func (d *Device) HandleClick() {
145153
d.active = false
146154
}
@@ -149,22 +157,15 @@ func (d *Device) HandleChange(value int) int {
149157
orig := d.cursor
150158

151159
d.cursor = value
152-
if d.cursor < 0 {
153-
d.cursor = 0
160+
if d.cursor < 0 { // wrap up
161+
d.cursor = len(d.settings) + 1
154162
}
155-
if d.cursor == len(d.settings) { // divider
156-
if orig < d.cursor {
157-
d.cursor++
158-
} else {
159-
d.cursor--
160-
}
161-
}
162-
if d.cursor > len(d.settings)+2 {
163-
d.cursor = len(d.settings) + 2
163+
if d.cursor > len(d.settings)+1 { // wrap down
164+
d.cursor = 0
164165
}
165166
scrollChanged := false
166167
if d.cursor-d.scroll < 0 {
167-
d.scroll--
168+
d.scroll -= d.scroll - d.cursor
168169
scrollChanged = true
169170
}
170171
if d.cursor-d.scroll > 4 {
@@ -176,8 +177,8 @@ func (d *Device) HandleChange(value int) int {
176177
d.Show()
177178
} else {
178179
if orig != d.cursor {
179-
display.Clear(10, 20+int16(orig-d.scroll)*10, ">")
180-
display.Print(10, 20+int16(d.cursor-d.scroll)*10, ">")
180+
display.Clear(1, 20+int16(orig-d.scroll)*10, ">")
181+
display.Print(1, 20+int16(d.cursor-d.scroll)*10, ">")
181182
display.device.Display()
182183
}
183184
}
@@ -381,7 +382,7 @@ func (d *Device) Get(send bool) error {
381382
len: 1,
382383
kind: SettingKindByte,
383384
show: SettingShowDec,
384-
title: "Addr Led",
385+
title: "Team Led",
385386
positionOffset: 20,
386387
})
387388

src/display.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ func (d *Display) Clear(x, y int16, message string) {
4040
tinyfont.WriteLineRotated(&d.device, &proggy.TinySZ8pt7b, x, y, message, BLACK, tinyfont.NO_ROTATION)
4141
}
4242

43-
func (d *Display) Rect(x, y, w, h int16, color color.RGBA) {
43+
func (d *Display) Fill(x, y, w, h int16, color color.RGBA) {
4444
tinydraw.FilledRectangle(&d.device, x, y, w, h, color)
4545
}
46+
47+
func (d *Display) Rect(x, y, w, h int16, color color.RGBA) {
48+
tinydraw.Rectangle(&d.device, x, y, w, h, color)
49+
}
50+
51+
func (d *Display) Line(x0, y0, x1, y1 int16, color color.RGBA) {
52+
tinydraw.Line(&d.device, x0, y0, x1, y1, color)
53+
}

src/scan.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,21 @@ func (s *Scan) Open() {
8181
func (s *Scan) Show() {
8282
if s.redraw {
8383
display.device.ClearDisplay()
84+
85+
display.Print(1, 6, "Scanning...")
86+
display.Line(1, 11, 126, 11, WHITE)
87+
8488
for i, dev := range s.devices {
8589
// fmt.Printf("%X | '%s'\r\n", dev.id, string(dev.name))
86-
display.Print(10, 10+int16(i)*10, fmt.Sprintf(" %X | %s ", dev.id, dev.name))
90+
display.Print(1, 20+int16(i)*10, fmt.Sprintf(" %X | %s ", dev.id, dev.name))
8791
}
88-
display.Print(10, 10+int16(s.cursor)*10, ">")
92+
display.Print(1, 20+int16(s.cursor)*10, ">")
8993
s.redraw = false
9094
display.device.Display()
9195
}
9296
if s.cursorPrev != s.cursor {
93-
display.Clear(10, 10+int16(s.cursorPrev)*10, ">")
94-
display.Print(10, 10+int16(s.cursor)*10, ">")
97+
display.Clear(1, 20+int16(s.cursorPrev)*10, ">")
98+
display.Print(1, 20+int16(s.cursor)*10, ">")
9599
s.cursorPrev = s.cursor
96100
display.device.Display()
97101
}
@@ -103,19 +107,28 @@ func (s *Scan) Receive() {
103107
}
104108
key := byte(0)
105109
name := make([]byte, 10)
110+
desc := make([]byte, 10)
106111
for i := 0; i < 22; i++ {
107112
b, err := serial.uart.ReadByte()
108113
if err != nil {
109114
return
110115
}
111-
if i == 0 && 0xA0 < b && b < 0xFF {
116+
switch {
117+
case i == 0:
118+
if !(0xA0 < b && b < 0xFF) {
119+
return
120+
}
112121
key = b
113-
}
114-
if 1 < i && i < 12 {
122+
case 1 < i && i < 12:
115123
if b == 0x2F {
116124
b = 0x20
117125
}
118126
name[i-2] = b
127+
case 11 < i && i < 22:
128+
if b == 0x2F {
129+
b = 0x20
130+
}
131+
desc[i-12] = b
119132
}
120133
}
121134
found := false
@@ -129,6 +142,7 @@ func (s *Scan) Receive() {
129142
device := NewDevice()
130143
device.id = key
131144
device.name = string(name)
145+
device.description = string(desc)
132146
s.devices = append(s.devices, device)
133147
s.redraw = true
134148
}

src/setting.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ func (s *Setting) Open(position int) {
5959
}
6060

6161
func (s *Setting) Show(position int) {
62-
display.Rect(30, int16(s.positionOffset+10*(position-1)+3), 100, 10, BLACK)
62+
display.Fill(20, int16(s.positionOffset+10*(position-1)+3), 128, 10, BLACK)
6363
switch s.show {
6464
case SettingShowDec:
65-
display.Print(10, int16(s.positionOffset+10*position), fmt.Sprintf(" %s: %d", s.title, s.value[0])) // TODO support global scroll
65+
display.Print(1, int16(s.positionOffset+10*position), fmt.Sprintf(" %s: %d", s.title, s.value[0])) // TODO support global scroll
6666
case SettingShowHex:
67-
display.Print(10, int16(s.positionOffset+10*position), fmt.Sprintf(" %s: %X", s.title, s.value[0])) // TODO support global scroll
67+
display.Print(1, int16(s.positionOffset+10*position), fmt.Sprintf(" %s: %X", s.title, s.value[0])) // TODO support global scroll
6868
case SettingShowChar:
69-
display.Print(10, int16(s.positionOffset+10*position), fmt.Sprintf(" %s: %s", s.title, string(s.value))) // TODO support global scroll
69+
display.Print(1, int16(s.positionOffset+10*position), fmt.Sprintf(" %s: %s", s.title, string(s.value))) // TODO support global scroll
7070
}
7171
display.device.Display()
7272
}

0 commit comments

Comments
 (0)