-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
raspi_adaptor.go
245 lines (209 loc) · 6.71 KB
/
raspi_adaptor.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package raspi
import (
"fmt"
"strconv"
"strings"
"sync"
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/system"
)
const (
infoFile = "/proc/cpuinfo"
defaultSpiBusNumber = 0
defaultSpiChipNumber = 0
defaultSpiMode = 0
defaultSpiBitsNumber = 8
defaultSpiMaxSpeed = 500000
)
type analogPinDefinition struct {
path string
r bool // readable
w bool // writable
bufLen uint16
}
// Adaptor is the Gobot Adaptor for the Raspberry Pi
type Adaptor struct {
name string
mutex sync.Mutex
sys *system.Accesser
revision string
*adaptors.AnalogPinsAdaptor
*adaptors.DigitalPinsAdaptor
*adaptors.PWMPinsAdaptor
*adaptors.I2cBusAdaptor
*adaptors.SpiBusAdaptor
}
// NewAdaptor creates a Raspi Adaptor
//
// Optional parameters:
//
// adaptors.WithGpiodAccess(): use character device gpiod driver instead of sysfs (still used by default)
// adaptors.WithSpiGpioAccess(sclk, ncs, sdo, sdi): use GPIO's instead of /dev/spidev#.#
// adaptors.WithGpiosActiveLow(pin's): invert the pin behavior
// adaptors.WithGpiosPullUp/Down(pin's): sets the internal pull resistor
// adaptors.WithGpiosOpenDrain/Source(pin's): sets the output behavior
// adaptors.WithGpioDebounce(pin, period): sets the input debouncer
// adaptors.WithGpioEventOnFallingEdge/RaisingEdge/BothEdges(pin, handler): activate edge detection
func NewAdaptor(opts ...interface{}) *Adaptor {
sys := system.NewAccesser(system.WithDigitalPinGpiodAccess())
a := &Adaptor{
name: gobot.DefaultName("RaspberryPi"),
sys: sys,
}
var digitalPinsOpts []func(adaptors.DigitalPinsOptioner)
var pwmPinsOpts []adaptors.PwmPinsOptionApplier
for _, opt := range opts {
switch o := opt.(type) {
case func(adaptors.DigitalPinsOptioner):
digitalPinsOpts = append(digitalPinsOpts, o)
case adaptors.PwmPinsOptionApplier:
pwmPinsOpts = append(pwmPinsOpts, o)
default:
panic(fmt.Sprintf("'%s' can not be applied on adaptor '%s'", opt, a.name))
}
}
a.AnalogPinsAdaptor = adaptors.NewAnalogPinsAdaptor(sys, a.translateAnalogPin)
a.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, a.getPinTranslatorFunction(), digitalPinsOpts...)
a.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, a.getPinTranslatorFunction(), pwmPinsOpts...)
a.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, a.validateI2cBusNumber, 1)
a.SpiBusAdaptor = adaptors.NewSpiBusAdaptor(sys, a.validateSpiBusNumber, defaultSpiBusNumber, defaultSpiChipNumber,
defaultSpiMode, defaultSpiBitsNumber, defaultSpiMaxSpeed)
return a
}
// Name returns the adaptors name
func (a *Adaptor) Name() string {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.name
}
// SetName sets the adaptors name
func (a *Adaptor) SetName(n string) {
a.mutex.Lock()
defer a.mutex.Unlock()
a.name = n
}
// Connect create new connection to board and pins.
func (a *Adaptor) Connect() error {
a.mutex.Lock()
defer a.mutex.Unlock()
if err := a.SpiBusAdaptor.Connect(); err != nil {
return err
}
if err := a.I2cBusAdaptor.Connect(); err != nil {
return err
}
if err := a.AnalogPinsAdaptor.Connect(); err != nil {
return err
}
if err := a.PWMPinsAdaptor.Connect(); err != nil {
return err
}
return a.DigitalPinsAdaptor.Connect()
}
// Finalize closes connection to board and pins
func (a *Adaptor) Finalize() error {
a.mutex.Lock()
defer a.mutex.Unlock()
err := a.DigitalPinsAdaptor.Finalize()
if e := a.PWMPinsAdaptor.Finalize(); e != nil {
err = multierror.Append(err, e)
}
if e := a.AnalogPinsAdaptor.Finalize(); e != nil {
err = multierror.Append(err, e)
}
if e := a.I2cBusAdaptor.Finalize(); e != nil {
err = multierror.Append(err, e)
}
if e := a.SpiBusAdaptor.Finalize(); e != nil {
err = multierror.Append(err, e)
}
return err
}
// DefaultI2cBus returns the default i2c bus for this platform.
// This overrides the base function due to the revision dependency.
func (a *Adaptor) DefaultI2cBus() int {
rev := a.readRevision()
if rev == "2" || rev == "3" {
return 1
}
return 0
}
func (a *Adaptor) validateSpiBusNumber(busNr int) error {
// Valid bus numbers are [0,1] which corresponds to /dev/spidev0.x through /dev/spidev1.x.
// x is the chip number <255
if (busNr < 0) || (busNr > 1) {
return fmt.Errorf("Bus number %d out of range", busNr)
}
return nil
}
func (a *Adaptor) validateI2cBusNumber(busNr int) error {
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
if (busNr < 0) || (busNr > 1) {
return fmt.Errorf("Bus number %d out of range", busNr)
}
return nil
}
func (a *Adaptor) translateAnalogPin(id string) (string, bool, bool, uint16, error) {
pinInfo, ok := analogPinDefinitions[id]
if !ok {
return "", false, false, 0, fmt.Errorf("'%s' is not a valid id for a analog pin", id)
}
path := pinInfo.path
info, err := a.sys.Stat(path)
if err != nil {
return "", false, false, 0, fmt.Errorf("Error (%v) on access '%s'", err, path)
}
if info.IsDir() {
return "", false, false, 0, fmt.Errorf("The item '%s' is a directory, which is not expected", path)
}
return path, pinInfo.r, pinInfo.w, pinInfo.bufLen, nil
}
// getPinTranslatorFunction returns a function to be able to translate GPIO and PWM pins.
// This means for pi-blaster usage, each pin can be used and therefore the pin is given as number, like a GPIO pin.
// For sysfs-PWM usage, the pin will be given as "pwm0" or "pwm1", because the real pin number depends on the user
// configuration in "/boot/config.txt". For further details, see "/boot/overlays/README".
func (a *Adaptor) getPinTranslatorFunction() func(string) (string, int, error) {
return func(pin string) (string, int, error) {
var line int
if val, ok := pins[pin][a.readRevision()]; ok {
line = val
} else if val, ok := pins[pin]["*"]; ok {
line = val
} else {
return "", 0, fmt.Errorf("'%s' is not a valid pin id for raspi revision %s", pin, a.revision)
}
// We always use "gpiochip0", because currently all pins are available with this approach. A change of the
// translator would be needed to support different chips (e.g. gpiochip1) with different revisions.
path := "gpiochip0"
if strings.HasPrefix(pin, "pwm") {
path = "/sys/class/pwm/pwmchip0"
}
return path, line, nil
}
}
func (a *Adaptor) readRevision() string {
if a.revision == "" {
a.revision = "0"
content, err := a.sys.ReadFile(infoFile)
if err != nil {
return a.revision
}
for _, v := range strings.Split(string(content), "\n") {
if strings.Contains(v, "Revision") {
s := strings.Split(v, " ")
version, _ := strconv.ParseInt("0x"+s[len(s)-1], 0, 64)
switch {
case version <= 3:
a.revision = "1"
case version <= 15:
a.revision = "2"
default:
a.revision = "3"
}
}
}
}
return a.revision
}