forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip_adaptor.go
301 lines (263 loc) · 6.77 KB
/
chip_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package chip
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"sync"
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/sysfs"
)
type sysfsPin struct {
pin int
pwmPin int
}
// Adaptor represents a Gobot Adaptor for a C.H.I.P.
type Adaptor struct {
name string
board string
pinmap map[string]sysfsPin
digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin
i2cBuses [3]i2c.I2cDevice
mutex *sync.Mutex
}
// NewAdaptor creates a C.H.I.P. Adaptor
func NewAdaptor() *Adaptor {
c := &Adaptor{
name: gobot.DefaultName("CHIP"),
board: "chip",
mutex: &sync.Mutex{},
}
c.setPins()
return c
}
// NewAdaptor creates a C.H.I.P. Pro Adaptor
func NewProAdaptor() *Adaptor {
c := &Adaptor{
name: gobot.DefaultName("CHIP Pro"),
board: "pro",
mutex: &sync.Mutex{},
}
c.setPins()
return c
}
// Name returns the name of the Adaptor
func (c *Adaptor) Name() string { return c.name }
// SetName sets the name of the Adaptor
func (c *Adaptor) SetName(n string) { c.name = n }
// Connect initializes the board
func (c *Adaptor) Connect() (err error) {
return nil
}
// Finalize closes connection to board and pins
func (c *Adaptor) Finalize() (err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
for _, pin := range c.digitalPins {
if pin != nil {
if e := pin.Unexport(); e != nil {
err = multierror.Append(err, e)
}
}
}
for _, pin := range c.pwmPins {
if pin != nil {
if errs := pin.Enable(false); errs != nil {
err = multierror.Append(err, errs)
}
if errs := pin.Unexport(); errs != nil {
err = multierror.Append(err, errs)
}
}
}
for _, bus := range c.i2cBuses {
if bus != nil {
if e := bus.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}
return
}
// DigitalRead reads digital value from the specified pin.
// Valids pins are the XIO-P0 through XIO-P7 pins from the
// extender (pins 13-20 on header 14), as well as the SoC pins
// aka all the other pins.
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
sysfsPin, err := c.DigitalPin(pin, sysfs.IN)
if err != nil {
return
}
return sysfsPin.Read()
}
// DigitalWrite writes digital value to the specified pin.
// Valids pins are the XIO-P0 through XIO-P7 pins from the
// extender (pins 13-20 on header 14), as well as the SoC pins
// aka all the other pins.
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
sysfsPin, err := c.DigitalPin(pin, sysfs.OUT)
if err != nil {
return err
}
return sysfsPin.Write(int(val))
}
// GetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..2] which corresponds to /dev/i2c-0 through /dev/i2c-2.
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if (bus < 0) || (bus > 2) {
return nil, fmt.Errorf("Bus number %d out of range", bus)
}
if c.i2cBuses[bus] == nil {
c.i2cBuses[bus], err = sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
}
return i2c.NewConnection(c.i2cBuses[bus], address), err
}
// GetDefaultBus returns the default i2c bus for this platform
func (c *Adaptor) GetDefaultBus() int {
return 1
}
// digitalPin returns matched digitalPin for specified values
func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinner, err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
i, err := c.translatePin(pin)
if err != nil {
return
}
if c.digitalPins[i] == nil {
c.digitalPins[i] = sysfs.NewDigitalPin(i)
if err = c.digitalPins[i].Export(); err != nil {
return
}
}
if err = c.digitalPins[i].Direction(dir); err != nil {
return
}
return c.digitalPins[i], nil
}
// pwmPin returns matched pwmPin for specified pin number
func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
sysPin := c.pinmap[pin]
if sysPin.pwmPin != -1 {
if c.pwmPins[sysPin.pwmPin] == nil {
newPin := sysfs.NewPWMPin(sysPin.pwmPin)
if err = newPin.Export(); err != nil {
return
}
// Make sure pwm is disabled when setting polarity
if err = newPin.Enable(false); err != nil {
return
}
if err = newPin.InvertPolarity(false); err != nil {
return
}
if err = newPin.Enable(true); err != nil {
return
}
if err = newPin.SetPeriod(10000000); err != nil {
return
}
c.pwmPins[sysPin.pwmPin] = newPin
}
sysfsPin = c.pwmPins[sysPin.pwmPin]
return
}
err = errors.New("Not a PWM pin")
return
}
// PwmWrite writes a PWM signal to the specified pin
func (c *Adaptor) PwmWrite(pin string, val byte) (err error) {
pwmPin, err := c.PWMPin(pin)
if err != nil {
return
}
period, err := pwmPin.Period()
if err != nil {
return err
}
duty := gobot.FromScale(float64(val), 0, 255.0)
return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
}
// ServoWrite writes a servo signal to the specified pin
func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) {
pwmPin, err := c.PWMPin(pin)
if err != nil {
return
}
// 0.5 ms => -90
// 1.5 ms => 0
// 2.0 ms => 90
//
// Duty cycle is in nanos
const minDuty = 0.0005 * 1e9
const maxDuty = 0.0020 * 1e9
duty := uint32(gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), minDuty, maxDuty))
return pwmPin.SetDutyCycle(duty)
}
// SetBoard sets the name of the type of board
func (c *Adaptor) SetBoard(n string) (err error) {
if n == "chip" || n == "pro" {
c.board = n
c.setPins()
return
}
return errors.New("Invalid board type")
}
func (c *Adaptor) setPins() {
c.digitalPins = make(map[int]*sysfs.DigitalPin)
c.pwmPins = make(map[int]*sysfs.PWMPin)
if c.board == "pro" {
c.pinmap = chipProPins
return
}
// otherwise, original CHIP
c.pinmap = chipPins
baseAddr, _ := getXIOBase()
for i := 0; i < 8; i++ {
pin := fmt.Sprintf("XIO-P%d", i)
c.pinmap[pin] = sysfsPin{pin: baseAddr + i, pwmPin: -1}
}
}
func getXIOBase() (baseAddr int, err error) {
// Default to original base from 4.3 kernel
baseAddr = 408
const expanderID = "pcf8574a"
labels, err := filepath.Glob("/sys/class/gpio/*/label")
if err != nil {
return
}
for _, labelPath := range labels {
label, err := ioutil.ReadFile(labelPath)
if err != nil {
return baseAddr, err
}
if strings.HasPrefix(string(label), expanderID) {
expanderPath, _ := filepath.Split(labelPath)
basePath := filepath.Join(expanderPath, "base")
base, err := ioutil.ReadFile(basePath)
if err != nil {
return baseAddr, err
}
baseAddr, _ = strconv.Atoi(strings.TrimSpace(string(base)))
break
}
}
return baseAddr, nil
}
func (c *Adaptor) translatePin(pin string) (i int, err error) {
if val, ok := c.pinmap[pin]; ok {
i = val.pin
} else {
err = errors.New("Not a valid pin")
}
return
}