Skip to content

Commit

Permalink
pkg/gpio: wrap errors and remove second trim
Browse files Browse the repository at this point in the history
Signed-off-by: Siarhiej Siemianczuk <pdp.eleven11@gmail.com>
  • Loading branch information
binjip978 authored and rminnich committed Jan 16, 2024
1 parent 4740ac1 commit 4478dd7
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/gpio/gpio_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func readInt(filename string) (int, error) {
baseStr := strings.TrimSpace(string(buf))
num, err := strconv.Atoi(baseStr)
if err != nil {
return 0, fmt.Errorf("could not convert %s contents %s to integer: %v", filename, baseStr, err)
return 0, fmt.Errorf("could not convert %s contents %s to integer: %w", filename, baseStr, err)
}
return num, nil
}
Expand All @@ -68,25 +68,25 @@ func GetPinID(controller string, pin uint) (int, error) {
// Get label (name of the controller)
buf, err := os.ReadFile(filepath.Join(c, "label"))
if err != nil {
return 0, fmt.Errorf("failed to read label of %s: %v", c, err)
return 0, fmt.Errorf("failed to read label of %s: %w", c, err)
}
label := strings.TrimSpace(string(buf))

// Check that this is the controller we want
if strings.TrimSpace(label) != controller {
if label != controller {
continue
}

// Get base offset (the first GPIO managed by this chip)
base, err := readInt(filepath.Join(c, "base"))
if err != nil {
return 0, fmt.Errorf("failed to read base: %v", err)
return 0, fmt.Errorf("failed to read base: %w", err)
}

// Get the number of GPIOs managed by this chip.
ngpio, err := readInt(filepath.Join(c, "ngpio"))
if err != nil {
return 0, fmt.Errorf("failed to read number of gpios: %v", err)
return 0, fmt.Errorf("failed to read number of gpios: %w", err)
}
if int(pin) >= ngpio {
return 0, fmt.Errorf("requested pin %d of controller %s, but controller only has %d pins", pin, controller, ngpio)
Expand All @@ -108,7 +108,7 @@ func SetOutputValue(pin int, val Value) error {
}
defer outFile.Close()
if _, err := outFile.WriteString(dir); err != nil {
return fmt.Errorf("failed to set gpio %d to %s: %v", pin, dir, err)
return fmt.Errorf("failed to set gpio %d to %s: %w", pin, dir, err)
}
return nil
}
Expand All @@ -119,7 +119,7 @@ func ReadValue(pin int) (Value, error) {
path := filepath.Join(gpioPath, fmt.Sprintf("gpio%d", pin), "value")
buf, err := os.ReadFile(path)
if err != nil {
return Low, fmt.Errorf("failed to read value of gpio %d: %v", pin, err)
return Low, fmt.Errorf("failed to read value of gpio %d: %w", pin, err)
}
switch string(buf) {
case "0\n":
Expand Down

0 comments on commit 4478dd7

Please sign in to comment.