1+ //go:build baremetal
2+
13// Package easystepper provides a simple driver to rotate a 4-wire stepper motor.
24package easystepper // import "tinygo.org/x/drivers/easystepper"
35
46import (
5- "errors"
6- "machine"
77 "time"
8+
9+ "tinygo.org/x/drivers"
810)
911
1012// StepMode determines the coil sequence used to perform a single step
@@ -30,28 +32,10 @@ func (sm StepMode) stepCount() uint {
3032 }
3133}
3234
33- // DeviceConfig contains the configuration data for a single easystepper driver
34- type DeviceConfig struct {
35- // Pin1 ... Pin4 determines the pins to configure and use for the device
36- Pin1 , Pin2 , Pin3 , Pin4 machine.Pin
37- // StepCount is the number of steps required to perform a full revolution of the stepper motor
38- StepCount uint
39- // RPM determines the speed of the stepper motor in 'Revolutions per Minute'
40- RPM uint
41- // Mode determines the coil sequence used to perform a single step
42- Mode StepMode
43- }
44-
45- // DualDeviceConfig contains the configuration data for a dual easystepper driver
46- type DualDeviceConfig struct {
47- DeviceConfig
48- // Pin5 ... Pin8 determines the pins to configure and use for the second device
49- Pin5 , Pin6 , Pin7 , Pin8 machine.Pin
50- }
51-
5235// Device holds the pins and the delay between steps
5336type Device struct {
54- pins [4 ]machine.Pin
37+ pins [4 ]drivers.PinOutput
38+ config func ()
5539 stepDelay time.Duration
5640 stepNumber uint8
5741 stepMode StepMode
@@ -62,51 +46,6 @@ type DualDevice struct {
6246 devices [2 ]* Device
6347}
6448
65- // New returns a new single easystepper driver given a DeviceConfig
66- func New (config DeviceConfig ) (* Device , error ) {
67- if config .StepCount == 0 || config .RPM == 0 {
68- return nil , errors .New ("config.StepCount and config.RPM must be > 0" )
69- }
70- return & Device {
71- pins : [4 ]machine.Pin {config .Pin1 , config .Pin2 , config .Pin3 , config .Pin4 },
72- stepDelay : time .Second * 60 / time .Duration ((config .StepCount * config .RPM )),
73- stepMode : config .Mode ,
74- }, nil
75- }
76-
77- // Configure configures the pins of the Device
78- func (d * Device ) Configure () {
79- for _ , pin := range d .pins {
80- pin .Configure (machine.PinConfig {Mode : machine .PinOutput })
81- }
82- }
83-
84- // NewDual returns a new dual easystepper driver given 8 pins, number of steps and rpm
85- func NewDual (config DualDeviceConfig ) (* DualDevice , error ) {
86- // Create the first device
87- dev1 , err := New (config .DeviceConfig )
88- if err != nil {
89- return nil , err
90- }
91- // Create the second device
92- config .DeviceConfig .Pin1 = config .Pin5
93- config .DeviceConfig .Pin2 = config .Pin6
94- config .DeviceConfig .Pin3 = config .Pin7
95- config .DeviceConfig .Pin4 = config .Pin8
96- dev2 , err := New (config .DeviceConfig )
97- if err != nil {
98- return nil , err
99- }
100- // Return composite dual device
101- return & DualDevice {devices : [2 ]* Device {dev1 , dev2 }}, nil
102- }
103-
104- // Configure configures the pins of the DualDevice
105- func (d * DualDevice ) Configure () {
106- d .devices [0 ].Configure ()
107- d .devices [1 ].Configure ()
108- }
109-
11049// Move rotates the motor the number of given steps
11150// (negative steps will rotate it the opposite direction)
11251func (d * Device ) Move (steps int32 ) {
@@ -126,7 +65,7 @@ func (d *Device) Move(steps int32) {
12665// Off turns off all motor pins
12766func (d * Device ) Off () {
12867 for _ , pin := range d .pins {
129- pin . Low ( )
68+ pin ( false )
13069 }
13170}
13271
@@ -187,28 +126,28 @@ func (d *Device) stepMotor(step uint8) {
187126func (d * Device ) stepMotor4 (step uint8 ) {
188127 switch step {
189128 case 0 :
190- d .pins [0 ]. High ( )
191- d .pins [1 ]. Low ( )
192- d .pins [2 ]. High ( )
193- d .pins [3 ]. Low ( )
129+ d .pins [0 ]( true )
130+ d .pins [1 ]( false )
131+ d .pins [2 ]( true )
132+ d .pins [3 ]( false )
194133 break
195134 case 1 :
196- d .pins [0 ]. Low ( )
197- d .pins [1 ]. High ( )
198- d .pins [2 ]. High ( )
199- d .pins [3 ]. Low ( )
135+ d .pins [0 ]( false )
136+ d .pins [1 ]( true )
137+ d .pins [2 ]( true )
138+ d .pins [3 ]( false )
200139 break
201140 case 2 :
202- d .pins [0 ]. Low ( )
203- d .pins [1 ]. High ( )
204- d .pins [2 ]. Low ( )
205- d .pins [3 ]. High ( )
141+ d .pins [0 ]( false )
142+ d .pins [1 ]( true )
143+ d .pins [2 ]( false )
144+ d .pins [3 ]( true )
206145 break
207146 case 3 :
208- d .pins [0 ]. High ( )
209- d .pins [1 ]. Low ( )
210- d .pins [2 ]. Low ( )
211- d .pins [3 ]. High ( )
147+ d .pins [0 ]( true )
148+ d .pins [1 ]( false )
149+ d .pins [2 ]( false )
150+ d .pins [3 ]( true )
212151 break
213152 }
214153 d .stepNumber = step
@@ -218,45 +157,45 @@ func (d *Device) stepMotor4(step uint8) {
218157func (d * Device ) stepMotor8 (step uint8 ) {
219158 switch step {
220159 case 0 :
221- d .pins [0 ]. High ( )
222- d .pins [2 ]. Low ( )
223- d .pins [1 ]. Low ( )
224- d .pins [3 ]. Low ( )
160+ d .pins [0 ]( true )
161+ d .pins [2 ]( false )
162+ d .pins [1 ]( false )
163+ d .pins [3 ]( false )
225164 case 1 :
226- d .pins [0 ]. High ( )
227- d .pins [2 ]. High ( )
228- d .pins [1 ]. Low ( )
229- d .pins [3 ]. Low ( )
165+ d .pins [0 ]( true )
166+ d .pins [2 ]( true )
167+ d .pins [1 ]( false )
168+ d .pins [3 ]( false )
230169 case 2 :
231- d .pins [0 ]. Low ( )
232- d .pins [2 ]. High ( )
233- d .pins [1 ]. Low ( )
234- d .pins [3 ]. Low ( )
170+ d .pins [0 ]( false )
171+ d .pins [2 ]( true )
172+ d .pins [1 ]( false )
173+ d .pins [3 ]( false )
235174 case 3 :
236- d .pins [0 ]. Low ( )
237- d .pins [2 ]. High ( )
238- d .pins [1 ]. High ( )
239- d .pins [3 ]. Low ( )
175+ d .pins [0 ]( false )
176+ d .pins [2 ]( true )
177+ d .pins [1 ]( true )
178+ d .pins [3 ]( false )
240179 case 4 :
241- d .pins [0 ]. Low ( )
242- d .pins [2 ]. Low ( )
243- d .pins [1 ]. High ( )
244- d .pins [3 ]. Low ( )
180+ d .pins [0 ]( false )
181+ d .pins [2 ]( false )
182+ d .pins [1 ]( true )
183+ d .pins [3 ]( false )
245184 case 5 :
246- d .pins [0 ]. Low ( )
247- d .pins [2 ]. Low ( )
248- d .pins [1 ]. High ( )
249- d .pins [3 ]. High ( )
185+ d .pins [0 ]( false )
186+ d .pins [2 ]( false )
187+ d .pins [1 ]( true )
188+ d .pins [3 ]( true )
250189 case 6 :
251- d .pins [0 ]. Low ( )
252- d .pins [2 ]. Low ( )
253- d .pins [1 ]. Low ( )
254- d .pins [3 ]. High ( )
190+ d .pins [0 ]( false )
191+ d .pins [2 ]( false )
192+ d .pins [1 ]( false )
193+ d .pins [3 ]( true )
255194 case 7 :
256- d .pins [0 ]. High ( )
257- d .pins [2 ]. Low ( )
258- d .pins [1 ]. Low ( )
259- d .pins [3 ]. High ( )
195+ d .pins [0 ]( true )
196+ d .pins [2 ]( false )
197+ d .pins [1 ]( false )
198+ d .pins [3 ]( true )
260199 }
261200 d .stepNumber = step
262201}
0 commit comments