mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-09 09:23:39 +00:00
revision: modify proposed pin HAL.
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
package legacy
|
||||
|
||||
import "errors"
|
||||
|
||||
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
|
||||
// This is an alternative to drivers.PinOutput abstraction which is a function type. Pros and cons
|
||||
// of both approaches have been discussed in the [relevant issue]. PinOutput should only be used
|
||||
// to expose an initialization function of a driver that receives pins of this type. Ideally
|
||||
// driver developers should also expose the initialization with drivers.Pin type:
|
||||
//
|
||||
// func New(p1, p2, p3 legacy.PinOutput) *Device {
|
||||
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
|
||||
// }
|
||||
//
|
||||
// func NewWithPinfuncs(p1, p2, p3 drivers.PinOutput) *Device {
|
||||
// return &Device{p1:p1, p2:p2, p3:p3}
|
||||
// }
|
||||
//
|
||||
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
|
||||
type PinOutput interface {
|
||||
Set(level bool)
|
||||
}
|
||||
|
||||
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
|
||||
// more information on why this is "legacy".
|
||||
type PinInput interface {
|
||||
Get() (level bool)
|
||||
}
|
||||
|
||||
// ConfigurePinOut is a legacy function used to configure pins as outputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinOut(po PinOutput) {
|
||||
configurePinOut(po)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInputPulldown(pi PinInput) {
|
||||
configurePinInputPulldown(pi)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInput(pi PinInput) {
|
||||
configurePinInput(pi)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInputPullup(pi PinInput) {
|
||||
configurePinInputPullup(pi)
|
||||
}
|
||||
|
||||
// PinIsNoPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
|
||||
//
|
||||
// Deprecated: Drivers do not require pin knowledge from now on.
|
||||
func PinIsNoPin(pin any) bool {
|
||||
return pinIsNoPin(pin)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
|
||||
)
|
||||
@@ -1,9 +0,0 @@
|
||||
//go:build !baremetal
|
||||
|
||||
package legacy
|
||||
|
||||
func configurePinOut(p PinOutput) {}
|
||||
func configurePinInput(p PinInput) {}
|
||||
func configurePinInputPulldown(p PinInput) {}
|
||||
func configurePinInputPullup(p PinInput) {}
|
||||
func pinIsNoPin(a any) bool { return false }
|
||||
@@ -1,33 +0,0 @@
|
||||
//go:build baremetal
|
||||
|
||||
package legacy
|
||||
|
||||
import "machine"
|
||||
|
||||
func configurePinOut(p PinOutput) {
|
||||
configurePin(p, machine.PinOutput)
|
||||
}
|
||||
|
||||
func configurePinInputPulldown(p PinInput) {
|
||||
configurePin(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
|
||||
}
|
||||
|
||||
func configurePinInput(p PinInput) {
|
||||
configurePin(p, machine.PinInput)
|
||||
}
|
||||
|
||||
func configurePinInputPullup(p PinInput) {
|
||||
configurePin(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
|
||||
}
|
||||
|
||||
func pinIsNoPin(a any) bool {
|
||||
p, ok := a.(machine.Pin)
|
||||
return ok && p == machine.NoPin
|
||||
}
|
||||
|
||||
func configurePin(p any, mode machine.PinMode) {
|
||||
machinePin, ok := p.(machine.Pin)
|
||||
if ok {
|
||||
machinePin.Configure(machine.PinConfig{Mode: mode})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package pin
|
||||
|
||||
import "errors"
|
||||
|
||||
// OutputFunc is hardware abstraction for a pin which outputs a
|
||||
// digital signal (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.OutputFunc
|
||||
// led := machine.LED
|
||||
// led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
// var p pin.OutputFuncFunc = led.Set // Going from a machine.Pin to a pin.OutputFunc
|
||||
type OutputFunc func(level bool)
|
||||
|
||||
func (o OutputFunc) High() {
|
||||
o(true)
|
||||
}
|
||||
|
||||
func (o OutputFunc) Low() {
|
||||
o(false)
|
||||
}
|
||||
|
||||
// InputFunc is hardware abstraction for a pin which receives a
|
||||
// digital signal and reads it (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.InputFunc
|
||||
// input := machine.LED
|
||||
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.PinInput
|
||||
// var p pin.InputFunc = input.Get // Going from a machine.Pin to a drivers.PinInput
|
||||
type InputFunc func() (level bool)
|
||||
|
||||
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
|
||||
// This is an wrapper to pin.OutputFunc abstraction which is a function type.
|
||||
//
|
||||
// func New(p1, p2, p3 pin.Output) *Device {
|
||||
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
|
||||
// }
|
||||
//
|
||||
// func NewWithPinfuncs(p1, p2, p3 pin.OutputFunc) *Device {
|
||||
// return &Device{p1:p1, p2:p2, p3:p3}
|
||||
// }
|
||||
//
|
||||
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
|
||||
type Output interface {
|
||||
Set(level bool)
|
||||
}
|
||||
|
||||
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
|
||||
// more information on why this is "legacy".
|
||||
type Input interface {
|
||||
Get() (level bool)
|
||||
}
|
||||
|
||||
// ConfigureOutput is a legacy function used to configure pins as outputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureOutput(po Output) {
|
||||
configureOutput(po)
|
||||
}
|
||||
|
||||
// ConfigureInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInputPulldown(pi Input) {
|
||||
configureInputPulldown(pi)
|
||||
}
|
||||
|
||||
// ConfigureInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInput(pi Input) {
|
||||
configureInput(pi)
|
||||
}
|
||||
|
||||
// ConfigureInputPullup is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInputPullup(pi Input) {
|
||||
configureInputPullup(pi)
|
||||
}
|
||||
|
||||
// IsNotPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
|
||||
//
|
||||
// Deprecated: Drivers should not require pin knowledge.
|
||||
func IsNotPin(pin any) bool {
|
||||
return isNotPin(pin)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !baremetal
|
||||
|
||||
package pin
|
||||
|
||||
func configureOutput(p Output) {}
|
||||
func configureInput(p Input) {}
|
||||
func configureInputPulldown(p Input) {}
|
||||
func configureInputPullup(p Input) {}
|
||||
func isNotPin(a any) bool { return false }
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build baremetal && fe310
|
||||
|
||||
package legacy
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build baremetal && !fe310
|
||||
|
||||
package legacy
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build baremetal
|
||||
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
func configureOutput(p Output) {
|
||||
configure(p, machine.PinOutput)
|
||||
}
|
||||
|
||||
func configureInputPulldown(p Input) {
|
||||
configure(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
|
||||
}
|
||||
|
||||
func configureInput(p Input) {
|
||||
configure(p, machine.PinInput)
|
||||
}
|
||||
|
||||
func configureInputPullup(p Input) {
|
||||
configure(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
|
||||
}
|
||||
|
||||
func isNotPin(a any) bool {
|
||||
p, ok := a.(machine.Pin)
|
||||
return ok && p == machine.NoPin
|
||||
}
|
||||
|
||||
func configure(p any, mode machine.PinMode) {
|
||||
machinePin, ok := p.(machine.Pin)
|
||||
if ok {
|
||||
machinePin.Configure(machine.PinConfig{Mode: mode})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user