/
main.go
177 lines (150 loc) · 4 KB
/
main.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
package main
// This is the most minimal blinky example and should run almost everywhere.
import (
"fmt"
"machine"
"runtime"
"runtime/volatile"
"time"
"tinygo.org/x/bluetooth"
)
// bluetooth
var (
adapter = bluetooth.DefaultAdapter
dc bluetooth.DeviceCharacteristic
serviceUUID = bluetooth.NewUUID([16]byte{0xa0, 0xb4, 0x00, 0x01, 0x92, 0x6d, 0x4d, 0x61, 0x98, 0xdf, 0x8c, 0x5c, 0x62, 0xee, 0x53, 0xb3})
charUUID = bluetooth.NewUUID([16]byte{0xa0, 0xb4, 0x00, 0x02, 0x92, 0x6d, 0x4d, 0x61, 0x98, 0xdf, 0x8c, 0x5c, 0x62, 0xee, 0x53, 0xb3})
)
var (
led = machine.LED
ledR = machine.LED_RED
ledG = machine.LED_GREEN
ledB = machine.LED_BLUE
b1 = machine.D0
b2 = machine.D1
patterns = [][]byte{
{0xFF, 0x00, 0x00, 0x00},
{0x00, 0xFF, 0x00, 0x00},
{0x00, 0x00, 0xFF, 0x00},
}
lightLed volatile.Register8
rgbLed volatile.Register8
)
func initBLE() {
println("starting")
must("enable BLE stack", adapter.Enable())
// The address to connect to. Set during scanning and read afterwards.
var foundDevice bluetooth.ScanResult
// Scan for NUS peripheral.
println("Scanning...")
err := adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) {
fmt.Printf("%#v\n", result.Address.String())
if result.LocalName() != "TinyGo ble-led-server" {
return
}
foundDevice = result
// Stop the scan.
err := adapter.StopScan()
if err != nil {
// Unlikely, but we can't recover from this.
println("failed to stop the scan:", err.Error())
}
})
if err != nil {
println("could not start a scan:", err.Error())
return
}
// Found a device: print this event.
if name := foundDevice.LocalName(); name == "" {
print("Connecting to ", foundDevice.Address.String(), "...")
println()
} else {
print("Connecting to ", name, " (", foundDevice.Address.String(), ")...")
println()
}
// Found a NUS peripheral. Connect to it.
device, err := adapter.Connect(foundDevice.Address, bluetooth.ConnectionParams{})
if err != nil {
println("Failed to connect:", err.Error())
return
}
// Connected. Look up the Nordic UART Service.
println("Discovering service...")
services, err := device.DiscoverServices([]bluetooth.UUID{serviceUUID})
if err != nil {
println("Failed to discover the Nordic UART Service:", err.Error())
return
}
service := services[0]
// Get the two characteristics present in this service.
chars, err := service.DiscoverCharacteristics([]bluetooth.UUID{charUUID})
if err != nil {
println("Failed to discover RX and TX characteristics:", err.Error())
return
}
dc = chars[0]
}
func main() {
initBLE()
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
ledR.Configure(machine.PinConfig{Mode: machine.PinOutput})
ledG.Configure(machine.PinConfig{Mode: machine.PinOutput})
ledB.Configure(machine.PinConfig{Mode: machine.PinOutput})
b1.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
b1.SetInterrupt(machine.PinFalling, func(machine.Pin) {
if lightLed.Get() != 0 {
lightLed.Set(0)
} else {
lightLed.Set(1)
}
})
b2.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
b2.SetInterrupt(machine.PinFalling, func(machine.Pin) {
if rgbLed.Get() != 0 {
rgbLed.Set(0)
} else {
rgbLed.Set(1)
}
})
ledValue := []byte{0x00, 0x00, 0x00, 0x00}
go func() {
index := 0
for {
if rgbLed.Get() != 0 {
ledValue[0] = patterns[index][0]
ledValue[1] = patterns[index][1]
ledValue[2] = patterns[index][2]
time.Sleep(time.Millisecond * 500)
index = (index + 1) % len(patterns)
} else {
runtime.Gosched()
}
}
}()
go func() {
for {
if lightLed.Get() != 0 {
ledValue[3] = 0x00
} else {
ledValue[3] = 0xFF
}
runtime.Gosched()
}
}()
for {
setLEDs(ledValue)
time.Sleep(10 * time.Millisecond)
}
}
func setLEDs(b []byte) {
ledR.Set(b[0] == 0x00)
ledG.Set(b[1] == 0x00)
ledB.Set(b[2] == 0x00)
led.Set(b[3] == 0x00)
dc.WriteWithoutResponse(b)
}
func must(action string, err error) {
if err != nil {
panic("failed to " + action + ": " + err.Error())
}
}