Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions plugin/gpio_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ type gpio struct {
// NewGpioPluginFromConfig creates a GPIO provider
func NewGpioPluginFromConfig(ctx context.Context, other map[string]any) (Plugin, error) {
cc := struct {
Function GpioType
Pin int
Chip string
Function GpioType
Pin int
ActiveLow bool
Bias GpioBias
Chip string
}{
Chip: "gpiochip0",
ActiveLow: false,
Bias: -1,
Chip: "gpiochip0",
}

if err := util.DecodeOther(other, &cc); err != nil {
Expand All @@ -38,13 +42,34 @@ func NewGpioPluginFromConfig(ctx context.Context, other map[string]any) (Plugin,
var opts []gpiocdev.LineReqOption
switch cc.Function {
case GpioTypeRead:
opts = append(opts, gpiocdev.AsInput, gpiocdev.WithPullUp)
opts = append(opts, gpiocdev.AsInput)
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
case GpioTypeWrite:
Comment on lines 42 to 46
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the default input configuration: previously read always requested the line with an internal pull-up, but now the default is Bias: as-is (no pull-up unless configured). That’s a behavior/backwards-compat change and can lead to floating inputs for common “switch to GND” wiring (especially when paired with activeLow). Consider keeping the previous default (pull-up for read) and only overriding it when bias is explicitly configured (or at least documenting this breaking behavior).

Copilot uses AI. Check for mistakes.
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:
Comment on lines +52 to +65
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Consider whether bias options should be applied for output lines as well as inputs.

The shared opts slice applies the selected bias to both GpioTypeRead and GpioTypeWrite. Depending on gpiocdev and the hardware, some biases may be invalid or ignored for outputs. Please confirm whether biases are intended for outputs as well; if not, consider applying them only for GpioTypeRead or constraining allowed bias values per function type.

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)
Expand Down
11 changes: 11 additions & 0 deletions plugin/gpiobias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package plugin

type GpioBias int

//go:generate go tool enumer -type GpioBias -trimprefix GpioBias -transform=kebab -text
const (
GpioBiasAsIs GpioBias = iota
GpioBiasDisabled
GpioBiasPullUp
GpioBiasPullDown
)
98 changes: 98 additions & 0 deletions plugin/gpiobias_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading