@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "strings"
6
7
"time"
7
8
)
8
9
17
18
var buf []byte = make ([]byte , 1000 )
18
19
19
20
type Device struct {
20
- id byte
21
- name string
21
+ id byte
22
+ name string
23
+ description string
22
24
23
25
settings []Setting
24
26
@@ -29,6 +31,7 @@ type Device struct {
29
31
cursor int
30
32
active bool
31
33
changed bool
34
+ failure bool
32
35
}
33
36
34
37
func NewDevice () * Device {
@@ -44,12 +47,12 @@ func (d *Device) Open() {
44
47
45
48
encoder .SetChangeHandler (nil )
46
49
47
- d .cursor = 4
50
+ d .cursor = 0
48
51
encoder .SetClickHandler (d .HandleClick )
49
52
50
53
display .device .ClearDisplay ()
51
54
52
- display . Print ( 10 , 10 , fmt . Sprintf ( "%X" , d . id ) )
55
+ d . ShowHeader ( )
53
56
54
57
display .Print (10 , 30 , "Refreshing..." )
55
58
@@ -59,7 +62,7 @@ func (d *Device) Open() {
59
62
60
63
err := d .Get (true )
61
64
if err != nil {
62
- println ( err . Error ())
65
+ d . failure = true
63
66
display .Clear (10 , 30 , "Refreshing..." )
64
67
display .Print (10 , 30 , err .Error ())
65
68
display .device .Display ()
@@ -72,27 +75,27 @@ func (d *Device) Open() {
72
75
73
76
for {
74
77
if ! d .active {
75
- if d .cursor == len (d .settings )+ 1 { // save & back
78
+ if d .cursor == len (d .settings ) { // save & back
76
79
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 ))
79
82
display .device .Display ()
80
83
err := d .Set ()
81
84
if err != nil {
82
- display .Print (10 , 10 , fmt .Sprintf ("%X !!!" , d .id ))
85
+ display .Print (1 , 10 , fmt .Sprintf ("%X !!!" , d .id ))
83
86
display .device .Display ()
84
87
d .active = true
85
88
continue
86
89
}
87
90
}
88
91
return
89
92
}
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
91
94
return
92
95
}
93
96
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 , "*" )
96
99
display .device .Display ()
97
100
98
101
setting := d .settings [d .cursor ]
@@ -106,8 +109,8 @@ func (d *Device) Open() {
106
109
}
107
110
}
108
111
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 , ">" )
111
114
display .device .Display ()
112
115
113
116
encoder .SetClickHandler (d .HandleClick )
@@ -123,7 +126,7 @@ func (d *Device) Open() {
123
126
func (d * Device ) Show () {
124
127
display .device .ClearDisplay ()
125
128
126
- display . Print ( 10 , 10 , fmt . Sprintf ( "%X" , d . id ) )
129
+ d . ShowHeader ( )
127
130
128
131
for i , s := range d .settings {
129
132
if i < d .scroll || i > d .scroll + 4 {
@@ -132,15 +135,20 @@ func (d *Device) Show() {
132
135
s .Show (i - d .scroll )
133
136
}
134
137
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" )
138
141
139
- display .Print (10 , 20 + int16 (d .cursor - d .scroll )* 10 , ">" )
142
+ display .Print (1 , 20 + int16 (d .cursor - d .scroll )* 10 , ">" )
140
143
141
144
display .device .Display ()
142
145
}
143
146
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
+
144
152
func (d * Device ) HandleClick () {
145
153
d .active = false
146
154
}
@@ -149,22 +157,15 @@ func (d *Device) HandleChange(value int) int {
149
157
orig := d .cursor
150
158
151
159
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
154
162
}
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
164
165
}
165
166
scrollChanged := false
166
167
if d .cursor - d .scroll < 0 {
167
- d .scroll --
168
+ d .scroll -= d . scroll - d . cursor
168
169
scrollChanged = true
169
170
}
170
171
if d .cursor - d .scroll > 4 {
@@ -176,8 +177,8 @@ func (d *Device) HandleChange(value int) int {
176
177
d .Show ()
177
178
} else {
178
179
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 , ">" )
181
182
display .device .Display ()
182
183
}
183
184
}
@@ -381,7 +382,7 @@ func (d *Device) Get(send bool) error {
381
382
len : 1 ,
382
383
kind : SettingKindByte ,
383
384
show : SettingShowDec ,
384
- title : "Addr Led" ,
385
+ title : "Team Led" ,
385
386
positionOffset : 20 ,
386
387
})
387
388
0 commit comments