-
Notifications
You must be signed in to change notification settings - Fork 1
/
asustor.go
339 lines (298 loc) · 6.46 KB
/
asustor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Implements the serial communication protocol for the ASUSTOR
LCD display. This includes controlling and updating and listening for
button presses.
asustor data format:
MESSAGE_TYPE DATA_LENGTH COMMAND [[DATA]...] [CRC]
*/
package display
import (
"bytes"
"errors"
"github.com/chmorgan/go-serial2/serial"
"io"
"sync"
"time"
)
// we hide the struct and its fields
// to keep the usage as simple as possible
// through the LCD interface
type asustor struct {
con io.ReadWriteCloser
readC chan []byte
btnC chan []byte
tty string
open bool
keepListening bool
m sync.Mutex
retry byte
// to keep track of the 10ms
// we have to wait for to be flushed
lastFlush time.Time
// keep the fields packed inside the struct
// to simplify the implementation of other
// displays on the package level
// and to prevent from reserving memory for
// unused package fields
cmdByte byte
replyByte byte
cmdDisplayStatus []byte
cmdDisplayOff []byte
cmdClearDisplay []byte
cmdDisplayOn []byte
cmdBtn []byte
cmdRdy []byte
cmdOkayCheck []byte
replyRdy []byte
replyMsgSentCheck []byte
}
/**
Supports the display of the following devices:
Asustor AS6404T
... add more ..
The constructor is responsible for init and probe.
To simplify and unify the use of future displays.
*/
func NewAsustorLCD(tty string) (LCD, error) {
if tty == "" {
tty = DefaultTTy
}
cmdByte := byte(240)
replyByte := byte(241)
m := &asustor{
tty: tty,
readC: make(chan []byte, 100),
btnC: make(chan []byte, 100),
cmdByte: cmdByte,
replyByte: replyByte,
cmdDisplayStatus: []byte{cmdByte, 1, 17, 1},
cmdDisplayOff: []byte{cmdByte, 1, 17, 0},
cmdClearDisplay: []byte{cmdByte, 1, 18, 1},
cmdDisplayOn: []byte{cmdByte, 1, 34, 0},
cmdBtn: []byte{cmdByte, 1, 128},
replyRdy: []byte{replyByte, 1},
replyMsgSentCheck: []byte{replyByte, 1, 39, 0, 25},
}
// initial check if we can connect to a device
// that works our way
err := m.Open()
// return only an error as the display
// can't be controlled by this implementation
if err != nil {
return nil, err
}
return m, err
}
func (a *asustor) Open() error {
a.m.Lock()
defer a.m.Unlock()
if a.open {
return nil
}
var err error
if a.con != nil {
_ = a.con.Close()
}
a.con, err = serial.Open(serial.OpenOptions{
PortName: a.tty,
BaudRate: 115200,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 1,
})
if err != nil {
return err
}
a.open = true
go a.read()
return a.establish()
}
func (a *asustor) establish() error {
err := a.flush(a.cmdDisplayStatus)
if err != nil {
_ = a.con.Close()
_ = a.forceClose()
return err
}
if !a.responseEqual(true, a.replyRdy) {
_ = a.con.Close()
_ = a.forceClose()
return ErrDisplayNotWorking
}
return nil
}
// Write messages to the display. Note that checksum is omitted,
// this is handled by the implementation.
// If text is longer than supported, it will be cut.
func (a *asustor) Write(line Line, text string) error {
a.m.Lock()
defer a.m.Unlock()
return a.write(a.strToBytes(line, text))
}
func (a *asustor) Enable(yes bool) error {
a.m.Lock()
defer a.m.Unlock()
if !a.open {
return ErrClosed
}
if yes {
return a.flush(a.cmdDisplayOn)
} else {
return a.flush(a.cmdDisplayOff)
}
}
func (a *asustor) Listen(l func(btn int, released bool) bool) {
if !a.open {
return
}
a.keepListening = true
for a.open {
res := <-a.btnC
if !a.open {
return
}
if a.keepListening {
if !l(int(res[3]), true) {
a.keepListening = false
return
}
}
}
}
func (a *asustor) write(msg []byte) error {
if !a.open {
return ErrClosed
}
err := a.flush(msg)
if err != nil {
return err
}
if !a.responseEqual(false, a.replyMsgSentCheck) {
if a.retry > 10 {
return ErrDisplayNotWorking
} else {
a.retry++
//log.Println("try", a.retry)
return a.write(msg)
}
} else {
a.retry = 0
}
return err
}
func (a *asustor) responseEqual(hasPrefix bool, checks ...[]byte) bool {
ch := make(chan bool, 1)
go func() {
select {
case res := <-a.readC:
if !a.open {
ch <- false
return
}
for _, check := range checks {
if hasPrefix {
if bytes.HasPrefix(res, check) {
//log.Println("msg check OK!")
ch <- true
return
}
} else {
if bytes.Equal(res, check) {
//log.Println("msg check OK!")
ch <- true
return
}
}
}
ch <- false
case <-time.After(40 * time.Millisecond):
ch <- false
}
}()
return <-ch
}
// read reads asynchronously from the serial port
// and transmits messages on the read or btn channel.
func (a *asustor) read() {
buf := bytes.Buffer{}
startFound := false
res := make([]byte, 20)
for a.open {
i, er := a.con.Read(res)
if er != nil || !a.open {
return
}
for c := 0; c < i; c++ {
if startFound || res[c] == a.replyByte || res[c] == a.cmdByte {
startFound = true
buf.WriteByte(res[c])
if buf.Len() == 5 {
startFound = false
a.pass(buf.Bytes())
buf.Reset()
}
}
}
}
}
func (a *asustor) pass(res []byte) {
//log.Println("read", res)
if bytes.HasPrefix(res, a.cmdBtn) {
a.btnC <- res
} else {
a.readC <- res
}
}
// write synchronously to the serial port.
func (a *asustor) flush(data []byte) error {
data = a.makemsg(data)
a.waitForFlushBetweenWrites()
n, err := a.con.Write(data)
if err != nil {
return err
}
if n != len(data) {
return errors.New("written size does not match")
}
return err
}
func (a *asustor) makemsg(msg []byte) []byte {
data := make([]byte, len(msg), len(msg)+1)
copy(data, msg)
data = append(data, checksum(data))
return data
}
func (a *asustor) waitForFlushBetweenWrites() {
timeDiff := a.lastFlush.Add(10 * time.Millisecond).Sub(time.Now())
if timeDiff > 0 {
time.Sleep(timeDiff)
}
a.lastFlush = time.Now()
}
func checksum(b []byte) (s byte) {
for _, bb := range b {
s += bb
}
return s
}
func (a *asustor) strToBytes(line Line, text string) []byte {
return a.createMsg(line, []byte(prepareTxt(text)))
}
func (a *asustor) createMsg(line Line, text []byte) []byte {
return append([]byte{a.cmdByte, 0x12, 0x27, byte(line), byte(0)}, text...)
}
// Close the serial connection.
func (a *asustor) Close() error {
a.m.Lock()
defer a.m.Unlock()
if !a.open {
return nil
}
return a.forceClose()
}
func (a *asustor) forceClose() error {
a.open = false
a.readC <- []byte{}
a.btnC <- []byte{}
return a.con.Close()
}