-
Notifications
You must be signed in to change notification settings - Fork 110
/
gpio.go
381 lines (335 loc) · 9.32 KB
/
gpio.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Package gpio implements a gpio/adc based input.Controller.
package gpio
import (
"context"
"fmt"
"sync"
"time"
"github.com/bep/debounce"
"github.com/pkg/errors"
"go.viam.com/utils"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/input"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
var model = resource.DefaultModelFamily.WithModel("gpio")
// Config is the overall config.
type Config struct {
Board string `json:"board"`
Buttons map[string]*ButtonConfig `json:"buttons"`
Axes map[string]*AxisConfig `json:"axes"`
}
// AxisConfig is a subconfig for axes.
type AxisConfig struct {
Control input.Control `json:"control"`
Min int `json:"min"`
Max int `json:"max"`
Bidirectional bool `json:"bidirectional"`
Deadzone int `json:"deadzone"`
MinChange int `json:"min_change"`
PollHz float64 `json:"poll_hz"`
Invert bool `json:"invert"`
}
// ButtonConfig is a subconfig for buttons.
type ButtonConfig struct {
Control input.Control `json:"control"`
Invert bool `json:"invert"`
DebounceMs int `json:"debounce_msec"` // set to -1 to disable, default=5
}
// Validate ensures all parts of the config are valid.
func (conf *Config) Validate(path string) ([]string, error) {
var deps []string
if conf.Board == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "board")
}
if len(conf.Axes) == 0 && len(conf.Buttons) == 0 {
return nil, resource.NewConfigValidationError(path, errors.New("buttons and axes cannot be both empty"))
}
deps = append(deps, conf.Board)
return deps, nil
}
func (conf *Config) validateValues() error {
for _, control := range conf.Buttons {
if control.DebounceMs == 0 {
control.DebounceMs = 5
}
}
for _, axis := range conf.Axes {
if axis.MinChange < 1 {
axis.MinChange = 1
}
if axis.PollHz <= 0 {
axis.PollHz = 10
}
if axis.Min >= axis.Max {
return fmt.Errorf("min (%d) must be less than max (%d)", axis.Min, axis.Max)
}
}
return nil
}
func init() {
resource.RegisterComponent(input.API, model, resource.Registration[input.Controller, *Config]{
Constructor: NewGPIOController,
})
}
// NewGPIOController returns a new input.Controller.
func NewGPIOController(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (input.Controller, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(ctx)
c := Controller{
Named: conf.ResourceName().AsNamed(),
logger: logger,
cancelFunc: cancel,
callbacks: map[input.Control]map[input.EventType]input.ControlFunction{},
lastEvents: map[input.Control]input.Event{},
}
if err := newConf.validateValues(); err != nil {
return nil, err
}
brd, err := board.FromDependencies(deps, newConf.Board)
if err != nil {
return nil, err
}
for interrupt, control := range newConf.Buttons {
err := c.newButton(ctx, brd, interrupt, *control)
if err != nil {
return nil, err
}
}
for reader, axis := range newConf.Axes {
err := c.newAxis(ctx, brd, reader, *axis)
if err != nil {
return nil, err
}
}
c.sendConnectionStatus(ctx, true)
return &c, nil
}
// A Controller creates an input.Controller from DigitalInterrupts and AnalogReaders.
type Controller struct {
resource.Named
resource.AlwaysRebuild
mu sync.RWMutex
controls []input.Control
lastEvents map[input.Control]input.Event
logger logging.Logger
activeBackgroundWorkers sync.WaitGroup
cancelFunc func()
callbacks map[input.Control]map[input.EventType]input.ControlFunction
}
// Controls lists the inputs.
func (c *Controller) Controls(ctx context.Context, extra map[string]interface{}) ([]input.Control, error) {
c.mu.RLock()
defer c.mu.RUnlock()
out := append([]input.Control(nil), c.controls...)
return out, nil
}
// Events returns the last input.Event (the current state) of each control.
func (c *Controller) Events(ctx context.Context, extra map[string]interface{}) (map[input.Control]input.Event, error) {
c.mu.RLock()
defer c.mu.RUnlock()
out := make(map[input.Control]input.Event)
for key, value := range c.lastEvents {
out[key] = value
}
return out, nil
}
// RegisterControlCallback registers a callback function to be executed on the specified trigger Event.
func (c *Controller) RegisterControlCallback(
ctx context.Context,
control input.Control,
triggers []input.EventType,
ctrlFunc input.ControlFunction,
extra map[string]interface{},
) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.callbacks[control] == nil {
c.callbacks[control] = make(map[input.EventType]input.ControlFunction)
}
for _, trigger := range triggers {
if trigger == input.ButtonChange {
c.callbacks[control][input.ButtonRelease] = ctrlFunc
c.callbacks[control][input.ButtonPress] = ctrlFunc
} else {
c.callbacks[control][trigger] = ctrlFunc
}
}
return nil
}
// Close terminates background worker threads.
func (c *Controller) Close(ctx context.Context) error {
c.cancelFunc()
c.activeBackgroundWorkers.Wait()
return nil
}
func (c *Controller) makeCallbacks(ctx context.Context, eventOut input.Event) {
c.mu.Lock()
c.lastEvents[eventOut.Control] = eventOut
c.mu.Unlock()
c.mu.RLock()
_, ok := c.callbacks[eventOut.Control]
c.mu.RUnlock()
if !ok {
c.mu.Lock()
c.callbacks[eventOut.Control] = make(map[input.EventType]input.ControlFunction)
c.mu.Unlock()
}
c.mu.RLock()
defer c.mu.RUnlock()
ctrlFunc, ok := c.callbacks[eventOut.Control][eventOut.Event]
if ok && ctrlFunc != nil {
c.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer c.activeBackgroundWorkers.Done()
ctrlFunc(ctx, eventOut)
})
}
ctrlFuncAll, ok := c.callbacks[eventOut.Control][input.AllEvents]
if ok && ctrlFuncAll != nil {
c.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer c.activeBackgroundWorkers.Done()
ctrlFuncAll(ctx, eventOut)
})
}
}
func (c *Controller) sendConnectionStatus(ctx context.Context, connected bool) {
evType := input.Disconnect
now := time.Now()
if connected {
evType = input.Connect
}
for _, control := range c.controls {
if c.lastEvents[control].Event != evType {
eventOut := input.Event{
Time: now,
Event: evType,
Control: control,
Value: 0,
}
c.makeCallbacks(ctx, eventOut)
}
}
}
func (c *Controller) newButton(ctx context.Context, brd board.Board, intName string, cfg ButtonConfig) error {
tickChan := make(chan board.Tick)
err := brd.StreamTicks(ctx, []string{intName}, tickChan, nil)
if err != nil {
return errors.Wrap(err, "error getting digital interrupt ticks")
}
c.activeBackgroundWorkers.Add(1)
utils.ManagedGo(func() {
// Remove the callbacks added by the interrupt stream once we are done.
defer utils.UncheckedErrorFunc(func() error { return board.RemoveCallbacks(brd, []string{intName}, tickChan) })
debounced := debounce.New(time.Millisecond * time.Duration(cfg.DebounceMs))
for {
var val bool
select {
case <-ctx.Done():
return
case tick := <-tickChan:
val = tick.High
}
if cfg.Invert {
val = !val
}
evt := input.ButtonPress
outVal := 1.0
if !val {
evt = input.ButtonRelease
outVal = 0
}
eventOut := input.Event{
Time: time.Now(),
Event: evt,
Control: cfg.Control,
Value: outVal,
}
if cfg.DebounceMs < 0 {
c.makeCallbacks(ctx, eventOut)
} else {
debounced(func() { c.makeCallbacks(ctx, eventOut) })
}
}
}, c.activeBackgroundWorkers.Done)
c.controls = append(c.controls, cfg.Control)
return nil
}
func (c *Controller) newAxis(ctx context.Context, brd board.Board, analogName string, cfg AxisConfig) error {
reader, ok := brd.AnalogReaderByName(analogName)
if !ok {
return fmt.Errorf("can't find AnalogReader (%s)", analogName)
}
c.activeBackgroundWorkers.Add(1)
utils.ManagedGo(func() {
var prevVal int
ticker := time.NewTicker(time.Second / time.Duration(cfg.PollHz))
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
rawVal, err := reader.Read(ctx, nil)
if err != nil {
c.logger.CError(ctx, err)
}
if rawVal > cfg.Max {
rawVal = cfg.Max
} else if rawVal < cfg.Min {
rawVal = cfg.Min
}
var outVal float64
if cfg.Bidirectional {
center := (cfg.Min + cfg.Max) / 2
if abs(rawVal-center) < cfg.Deadzone {
rawVal = center
outVal = 0.0
} else {
outVal = scaleAxis(rawVal, cfg.Min, cfg.Max, -1, 1)
}
} else {
if abs(rawVal-cfg.Min) < cfg.Deadzone {
rawVal = cfg.Min
}
outVal = scaleAxis(rawVal, cfg.Min, cfg.Max, 0, 1)
}
if abs(rawVal-prevVal) < cfg.MinChange {
continue
}
if cfg.Invert {
outVal *= -1
}
prevVal = rawVal
eventOut := input.Event{
Time: time.Now(),
Event: input.PositionChangeAbs,
Control: cfg.Control,
Value: outVal,
}
c.makeCallbacks(ctx, eventOut)
}
}, c.activeBackgroundWorkers.Done)
c.controls = append(c.controls, cfg.Control)
return nil
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func scaleAxis(x, inMin, inMax int, outMin, outMax float64) float64 {
return float64(x-inMin)*(outMax-outMin)/float64(inMax-inMin) + outMin
}