-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerup.go
executable file
·84 lines (66 loc) · 1.98 KB
/
powerup.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
package powerup
import (
"errors"
"tinygo.org/x/bluetooth"
)
type Airplane struct {
device *bluetooth.Device
control *bluetooth.DeviceService
motor *bluetooth.DeviceCharacteristic
rudder *bluetooth.DeviceCharacteristic
buf []byte
}
var (
// BLE services
// 75b64e51f1814ed1921a476090d80ba7
powerupService = bluetooth.NewUUID([16]byte{0x75, 0xb6, 0x4e, 0x51, 0xf1, 0x81, 0x4e, 0xd1, 0x92, 0x1a, 0x47, 0x60, 0x90, 0xd8, 0x0b, 0xa7})
motorCharacteristic = bluetooth.NewUUID([16]byte{0x75, 0xb6, 0x4e, 0x51, 0xf1, 0x84, 0x4e, 0xd1, 0x92, 0x1a, 0x47, 0x60, 0x90, 0xd8, 0x0b, 0xa7})
rudderCharacteristic = bluetooth.NewUUID([16]byte{0x75, 0xb6, 0x4e, 0x51, 0xf1, 0x85, 0x4e, 0xd1, 0x92, 0x1a, 0x47, 0x60, 0x90, 0xd8, 0x0b, 0xa7})
)
// NewAirplane creates a new Powerup Airplane.
func NewAirplane(dev *bluetooth.Device) *Airplane {
a := &Airplane{
device: dev,
buf: make([]byte, 255),
}
return a
}
func (a *Airplane) Start() (err error) {
srvcs, err := a.device.DiscoverServices([]bluetooth.UUID{
powerupService,
})
if err != nil || len(srvcs) == 0 {
return errors.New("could not find services")
}
a.control = &srvcs[0]
println("found powerup control service", a.control.UUID().String())
chars, err := a.control.DiscoverCharacteristics([]bluetooth.UUID{
motorCharacteristic,
rudderCharacteristic,
})
if err != nil || len(chars) == 0 {
return errors.New("could not find powerup control characteristic")
}
a.motor = &chars[0]
a.rudder = &chars[1]
return
}
// Stops stops the Airplane.
func (a *Airplane) Stop() (err error) {
a.Throttle(0)
a.Rudder(0)
return
}
// Throttle sets the throttle of the Airplane.
func (a *Airplane) Throttle(thrust int) (err error) {
buf := []byte{uint8(thrust)}
_, err = a.motor.WriteWithoutResponse(buf)
return err
}
// Rudder sets the rudder of the Airplane.
// angle goes from -45 to 45.
func (a *Airplane) Rudder(angle int) (err error) {
buf := []byte{byte(angle)}
_, err = a.rudder.WriteWithoutResponse(buf)
return err
}