-
Notifications
You must be signed in to change notification settings - Fork 1
/
car.js
86 lines (81 loc) · 1.75 KB
/
car.js
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
var async = require('async');
var gpio = require('pi-gpio');
/* GPIO Pins that we will use */
var pins = {
motor1a: 16,
motor1b: 18,
motor1e: 22,
motor2a: 21,
motor2b: 19,
motor2e: 23
};
var ready = false;
/* Returns an array of functions to be called with async.parallel */
function createTasks(obj, which) {
return Object.keys(obj).map(function (key) {
if (which === 'open') {
return function (callback) {
gpio.open(obj[key], "output", function (err) {
if (err) console.log(err);
callback();
});
};
} else if (which === 'close') {
return function (callback) {
gpio.close(obj[key], function (err) {
if (err) console.log(err);
callback();
});
};
} else if (which === 'write') {
return function (callback) {
gpio.write(parseInt(key), obj[key], function (err) {
if (err) console.log(err);
callback();
});
};
}
});
}
/* Set two pins to a specific value */
function setPins(pin1, pin2, value) {
if (ready) {
ready = false;
var obj = {
[pin1]: value,
[pin2]: value
};
var tasks = createTasks(obj, 'write');
async.parallel(tasks, function () {
ready = true;
});
}
}
module.exports = {
init: function (cb) {
var tasks = createTasks(pins, 'open');
async.parallel(tasks, function () {
ready = true;
cb();
});
},
destroy: function (cb) {
var tasks = createTasks(pins, 'close');
async.parallel(tasks, function () {
ready = false;
cb();
});
},
forward: function (value) {
setPins(pins.motor1a, pins.motor1e, value);
},
backward: function (value) {
setPins(pins.motor1b, pins.motor1e, value);
},
left: function (value) {
setPins(pins.motor2b, pins.motor2e, value);
},
right: function (value) {
setPins(pins.motor2a, pins.motor2e, value);
}
};