forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
megapi_adaptor.go
77 lines (68 loc) · 1.89 KB
/
megapi_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
package megapi
import (
"github.com/hybridgroup/gobot"
"github.com/tarm/serial"
"io"
"time"
)
var _ gobot.Adaptor = (*MegaPiAdaptor)(nil)
// MegaPiAdaptor is the Gobot adaptor for the MakeBlock MegaPi board
type MegaPiAdaptor struct {
name string
connection io.ReadWriteCloser
serialConfig *serial.Config
writeBytesChannel chan []byte
finalizeChannel chan struct{}
}
// NewMegaPiAdaptor returns a new MegaPiAdaptor with specified name and specified serial port used to talk to the MegaPi with a baud rate of 115200
func NewMegaPiAdaptor(name string, device string) *MegaPiAdaptor {
c := &serial.Config{Name: device, Baud: 115200}
return &MegaPiAdaptor{
name: name,
connection: nil,
serialConfig: c,
writeBytesChannel: make(chan []byte),
finalizeChannel: make(chan struct{}),
}
}
// Name returns the name of this adaptor
func (megaPi *MegaPiAdaptor) Name() string {
return megaPi.name
}
// Connect starts a connection to the board
func (megaPi *MegaPiAdaptor) Connect() (errs []error) {
if megaPi.connection == nil {
sp, err := serial.OpenPort(megaPi.serialConfig)
if err != nil {
return []error{err}
}
// sleeping is required to give the board a chance to reset
time.Sleep(2 * time.Second)
megaPi.connection = sp
}
// kick off thread to send bytes to the board
go func() {
for {
select {
case bytes := <-megaPi.writeBytesChannel:
megaPi.connection.Write(bytes)
time.Sleep(10 * time.Millisecond)
case <-megaPi.finalizeChannel:
megaPi.finalizeChannel <- struct{}{}
return
default:
time.Sleep(10 * time.Millisecond)
}
}
}()
return
}
// Finalize terminates the connection to the board
func (megaPi *MegaPiAdaptor) Finalize() (errs []error) {
megaPi.finalizeChannel <- struct{}{}
<-megaPi.finalizeChannel
if err := megaPi.connection.Close(); err != nil {
return []error{err}
}
return
}