65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/encoders"
|
|
)
|
|
|
|
const deadzone = 5000
|
|
|
|
func main() {
|
|
btnA := machine.P1_06
|
|
btnB := machine.P1_04
|
|
btnC := machine.P0_22 // rotary encoder push button
|
|
btnA.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
|
btnB.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
|
btnC.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
|
|
|
enc := encoders.NewQuadratureViaInterrupt(machine.P1_00, machine.P0_24)
|
|
enc.Configure(encoders.QuadratureConfig{Precision: 4})
|
|
|
|
machine.InitADC()
|
|
ax := machine.ADC{Pin: machine.P0_02}
|
|
ay := machine.ADC{Pin: machine.P0_29}
|
|
ax.Configure(machine.ADCConfig{})
|
|
ay.Configure(machine.ADCConfig{})
|
|
|
|
prevA := true
|
|
prevB := true
|
|
prevC := true
|
|
prevPos := enc.Position()
|
|
|
|
for {
|
|
a := btnA.Get()
|
|
b := btnB.Get()
|
|
c := btnC.Get()
|
|
if !a && prevA {
|
|
println("button A pressed")
|
|
}
|
|
if !b && prevB {
|
|
println("button B pressed")
|
|
}
|
|
if !c && prevC {
|
|
println("encoder button pressed")
|
|
}
|
|
prevA = a
|
|
prevB = b
|
|
prevC = c
|
|
|
|
pos := enc.Position()
|
|
if pos != prevPos {
|
|
println("encoder:", pos)
|
|
prevPos = pos
|
|
}
|
|
|
|
rawX := int(ax.Get()) - 32767
|
|
rawY := int(ay.Get()) - 32767
|
|
if rawX > deadzone || rawX < -deadzone || rawY > deadzone || rawY < -deadzone {
|
|
println("joystick:", "x=", rawX, "y=", rawY)
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|