-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
jetson_adaptor.go
166 lines (138 loc) · 4.29 KB
/
jetson_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
package jetson
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 (
pwmPeriodDefault = 3000000 // 3 ms = 333 Hz
pwmPeriodMinimum = 5334
pwmDutyRateMinimum = 0.0005 // minimum duty of 1500 for default period, ~3 for minimum period
defaultI2cBusNumber = 1
defaultSpiBusNumber = 0
defaultSpiChipNumber = 0
defaultSpiMode = 0
defaultSpiBitsNumber = 8
defaultSpiMaxSpeed = 10000000
)
// Adaptor is the Gobot adaptor for the Jetson Nano
type Adaptor struct {
name string
sys *system.Accesser
mutex *sync.Mutex
*adaptors.DigitalPinsAdaptor
*adaptors.PWMPinsAdaptor
*adaptors.I2cBusAdaptor
*adaptors.SpiBusAdaptor
}
// NewAdaptor creates a Jetson Nano adaptor
//
// Optional parameters:
//
// adaptors.WithGpiodAccess(): use character device gpiod driver instead of sysfs
// adaptors.WithSpiGpioAccess(sclk, ncs, sdo, sdi): use GPIO's instead of /dev/spidev#.#
//
// Optional parameters for PWM, see [adaptors.NewPWMPinsAdaptor]
func NewAdaptor(opts ...interface{}) *Adaptor {
sys := system.NewAccesser()
a := &Adaptor{
name: gobot.DefaultName("JetsonNano"),
sys: sys,
mutex: &sync.Mutex{},
}
var digitalPinsOpts []func(adaptors.DigitalPinsOptioner)
pwmPinsOpts := []adaptors.PwmPinsOptionApplier{
adaptors.WithPWMDefaultPeriod(pwmPeriodDefault),
adaptors.WithPWMMinimumPeriod(pwmPeriodMinimum),
adaptors.WithPWMMinimumDutyRate(pwmDutyRateMinimum),
}
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.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 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.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.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,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) translateDigitalPin(id string) (string, int, error) {
if line, ok := gpioPins[id]; ok {
return "", line, nil
}
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
}
func (a *Adaptor) translatePWMPin(id string) (string, int, error) {
if channel, ok := pwmPins[id]; ok {
return "/sys/class/pwm/pwmchip0", channel, nil
}
return "", 0, fmt.Errorf("'%s' is not a valid pin id for PWM on '%s'", id, a.name)
}