Skip to content

Commit b471d4c

Browse files
committed
examples: add example that uses ble advertising
Signed-off-by: deadprogram <[email protected]>
1 parent 294aff5 commit b471d4c

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

examples/ble-advertiser/main.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//go:build circuitplay_bluefruit
2+
// +build circuitplay_bluefruit
3+
4+
package main
5+
6+
import (
7+
"image/color"
8+
"time"
9+
10+
"github.com/hybridgroup/gopherbot"
11+
"tinygo.org/x/bluetooth"
12+
)
13+
14+
var adapter = bluetooth.DefaultAdapter
15+
16+
func main() {
17+
must("enable BLE interface", adapter.Enable())
18+
19+
adv := adapter.DefaultAdvertisement()
20+
must("configure advertisement", adv.Configure(bluetooth.AdvertisementOptions{
21+
LocalName: "Gopherbot",
22+
}))
23+
24+
must("start advertising", adv.Start())
25+
26+
accel = gopherbot.Accelerometer()
27+
visor = gopherbot.Visor()
28+
speaker = gopherbot.Speaker()
29+
30+
antenna := gopherbot.Antenna()
31+
backpack := gopherbot.Backpack()
32+
33+
left := gopherbot.LeftButton()
34+
right := gopherbot.RightButton()
35+
36+
mode := redVisor
37+
38+
go antenna.Blink()
39+
40+
for {
41+
if right.Pushed() {
42+
mode++
43+
if mode > xmasVisor {
44+
mode = greenVisor
45+
}
46+
}
47+
48+
if left.Pushed() {
49+
mode--
50+
if mode < greenVisor {
51+
mode = xmasVisor
52+
}
53+
}
54+
55+
switch mode {
56+
case greenVisor:
57+
visor.Green()
58+
backpack.Alternate(color.RGBA{R: 0x00, G: 0xff, B: 0x00}, color.RGBA{R: 0x00, G: 0x00, B: 0xff})
59+
case redVisor:
60+
visor.Red()
61+
backpack.Red()
62+
case cylonVisor:
63+
visor.Cylon()
64+
backpack.Alternate(color.RGBA{R: 0xff, G: 0x00, B: 0x00}, color.RGBA{R: 0x00, G: 0x00, B: 0x00})
65+
case tiltVisor:
66+
tilt()
67+
case xmasVisor:
68+
visor.Xmas()
69+
backpack.Xmas()
70+
}
71+
72+
time.Sleep(200 * time.Millisecond)
73+
}
74+
}
75+
76+
func must(action string, err error) {
77+
if err != nil {
78+
panic("failed to " + action + ": " + err.Error())
79+
}
80+
}
81+
82+
const (
83+
greenVisor = iota
84+
redVisor
85+
cylonVisor
86+
tiltVisor
87+
xmasVisor
88+
)
89+
90+
const (
91+
forward = iota
92+
backward
93+
)
94+
95+
var (
96+
accel *gopherbot.AccelerometerDevice
97+
visor *gopherbot.VisorDevice
98+
speaker *gopherbot.SpeakerDevice
99+
100+
pos = 0
101+
)
102+
103+
func tilt() {
104+
x, y, z, _ := accel.ReadAcceleration()
105+
println(x, y, z)
106+
107+
switch {
108+
case x < 300000 && x > -300000:
109+
switch {
110+
case pos <= gopherbot.VisorLEDCount/2-1:
111+
pos++
112+
case pos >= gopherbot.VisorLEDCount/2+1:
113+
pos--
114+
}
115+
case x > 300000:
116+
pos++
117+
if pos == gopherbot.VisorLEDCount {
118+
pos = gopherbot.VisorLEDCount - 1
119+
speaker.Bleep()
120+
}
121+
case x < -300000:
122+
pos--
123+
if pos < 0 {
124+
pos = 0
125+
speaker.Bleep()
126+
}
127+
}
128+
129+
for i := range visor.LED {
130+
if i == pos {
131+
visor.LED[i] = color.RGBA{R: 0x00, G: 0xff, B: 0x00, A: 0x77}
132+
} else {
133+
visor.LED[i] = color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x77}
134+
}
135+
}
136+
137+
visor.Show()
138+
}

0 commit comments

Comments
 (0)