-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstepper.js
More file actions
103 lines (80 loc) · 2.08 KB
/
stepper.js
File metadata and controls
103 lines (80 loc) · 2.08 KB
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
const gpio = require('rpi-gpio');
const async = require('async');
gpio.setMode(gpio.MODE_BCM)
// GPIO17,GPIO18,GPIO21,GPIO22
const stepPins = [11, 12, 13, 15];
// Read wait time from command line
const waitTime = 10 / 1000;
// Define advanced sequence
// as shown in manufacturers datasheet
const seq = [[1, 0, 0, 1],
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]];
function setupPin(pin) {
return new Promise((resolve, reject) => {
gpio.setup(pin, gpio.DIR_OUT, err => {
if (err) {
return reject(err);
}
console.log(`Pin ${pin} was setup`);
return resolve(pin);
});
});
}
function write(pin, direction) {
return new Promise((resolve, reject) => {
gpio.output(pin, direction, err => {
if (err) {
return reject(err);
}
console.log(`Pin ${pin} was set to ${direction}`);
return resolve(pin, direction);
});
});
}
function setupAllPins() {
const allPinsSetup = stepPins.map(pin => setupPin(pin));
return Promise.all(allPinsSetup).then(() => console.log('all pins setup', stepPins));
}
function writeAllPins(values) {
const allPinsWritten = values.map((value, index) => write(stepPins[index], value));
return Promise.all(allPinsWritten).then(() => console.log('all prins written', values));
}
function setup() {
return setupAllPins()
.then(() => writeAllPins([0, 0, 0, 0]))
.then(() => console.log('All pins should be turned off'));
}
function step(stepCounter) {
const allPins = [];
stepPins.forEach((pin, index) => {
if (seq[stepCounter][index] !== 0) {
allPins.push(write(pin, true));
} else {
allPins.push(write(pin, false));
}
});
return Promise.all(allPins).then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(stepCounter += 1), waitTime);
});
});
}
//Start main loop
function sequenzer() {
step(0)
.then(step)
.then(step)
.then(step)
.then(step)
.then(step)
.then(step)
.then(step)
.then(sequenzer());
}
setup().then(() => sequenzer());