-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bluefruit.go
109 lines (92 loc) · 2.55 KB
/
bluefruit.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
//go:build circuitplay_bluefruit
// +build circuitplay_bluefruit
package gopherhelmet
import (
"machine"
"tinygo.org/x/bluetooth"
"tinygo.org/x/drivers/servo"
)
var (
servo1 servo.PWM = machine.PWM1
servo2 servo.PWM = machine.PWM2
adapter = bluetooth.DefaultAdapter
serviceUUID = [16]byte{0x28, 0x75, 0x99, 0x68, 0xf4, 0x50, 0x11, 0xec, 0xa9, 0xd6, 0xeb, 0x6f, 0x98, 0x8b, 0x5b, 0x69}
charUUID = [16]byte{0x28, 0x75, 0x99, 0x90, 0xf4, 0x50, 0x11, 0xec, 0xa9, 0xd7, 0x87, 0x73, 0xad, 0x8e, 0x89, 0x62}
connected = false
disconnected = true
alt = true
timeCount = 0
earLeftPin = machine.A1
earRightPin = machine.A3
antennaPin = machine.A6
neopixelsPin = machine.NEOPIXELS
btnAPin = machine.BUTTONA
btnBPin = machine.BUTTONB
sclPin = machine.SCL1_PIN
sdaPin = machine.SDA1_PIN
ledPin = machine.LED
tempPin = machine.TEMPSENSOR
lightPin = machine.LIGHTSENSOR
sliderPin = machine.SLIDER
visorPin = machine.A2
speakerEnablePin = machine.D11
speakerPin = machine.D12
)
func InitBLE(device *BackpackDevice, fn BLECallback) {
/*println("starting")
adapter.SetConnectHandler(func(d bluetooth.Addresser, c bool) {
connected = c
if !connected && !disconnected {
device.Clear()
timeCount = 0
disconnected = true
}
if connected {
timeCount = 0
disconnected = false
}
})
must("enable BLE stack", adapter.Enable())
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "GopherHelmet dev1.3",
}))
must("start adv", adv.Start())
var stringCharacteristic bluetooth.Characteristic
var stringMsg string
must("add service", adapter.AddService(&bluetooth.Service{
UUID: bluetooth.NewUUID(serviceUUID),
Characteristics: []bluetooth.CharacteristicConfig{
{
Handle: &stringCharacteristic,
UUID: bluetooth.NewUUID(charUUID),
Value: []byte(stringMsg),
Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission,
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
stringMsg = string(value)
fn(stringMsg)
println(offset, string(value), stringMsg)
},
},
},
}))
for {
alt = !alt
if alt && timeCount < 30 {
timeCount++
if connected {
device.Green()
} else {
device.Red()
}
} else {
device.Clear()
}
time.Sleep(100 * time.Millisecond)
} */
}
func must(action string, err error) {
if err != nil {
panic("failed to " + action + ": " + err.Error())
}
}