-
Notifications
You must be signed in to change notification settings - Fork 109
/
gpio.go
120 lines (98 loc) · 3.34 KB
/
gpio.go
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
//go:build linux
// Package genericlinux is for Linux boards, and this particular file is for GPIO pins using the
// ioctl interface, indirectly by way of mkch's gpio package.
package genericlinux
import (
"context"
"fmt"
"math"
"sync"
"github.com/mkch/gpio"
"github.com/pkg/errors"
"go.viam.com/rdk/components/board"
)
type gpioPin struct {
// These values should both be considered immutable. The mutex is only here so that the use of
// the multiple calls to the gpio package don't have race conditions.
devicePath string
offset uint32
mu sync.Mutex
}
// This helps implement the board.GPIOPin interface for gpioPin.
func (pin *gpioPin) Set(ctx context.Context, isHigh bool, extra map[string]interface{}) (err error) {
pin.mu.Lock()
defer pin.mu.Unlock()
var value byte
if isHigh {
value = 1
} else {
value = 0
}
chip, err := gpio.OpenChip(pin.devicePath)
if err != nil {
return err
}
defer func() { err = chip.Close() }()
// The line returned has its default value set to the intended GPIO output, so we don't need to
// do anything else with it but close the file descriptor.
line, err := chip.OpenLine(pin.offset, value, gpio.Output, "viam-gpio")
if err != nil {
return err
}
defer func() { err = line.Close() }()
return nil
}
// This helps implement the board.GPIOPin interface for gpioPin.
func (pin *gpioPin) Get(ctx context.Context, extra map[string]interface{}) (result bool, err error) {
pin.mu.Lock()
defer pin.mu.Unlock()
chip, err := gpio.OpenChip(pin.devicePath)
if err != nil {
return false, err
}
defer func() { err = chip.Close() }()
line, err := chip.OpenLine(pin.offset, 0, gpio.Input, "viam-gpio")
if err != nil {
return false, err
}
defer func() { err = line.Close() }()
value, err := line.Value()
if err != nil {
return false, err
}
// We'd expect value to be either 0 or 1, but any non-zero value should be considered high.
return (value != 0), nil
}
// This helps implement the board.GPIOPin interface for gpioPin.
func (pin *gpioPin) PWM(ctx context.Context, extra map[string]interface{}) (float64, error) {
return math.NaN(), errors.New("PWM stuff is not supported on ioctl GPIO pins yet")
}
// This helps implement the board.GPIOPin interface for gpioPin.
func (pin *gpioPin) SetPWM(ctx context.Context, dutyCyclePct float64, extra map[string]interface{}) error {
return errors.New("PWM stuff is not supported on ioctl GPIO pins yet")
}
// This helps implement the board.GPIOPin interface for gpioPin.
func (pin *gpioPin) PWMFreq(ctx context.Context, extra map[string]interface{}) (uint, error) {
return 0, errors.New("PWM stuff is not supported on ioctl GPIO pins yet")
}
// This helps implement the board.GPIOPin interface for gpioPin.
func (pin *gpioPin) SetPWMFreq(ctx context.Context, freqHz uint, extra map[string]interface{}) error {
return errors.New("PWM stuff is not supported on ioctl GPIO pins yet")
}
var pins map[string]*gpioPin
func gpioInitialize(gpioMappings map[int]GPIOBoardMapping) {
pins = make(map[string]*gpioPin)
for pin, mapping := range gpioMappings {
pins[fmt.Sprintf("%d", pin)] = &gpioPin{
devicePath: mapping.GPIOChipDev,
offset: uint32(mapping.GPIO),
}
}
}
func gpioGetPin(pinName string) (board.GPIOPin, error) {
pin, ok := pins[pinName]
if !ok {
return nil, errors.Errorf("Cannot set GPIO for unknown pin: %s", pinName)
}
return pin, nil
}