Created
August 19, 2019 13:22
-
-
Save deadprogram/9e9b8fe7931007c30a0ff71f98ed691e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TinyGo flight stick using analog joystick and 5 buttons | |
// Outputs data via serial port in very simple space-delimited format | |
// End of each line of data has "CR-LF" aka 0x13 0x10 | |
// Each update is sent every 50 ms. | |
package main | |
import ( | |
"image/color" | |
"machine" | |
"strconv" | |
"time" | |
"github.com/conejoninja/tinydraw" | |
"github.com/conejoninja/tinyfont" | |
// comes from "github.com/conejoninja/tinyfont/freemono" | |
freemono "./fonts" | |
"tinygo.org/x/drivers/ssd1306" | |
) | |
var ( | |
xPos uint16 | |
yPos uint16 | |
b1push bool | |
b2push bool | |
b3push bool | |
b4push bool | |
joypush bool | |
) | |
func main() { | |
machine.I2C0.Configure(machine.I2CConfig{ | |
Frequency: machine.TWI_FREQ_400KHZ, | |
}) | |
go handleDisplay() | |
machine.InitADC() | |
// joystick | |
stickX := machine.ADC{machine.A1} | |
stickX.Configure() | |
stickY := machine.ADC{machine.A0} | |
stickY.Configure() | |
// buttons | |
b1 := machine.D8 | |
b1.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) | |
b2 := machine.D9 | |
b2.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) | |
b3 := machine.D10 | |
b3.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) | |
b4 := machine.D11 | |
b4.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) | |
bJoy := machine.D12 | |
bJoy.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) | |
for { | |
xPos = stickX.Get() | |
yPos = stickY.Get() | |
b1push = !b1.Get() | |
b2push = !b2.Get() | |
b3push = !b3.Get() | |
b4push = !b4.Get() | |
joypush = !bJoy.Get() | |
println(xPos, yPos, b1push, b2push, b3push, b4push, joypush) | |
time.Sleep(time.Millisecond * 50) | |
} | |
} | |
func handleDisplay() { | |
display := ssd1306.NewI2C(machine.I2C0) | |
display.Configure(ssd1306.Config{ | |
Address: ssd1306.Address_128_32, | |
Width: 128, | |
Height: 64, | |
}) | |
display.ClearDisplay() | |
black := color.RGBA{1, 1, 1, 255} | |
for { | |
display.ClearBuffer() | |
x := strconv.Itoa(int(xPos)) | |
y := strconv.Itoa(int(yPos)) | |
msg := []byte("x: " + x) | |
tinyfont.WriteLine(&display, &freemono.Bold9pt7b, 10, 20, msg, black) | |
msg2 := []byte("y: " + y) | |
tinyfont.WriteLine(&display, &freemono.Bold9pt7b, 10, 40, msg2, black) | |
var radius int16 = 4 | |
if b1push { | |
tinydraw.FilledCircle(&display, 16+32*0, 64-radius-1, radius, black) | |
} else { | |
tinydraw.Circle(&display, 16+32*0, 64-radius-1, radius, black) | |
} | |
if b2push { | |
tinydraw.FilledCircle(&display, 16+32*1, 64-radius-1, radius, black) | |
} else { | |
tinydraw.Circle(&display, 16+32*1, 64-radius-1, radius, black) | |
} | |
if b3push { | |
tinydraw.FilledCircle(&display, 16+32*2, 64-radius-1, radius, black) | |
} else { | |
tinydraw.Circle(&display, 16+32*2, 64-radius-1, radius, black) | |
} | |
if b4push { | |
tinydraw.FilledCircle(&display, 16+32*3, 64-radius-1, radius, black) | |
} else { | |
tinydraw.Circle(&display, 16+32*3, 64-radius-1, radius, black) | |
} | |
display.Display() | |
time.Sleep(100 * time.Millisecond) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment