revision: modify proposed pin HAL.

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2025-09-16 20:33:50 +02:00
parent f55c0b1853
commit dcf53ac04a
38 changed files with 554 additions and 554 deletions
+52 -52
View File
@@ -4,7 +4,7 @@ package easystepper // import "tinygo.org/x/drivers/easystepper"
import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
)
// StepMode determines the coil sequence used to perform a single step
@@ -32,7 +32,7 @@ func (sm StepMode) stepCount() uint {
// Device holds the pins and the delay between steps
type Device struct {
pins [4]drivers.PinOutput
pins [4]pin.OutputFunc
config func()
stepDelay time.Duration
stepNumber uint8
@@ -62,8 +62,8 @@ func (d *Device) Move(steps int32) {
// Off turns off all motor pins
func (d *Device) Off() {
for _, pin := range d.pins {
pin(false)
for _, p := range d.pins {
p.Low()
}
}
@@ -124,28 +124,28 @@ func (d *Device) stepMotor(step uint8) {
func (d *Device) stepMotor4(step uint8) {
switch step {
case 0:
d.pins[0](true)
d.pins[1](false)
d.pins[2](true)
d.pins[3](false)
d.pins[0].High()
d.pins[1].Low()
d.pins[2].High()
d.pins[3].Low()
break
case 1:
d.pins[0](false)
d.pins[1](true)
d.pins[2](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[1].High()
d.pins[2].High()
d.pins[3].Low()
break
case 2:
d.pins[0](false)
d.pins[1](true)
d.pins[2](false)
d.pins[3](true)
d.pins[0].Low()
d.pins[1].High()
d.pins[2].Low()
d.pins[3].High()
break
case 3:
d.pins[0](true)
d.pins[1](false)
d.pins[2](false)
d.pins[3](true)
d.pins[0].High()
d.pins[1].Low()
d.pins[2].Low()
d.pins[3].High()
break
}
d.stepNumber = step
@@ -155,45 +155,45 @@ func (d *Device) stepMotor4(step uint8) {
func (d *Device) stepMotor8(step uint8) {
switch step {
case 0:
d.pins[0](true)
d.pins[2](false)
d.pins[1](false)
d.pins[3](false)
d.pins[0].High()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].Low()
case 1:
d.pins[0](true)
d.pins[2](true)
d.pins[1](false)
d.pins[3](false)
d.pins[0].High()
d.pins[2].High()
d.pins[1].Low()
d.pins[3].Low()
case 2:
d.pins[0](false)
d.pins[2](true)
d.pins[1](false)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].High()
d.pins[1].Low()
d.pins[3].Low()
case 3:
d.pins[0](false)
d.pins[2](true)
d.pins[1](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].High()
d.pins[1].High()
d.pins[3].Low()
case 4:
d.pins[0](false)
d.pins[2](false)
d.pins[1](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].High()
d.pins[3].Low()
case 5:
d.pins[0](false)
d.pins[2](false)
d.pins[1](true)
d.pins[3](true)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].High()
d.pins[3].High()
case 6:
d.pins[0](false)
d.pins[2](false)
d.pins[1](false)
d.pins[3](true)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].High()
case 7:
d.pins[0](true)
d.pins[2](false)
d.pins[1](false)
d.pins[3](true)
d.pins[0].High()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].High()
}
d.stepNumber = step
}
+6 -3
View File
@@ -4,20 +4,23 @@ import (
"errors"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
)
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]drivers.PinOutput) (*Device, error) {
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]pin.Output) (*Device, error) {
if stepcount == 0 || rpm == 0 {
return nil, errors.New("zero rpm and/or stepcount")
}
var ps [4]pin.OutputFunc
for i := range pins {
if pins[i] == nil {
return nil, errors.New("nil pin")
}
ps[i] = pins[i].Set
}
d := &Device{
pins: pins,
pins: ps,
stepDelay: time.Second * 60 / time.Duration((stepcount * rpm)),
stepMode: mode,
config: func() {},
+7 -8
View File
@@ -7,8 +7,7 @@ import (
"machine"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// New returns a new single easystepper driver given a DeviceConfig
@@ -17,14 +16,14 @@ func New(config DeviceConfig) (*Device, error) {
return nil, errors.New("config.StepCount and config.RPM must be > 0")
}
return &Device{
pins: [4]drivers.PinOutput{config.Pin1.Set, config.Pin2.Set, config.Pin3.Set, config.Pin4.Set},
pins: [4]pin.OutputFunc{config.Pin1.Set, config.Pin2.Set, config.Pin3.Set, config.Pin4.Set},
stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)),
stepMode: config.Mode,
config: func() {
legacy.ConfigurePinOut(config.Pin1)
legacy.ConfigurePinOut(config.Pin2)
legacy.ConfigurePinOut(config.Pin3)
legacy.ConfigurePinOut(config.Pin4)
pin.ConfigureOutput(config.Pin1)
pin.ConfigureOutput(config.Pin2)
pin.ConfigureOutput(config.Pin3)
pin.ConfigureOutput(config.Pin4)
},
}, nil
}
@@ -32,7 +31,7 @@ func New(config DeviceConfig) (*Device, error) {
// Configure configures the pins of the Device
func (d *Device) Configure() {
if d.config == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.config()
}