-
Notifications
You must be signed in to change notification settings - Fork 110
/
analog.go
52 lines (45 loc) · 1.35 KB
/
analog.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
package inject
import (
"context"
"go.viam.com/rdk/components/board"
)
// Analog is an injected analog pin.
type Analog struct {
board.Analog
ReadFunc func(ctx context.Context, extra map[string]interface{}) (board.AnalogValue, error)
readCap []interface{}
WriteFunc func(ctx context.Context, value int, extra map[string]interface{}) error
writeCap []interface{}
}
// Read calls the injected Read or the real version.
func (a *Analog) Read(ctx context.Context, extra map[string]interface{}) (board.AnalogValue, error) {
a.readCap = []interface{}{ctx}
if a.ReadFunc == nil {
return a.Analog.Read(ctx, extra)
}
return a.ReadFunc(ctx, extra)
}
// ReadCap returns the last parameters received by Read, and then clears them.
func (a *Analog) ReadCap() []interface{} {
if a == nil {
return nil
}
defer func() { a.readCap = nil }()
return a.readCap
}
// Write calls the injected Write or the real version.
func (a *Analog) Write(ctx context.Context, value int, extra map[string]interface{}) error {
a.writeCap = []interface{}{ctx, value}
if a.WriteFunc == nil {
return a.Analog.Write(ctx, value, extra)
}
return a.WriteFunc(ctx, value, extra)
}
// WriteCap returns the last parameters received by Write, and then clears them.
func (a *Analog) WriteCap() []interface{} {
if a == nil {
return nil
}
defer func() { a.writeCap = nil }()
return a.writeCap
}