Skip to content

Commit d5a69db

Browse files
committed
GPIO: Add support for active low
“Active low” pins have inverted state, i.e. they're “true” or 1 when they're pulled to the ground and “false”/0 when left floating around VCC. This is very common configuration when building interfaces with semiconductors (like optocouplers), less so with relays where the polarity mostly doesn't matter. Signed-off-by: Wojciech Porczyk <wojciech.porczyk@connectpoint.pl>
1 parent dba93a9 commit d5a69db

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

plugin/gpio_linux.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ type gpio struct {
2424
// NewGpioPluginFromConfig creates a GPIO provider
2525
func NewGpioPluginFromConfig(ctx context.Context, other map[string]any) (Plugin, error) {
2626
cc := struct {
27-
Function GpioType
28-
Pin int
29-
Chip string
27+
Function GpioType
28+
Pin int
29+
ActiveLow bool
30+
Chip string
3031
}{
31-
Chip: "gpiochip0",
32+
ActiveLow: false,
33+
Chip: "gpiochip0",
3234
}
3335

3436
if err := util.DecodeOther(other, &cc); err != nil {
@@ -38,13 +40,19 @@ func NewGpioPluginFromConfig(ctx context.Context, other map[string]any) (Plugin,
3840
var opts []gpiocdev.LineReqOption
3941
switch cc.Function {
4042
case GpioTypeRead:
41-
opts = append(opts, gpiocdev.AsInput, gpiocdev.WithPullUp)
43+
opts = append(opts, gpiocdev.AsInput)
4244
case GpioTypeWrite:
4345
opts = append(opts, gpiocdev.AsOutput(0))
4446
default:
4547
return nil, fmt.Errorf("invalid type: %s", cc.Function)
4648
}
4749

50+
if cc.ActiveLow {
51+
opts = append(opts, gpiocdev.WithPullUp, gpiocdev.AsActiveLow)
52+
} else {
53+
opts = append(opts, gpiocdev.WithPullDown)
54+
}
55+
4856
line, err := gpiocdev.RequestLine(cc.Chip, cc.Pin, opts...)
4957
if err != nil {
5058
return nil, fmt.Errorf("failed to open GPIO: %w", err)

0 commit comments

Comments
 (0)