133 lines
2.8 KiB
Go
133 lines
2.8 KiB
Go
//go:build gopher_badge
|
|
// +build gopher_badge
|
|
|
|
package main
|
|
|
|
import (
|
|
"image/color"
|
|
"machine"
|
|
|
|
"tinygo.org/x/drivers/lis3dh"
|
|
"tinygo.org/x/drivers/st7789"
|
|
"tinygo.org/x/drivers/ws2812"
|
|
)
|
|
|
|
const (
|
|
displayWidth = 320
|
|
displayHeight = 240
|
|
)
|
|
|
|
// UI layout constants for gopherbadge (320x240)
|
|
const (
|
|
// myNameIs layout
|
|
cornerRadius = 10
|
|
TopBandHeight = 54
|
|
BottomBandHeight = 20
|
|
HelloY = 34
|
|
MyNameIsY = 54
|
|
GophersY = 208
|
|
GophersX = 84
|
|
|
|
// menu layout
|
|
menuItemSpace = 20
|
|
menuCircleR = 6
|
|
|
|
numLEDs = 2
|
|
|
|
buttonBack = buttonB
|
|
)
|
|
|
|
var menuOptions = []int8{
|
|
modeBadge,
|
|
modeSchedule,
|
|
modeAdventure,
|
|
modeLEDs,
|
|
modeAccelerometer,
|
|
modeMusic,
|
|
modeGameSnake,
|
|
modeGameLife,
|
|
modeGameColors,
|
|
modeInfo,
|
|
}
|
|
|
|
// Font size thresholds for gopherbadge
|
|
const (
|
|
FontWidthThreshold = 300
|
|
)
|
|
|
|
var (
|
|
display st7789.Device
|
|
btnA machine.Pin
|
|
btnB machine.Pin
|
|
btnUp machine.Pin
|
|
btnLeft machine.Pin
|
|
btnDown machine.Pin
|
|
btnRight machine.Pin
|
|
)
|
|
|
|
func initHardware() {
|
|
machine.SPI0.Configure(machine.SPIConfig{
|
|
Frequency: 8000000,
|
|
Mode: 0,
|
|
})
|
|
|
|
machine.I2C0.Configure(machine.I2CConfig{
|
|
SCL: machine.I2C0_SCL_PIN,
|
|
SDA: machine.I2C0_SDA_PIN,
|
|
})
|
|
accel = lis3dh.New(machine.I2C0)
|
|
accel.Address = lis3dh.Address0
|
|
accel.Configure()
|
|
|
|
display = st7789.New(machine.SPI0,
|
|
machine.TFT_RST, // TFT_RESET
|
|
machine.TFT_WRX, // TFT_DC
|
|
machine.TFT_CS, // TFT_CS
|
|
machine.TFT_BACKLIGHT) // TFT_LITE
|
|
|
|
display.Configure(st7789.Config{
|
|
Rotation: st7789.ROTATION_270,
|
|
Height: 320,
|
|
})
|
|
|
|
neo := machine.NEOPIXELS
|
|
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
leds = ws2812.New(neo)
|
|
|
|
bzrPin = machine.SPEAKER
|
|
bzrPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
|
|
speaker := machine.SPEAKER_ENABLE
|
|
speaker.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
speaker.High()
|
|
|
|
btnA = machine.BUTTON_A
|
|
btnB = machine.BUTTON_B
|
|
btnUp = machine.BUTTON_UP
|
|
btnLeft = machine.BUTTON_LEFT
|
|
btnDown = machine.BUTTON_DOWN
|
|
btnRight = machine.BUTTON_RIGHT
|
|
btnA.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
btnB.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
btnUp.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
btnLeft.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
btnDown.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
btnRight.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
|
|
black := color.RGBA{0, 0, 0, 255}
|
|
display.FillScreen(black)
|
|
|
|
}
|
|
|
|
func getInput() {
|
|
for b := 0; b < 9; b++ {
|
|
buttonsOldState[b] = buttonsState[b]
|
|
}
|
|
|
|
buttonsState[buttonUp] = !btnUp.Get()
|
|
buttonsState[buttonRight] = !btnRight.Get()
|
|
buttonsState[buttonDown] = !btnDown.Get()
|
|
buttonsState[buttonLeft] = !btnLeft.Get()
|
|
buttonsState[buttonA] = !btnA.Get()
|
|
buttonsState[buttonB] = !btnB.Get()
|
|
}
|