-
Notifications
You must be signed in to change notification settings - Fork 110
/
board.go
180 lines (159 loc) · 5.64 KB
/
board.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
package inject
import (
"context"
commonpb "go.viam.com/api/common/v1"
"go.viam.com/utils"
"go.viam.com/rdk/components/board"
)
// Board is an injected board.
type Board struct {
board.LocalBoard
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
SPIByNameFunc func(name string) (board.SPI, bool)
spiByNameCap []interface{}
I2CByNameFunc func(name string) (board.I2C, bool)
i2cByNameCap []interface{}
AnalogReaderByNameFunc func(name string) (board.AnalogReader, bool)
analogReaderByNameCap []interface{}
DigitalInterruptByNameFunc func(name string) (board.DigitalInterrupt, bool)
digitalInterruptByNameCap []interface{}
GPIOPinByNameFunc func(name string) (board.GPIOPin, error)
gpioPinByNameCap []interface{}
SPINamesFunc func() []string
I2CNamesFunc func() []string
AnalogReaderNamesFunc func() []string
DigitalInterruptNamesFunc func() []string
GPIOPinNamesFunc func() []string
CloseFunc func(ctx context.Context) error
StatusFunc func(ctx context.Context, extra map[string]interface{}) (*commonpb.BoardStatus, error)
statusCap []interface{}
}
// SPIByName calls the injected SPIByName or the real version.
func (b *Board) SPIByName(name string) (board.SPI, bool) {
b.spiByNameCap = []interface{}{name}
if b.SPIByNameFunc == nil {
return b.LocalBoard.SPIByName(name)
}
return b.SPIByNameFunc(name)
}
// I2CByName calls the injected I2CByName or the real version.
func (b *Board) I2CByName(name string) (board.I2C, bool) {
b.i2cByNameCap = []interface{}{name}
if b.I2CByNameFunc == nil {
return b.LocalBoard.I2CByName(name)
}
return b.I2CByNameFunc(name)
}
// AnalogReaderByName calls the injected AnalogReaderByName or the real version.
func (b *Board) AnalogReaderByName(name string) (board.AnalogReader, bool) {
b.analogReaderByNameCap = []interface{}{name}
if b.AnalogReaderByNameFunc == nil {
return b.LocalBoard.AnalogReaderByName(name)
}
return b.AnalogReaderByNameFunc(name)
}
// AnalogReaderByNameCap returns the last parameters received by AnalogReaderByName, and then clears them.
func (b *Board) AnalogReaderByNameCap() []interface{} {
if b == nil {
return nil
}
defer func() { b.analogReaderByNameCap = nil }()
return b.analogReaderByNameCap
}
// DigitalInterruptByName calls the injected DigitalInterruptByName or the real version.
func (b *Board) DigitalInterruptByName(name string) (board.DigitalInterrupt, bool) {
b.digitalInterruptByNameCap = []interface{}{name}
if b.DigitalInterruptByNameFunc == nil {
return b.LocalBoard.DigitalInterruptByName(name)
}
return b.DigitalInterruptByNameFunc(name)
}
// DigitalInterruptByNameCap returns the last parameters received by DigitalInterruptByName, and then clears them.
func (b *Board) DigitalInterruptByNameCap() []interface{} {
if b == nil {
return nil
}
defer func() { b.digitalInterruptByNameCap = nil }()
return b.digitalInterruptByNameCap
}
// GPIOPinByName calls the injected GPIOPinByName or the real version.
func (b *Board) GPIOPinByName(name string) (board.GPIOPin, error) {
b.gpioPinByNameCap = []interface{}{name}
if b.GPIOPinByNameFunc == nil {
return b.LocalBoard.GPIOPinByName(name)
}
return b.GPIOPinByNameFunc(name)
}
// GPIOPinByNameCap returns the last parameters received by GPIOPinByName, and then clears them.
func (b *Board) GPIOPinByNameCap() []interface{} {
if b == nil {
return nil
}
defer func() { b.gpioPinByNameCap = nil }()
return b.gpioPinByNameCap
}
// SPINames calls the injected SPINames or the real version.
func (b *Board) SPINames() []string {
if b.SPINamesFunc == nil {
return b.LocalBoard.SPINames()
}
return b.SPINamesFunc()
}
// I2CNames calls the injected SPINames or the real version.
func (b *Board) I2CNames() []string {
if b.I2CNamesFunc == nil {
return b.LocalBoard.I2CNames()
}
return b.I2CNamesFunc()
}
// AnalogReaderNames calls the injected AnalogReaderNames or the real version.
func (b *Board) AnalogReaderNames() []string {
if b.AnalogReaderNamesFunc == nil {
return b.LocalBoard.AnalogReaderNames()
}
return b.AnalogReaderNamesFunc()
}
// DigitalInterruptNames calls the injected DigitalInterruptNames or the real version.
func (b *Board) DigitalInterruptNames() []string {
if b.DigitalInterruptNamesFunc == nil {
return b.LocalBoard.DigitalInterruptNames()
}
return b.DigitalInterruptNamesFunc()
}
// GPIOPinNames calls the injected GPIOPinNames or the real version.
func (b *Board) GPIOPinNames() []string {
if b.GPIOPinNamesFunc == nil {
return b.LocalBoard.GPIOPinNames()
}
return b.GPIOPinNamesFunc()
}
// Close calls the injected Close or the real version.
func (b *Board) Close(ctx context.Context) error {
if b.CloseFunc == nil {
return utils.TryClose(ctx, b.LocalBoard)
}
return b.CloseFunc(ctx)
}
// Status calls the injected Status or the real version.
func (b *Board) Status(ctx context.Context, extra map[string]interface{}) (*commonpb.BoardStatus, error) {
b.statusCap = []interface{}{ctx}
if b.StatusFunc == nil {
return b.LocalBoard.Status(ctx, extra)
}
return b.StatusFunc(ctx, extra)
}
// StatusCap returns the last parameters received by Status, and then clears them.
func (b *Board) StatusCap() []interface{} {
if b == nil {
return nil
}
defer func() { b.statusCap = nil }()
return b.statusCap
}
// DoCommand calls the injected DoCommand or the real version.
func (b *Board) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
if b.DoFunc == nil {
return b.LocalBoard.DoCommand(ctx, cmd)
}
return b.DoFunc(ctx, cmd)
}