forked from sbstjn/duino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.js
More file actions
232 lines (206 loc) · 5.73 KB
/
Copy pathboard.js
File metadata and controls
232 lines (206 loc) · 5.73 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
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
227
228
229
230
231
232
define(function(require, exports, module) {
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
* @param object options object of option key -> value relations
* @param function callback function to be called after connection
*/
var Board = function (options, callback) {
this.log('info', 'initializing');
this.debug = options && options.debug || false;
this.writeBuffer = [];
this.connected = false;
var self = this;
this.detect(function (err, serial) {
if (err) { return callback(new Error('Adruino not found')); }
self.serial = serial;
self.emit('connected');
callback(null, self);
self.log('info', 'binding serial events');
self.serial.on('data', function(data){
if (self.connected == false) {
self.connected = true;
self.emit('ready');
}
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.IAmDave();
setTimeout(function() {self.IAmDave();}, 500);
// 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 (cb) {
this.log('info', 'attempting to find Arduino board');
var self = this;
child.exec('ls /dev | grep usb', function(err, stdout, stderr){
var possible = stdout.slice(0, -1).split('\n'),
found = false;
for (var i in possible) {
var tempSerial, err;
try {
tempSerial = new serial.SerialPort('/dev/' + possible[i], {
baudrate: 115200,
parser: serial.parsers.readline("\n")
});
} catch (e) {
err = e;
}
if (!err) {
found = tempSerial;
self.log('info', 'found board at ' + tempSerial.port);
break;
}
}
if (found) cb(null, found);
else cb(new Error('Could not find Arduino'));
});
}
/*
* 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');
}
Board.prototype.IAmDave = function() {
this.log('info', 'I\'m Dave!');
this.serial.write('90000000');
this.write('90000000');
}
/*
* 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 ("00" + pin).substr(-2);
/*pin = String(pin).split('');
if (pin.length > 1) {
return pin.join('');
} else {
pin.unshift('0');
return pin.join('');
}*/
}
Board.prototype.normalizeVal = function(val) {
return ("000" + val).substr(-3);
}
/*
* 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, callback) {
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) {
if (this.debug) {
console.log(String(+new Date()).grey + ' duino '.blue + level.magenta + ' ' + message);
}
}
return Board;
});