/
board.js
226 lines (199 loc) · 5.51 KB
/
board.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var events = require('events'),
child = require('child_process'),
util = require('util'),
colors = require('colors'),
serial = require('serialport');
/*
* The main Arduino constructor
* Connect to the serial port and bind
*/
var Board = function (options) {
this.log('info', 'initializing');
this.debug = options && options.debug || false;
this.device = options && options.device || 'usb';
this.writeBuffer = [];
var self = this;
this.detect(function (err, serial) {
if (err) {
if(self.listeners('error').length)
self.emit('error', err);
else
throw new Error(err);
}else{
self.serial = serial;
self.emit('connected');
self.log('info', 'binding serial events');
self.serial.on('data', function(data){
self.log('receive', data.toString().red);
self.emit('data', data);
});
setTimeout(function(){
self.log('info', 'board ready');
self.sendClearingBytes();
if (self.debug) {
self.log('info', 'sending debug mode toggle on to board');
self.write('99' + self.normalizePin(0) + self.normalizeVal(1));
process.on('SIGINT', function(){
self.log('info', 'sending debug mode toggle off to board');
self.write('99' + self.normalizePin(0) + self.normalizeVal(0));
delete self.serial;
setTimeout(function(){
process.exit();
}, 100);
});
}
if (self.writeBuffer.length > 0) {
self.processWriteBuffer();
}
self.emit('ready');
}, 500);
}
});
}
/*
* EventEmitter, I choose you!
*/
util.inherits(Board, events.EventEmitter);
/*
* Detect an Arduino board
* Loop through all USB devices and try to connect
* This should really message the device and wait for a correct response
*/
Board.prototype.detect = function (callback) {
this.log('info', 'attempting to find Arduino board');
var self = this;
child.exec('ls /dev | grep '+ this.device, function(err, stdout, stderr){
var usb = stdout.slice(0, -1).split('\n'),
found = false,
err = null,
possible, temp;
while ( usb.length ) {
possible = usb.pop();
if (possible.slice(0, 2) !== 'cu') {
try {
temp = new serial.SerialPort('/dev/' + possible, {
baudrate: 115200,
parser: serial.parsers.readline('\n')
});
} catch (e) {
err = e;
}
if (!err) {
found = temp;
self.log('info', 'found board at ' + temp.port);
break;
} else {
err = new Error('Could not find Arduino');
}
}
}
callback(err, found);
});
}
/*
* The board will eat the first 4 bytes of the session
* So we give it crap to eat
*/
Board.prototype.sendClearingBytes = function () {
this.serial.write('00000000');
}
/*
* Process the writeBuffer (messages attempted before serial was ready)
*/
Board.prototype.processWriteBuffer = function () {
this.log('info', 'processing buffered messages');
while (this.writeBuffer.length > 0) {
this.log('info', 'writing buffered message');
this.write(this.writeBuffer.shift());
}
}
/*
* Low-level serial write
*/
Board.prototype.write = function (m) {
if (this.serial) {
this.log('write', m);
this.serial.write('!' + m + '.');
} else {
this.log('info', 'serial not ready, buffering message: ' + m.red);
this.writeBuffer.push(m);
}
}
/*
* Add a 0 to the front of a single-digit pin number
*/
Board.prototype.normalizePin = function (pin) {
return this.lpad( 2, '0', pin );
}
Board.prototype.normalizeVal = function(val) {
return this.lpad( 3, '0', val );
}
//
Board.prototype.lpad = function(len, chr, str) {
return (Array(len + 1).join(chr || ' ') + str).substr(-len);
};
/*
* Define constants
*/
Board.prototype.HIGH = '255';
Board.prototype.LOW = '000';
/*
* Set a pin's mode
* val == out = 001
* val == in = 000
*/
Board.prototype.pinMode = function (pin, val) {
pin = this.normalizePin(pin);
this.log('info', 'set pin ' + pin + ' mode to ' + val);
val = (
val == 'out' ?
this.normalizeVal(1) :
this.normalizeVal(0)
);
this.write('00' + pin + val);
}
/*
* Tell the board to write to a digital pin
*/
Board.prototype.digitalWrite = function (pin, val) {
pin = this.normalizePin(pin);
val = this.normalizeVal(val);
this.log('info', 'digitalWrite to pin ' + pin + ': ' + val.green);
this.write('01' + pin + val);
}
/*
* Tell the board to extract data from a pin
*/
Board.prototype.digitalRead = function (pin) {
pin = this.normalizePin(pin);
this.log('info', 'digitalRead from pin ' + pin);
this.write('02' + pin + this.normalizeVal(0));
}
Board.prototype.analogWrite = function (pin, val) {
pin = this.normalizePin(pin);
val = this.normalizeVal(val);
this.log('info', 'analogWrite to pin ' + pin + ': ' + val.green);
this.write('03' + pin + val);
}
Board.prototype.analogRead = function (pin) {
pin = this.normalizePin(pin);
this.log('info', 'analogRead from pin ' + pin);
this.write('04' + pin + this.normalizeVal(0));
}
/*
* Utility function to pause for a given time
*/
Board.prototype.delay = function (ms) {
ms += +new Date();
while (+new Date() < ms) { }
}
/*
* Logger utility function
*/
Board.prototype.log = function (/*level, message*/) {
var args = [].slice.call(arguments);
if (this.debug) {
console.log(String(+new Date()).grey + ' duino '.blue + args.shift().magenta + ' ' + args.join(', '));
}
}
module.exports = Board;