-
Notifications
You must be signed in to change notification settings - Fork 0
/
mip.go
executable file
·189 lines (147 loc) · 4.63 KB
/
mip.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
package mip
import (
"errors"
"image/color"
"tinygo.org/x/bluetooth"
)
type Robot struct {
device *bluetooth.Device
receive *bluetooth.DeviceService
receiveNotify *bluetooth.DeviceCharacteristic
send *bluetooth.DeviceService
sendData *bluetooth.DeviceCharacteristic
buf []byte
}
var (
// BLE services
mipReceiveDataService = bluetooth.New16BitUUID(0xffe0)
mipReceiveDataNotifyCharacteristic = bluetooth.New16BitUUID(0xffe4)
mipSendDataService = bluetooth.New16BitUUID(0xffe5)
mipSendDataWriteCharacteristic = bluetooth.New16BitUUID(0xffe9)
)
// NewRobot creates a new MiP robot.
func NewRobot(dev *bluetooth.Device) *Robot {
r := &Robot{
device: dev,
buf: make([]byte, 255),
}
return r
}
func (r *Robot) Start() (err error) {
srvcs, err := r.device.DiscoverServices([]bluetooth.UUID{
mipReceiveDataService,
mipSendDataService,
})
if err != nil || len(srvcs) == 0 {
return errors.New("could not find services")
}
r.receive = &srvcs[0]
r.send = &srvcs[1]
if debug {
println("found mip receive service", r.receive.UUID().String())
println("found mip send service", r.send.UUID().String())
}
chars, err := r.receive.DiscoverCharacteristics([]bluetooth.UUID{
mipReceiveDataNotifyCharacteristic,
})
if err != nil || len(chars) == 0 {
return errors.New("could not find mip receive characteristic")
}
r.receiveNotify = &chars[0]
chars, err = r.send.DiscoverCharacteristics([]bluetooth.UUID{
mipSendDataWriteCharacteristic,
})
if err != nil || len(chars) == 0 {
return errors.New("could not find mip write characteristic")
}
r.sendData = &chars[0]
return
}
func (r *Robot) Halt() (err error) {
return
}
// Stops stops the MIP from moving.
func (r *Robot) Stop() (err error) {
buf := []byte{Stop}
_, err = r.sendData.WriteWithoutResponse(buf)
return
}
// SetChestLED sets the chest LED of the MiP
func (r *Robot) SetChestLED(c color.RGBA) (err error) {
buf := []byte{SetChestLED, c.R, c.G, c.B}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// FlashChestLED flashes the chest LED of the MiP
func (r *Robot) FlashChestLED(c color.RGBA, on, off int) (err error) {
buf := []byte{FlashChestLED, c.R, c.G, c.B, byte(on), byte(off)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// SetHeadLED sets the head LEDs of the MiP based on [HeadLED] values.
func (r *Robot) SetHeadLED(l1, l2, l3, l4 HeadLED) (err error) {
buf := []byte{SetHeadLED, byte(l1), byte(l2), byte(l3), byte(l4)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// GetUp tells the MiP to stand up. [GetUpMode] is the mode to stand up in.
func (r *Robot) GetUp(stand GetUpMode) (err error) {
buf := []byte{GetUp, byte(stand)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// DriveForward drives the MiP forward at a given speed for a given duration (in ms)
func (r *Robot) DriveForward(speed int, duration int) (err error) {
if speed > 30 {
speed = 30
}
// Time in 7ms intervals
buf := []byte{DriveForwardTime, byte(speed), byte(duration / 7)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// DriveBackward drives the MiP backward at a given speed for a given duration (in ms)
func (r *Robot) DriveBackward(speed int, duration int) (err error) {
if speed > 30 {
speed = 30
}
// Time in 7ms intervals
buf := []byte{DriveBackwardTime, byte(speed), byte(duration / 7)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// TurnLeft turns the MiP left to a given angle at at given speed.
func (r *Robot) TurnLeft(angle int, speed int) (err error) {
if speed > 24 {
speed = 24
}
// Angle is in intervals of 5 degrees
buf := []byte{TurnLeftAngle, byte(angle / 5), byte(speed)}
_, err = r.sendData.WriteWithoutResponse(buf)
return
}
// TurnRight turns the MiP right to a given angle at at given speed.
func (r *Robot) TurnRight(angle int, speed int) (err error) {
if speed > 24 {
speed = 24
}
// Angle is in intervals of 5 degrees
buf := []byte{TurnRightAngle, byte(angle / 5), byte(speed)}
_, err = r.sendData.WriteWithoutResponse(buf)
return
}
// SetGameMode tells the MiP to start playing a game using a [GameMode].
func (r *Robot) SetGameMode(mode GameMode) (err error) {
buf := []byte{SetGameMode, byte(mode)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}
// PlaySound tells the MiP to play a sound.
// [Sound] file index (1~106) or send 0xF7-0xFE for volume control.
// [Delay] in ms.
func (r *Robot) PlaySound(sound Sound, delay int) (err error) {
// Delay in intervals of 30ms (0~255)
buf := []byte{PlaySound, byte(sound), byte(delay / 30)}
_, err = r.sendData.WriteWithoutResponse(buf)
return err
}