-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathgpio_linux.go
More file actions
128 lines (105 loc) Β· 2.48 KB
/
gpio_linux.go
File metadata and controls
128 lines (105 loc) Β· 2.48 KB
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
//go:build linux
package plugin
import (
"context"
"fmt"
"sync"
"github.com/evcc-io/evcc/util"
"github.com/warthog618/go-gpiocdev"
)
func init() {
registry.AddCtx("gpio", NewGpioPluginFromConfig)
}
type gpio struct {
mu sync.Mutex
typ GpioType
line *gpiocdev.Line
}
// NewGpioPluginFromConfig creates a GPIO provider
func NewGpioPluginFromConfig(ctx context.Context, other map[string]any) (Plugin, error) {
cc := struct {
Function GpioType
Pin int
ActiveLow bool
Bias GpioBias
Chip string
}{
ActiveLow: false,
Bias: -1,
Chip: "gpiochip0",
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
var opts []gpiocdev.LineReqOption
switch cc.Function {
case GpioTypeRead:
opts = append(opts, gpiocdev.AsInput)
case GpioTypeWrite:
opts = append(opts, gpiocdev.AsOutput(0))
default:
return nil, fmt.Errorf("invalid type: %s", cc.Function)
}
switch cc.Bias {
case -1:
if cc.Function == GpioTypeRead {
opts = append(opts, gpiocdev.WithPullUp)
}
case GpioBiasAsIs:
opts = append(opts, gpiocdev.WithBiasAsIs)
case GpioBiasDisabled:
opts = append(opts, gpiocdev.WithBiasDisabled)
case GpioBiasPullUp:
opts = append(opts, gpiocdev.WithPullUp)
case GpioBiasPullDown:
opts = append(opts, gpiocdev.WithPullDown)
default:
return nil, fmt.Errorf("invalid bias: %s", cc.Bias)
}
if cc.ActiveLow {
opts = append(opts, gpiocdev.AsActiveLow)
}
line, err := gpiocdev.RequestLine(cc.Chip, cc.Pin, opts...)
if err != nil {
return nil, fmt.Errorf("failed to open GPIO: %w", err)
}
return &gpio{
typ: cc.Function,
line: line,
}, nil
}
var _ BoolGetter = (*gpio)(nil)
// BoolGetter returns GPIO pin active
func (p *gpio) BoolGetter() (func() (bool, error), error) {
if p.typ != GpioTypeRead {
return nil, fmt.Errorf("invalid gpio type: %s", p.typ)
}
return func() (bool, error) {
p.mu.Lock()
defer p.mu.Unlock()
val, err := p.line.Value()
if err != nil {
return false, fmt.Errorf("failed to read GPIO: %w", err)
}
return val != 0, nil
}, nil
}
var _ BoolSetter = (*gpio)(nil)
// BoolSetter returns GPIO pin active
func (p *gpio) BoolSetter(_ string) (func(bool) error, error) {
if p.typ != GpioTypeWrite {
return nil, fmt.Errorf("invalid gpio type: %s", p.typ)
}
return func(b bool) error {
p.mu.Lock()
defer p.mu.Unlock()
val := 0
if b {
val = 1
}
if err := p.line.SetValue(val); err != nil {
return fmt.Errorf("failed to write GPIO: %w", err)
}
return nil
}, nil
}