Neale Pickett
·
2026-02-23
bellows-test.go
1package main
2
3import (
4 "context"
5 "image/color"
6 "machine"
7 "time"
8
9 "git.woozle.org/neale/concertina/pkg/led"
10 "tinygo.org/x/drivers/ssd1306"
11 "tinygo.org/x/drivers/vl6180x"
12)
13
14// For crying out loud.
15// https://github.com/tinygo-org/drivers/issues/837`
16var White = color.RGBAModel.Convert(color.White).(color.RGBA)
17var Black = color.RGBAModel.Convert(color.Black).(color.RGBA)
18
19func main() {
20 led.Builtin.Enable()
21
22 if err := machine.I2C0.Configure(machine.I2CConfig{}); err != nil {
23 led.Flash(context.Background(), led.Builtin, time.Second/4, time.Second/4)
24 }
25
26 display := ssd1306.NewI2C(machine.I2C0)
27 display.Configure(
28 ssd1306.Config{
29 Width: 64,
30 Height: 32,
31
32 // https://github.com/tinygo-org/drivers/issues/842
33 ResetCol: ssd1306.ResetValue{0x20, 0x20+64-1},
34
35 Address: 0x3C,
36 },
37 )
38 display.ClearDisplay()
39 for i := 0; i < 64; i += 4 {
40 display.FillRectangle(0, 30, int16(i), 2, White)
41 display.Display()
42 }
43
44 prox := vl6180x.New(machine.I2C0)
45 prox.Configure(false)
46 prox.StartRangeContinuous(40) // milliseconds between samples
47
48 machine.D3.Configure(machine.PinConfig{machine.PinInputPullup})
49
50 var distance uint16
51 for {
52 if machine.D3.Get() {
53 continue
54 }
55
56 led.Builtin.SetColor(color.RGBA{0, 0, 1, 0})
57 start := time.Now()
58 distance = prox.Read()
59 duration := time.Now().Sub(start)
60 led.Builtin.SetColor(color.RGBA{0, 0, 0, 0})
61
62 // The top bar shows how long it took to read the value.
63 // One pixel is 100 microseconds.
64 // I'm seeing values around 2ms
65 duration_ms := duration / (100 * time.Microsecond)
66 if duration_ms > 50 {
67 duration_ms = 50
68 }
69 display.FillRectangle(int16(duration_ms), 0, 10, 8, Black)
70 display.FillRectangle(0, 0, int16(duration_ms), 8, White)
71
72 // The bottom bar is roughly what the bellows is reading.
73 distance = distance / 2
74 if distance > 50 {
75 distance = 50
76 }
77 display.FillRectangle(0, 16, 64, 8, Black)
78 display.FillRectangle(0, 16, int16(distance), 8, White)
79 display.Display()
80 }
81}