forked from tinygo-org/gobadge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.go
154 lines (130 loc) · 4.24 KB
/
badge.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
package main
import (
"image/color"
"time"
"tinygo.org/x/drivers/shifter"
"tinygo.org/x/tinydraw"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freesans"
"tinygo.org/x/tinyfont/gophers"
)
type Badge struct {
rainbow []color.RGBA
quit bool
}
func NewBadge() *Badge {
rainbow := make([]color.RGBA, 256)
for i := 0; i < 256; i++ {
rainbow[i] = getRainbowRGB(uint8(i))
}
if Name == "" {
Name = DefaultName
}
if Slogan1 == "" {
Slogan1 = DefaultSlogan1
}
if Slogan2 == "" {
Slogan2 = DefaultSlogan2
}
return &Badge{rainbow: rainbow}
}
func (b *Badge) Draw() {
b.quit = false
display.FillScreen(colors[GOPHERBLUE])
for {
b.logo()
if b.quit {
break
}
display.FillScreen(colors[WHITE])
b.showSlogan(Slogan1, Slogan2)
if b.quit {
break
}
display.FillScreen(colors[GOPHERBLUE])
b.scrollGoBanner()
if b.quit {
break
}
display.FillScreen(colors[WHITE])
b.greetSelf(Name)
if b.quit {
break
}
}
}
func (b *Badge) logo() {
const logoDisplayTime = 8 * time.Second
display.FillRectangleWithBuffer(0, 0, WIDTH, HEIGHT, logoRGBA)
time.Sleep(logoDisplayTime)
}
func (b *Badge) greetSelf(name string) {
const r int16 = 8
// black corners
display.FillRectangle(0, 0, r, r, colors[BLACK])
display.FillRectangle(0, HEIGHT-r, r, r, colors[BLACK])
display.FillRectangle(WIDTH-r, 0, r, r, colors[BLACK])
display.FillRectangle(WIDTH-r, HEIGHT-r, r, r, colors[BLACK])
// round corners
tinydraw.FilledCircle(&display, r, r, r, colors[GOPHERBLUE])
tinydraw.FilledCircle(&display, WIDTH-r-1, r, r, colors[GOPHERBLUE])
tinydraw.FilledCircle(&display, r, HEIGHT-r-1, r, colors[GOPHERBLUE])
tinydraw.FilledCircle(&display, WIDTH-r-1, HEIGHT-r-1, r, colors[GOPHERBLUE])
// top band
display.FillRectangle(r, 0, WIDTH-2*r-1, r, colors[GOPHERBLUE])
display.FillRectangle(0, r, WIDTH, 26, colors[GOPHERBLUE])
// bottom band
display.FillRectangle(r, HEIGHT-r-1, WIDTH-2*r-1, r+1, colors[GOPHERBLUE])
display.FillRectangle(0, HEIGHT-2*r-1, WIDTH, r, colors[GOPHERBLUE])
// gophers fonts
tinyfont.WriteLine(&display, &gophers.Regular32pt, 10, 32, "BXYZWB", colors[WHITE])
tinyfont.WriteLine(&display, &gophers.Regular32pt, 10, 110, "AGENIV", colors[GOPHERBLUE])
w32, _ := tinyfont.LineWidth(&freesans.Bold9pt7b, name)
for i := 0; i < 230; i++ {
tinyfont.WriteLineColors(&display, &freesans.Bold9pt7b, (WIDTH-int16(w32))/2, 72, name, b.rainbow[i:])
buttons.ReadInput()
if buttons.Pins[shifter.BUTTON_START].Get() {
b.quit = true
break
}
}
}
func (b *Badge) scrollGoBanner() {
const top, mid, bottom = "KEEP CALM", "AND", " ON"
w32top, _ := tinyfont.LineWidth(&freesans.Bold9pt7b, top)
w32middle, _ := tinyfont.LineWidth(&freesans.Bold9pt7b, mid)
w32bottomRight, _ := tinyfont.LineWidth(&freesans.Bold9pt7b, bottom)
w32bottomLeft, _ := tinyfont.LineWidth(&gophers.Regular32pt, "H")
tinyfont.WriteLine(&display, &gophers.Regular32pt, (WIDTH-int16(w32middle))/2+8, 30, "P", colors[BLACK])
tinyfont.WriteLine(&display, &freesans.Bold9pt7b, (WIDTH-int16(w32top))/2, 40, top, colors[WHITE])
tinyfont.WriteLine(&display, &freesans.Bold9pt7b, (WIDTH-int16(w32middle))/2, 70, mid, colors[WHITE])
tinyfont.WriteLine(&display, &gophers.Regular58pt, (WIDTH-int16(w32bottomRight+w32bottomLeft+45))/2, 115, "H", colors[BLACK])
tinyfont.WriteLine(&display, &freesans.Bold9pt7b, (WIDTH-int16(w32bottomRight+w32bottomLeft+45))/2+45, 100, bottom, colors[WHITE])
display.SetScrollArea(0, 0)
for k := 0; k < 4; k++ {
for i := int16(159); i >= 0; i-- {
buttons.ReadInput()
if buttons.Pins[shifter.BUTTON_START].Get() {
b.quit = true
break
}
display.SetScroll(i)
time.Sleep(10 * time.Millisecond)
}
}
display.SetScroll(0)
display.StopScroll()
}
func (b *Badge) showSlogan(topline, bottomline string) {
w32top, _ := tinyfont.LineWidth(&freesans.Regular9pt7b, topline)
w32bottom, _ := tinyfont.LineWidth(&freesans.Regular9pt7b, bottomline)
for i := int16(0); i < 20; i++ {
tinyfont.WriteLine(&display, &freesans.Regular9pt7b, (WIDTH-int16(w32top))/2, 50, topline, getRainbowRGB(uint8(i*12)))
tinyfont.WriteLine(&display, &freesans.Regular9pt7b, (WIDTH-int16(w32bottom))/2, 80, bottomline, getRainbowRGB(uint8(i*12)))
buttons.ReadInput()
if buttons.Pins[shifter.BUTTON_START].Get() {
b.quit = true
break
}
}
}