Skip to content

Commit 4cf35d2

Browse files
authored
Openwb-native: fix gpio open/close (#25358)
1 parent c960771 commit 4cf35d2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

charger/openwb-native_linux.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,24 @@ func NewOpenWbNative(ctx context.Context, uri, device, comset string, baudrate i
104104
identify = wb.identify
105105
}
106106

107-
// set pins to output
107+
// initialize GPIO and set pins to output
108+
if err := rpio.Open(); err != nil {
109+
return nil, fmt.Errorf("failed to open GPIO: %w", err)
110+
}
111+
108112
for _, pin := range structs.Fields(native.ChargePoints[connector-1]) {
109113
rpio.Pin(pin.Value().(int)).Output()
110114
}
111115

116+
// close GPIO when context is cancelled
117+
go func() {
118+
<-ctx.Done()
119+
log.DEBUG.Println("context canceled, closing GPIO")
120+
if err := rpio.Close(); err != nil {
121+
log.ERROR.Printf("error closing GPIO: %v", err)
122+
}
123+
}()
124+
112125
return decorateOpenWbNative(wb, phases1p3p, identify), nil
113126
}
114127

@@ -151,11 +164,6 @@ func (wb *OpenWbNative) runGpioSequence(seq []gpioAction) error {
151164
return err
152165
}
153166

154-
if err := rpio.Open(); err != nil {
155-
return err
156-
}
157-
defer rpio.Close()
158-
159167
for _, a := range seq {
160168
a.pin()
161169
if a.delay > 0 {

0 commit comments

Comments
 (0)