Commit 121a42bf authored by Thierry's avatar Thierry
Browse files

add joystick and button for pygamer

parent 85ff7ee8
module joystick_buttons
go 1.15
require (
tinygo.org/x/drivers v0.17.1
tinygo.org/x/tinydraw v0.0.0-20200416172542-c30d6d84353c
tinygo.org/x/tinyfont v0.2.1
)
github.com/bgould/http v0.0.0-20190627042742-d268792bdee7/go.mod h1:BTqvVegvwifopl4KTEDth6Zezs9eR+lCWhvGKvkxJHE=
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
tinygo.org/x/drivers v0.16.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
tinygo.org/x/drivers v0.17.1 h1:pwr/gZAfQgx7Gs71AO+YZBG0QlWfSZrfQ4H17TIGRE0=
tinygo.org/x/drivers v0.17.1/go.mod h1:+uFfVgSjxRPqsnalFrcQse/Tmhoxwl9AJmJIVuRbuRo=
tinygo.org/x/tinydraw v0.0.0-20200416172542-c30d6d84353c h1:QKOUxoOXDNosHIoX7PrANTGd5nmLjKKFZ81pXhkjw58=
tinygo.org/x/tinydraw v0.0.0-20200416172542-c30d6d84353c/go.mod h1:ygmD8mKwhhF6HLXIs4FCe5JTYurTD2w32cmymeaYrEw=
tinygo.org/x/tinyfont v0.2.1 h1:FAaemBzw8wsfhAtG6fWW+QjyWw/K8YqEeiWo4N1pv4o=
tinygo.org/x/tinyfont v0.2.1/go.mod h1:eLqnYSrFRjt5STxWaMeOWJTzrKhXqpWw7nU3bPfKOAM=
tinygo.org/x/tinyfs v0.1.0/go.mod h1:ysc8Y92iHfhTXeyEM9+c7zviUQ4fN9UCFgSOFfMWv20=
tinygo.org/x/tinyterm v0.1.0/go.mod h1:/DDhNnGwNF2/tNgHywvyZuCGnbH3ov49Z/6e8LPLRR4=
package main
import (
"image/color"
"machine"
"strconv"
"tinygo.org/x/drivers/st7735"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freemono"
)
var black = color.RGBA{0, 0, 0, 0}
var red = color.RGBA{255, 0, 0, 255}
var gray = color.RGBA{200, 200, 200, 255}
func main() {
buttons := NewButtons()
buttons.Configure()
joystick := NewJoystick()
joystick.configure()
display := st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)
machine.SPI1.Configure(machine.SPIConfig{
Frequency: 16000000,
SCK: machine.SPI1_SCK_PIN,
SDO: machine.SPI1_SDO_PIN,
SDI: machine.SPI1_SDI_PIN,
})
display.Configure(st7735.Config{Rotation: st7735.ROTATION_90})
display.FillScreen(black)
var buttons_pressed = ""
var x_axis uint16 = 0
var y_axis uint16 = 0
for {
x_axis = joystick.getX()
y_axis = joystick.getY()
buttons_pressed = ""
pressed, _ := buttons.Read8Input()
if pressed != 0 {
println(pressed)
}
if pressed&machine.BUTTON_A_MASK > 0 {
buttons_pressed += "BTN A"
println("BTN A")
}
if pressed&machine.BUTTON_B_MASK > 0 {
buttons_pressed += "BTN B"
println("BTN B")
}
if pressed&machine.BUTTON_SELECT_MASK > 0 {
buttons_pressed += "SELECT"
println("SELECT")
}
if pressed&machine.BUTTON_START_MASK > 0 {
buttons_pressed += "START"
println("START")
}
display.FillScreen(black)
tinyfont.WriteLine(&display, &freemono.Bold9pt7b, 10, 20, strconv.Itoa(int(x_axis)), gray)
tinyfont.WriteLine(&display, &freemono.Bold9pt7b, 10, 50, strconv.Itoa(int(y_axis)), gray)
tinyfont.WriteLine(&display, &freemono.Bold9pt7b, 10, 80, buttons_pressed, gray)
}
}
package main
import (
"machine"
"tinygo.org/x/drivers/shifter"
)
// NewButtons returns a new shifter device for the buttons on an AdaFruit PyBadge
func NewButtons() *shifter.Device {
var device = shifter.New(shifter.EIGHT_BITS, machine.BUTTON_LATCH, machine.BUTTON_CLK, machine.BUTTON_OUT)
return &device
}
type joystick struct {
x machine.ADC
y machine.ADC
}
func NewJoystick() *joystick {
return &joystick{x: machine.ADC{machine.JOYX}, y: machine.ADC{machine.JOYY}}
}
func (j *joystick) configure() {
j.x.Configure(machine.ADCConfig{})
j.y.Configure(machine.ADCConfig{})
}
func (j *joystick) getX() uint16 {
return j.x.Get()
}
func (j *joystick) getY() uint16 {
return j.y.Get()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment