-
Notifications
You must be signed in to change notification settings - Fork 37
/
errorhandler.go
73 lines (60 loc) · 1.23 KB
/
errorhandler.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
// Copyright 2021 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package waveshare2in13v2
import (
"time"
"periph.io/x/conn/v3/gpio"
)
// errorHandler is a wrapper for error management.
type errorHandler struct {
d Dev
err error
}
func (eh *errorHandler) rstOut(l gpio.Level) {
if eh.err != nil {
return
}
eh.err = eh.d.rst.Out(l)
}
func (eh *errorHandler) cTx(w []byte, r []byte) {
if eh.err != nil {
return
}
eh.err = eh.d.c.Tx(w, r)
}
func (eh *errorHandler) dcOut(l gpio.Level) {
if eh.err != nil {
return
}
eh.err = eh.d.dc.Out(l)
}
func (eh *errorHandler) csOut(l gpio.Level) {
if eh.err != nil {
return
}
eh.err = eh.d.cs.Out(l)
}
func (eh *errorHandler) waitUntilIdle() {
for busy := eh.d.busy; busy.Read() == gpio.High; {
busy.WaitForEdge(100 * time.Millisecond)
}
}
func (eh *errorHandler) sendCommand(cmd byte) {
if eh.err != nil {
return
}
eh.dcOut(gpio.Low)
eh.csOut(gpio.Low)
eh.cTx([]byte{cmd}, nil)
eh.csOut(gpio.High)
}
func (eh *errorHandler) sendData(data []byte) {
if eh.err != nil {
return
}
eh.dcOut(gpio.High)
eh.csOut(gpio.Low)
eh.cTx(data, nil)
eh.csOut(gpio.High)
}