-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
visor.go
124 lines (106 loc) · 2.74 KB
/
visor.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
package gopherhelmet
import (
"image/color"
"machine"
"strings"
"time"
"github.com/conejoninja/gopher-helmet/fonts"
"github.com/conejoninja/gopher-helmet/ledstripdisplay"
"tinygo.org/x/tinyfont"
"tinygo.org/x/drivers/ws2812"
)
// VisorDevice controls the Gopherbot Visor Neopixel LED.
type VisorDevice struct {
*ledstripdisplay.Device
}
// Visor returns a new VisorDevice to control Gopherbot Visor.
func Visor() *VisorDevice {
neo := visorPin
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
v := ws2812.New(neo)
display := ledstripdisplay.New(&v, VisorWidth, VisorHeight, ledstripdisplay.LAYOUT_Z)
display.Configure(ledstripdisplay.Config{Rotation: ledstripdisplay.ROTATION_180})
display.ClearDisplay()
display.Display()
return &VisorDevice{
Device: &display,
}
}
// Show sets the visor to display the current LED array state.
func (v *VisorDevice) Show() {
v.Display()
}
// Off turns off all the LEDs.
func (v *VisorDevice) Off() {
v.Clear()
}
// Clear clears the visor.
func (v *VisorDevice) Clear() {
v.ClearDisplay()
}
func (v *VisorDevice) BootUp() {
// 3 beeps
for i := 0; i < 3; i++ {
time.Sleep(800 * time.Millisecond)
v.ClearDisplay()
v.Display()
time.Sleep(800 * time.Millisecond)
v.SetPixel(8, 2, Red)
v.Display()
}
time.Sleep(400 * time.Millisecond)
// line
for i := int16(0); i < 8; i++ {
v.SetPixel(8-1-i, 2, Red)
v.SetPixel(8+1+i, 2, Red)
v.Display()
time.Sleep(100 * time.Millisecond)
}
// grow vertically
for i := int16(0); i < 17; i++ {
v.SetPixel(i, 1, Red)
v.SetPixel(i, 3, Red)
}
v.Display()
time.Sleep(100 * time.Millisecond)
for i := int16(0); i < 17; i++ {
v.SetPixel(i, 0, Red)
v.SetPixel(i, 4, Red)
}
v.Display()
time.Sleep(100 * time.Millisecond)
v.Clear()
}
func (v *VisorDevice) Marquee(text string, c color.RGBA) {
w32, _ := tinyfont.LineWidth(&fonts.TomThumb, text)
for i := int16(17); i > int16(-w32); i-- {
v.Clear()
tinyfont.WriteLine(v.Device, &fonts.TomThumb, i, 5, text, c)
v.Display()
time.Sleep(200 * time.Millisecond)
}
}
// TextColorSequence - sequence of stings, each can have different color
type TextColorSequence []TextColorSequenceElement
type TextColorSequenceElement struct {
Text string
Color color.RGBA
}
func (v *VisorDevice) MarqueeColored(sequence TextColorSequence) {
var wholeText strings.Builder
var colors []color.RGBA
for _, element := range sequence {
for _, t := range element.Text {
wholeText.WriteRune(t)
colors = append(colors, element.Color)
}
}
text := wholeText.String()
w32, _ := tinyfont.LineWidth(&tinyfont.TomThumb, text)
for i := int16(17); i > int16(-w32); i-- {
v.Clear()
tinyfont.WriteLineColors(v.Device, &tinyfont.TomThumb, i, 5, text, colors)
v.Display()
time.Sleep(200 * time.Millisecond)
}
}