This repository was archived by the owner on Mar 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
179 lines (160 loc) · 5.53 KB
/
main.go
File metadata and controls
179 lines (160 loc) · 5.53 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers/st7735"
"tinygo.org/x/drivers/shifter"
)
func main() {
machine.SPI1.Configure(machine.SPIConfig{
SCK: machine.SPI1_SCK_PIN,
SDO: machine.SPI1_SDO_PIN,
SDI: machine.SPI1_SDI_PIN,
Frequency: 8000000,
})
machine.I2C0.Configure(machine.I2CConfig{SCL: machine.SCL_PIN, SDA: machine.SDA_PIN})
display := st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)
display.Configure(st7735.Config{
Rotation: st7735.ROTATION_90,
})
var buttons shifter.Device
buttons = shifter.NewButtons()
buttons.Configure()
bzrPin := machine.A0
bzrPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
speaker := machine.SPEAKER_ENABLE
speaker.Configure(machine.PinConfig{Mode: machine.PinOutput})
speaker.High()
display.EnableBacklight(true)
display.FillScreen(color.RGBA{R: 0, G: 0, B: 0})
currentMode := 0
oldMode := -1
funMode := false
released := true
funmodePitchAlternate := false
objectX := int16(10)
objectY := int16(10)
objectVelocityX := int16(2)
objectVelocityY := int16(4)
for {
pressed, _ := buttons.ReadInput()
if released && buttons.Pins[shifter.BUTTON_START].Get() {
funMode = !funMode
if !funMode {
oldMode = -1
playTone(1500, 15, &bzrPin)
} else {
playTone(1300, 15, &bzrPin)
}
// Debounce
time.Sleep(100 * time.Millisecond)
}
if released && buttons.Pins[shifter.BUTTON_SELECT].Get() {
if currentMode < 2 {
currentMode += 1
} else {
currentMode = 0
}
playTone(2000, 15, &bzrPin)
// Debounce
time.Sleep(100 * time.Millisecond)
}
if pressed == 0 {
released = true
} else {
released = false
}
//|| funMode
if oldMode != currentMode {
display.FillRectangleWithBuffer(0, 0, 160, 36, logoRGBAheader)
if currentMode == 0 {
display.FillRectangleWithBuffer(0, 36, 160, 71, logoRGBA2)
} else if currentMode == 1 {
display.FillRectangleWithBuffer(0, 36, 160, 71, logoRGBA3)
} else if currentMode == 2 {
display.FillRectangleWithBuffer(0, 36, 160, 71, logoRGBA4)
} else {
display.FillScreen(color.RGBA{R: 0, G: 0, B: 0})
}
display.FillRectangleWithBuffer(0, 107, 160, 21, logoRGBAfooter)
oldMode = currentMode
}
if funMode {
objectX += objectVelocityX
objectY += objectVelocityY
if objectX < 0 || objectX+28 > 160 {
if objectX < 0 {
objectX = 1
} else if objectX+28 > 160 {
objectX = 160 - 29
}
// Bounce
objectVelocityX = -objectVelocityX
if funmodePitchAlternate {
playTone(900, 30, &bzrPin)
} else {
playTone(700, 30, &bzrPin)
}
funmodePitchAlternate = !funmodePitchAlternate
}
if objectY < 0 || objectY+28 > 128 {
if objectY < 0 {
objectY = 1
} else if objectY+28 > 128 {
objectY = 128 - 29
}
// Bounce
objectVelocityY = -objectVelocityY
if funmodePitchAlternate {
playTone(300, 30, &bzrPin)
} else {
playTone(500, 30, &bzrPin)
}
funmodePitchAlternate = !funmodePitchAlternate
}
for x := int16(0); x < 28; x++ {
display.DrawFastVLine(objectX+x, objectY, objectY+28, color.RGBA{R: 255, G: 255, B: 255})
}
display.DrawFastVLine(objectX+3, objectY+3, objectY+19, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastVLine(objectX+20, objectY+3, objectY+7, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastVLine(objectX+6, objectY+7, objectY+17, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastVLine(objectX+20, objectY+17, objectY+20, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastHLine(objectX+3, objectX+20, objectY+3, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastHLine(objectX+6, objectX+20, objectY+7, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastHLine(objectX+6, objectX+20, objectY+17, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastHLine(objectX+3, objectX+20, objectY+20, color.RGBA{R: 255, G: 102, B: 255})
display.DrawFastVLine(objectX+9, objectY+9, objectY+20, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastVLine(objectX+26, objectY+10, objectY+13, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastVLine(objectX+12, objectY+13, objectY+20, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastVLine(objectX+9, objectY+23, objectY+27, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastVLine(objectX+23, objectY+17, objectY+23, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastVLine(objectX+26, objectY+17, objectY+27, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastHLine(objectX+9, objectX+26, objectY+10, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastHLine(objectX+12, objectX+26, objectY+13, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastHLine(objectX+9, objectX+12, objectY+20, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastHLine(objectX+23, objectX+26, objectY+17, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastHLine(objectX+9, objectX+23, objectY+23, color.RGBA{R: 255, G: 76, B: 38})
display.DrawFastHLine(objectX+9, objectX+26, objectY+27, color.RGBA{R: 255, G: 76, B: 38})
time.Sleep(30 * time.Millisecond)
}
}
}
func playTone(tone int, length int, bzrPin *machine.Pin) {
for i := 0; i < length; i++ {
bzrPin.High()
time.Sleep(time.Duration(tone) * time.Microsecond)
bzrPin.Low()
time.Sleep(time.Duration(tone) * time.Microsecond)
}
}
func getRainbowRGB(i uint8) color.RGBA {
if i < 85 {
return color.RGBA{i * 3, 255 - i*3, 0, 255}
} else if i < 170 {
i -= 85
return color.RGBA{255 - i*3, 0, i * 3, 255}
}
i -= 170
return color.RGBA{0, i * 3, 255 - i*3, 255}
}