-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
firmata_adaptor.go
247 lines (210 loc) · 5.91 KB
/
firmata_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
package firmata
import (
"io"
"strconv"
"time"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata/client"
"github.com/tarm/goserial"
)
type firmataBoard interface {
Connect(io.ReadWriteCloser) error
Disconnect() error
Pins() []client.Pin
AnalogWrite(int, int) error
SetPinMode(int, int) error
ReportAnalog(int, int) error
ReportDigital(int, int) error
DigitalWrite(int, int) error
I2cRead(int, int) error
I2cWrite(int, []byte) error
I2cConfig(int) error
ServoConfig(int, int, int) error
Event(string) string
}
// FirmataAdaptor is the Gobot Adaptor for Firmata based boards
type FirmataAdaptor struct {
name string
port string
board firmataBoard
conn io.ReadWriteCloser
openSP func(port string) (io.ReadWriteCloser, error)
gobot.Eventer
}
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and optionally accepts:
//
// string: port the FirmataAdaptor uses to connect to a serial port with a baude rate of 57600
// io.ReadWriteCloser: connection the FirmataAdaptor uses to communication with the hardware
//
// If an io.ReadWriteCloser is not supplied, the FirmataAdaptor will open a connection
// to a serial port with a baude rate of 57600. If an io.ReadWriteCloser
// is supplied, then the FirmataAdaptor will use the provided io.ReadWriteCloser and use the
// string port as a label to be displayed in the log and api.
func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
f := &FirmataAdaptor{
name: name,
port: "",
conn: nil,
board: client.New(),
openSP: func(port string) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
},
Eventer: gobot.NewEventer(),
}
for _, arg := range args {
switch arg.(type) {
case string:
f.port = arg.(string)
case io.ReadWriteCloser:
f.conn = arg.(io.ReadWriteCloser)
}
}
return f
}
// Connect starts a connection to the board.
func (f *FirmataAdaptor) Connect() (errs []error) {
if f.conn == nil {
sp, err := f.openSP(f.Port())
if err != nil {
return []error{err}
}
f.conn = sp
}
if err := f.board.Connect(f.conn); err != nil {
return []error{err}
}
return
}
// Disconnect closes the io connection to the board
func (f *FirmataAdaptor) Disconnect() (err error) {
if f.board != nil {
return f.board.Disconnect()
}
return nil
}
// Finalize terminates the firmata connection
func (f *FirmataAdaptor) Finalize() (errs []error) {
if err := f.Disconnect(); err != nil {
return []error{err}
}
return
}
// Port returns the FirmataAdaptors port
func (f *FirmataAdaptor) Port() string { return f.port }
// Name returns the FirmataAdaptors name
func (f *FirmataAdaptor) Name() string { return f.name }
// ServoConfig sets the pulse width in microseconds for a pin attached to a servo
func (f *FirmataAdaptor) ServoConfig(pin string, min, max int) error {
p, err := strconv.Atoi(pin)
if err != nil {
return err
}
return f.board.ServoConfig(p, max, min)
}
// ServoWrite writes the 0-180 degree angle to the specified pin.
func (f *FirmataAdaptor) ServoWrite(pin string, angle byte) (err error) {
p, err := strconv.Atoi(pin)
if err != nil {
return err
}
if f.board.Pins()[p].Mode != client.Servo {
err = f.board.SetPinMode(p, client.Servo)
if err != nil {
return err
}
}
err = f.board.AnalogWrite(p, int(angle))
return
}
// PwmWrite writes the 0-254 value to the specified pin
func (f *FirmataAdaptor) PwmWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin)
if err != nil {
return err
}
if f.board.Pins()[p].Mode != client.Pwm {
err = f.board.SetPinMode(p, client.Pwm)
if err != nil {
return err
}
}
err = f.board.AnalogWrite(p, int(level))
return
}
// DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) (err error) {
p, err := strconv.Atoi(pin)
if err != nil {
return
}
if f.board.Pins()[p].Mode != client.Output {
err = f.board.SetPinMode(p, client.Output)
if err != nil {
return
}
}
err = f.board.DigitalWrite(p, int(level))
return
}
// DigitalRead retrieves digital value from specified pin.
// Returns -1 if the response from the board has timed out
func (f *FirmataAdaptor) DigitalRead(pin string) (val int, err error) {
p, err := strconv.Atoi(pin)
if err != nil {
return
}
if f.board.Pins()[p].Mode != client.Input {
if err = f.board.SetPinMode(p, client.Input); err != nil {
return
}
if err = f.board.ReportDigital(p, 1); err != nil {
return
}
<-time.After(10 * time.Millisecond)
}
return f.board.Pins()[p].Value, nil
}
// AnalogRead retrieves value from analog pin.
// Returns -1 if the response from the board has timed out
func (f *FirmataAdaptor) AnalogRead(pin string) (val int, err error) {
p, err := strconv.Atoi(pin)
if err != nil {
return
}
p = f.digitalPin(p)
if f.board.Pins()[p].Mode != client.Analog {
if err = f.board.SetPinMode(p, client.Analog); err != nil {
return
}
if err = f.board.ReportAnalog(p, 1); err != nil {
return
}
<-time.After(10 * time.Millisecond)
}
return f.board.Pins()[p].Value, nil
}
// digitalPin converts pin number to digital mapping
func (f *FirmataAdaptor) digitalPin(pin int) int {
return pin + 14
}
// I2cStart starts an i2c device at specified address
func (f *FirmataAdaptor) I2cStart(address int) (err error) {
return f.board.I2cConfig(0)
}
// I2cRead returns size bytes from the i2c device
// Returns an empty array if the response from the board has timed out
func (f *FirmataAdaptor) I2cRead(address int, size int) (data []byte, err error) {
ret := make(chan []byte)
if err = f.board.I2cRead(address, size); err != nil {
return
}
f.Once(f.board.Event("I2cReply"), func(data interface{}) {
ret <- data.(client.I2cReply).Data
})
data = <-ret
return
}
// I2cWrite writes data to i2c device
func (f *FirmataAdaptor) I2cWrite(address int, data []byte) (err error) {
return f.board.I2cWrite(address, data)
}