-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.go
255 lines (209 loc) · 5.98 KB
/
arduino.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
package main
// This file contains the implementation of an arduino interface
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/tarm/serial"
)
var cmd = `#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo -e "/dev/$devname - $ID_SERIAL\n"
)
done `
func sendError(timeout time.Duration, err error, errorC chan<- error) {
select {
case <-time.After(timeout):
case errorC <- err:
}
}
func run(timeout time.Duration, outputC chan string, errorC chan error, command string, args ...string) {
defer func() {
errorC <- nil
}()
// instantiate new command
cmd := exec.Command(command, args...)
// get pipe to standard output
stdout, err := cmd.StdoutPipe()
if err != nil {
sendError(timeout, err, errorC)
return
}
// start process via command
if err := cmd.Start(); err != nil {
sendError(timeout, err, errorC)
return
}
// setup a buffer to capture standard output
var buf bytes.Buffer
// create a channel to capture any errors from wait
done := make(chan error)
go func() {
if _, err := buf.ReadFrom(stdout); err != nil {
sendError(timeout, err, errorC)
}
done <- cmd.Wait()
}()
// block on select, and switch based on actions received
select {
case <-time.After(timeout):
if err := cmd.Process.Kill(); err != nil {
sendError(timeout, err, errorC)
return
}
sendError(timeout, fmt.Errorf("process timed out"), errorC)
return
case err := <-done:
if err != nil {
sendError(timeout, err, errorC)
close(done)
}
outputC <- buf.String()
}
}
// findArduinos locates devices that implement the Arduino
// serial connection.
//
// This function returns a collection of the devices
// that are likely candidates for Arduino.
//
func findArduinos() (devices [][]string, err error) {
// Create a script to do some device discovery
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
return nil, err
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err = tmpfile.Write([]byte(cmd)); err != nil {
return nil, err
}
os.Chmod(tmpfile.Name(), 0700)
if err = tmpfile.Close(); err != nil {
return nil, err
}
devices = [][]string{}
outputC := make(chan string, 1)
defer close(outputC)
errorC := make(chan error, 1)
defer close(errorC)
go run(time.Duration(5*time.Second), outputC, errorC, tmpfile.Name())
for {
select {
case dev := <-outputC:
for _, line := range strings.Split(dev, "\n") {
if strings.Contains(line, "arduino") {
details := strings.Split(line, " -")
serial := strings.Split(line, "_")
devices = append(devices, []string{details[0], strings.TrimSpace(serial[len(serial)-1])})
continue
}
if strings.Contains(line, "ttyUSB") && strings.Contains(line, "USB_UART") {
details := strings.Split(line, " -")
deviceParts := strings.Split(details[0], "/")
if len(deviceParts) < 2 {
logW.Warn(fmt.Sprintf("Unexpected device line format %s", line))
continue
}
devices = append(devices, []string{details[0], strings.Trim(deviceParts[2], "/")})
continue
}
}
case err = <-errorC:
if err == nil {
return devices, nil
}
return nil, err
}
}
}
type arduino struct {
port *serial.Port
portal string // The name of ingress portal that this control device is associated with
devName string // The tty style device name
role string // The type of arduino that is present, core, or resonator cluster
}
// startDevice is used to start an individual arduino USB Serial device
//
func startDevice(portalName string, devName string) (device *arduino, err error) {
device = &arduino{}
device.port, err = serial.OpenPort(&serial.Config{Name: devName, Baud: 9600, ReadTimeout: time.Duration(time.Second * 2)})
if err != nil {
logW.Error(fmt.Sprintf("unable to open arduino at %s due to %s", devName, err.Error()), "error", err)
return nil, err
}
// Let the device stabilize before continuing
select {
case <-time.After(2 * time.Second):
}
if device.role, err = device.ping(); err != nil {
device.close()
logW.Error(fmt.Sprintf("unable to ping arduino at %s due to %s", devName, err.Error()), "error", err)
return nil, err
}
device.devName = devName
device.portal = portalName
return device, nil
}
func findDevices() (devices []string) {
// Parse the comma seperated device list
devices = strings.Split(*arduinos, ",")
// If the user did not specify arduinos to be used add then automatically
if len(devices) == 1 && len(devices[0]) == 0 {
deviceCatalog, err := findArduinos()
if err != nil {
logW.Error(err.Error())
os.Exit(-3)
}
if len(deviceCatalog) == 0 {
logW.Warn("No arduinos were specified and none could not be found, software will continue running and looking for devices")
}
devices = make([]string, 0, len(deviceCatalog))
for _, attribs := range deviceCatalog {
logW.Trace(fmt.Sprintf("arduino '%s' serial # '%s'", attribs[0], attribs[1]), "arduinoDevice", attribs[0], "audrinoSerial", attribs[1])
devices = append(devices, attribs[0])
}
}
return devices
}
func (dev *arduino) close() (err error) {
defer func() {
dev.port = nil
}()
return dev.port.Close()
}
func (dev *arduino) ping() (line string, err error) {
dev.port.Flush()
n, err := dev.port.Write([]byte("**********************\n"))
if err != nil {
return line, err
}
if n != 2 {
logW.Warn(fmt.Sprintf("%d bytes written out of %d", n, 2))
}
reader := bufio.NewReader(dev.port)
buf, err := reader.ReadBytes('\x0a')
return strings.TrimSpace(string(buf)), nil
}
func (dev *arduino) sendCmd(cmd []byte) (err error) {
dev.port.Flush()
// TODO Add an incremental write loop for serial devices
n, err := dev.port.Write(cmd)
if err != nil {
return err
}
if n != len(cmd) {
logW.Warn(fmt.Sprintf("%d bytes written out of %d", n, len(cmd)))
}
return nil
}