-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathadaptor.go
234 lines (198 loc) · 6.21 KB
/
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
package tinkerboard
import (
"fmt"
"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 (
defaultI2cBusNumber = 1
defaultSpiBusNumber = 0
defaultSpiChipNumber = 0
defaultSpiMode = 0
defaultSpiBitsNumber = 8
defaultSpiMaxSpeed = 500000
)
type cdevPin struct {
chip uint8
line uint8
}
type gpioPinDefinition struct {
sysfs int
cdev cdevPin
}
type analogPinDefinition struct {
path string
r bool // readable
w bool // writable
bufLen uint16
}
type pwmPinDefinition struct {
dir string
dirRegexp string
channel int
}
// Adaptor represents a Gobot Adaptor for the ASUS Tinker Board
type Adaptor struct {
name string
sys *system.Accesser
mutex *sync.Mutex
*adaptors.AnalogPinsAdaptor
*adaptors.DigitalPinsAdaptor
*adaptors.PWMPinsAdaptor
*adaptors.I2cBusAdaptor
*adaptors.SpiBusAdaptor
}
// NewAdaptor creates a Tinkerboard 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
//
// Optional parameters for PWM, see [adaptors.NewPWMPinsAdaptor]
//
// note from RK3288 datasheet: "The pull direction (pullup or pulldown) for all of GPIOs are software-programmable", but
// the latter is not working for any pin (armbian 22.08.7)
func NewAdaptor(opts ...interface{}) *Adaptor {
sys := system.NewAccesser(system.WithDigitalPinGpiodAccess())
a := &Adaptor{
name: gobot.DefaultName("Tinker Board"),
sys: sys,
mutex: &sync.Mutex{},
}
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.translateDigitalPin, digitalPinsOpts...)
a.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, a.translatePWMPin, pwmPinsOpts...)
a.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, a.validateI2cBusNumber, defaultI2cBusNumber)
a.SpiBusAdaptor = adaptors.NewSpiBusAdaptor(sys, a.validateSpiBusNumber, defaultSpiBusNumber, defaultSpiChipNumber,
defaultSpiMode, defaultSpiBitsNumber, defaultSpiMaxSpeed)
return a
}
// Name returns the name of the Adaptor
func (a *Adaptor) Name() string { return a.name }
// SetName sets the name of the Adaptor
func (a *Adaptor) SetName(n string) { 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, pins and bus
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
}
func (a *Adaptor) validateSpiBusNumber(busNr int) error {
// Valid bus numbers are [0,2] which corresponds to /dev/spidev0.x, /dev/spidev2.x
// x is the chip number <255
if (busNr != 0) && (busNr != 2) {
return fmt.Errorf("Bus number %d out of range", busNr)
}
return nil
}
func (a *Adaptor) validateI2cBusNumber(busNr int) error {
// Valid bus number is [0..4] which corresponds to /dev/i2c-0 through /dev/i2c-4.
// We don't support "/dev/i2c-6 DesignWare HDMI".
if (busNr < 0) || (busNr > 4) {
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
}
func (a *Adaptor) translateDigitalPin(id string) (string, int, error) {
pindef, ok := gpioPinDefinitions[id]
if !ok {
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
}
if a.sys.IsSysfsDigitalPinAccess() {
return "", pindef.sysfs, nil
}
chip := fmt.Sprintf("gpiochip%d", pindef.cdev.chip)
line := int(pindef.cdev.line)
return chip, line, nil
}
func (a *Adaptor) translatePWMPin(id string) (string, int, error) {
pinInfo, ok := pwmPinDefinitions[id]
if !ok {
return "", -1, fmt.Errorf("'%s' is not a valid id for a PWM pin", id)
}
path, err := pinInfo.findPWMDir(a.sys)
if err != nil {
return "", -1, err
}
return path, pinInfo.channel, nil
}
func (p pwmPinDefinition) findPWMDir(sys *system.Accesser) (string, error) {
items, _ := sys.Find(p.dir, p.dirRegexp)
if len(items) == 0 {
return "", fmt.Errorf("No path found for PWM directory pattern, '%s' in path '%s'. See README.md for activation",
p.dirRegexp, p.dir)
}
dir := items[0]
info, err := sys.Stat(dir)
if err != nil {
return "", fmt.Errorf("Error (%v) on access '%s'", err, dir)
}
if !info.IsDir() {
return "", fmt.Errorf("The item '%s' is not a directory, which is not expected", dir)
}
return dir, nil
}