hezhian hezhain / armtest

Fork of stepper by Xiaoyuan Yu

Revision:
3:fd565358f888
Parent:
2:e537d08c705b
Child:
4:35d44eec6939
--- a/stepper.cpp	Wed Jul 25 08:25:10 2018 +0000
+++ b/stepper.cpp	Wed Jul 25 11:09:25 2018 +0000
@@ -3,8 +3,11 @@
 
 void stepperMotor::moveStepper()
 {
-    if(remain == 0)
+    if(remain == 0 || limitSwitch.read() == 0)
+    {
+        remain = 0;
         return;
+    }
     if(step)
     {
         step = 0; //STEP 1->0
@@ -23,7 +26,8 @@
 
 void stepperMotor::enable(int inputRemain)
 {
-    remain = inputRemain;
+    setDir(inputRemain >= 0? true: false);
+    remain = inputRemain >= 0? inputRemain: -inputRemain;
     enable();
 }
 
@@ -43,8 +47,10 @@
     dir = direction;
 }
 
-stepperMotor::stepperMotor(PinName stepPin, PinName dirPin, PinName enPin, float period): remain(0), step(stepPin), dir(dirPin), en(enPin)
+stepperMotor::stepperMotor(PinName stepPin, PinName dirPin, PinName enPin, PinName limitSwitchPin, float period): 
+    remain(0), step(stepPin), dir(dirPin), en(enPin), limitSwitch(limitSwitchPin)
 {
     disable();
+    limitSwitch.mode(PullUp);
     stepper.attach(this, &stepperMotor::moveStepper, period);
 }