-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsphero.go
202 lines (167 loc) · 5.05 KB
/
sphero.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
package sphero
import (
"fmt"
"time"
"github.com/hashicorp/go-hclog"
"tinygo.org/x/bluetooth"
)
// Sphero defines a type that can communicate with a Sphero Mini over Bluetooth LE
// https://sdk.sphero.com/docs/api_spec/general_api
type Sphero struct {
device *bluetooth.Device
charAPIV2 bluetooth.DeviceCharacteristic
charAntiDOS bluetooth.DeviceCharacteristic
charDFU bluetooth.DeviceCharacteristic
charDFU2 bluetooth.DeviceCharacteristic
sequenceNo int
log hclog.Logger
outBuffer []byte
commandResponse chan *payload
streamingResponse chan *payload
expectedCommandSequence int
lastError error
backlightEnabled bool
next func()
}
// NewSphero creates a new Sphero and attempts to connect to the device
// addr can either be supplied as the mac address for the bluetooth device name
//
// example:
// logger := hclog.Default()
//
// // create the bluetooth adapter the sphero uses to interface with the computers bluetooth
// // stack
// adapter, err := sphero.NewBluetoothAdapter(logger)
// if err != nil {
// fmt.Printf("Unable to create a bluetooth adapter: %s\n", err)
// os.Exit(1)
// }
//
// // create the sphero
// ball, err := sphero.NewSphero(addr, adapter, logger)
// if err != nil {
// fmt.Printf("Unable to create a new sphero: %s\n", err)
// os.Exit(1)
// }
//
// // flash the LEDS Red, Green, and Blue
// ball.
// SetLEDColor(235, 64, 52).
// For(1*time.Second).
// SetLEDColor(52, 235, 88).
// For(1*time.Second).
// SetLEDColor(52, 122, 235).
// For(1 * time.Second)
func NewSphero(addr string, adapter *BluetoothAdapter, l hclog.Logger) (*Sphero, error) {
var bleAddress bluetooth.Addresser
l.Trace("Discovering device for", "address", addr)
ac := make(chan bluetooth.Addresser)
to := time.After(10 * time.Second)
sr := adapter.Scan()
go func() {
for r := range sr {
if r.Name == addr || r.Address.String() == addr {
l.Debug("Found device", "addr", addr)
ac <- r.Address
adapter.StopScanning()
}
}
}()
select {
case bleAddress = <-ac:
l.Debug("Found device", "address", addr)
case <-to:
return nil, fmt.Errorf("timeout while trying to connect to address: %s", addr)
}
l.Debug("Connecting", "device", addr)
device, err := adapter.Connect(bleAddress)
if err != nil {
l.Error("Unable to connect to bluetooth deivce", "address", addr)
return nil, err
}
services, err := device.DiscoverServices([]bluetooth.UUID{})
if err != nil {
l.Error("Unable to get services for bluetooth deivce", "address", addr, "error", err)
return nil, err
}
charAPIV2 := getCharacteristic(services, "00010002-574f-4f20-5370-6865726f2121")
charAntiDOS := getCharacteristic(services, "00020005-574f-4f20-5370-6865726f2121")
charDFU := getCharacteristic(services, "00020002-574f-4f20-5370-6865726f2121")
charDFU2 := getCharacteristic(services, "00020004-574f-4f20-5370-6865726f2121")
// ensure the device does not sleep after 10s
charAntiDOS.WriteWithoutResponse([]byte("usetheforce...band"))
s := &Sphero{
device: device,
charAPIV2: charAPIV2,
charAntiDOS: charAntiDOS,
charDFU: charDFU,
charDFU2: charDFU2,
log: l,
}
s.setup()
s.blink()
return s, nil
}
func (s *Sphero) blink() {
s.
SetLEDColor(255, 255, 255).
For(300*time.Millisecond).
SetLEDColor(255, 255, 255).
For(300*time.Millisecond).
SetLEDColor(255, 255, 255).
For(300*time.Millisecond).
SetLEDColor(0, 0, 0).
For(2 * time.Second)
}
func getCharacteristic(ds []bluetooth.DeviceService, uuid string) bluetooth.DeviceCharacteristic {
uu, err := bluetooth.ParseUUID(uuid)
if err != nil {
panic(err)
}
for _, s := range ds {
c, err := s.DiscoverCharacteristics([]bluetooth.UUID{uu})
if err == nil {
return c[0]
}
}
panic(fmt.Errorf("characteristic: %s not found", uuid))
}
func (s *Sphero) setup() error {
s.log.Debug("Setup Sphero")
s.commandResponse = make(chan *payload)
s.streamingResponse = make(chan *payload)
s.charAPIV2.EnableNotifications(func(buf []byte) {
//s.log.Trace("Got response apiv2", "data", buf)
// if start packet create a new buffer
if buf[0] == DataPacketStart {
s.outBuffer = []byte{}
}
// increment the buffer
s.outBuffer = append(s.outBuffer, buf[0])
// if end packet send to channel
if buf[0] == DataPacketEnd {
// construct the payload
p := &payload{}
p.decode(s.outBuffer)
if s.expectedCommandSequence == int(p.Sequence) {
s.expectedCommandSequence = 0
s.commandResponse <- p
return
}
s.log.Trace("Got response, disposed", "data", s.outBuffer)
}
})
s.charAntiDOS.EnableNotifications(func(buf []byte) {
s.log.Trace("Got response antidos", "data", buf)
})
s.charDFU.EnableNotifications(func(buf []byte) {
s.log.Trace("Got response dfu", "data", buf)
})
s.charDFU2.EnableNotifications(func(buf []byte) {
s.log.Trace("Got response dfu2", "data", buf)
})
s.charAntiDOS.WriteWithoutResponse([]byte("usetheforce...band"))
s.log.Debug("Send Wake")
s.Wake()
return nil
}