-
Notifications
You must be signed in to change notification settings - Fork 5
/
SimpleStepper.cpp
116 lines (90 loc) · 2.26 KB
/
SimpleStepper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
@file SimpleStepper.cpp
@author Evert Chin
@brief Fast and Simple Stepper Driver
* This file may be redistributed under the terms of the MIT license.
* A copy of this license has been included with this distribution in the file LICENSE.
*/
#include "SimpleStepper.h"
#include "TimerOne.h"
SimpleStepper *SimpleStepper::firstInstance;
SimpleStepper::SimpleStepper(uint8_t dirpin, uint8_t steppin){
if(!firstInstance){
firstInstance = this;
}
this->dirPin = Pin(dirpin);
this->stepPin = Pin(steppin);
}
void SimpleStepper::init(){
dirPin.setOutput();
stepPin.setOutput();
Timer1.initialize();
Timer1.attachInterrupt(SimpleStepper::ticking);
Timer1.stop();
this->pause();
}
void SimpleStepper::setPulse(long pulse){
Timer1.setPeriod(pulse);
}
bool SimpleStepper::step(long steps, uint8_t direction){
if(this->isStepping()){
return false;
}
this->ticksRemaining = steps * 2; //converting steps to ticks
if(direction == HIGH){
this->dirPin.setHigh();
} else {
this->dirPin.setLow();
}
return true;
}
bool SimpleStepper::step(long steps, uint8_t direction, long pulse){
if(this->isStepping()){
return false;
}
this->resume();
this->setPulse(pulse);
return this->step(steps, direction);
}
long SimpleStepper::getRemainingSteps(){
return ticksRemaining/2;
}
//returns the remaining steps
long SimpleStepper::stop(){
//each step = 2 ticks
long stepsRemaining = this->getRemainingSteps();
Timer1.stop();
if(ticksRemaining & 1){
ticksRemaining = 1;
} else{
ticksRemaining = 0;
}
Timer1.start();
return stepsRemaining;
}
void SimpleStepper::pause(){
paused = true;
Timer1.stop();
}
void SimpleStepper::resume(){
if(paused){
Timer1.start();
paused = false;
}
}
bool SimpleStepper::isStepping(){
return (ticksRemaining > 0);
}
bool SimpleStepper::isStopped(){
return (ticksRemaining <= 0);
}
bool SimpleStepper::isPaused(){
return paused;
}
void SimpleStepper::ticking(){
if(firstInstance->ticksRemaining > 0){
//generate high/low signal for the stepper driver
firstInstance->stepPin.toggleState();
--firstInstance->ticksRemaining;
}
}