-
Notifications
You must be signed in to change notification settings - Fork 1
/
BtStepper.cpp
142 lines (95 loc) · 1.59 KB
/
BtStepper.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Arduino.h"
#include "BtStepper.h"
BtStepper::BtStepper(int p1, int p2)
{
init();
pin1 = p1;
pin2 = p2;
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pin3 = 0;
pin4 = 0;
pinCount = 2;
}
BtStepper::BtStepper(int p1, int p2, int p3, int p4)
{
init();
pin1 = p1;
pin2 = p2;
pin3 = p3;
pin4 = p4;
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinCount = 4;
}
void BtStepper::init()
{
reverse = false;
steps[0][0] = HIGH;
steps[0][1] = LOW;
steps[0][2] = HIGH;
steps[0][3] = LOW;
steps[1][0] = LOW;
steps[1][1] = HIGH;
steps[1][2] = HIGH;
steps[1][3] = LOW;
steps[2][0] = LOW;
steps[2][1] = HIGH;
steps[2][2] = LOW;
steps[2][3] = HIGH;
steps[3][0] = HIGH;
steps[3][1] = LOW;
steps[3][2] = LOW;
steps[3][3] = HIGH;
stepNumber = 0;
}
BtStepper& BtStepper::operator++()
{
if(!reverse)
stepNumber++;
else
stepNumber--;
step();
return *this;
}
BtStepper BtStepper::operator++(int)
{
BtStepper tmp(*this);
operator++();
return tmp;
}
BtStepper& BtStepper::operator--()
{
if(!reverse)
stepNumber--;
else
stepNumber++;
step();
return *this;
}
BtStepper BtStepper::operator--(int)
{
BtStepper tmp(*this);
operator--();
return tmp;
}
void BtStepper::step()
{
int s = stepNumber%4;
if(s<0)
s += 4;
if (pinCount == 2)
{
digitalWrite(pin1, steps[s][1]);
digitalWrite(pin2, steps[s][2]);
}
else if (pinCount == 4)
{
digitalWrite(pin1, steps[s][0]);
digitalWrite(pin2, steps[s][1]);
digitalWrite(pin3, steps[s][2]);
digitalWrite(pin4, steps[s][3]);
}
}