mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 04:43:25 +00:00
shifter: simplify API surface for PyBadge (#137)
* shifter: simplify API surface and use build directive to directly match the PyBadge Signed-off-by: Ron Evans <ron@hybridgroup.com> * shifter: further simplify API for PyBadge Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
+11
-22
@@ -1,53 +1,42 @@
|
|||||||
|
// This example is designed to implement the button shifter for a PyBadge.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"machine"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tinygo.org/x/drivers/shifter"
|
"tinygo.org/x/drivers/shifter"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
BUTTON_LEFT = iota
|
|
||||||
BUTTON_UP
|
|
||||||
BUTTON_DOWN
|
|
||||||
BUTTON_RIGHT
|
|
||||||
BUTTON_SELECT
|
|
||||||
BUTTON_START
|
|
||||||
BUTTON_A
|
|
||||||
BUTTON_B
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
buttons := shifter.New(shifter.EIGHT_BITS, machine.BUTTON_LATCH, machine.BUTTON_CLK, machine.BUTTON_OUT)
|
buttons := shifter.NewButtons()
|
||||||
buttons.Configure()
|
buttons.Configure()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Update the pins state, to later be returned by .Get()
|
// Update the pins state, to later be returned by .Get()
|
||||||
buttons.Read8Input()
|
buttons.ReadInput()
|
||||||
|
|
||||||
if buttons.Pins[BUTTON_LEFT].Get() {
|
if buttons.Pins[shifter.BUTTON_LEFT].Get() {
|
||||||
println("Button LEFT pressed")
|
println("Button LEFT pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_UP].Get() {
|
if buttons.Pins[shifter.BUTTON_UP].Get() {
|
||||||
println("Button UP pressed")
|
println("Button UP pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_DOWN].Get() {
|
if buttons.Pins[shifter.BUTTON_DOWN].Get() {
|
||||||
println("Button DOWN pressed")
|
println("Button DOWN pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_RIGHT].Get() {
|
if buttons.Pins[shifter.BUTTON_RIGHT].Get() {
|
||||||
println("Button RIGHT pressed")
|
println("Button RIGHT pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_SELECT].Get() {
|
if buttons.Pins[shifter.BUTTON_SELECT].Get() {
|
||||||
println("Button SELECT pressed")
|
println("Button SELECT pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_START].Get() {
|
if buttons.Pins[shifter.BUTTON_START].Get() {
|
||||||
println("Button START pressed")
|
println("Button START pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_A].Get() {
|
if buttons.Pins[shifter.BUTTON_A].Get() {
|
||||||
println("Button A pressed")
|
println("Button A pressed")
|
||||||
}
|
}
|
||||||
if buttons.Pins[BUTTON_B].Get() {
|
if buttons.Pins[shifter.BUTTON_B].Get() {
|
||||||
println("Button B pressed")
|
println("Button B pressed")
|
||||||
}
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
// +build pybadge
|
||||||
|
|
||||||
|
package shifter
|
||||||
|
|
||||||
|
import "machine"
|
||||||
|
|
||||||
|
const (
|
||||||
|
BUTTON_LEFT = 0
|
||||||
|
BUTTON_UP = 1
|
||||||
|
BUTTON_DOWN = 2
|
||||||
|
BUTTON_RIGHT = 3
|
||||||
|
BUTTON_SELECT = 4
|
||||||
|
BUTTON_START = 5
|
||||||
|
BUTTON_A = 6
|
||||||
|
BUTTON_B = 7
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewButtons returns a new shifter device for the buttons on an AdaFruit PyBadge
|
||||||
|
func NewButtons() Device {
|
||||||
|
return Device{
|
||||||
|
latch: machine.BUTTON_LATCH,
|
||||||
|
clk: machine.BUTTON_CLK,
|
||||||
|
out: machine.BUTTON_OUT,
|
||||||
|
Pins: make([]ShiftPin, int(EIGHT_BITS)),
|
||||||
|
bits: EIGHT_BITS,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadInput returns the latest input readings from the PyBadge.
|
||||||
|
func (d *Device) ReadInput() (uint8, error) {
|
||||||
|
return d.Read8Input()
|
||||||
|
}
|
||||||
+1
-1
@@ -30,7 +30,7 @@ type ShiftPin struct {
|
|||||||
pressed bool
|
pressed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new thermistor driver given an ADC pin.
|
// New returns a new shifter driver given the correct pins.
|
||||||
func New(numBits NumberBit, latch, clk, out machine.Pin) Device {
|
func New(numBits NumberBit, latch, clk, out machine.Pin) Device {
|
||||||
return Device{
|
return Device{
|
||||||
latch: latch,
|
latch: latch,
|
||||||
|
|||||||
Reference in New Issue
Block a user