@@ -125,54 +125,54 @@ func NewAdafruit2348Driver(c Connector, options ...func(Config)) *Adafruit2348Dr
125125}
126126
127127// SetDCMotorSpeed will set the appropriate pins to run the specified DC motor for the given speed.
128- func (a * Adafruit2348Driver ) SetDCMotorSpeed (dcMotor int , speed int32 ) error {
129- return a .SetPWM (int (a .dcMotors [dcMotor ].pwmPin ), 0 , uint16 (speed * 16 )) //nolint:gosec // TODO: fix later
128+ func (d * Adafruit2348Driver ) SetDCMotorSpeed (dcMotor int , speed int32 ) error {
129+ return d .SetPWM (int (d .dcMotors [dcMotor ].pwmPin ), 0 , uint16 (speed * 16 )) //nolint:gosec // TODO: fix later
130130}
131131
132132// RunDCMotor will set the appropriate pins to run the specified DC motor for the given direction.
133- func (a * Adafruit2348Driver ) RunDCMotor (dcMotor int , dir Adafruit2348Direction ) error {
133+ func (d * Adafruit2348Driver ) RunDCMotor (dcMotor int , dir Adafruit2348Direction ) error {
134134 switch dir {
135135 case Adafruit2348Forward :
136- if err := a .setPin (a .dcMotors [dcMotor ].in2Pin , 0 ); err != nil {
136+ if err := d .setPin (d .dcMotors [dcMotor ].in2Pin , 0 ); err != nil {
137137 return err
138138 }
139- if err := a .setPin (a .dcMotors [dcMotor ].in1Pin , 1 ); err != nil {
139+ if err := d .setPin (d .dcMotors [dcMotor ].in1Pin , 1 ); err != nil {
140140 return err
141141 }
142142 case Adafruit2348Backward :
143- if err := a .setPin (a .dcMotors [dcMotor ].in1Pin , 0 ); err != nil {
143+ if err := d .setPin (d .dcMotors [dcMotor ].in1Pin , 0 ); err != nil {
144144 return err
145145 }
146- if err := a .setPin (a .dcMotors [dcMotor ].in2Pin , 1 ); err != nil {
146+ if err := d .setPin (d .dcMotors [dcMotor ].in2Pin , 1 ); err != nil {
147147 return err
148148 }
149149 case Adafruit2348Release :
150- if err := a .setPin (a .dcMotors [dcMotor ].in1Pin , 0 ); err != nil {
150+ if err := d .setPin (d .dcMotors [dcMotor ].in1Pin , 0 ); err != nil {
151151 return err
152152 }
153- if err := a .setPin (a .dcMotors [dcMotor ].in2Pin , 0 ); err != nil {
153+ if err := d .setPin (d .dcMotors [dcMotor ].in2Pin , 0 ); err != nil {
154154 return err
155155 }
156156 }
157157 return nil
158158}
159159
160160// SetStepperMotorSpeed sets the seconds-per-step for the given stepper motor. It is applied in the next cycle.
161- func (a * Adafruit2348Driver ) SetStepperMotorSpeed (stepperMotor int , rpm int ) error {
162- a .stepperSpeedMutex .Lock ()
163- defer a .stepperSpeedMutex .Unlock ()
161+ func (d * Adafruit2348Driver ) SetStepperMotorSpeed (stepperMotor int , rpm int ) error {
162+ d .stepperSpeedMutex .Lock ()
163+ defer d .stepperSpeedMutex .Unlock ()
164164
165- revSteps := a .stepperMotors [stepperMotor ].revSteps
166- a .stepperMotors [stepperMotor ].secPerStep = 60.0 / float64 (revSteps * rpm )
165+ revSteps := d .stepperMotors [stepperMotor ].revSteps
166+ d .stepperMotors [stepperMotor ].secPerStep = 60.0 / float64 (revSteps * rpm )
167167 return nil
168168}
169169
170170// Step will rotate the stepper motor the given number of steps, in the given direction and step style.
171- func (a * Adafruit2348Driver ) Step (motor , steps int , dir Adafruit2348Direction , style Adafruit2348StepStyle ) error {
172- a .stepperSpeedMutex .Lock ()
173- defer a .stepperSpeedMutex .Unlock ()
171+ func (d * Adafruit2348Driver ) Step (motor , steps int , dir Adafruit2348Direction , style Adafruit2348StepStyle ) error {
172+ d .stepperSpeedMutex .Lock ()
173+ defer d .stepperSpeedMutex .Unlock ()
174174
175- secPerStep := a .stepperMotors [motor ].secPerStep
175+ secPerStep := d .stepperMotors [motor ].secPerStep
176176 var latestStep int
177177 var err error
178178 if style == Adafruit2348Interleave {
@@ -186,7 +186,7 @@ func (a *Adafruit2348Driver) Step(motor, steps int, dir Adafruit2348Direction, s
186186 log .Printf ("[adafruit2348_driver] %f seconds per step" , secPerStep )
187187 }
188188 for i := 0 ; i < steps ; i ++ {
189- if latestStep , err = a .oneStep (motor , dir , style ); err != nil {
189+ if latestStep , err = d .oneStep (motor , dir , style ); err != nil {
190190 return err
191191 }
192192 time .Sleep (time .Duration (secPerStep ) * time .Second )
@@ -195,7 +195,7 @@ func (a *Adafruit2348Driver) Step(motor, steps int, dir Adafruit2348Direction, s
195195 // This is an edge case, if we are in between full steps, keep going to end on a full step
196196 if style == Adafruit2348Microstep {
197197 for latestStep != 0 && latestStep != adafruit2348StepperMicrosteps {
198- if latestStep , err = a .oneStep (motor , dir , style ); err != nil {
198+ if latestStep , err = d .oneStep (motor , dir , style ); err != nil {
199199 return err
200200 }
201201 time .Sleep (time .Duration (secPerStep ) * time .Second )
@@ -204,94 +204,94 @@ func (a *Adafruit2348Driver) Step(motor, steps int, dir Adafruit2348Direction, s
204204 return nil
205205}
206206
207- func (a * Adafruit2348Driver ) oneStep (motor int , dir Adafruit2348Direction , style Adafruit2348StepStyle ) (int , error ) {
207+ func (d * Adafruit2348Driver ) oneStep (motor int , dir Adafruit2348Direction , style Adafruit2348StepStyle ) (int , error ) {
208208 pwmA := 255
209209 pwmB := 255
210210
211211 // Determine the stepping procedure
212212 switch style {
213213 case Adafruit2348Single :
214- if (a .stepperMotors [motor ].currentStep / (adafruit2348StepperMicrosteps / 2 ) % 2 ) != 0 {
214+ if (d .stepperMotors [motor ].currentStep / (adafruit2348StepperMicrosteps / 2 ) % 2 ) != 0 {
215215 // we're at an odd step
216216 if dir == Adafruit2348Forward {
217- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps / 2
217+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps / 2
218218 } else {
219- a .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps / 2
219+ d .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps / 2
220220 }
221221 } else {
222222 // go to next even step
223223 if dir == Adafruit2348Forward {
224- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps
224+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps
225225 } else {
226- a .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps
226+ d .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps
227227 }
228228 }
229229 case Adafruit2348Double :
230- if (a .stepperMotors [motor ].currentStep / (adafruit2348StepperMicrosteps / 2 ) % 2 ) == 0 {
230+ if (d .stepperMotors [motor ].currentStep / (adafruit2348StepperMicrosteps / 2 ) % 2 ) == 0 {
231231 // we're at an even step, weird
232232 if dir == Adafruit2348Forward {
233- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps / 2
233+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps / 2
234234 } else {
235- a .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps / 2
235+ d .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps / 2
236236 }
237237 } else {
238238 // go to next odd step
239239 if dir == Adafruit2348Forward {
240- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps
240+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps
241241 } else {
242- a .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps
242+ d .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps
243243 }
244244 }
245245 case Adafruit2348Interleave :
246246 if dir == Adafruit2348Forward {
247- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps / 2
247+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps / 2
248248 } else {
249- a .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps / 2
249+ d .stepperMotors [motor ].currentStep -= adafruit2348StepperMicrosteps / 2
250250 }
251251 case Adafruit2348Microstep :
252252 if dir == Adafruit2348Forward {
253- a .stepperMotors [motor ].currentStep ++
253+ d .stepperMotors [motor ].currentStep ++
254254 } else {
255- a .stepperMotors [motor ].currentStep --
255+ d .stepperMotors [motor ].currentStep --
256256 }
257257 // go to next step and wrap around
258- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps * 4
259- a .stepperMotors [motor ].currentStep %= adafruit2348StepperMicrosteps * 4
258+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps * 4
259+ d .stepperMotors [motor ].currentStep %= adafruit2348StepperMicrosteps * 4
260260
261261 pwmA = 0
262262 pwmB = 0
263- currStep := a .stepperMotors [motor ].currentStep
263+ currStep := d .stepperMotors [motor ].currentStep
264264 switch {
265265 case currStep >= 0 && currStep < adafruit2348StepperMicrosteps :
266- pwmA = a .stepperMicrostepCurve [adafruit2348StepperMicrosteps - currStep ]
267- pwmB = a .stepperMicrostepCurve [currStep ]
266+ pwmA = d .stepperMicrostepCurve [adafruit2348StepperMicrosteps - currStep ]
267+ pwmB = d .stepperMicrostepCurve [currStep ]
268268 case currStep >= adafruit2348StepperMicrosteps && currStep < adafruit2348StepperMicrosteps * 2 :
269- pwmA = a .stepperMicrostepCurve [currStep - adafruit2348StepperMicrosteps ]
270- pwmB = a .stepperMicrostepCurve [adafruit2348StepperMicrosteps * 2 - currStep ]
269+ pwmA = d .stepperMicrostepCurve [currStep - adafruit2348StepperMicrosteps ]
270+ pwmB = d .stepperMicrostepCurve [adafruit2348StepperMicrosteps * 2 - currStep ]
271271 case currStep >= adafruit2348StepperMicrosteps * 2 && currStep < adafruit2348StepperMicrosteps * 3 :
272- pwmA = a .stepperMicrostepCurve [adafruit2348StepperMicrosteps * 3 - currStep ]
273- pwmB = a .stepperMicrostepCurve [currStep - adafruit2348StepperMicrosteps * 2 ]
272+ pwmA = d .stepperMicrostepCurve [adafruit2348StepperMicrosteps * 3 - currStep ]
273+ pwmB = d .stepperMicrostepCurve [currStep - adafruit2348StepperMicrosteps * 2 ]
274274 case currStep >= adafruit2348StepperMicrosteps * 3 && currStep < adafruit2348StepperMicrosteps * 4 :
275- pwmA = a .stepperMicrostepCurve [currStep - adafruit2348StepperMicrosteps * 3 ]
276- pwmB = a .stepperMicrostepCurve [adafruit2348StepperMicrosteps * 4 - currStep ]
275+ pwmA = d .stepperMicrostepCurve [currStep - adafruit2348StepperMicrosteps * 3 ]
276+ pwmB = d .stepperMicrostepCurve [adafruit2348StepperMicrosteps * 4 - currStep ]
277277 }
278278 } // switch
279279
280280 // go to next 'step' and wrap around
281- a .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps * 4
282- a .stepperMotors [motor ].currentStep %= adafruit2348StepperMicrosteps * 4
281+ d .stepperMotors [motor ].currentStep += adafruit2348StepperMicrosteps * 4
282+ d .stepperMotors [motor ].currentStep %= adafruit2348StepperMicrosteps * 4
283283
284284 // only really used for microstepping, otherwise always on!
285285 //nolint:gosec // TODO: fix later
286- if err := a .SetPWM (int (a .stepperMotors [motor ].pwmPinA ), 0 , uint16 (pwmA * 16 )); err != nil {
286+ if err := d .SetPWM (int (d .stepperMotors [motor ].pwmPinA ), 0 , uint16 (pwmA * 16 )); err != nil {
287287 return 0 , err
288288 }
289289 //nolint:gosec // TODO: fix later
290- if err := a .SetPWM (int (a .stepperMotors [motor ].pwmPinB ), 0 , uint16 (pwmB * 16 )); err != nil {
290+ if err := d .SetPWM (int (d .stepperMotors [motor ].pwmPinB ), 0 , uint16 (pwmB * 16 )); err != nil {
291291 return 0 , err
292292 }
293293 var coils []int32
294- currStep := a .stepperMotors [motor ].currentStep
294+ currStep := d .stepperMotors [motor ].currentStep
295295 if style == Adafruit2348Microstep {
296296 switch {
297297 case currStep >= 0 && currStep < adafruit2348StepperMicrosteps :
@@ -305,34 +305,34 @@ func (a *Adafruit2348Driver) oneStep(motor int, dir Adafruit2348Direction, style
305305 }
306306 } else {
307307 // step-2-coils is initialized in init()
308- coils = a .step2coils [(currStep / (adafruit2348StepperMicrosteps / 2 ))]
308+ coils = d .step2coils [(currStep / (adafruit2348StepperMicrosteps / 2 ))]
309309 }
310310 if adafruit2348Debug {
311311 log .Printf ("[adafruit2348_driver] currStep: %d, index into step2coils: %d\n " ,
312312 currStep , (currStep / (adafruit2348StepperMicrosteps / 2 )))
313313 log .Printf ("[adafruit2348_driver] coils state = %v" , coils )
314314 }
315- if err := a .setPin (a .stepperMotors [motor ].ain2 , coils [0 ]); err != nil {
315+ if err := d .setPin (d .stepperMotors [motor ].ain2 , coils [0 ]); err != nil {
316316 return 0 , err
317317 }
318- if err := a .setPin (a .stepperMotors [motor ].bin1 , coils [1 ]); err != nil {
318+ if err := d .setPin (d .stepperMotors [motor ].bin1 , coils [1 ]); err != nil {
319319 return 0 , err
320320 }
321- if err := a .setPin (a .stepperMotors [motor ].ain1 , coils [2 ]); err != nil {
321+ if err := d .setPin (d .stepperMotors [motor ].ain1 , coils [2 ]); err != nil {
322322 return 0 , err
323323 }
324- if err := a .setPin (a .stepperMotors [motor ].bin2 , coils [3 ]); err != nil {
324+ if err := d .setPin (d .stepperMotors [motor ].bin2 , coils [3 ]); err != nil {
325325 return 0 , err
326326 }
327- return a .stepperMotors [motor ].currentStep , nil
327+ return d .stepperMotors [motor ].currentStep , nil
328328}
329329
330- func (a * Adafruit2348Driver ) setPin (pin byte , value int32 ) error {
330+ func (d * Adafruit2348Driver ) setPin (pin byte , value int32 ) error {
331331 if value == 0 {
332- return a .SetPWM (int (pin ), 0 , 4096 )
332+ return d .SetPWM (int (pin ), 0 , 4096 )
333333 }
334334 if value == 1 {
335- return a .SetPWM (int (pin ), 4096 , 0 )
335+ return d .SetPWM (int (pin ), 4096 , 0 )
336336 }
337337 return errors .New ("invalid pin" )
338338}
0 commit comments