Skip to content

Commit ab40fc7

Browse files
committed
examples: add scanner for Adafruit Clue
Signed-off-by: deadprogram <[email protected]>
1 parent d472af8 commit ab40fc7

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

examples/clue-scanner/main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"image/color"
6+
"machine"
7+
"time"
8+
9+
"tinygo.org/x/bluetooth"
10+
"tinygo.org/x/drivers/st7789"
11+
"tinygo.org/x/tinyterm"
12+
"tinygo.org/x/tinyterm/fonts/proggy"
13+
)
14+
15+
var (
16+
display st7789.Device
17+
terminal = tinyterm.NewTerminal(&display)
18+
19+
black = color.RGBA{0, 0, 0, 255}
20+
font = &proggy.TinySZ8pt7b
21+
22+
adapter = bluetooth.DefaultAdapter
23+
)
24+
25+
func main() {
26+
initDisplay()
27+
time.Sleep(time.Second)
28+
29+
fmt.Fprintf(terminal, "\nenable interface...")
30+
println("enable interface...")
31+
must("enable BLE interface", adapter.Enable())
32+
time.Sleep(time.Second)
33+
34+
println("start scan...")
35+
fmt.Fprintf(terminal, "\nstart scan...")
36+
37+
must("start scan", adapter.Scan(scanHandler))
38+
39+
for {
40+
time.Sleep(time.Minute)
41+
println("scanning...")
42+
fmt.Fprintf(terminal, "\nscanning...")
43+
}
44+
}
45+
46+
func scanHandler(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
47+
println("device:", device.Address.String(), device.RSSI, device.LocalName())
48+
fmt.Fprintf(terminal, "\n%s %d %s", device.Address.String(), device.RSSI, device.LocalName())
49+
}
50+
51+
func must(action string, err error) {
52+
if err != nil {
53+
for {
54+
println("failed to " + action + ": " + err.Error())
55+
time.Sleep(time.Second)
56+
}
57+
}
58+
}
59+
60+
func initDisplay() {
61+
machine.SPI1.Configure(machine.SPIConfig{
62+
Frequency: 8000000,
63+
SCK: machine.TFT_SCK,
64+
SDO: machine.TFT_SDO,
65+
SDI: machine.TFT_SDO,
66+
Mode: 0,
67+
})
68+
69+
display = st7789.New(machine.SPI1,
70+
machine.TFT_RESET,
71+
machine.TFT_DC,
72+
machine.TFT_CS,
73+
machine.TFT_LITE)
74+
display.Configure(st7789.Config{
75+
Rotation: st7789.ROTATION_180,
76+
Height: 320,
77+
FrameRate: st7789.FRAMERATE_111,
78+
VSyncLines: st7789.MAX_VSYNC_SCANLINES,
79+
})
80+
display.FillScreen(black)
81+
82+
terminal.Configure(&tinyterm.Config{
83+
Font: font,
84+
FontHeight: 10,
85+
FontOffset: 6,
86+
})
87+
}

0 commit comments

Comments
 (0)