Skip to content

Commit 425b625

Browse files
committed
Merge branch 'master' into HEAD
Conflicts: examples/rc.js index.js lib/board.js lib/rc.js package.json src/du.ino
2 parents b19599d + dd7c08a commit 425b625

9 files changed

Lines changed: 426 additions & 49 deletions

File tree

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ The way this works is simple (in theory, not in practice). The Arduino listens f
3636

3737
##board
3838

39-
Right now, the board library will attempt to autodiscover the Arduino. I'm going to make it configurable, don't worry.
39+
````javascript
40+
var board = new arduino.Board({
41+
device: "ACM"
42+
});
43+
````
44+
The board library will attempt to autodiscover the Arduino.
45+
The `device` option can be used to set a regex filter that will help the library when scanning for matching devices.
46+
**Note**: the value of this parameter will be used as argument of the grep command
47+
48+
If this parameter is not provided the board library will attempt to autodiscover the Arduino by quering every device containing 'usb' in its name.
4049

4150
````javascript
4251
var board = new arduino.Board({
@@ -119,6 +128,67 @@ Fade the to full brightness then back to minimal brightness in `interval` ms. De
119128

120129
Current brightness of the LED
121130

131+
##lcd
132+
133+
This is a port of the [LiquidCrystal library](http://arduino.cc/en/Reference/LiquidCrystal) into JavaScript. Note that communicating with the LCD requires use of the synchronous `board.delay()` busy loop which will block other node.js events from being processed for several milliseconds at a time. (This could be converted to pause a board-level buffered message queue instead.)
134+
135+
````javascript
136+
var lcd = new d.LCD({
137+
board: board,
138+
pins: {rs:12, rw:11, e:10, data:[5, 4, 3, 2]}
139+
});
140+
lcd.begin(16, 2);
141+
lcd.print("Hello Internet.");
142+
````
143+
144+
In `options`, the "pins" field can either be an array matching a call to any of the [LiquidCrystal constructors](http://arduino.cc/en/Reference/LiquidCrystalConstructor) or an object with "rs", "rw" (optional), "e" and a 4- or 8-long array of "data" pins. Pins will default to `[12, 11, 5, 4, 3, 2]` if not provided.
145+
146+
###lcd.begin(), lcd.clear(), lcd.home(), lcd.setCursor(), lcd.scrollDisplayLeft(), lcd.scrollDisplayRight()
147+
148+
These should behave the same as their counterparts in the [LiquidCrystal library](http://arduino.cc/en/Reference/LiquidCrystal).
149+
150+
###lcd.display(on), lcd.cursor(on), lcd.blink(on), lcd.autoscroll(on)
151+
152+
These are similar to the methods in the [LiquidCrystal library](http://arduino.cc/en/Reference/LiquidCrystal), however they can take an optional boolean parameter. If true or not provided, the setting is enabled. If false, the setting is disabled. For compatibility `.noDisplay()`, `.noCursor()`, `.noBlink()` and `.noAutoscroll()` methods are provided as well.
153+
154+
###lcd.write(val), lcd.print(val)
155+
156+
These take a buffer, string or integer and send it to the display. The `.write` and `print` methods are equivalent, aliases to the same function.
157+
158+
###lcd.createChar(location, charmap)
159+
160+
Configures a custom character for code `location` (numbers 0–7). `charmap` can be a 40-byte buffer as in [the C++ method](http://arduino.cc/en/Reference/LiquidCrystalCreateChar), or an array of 5-bit binary strings, or a 40-character string with pixels denoted by any non-space (`' '`) character. These bits determine the 5x8 pixel pattern of the custom character.
161+
162+
````javascript
163+
var square = new Buffer("1f1f1f1f1f1f1f1f", 'hex');
164+
165+
var smiley = [
166+
'00000',
167+
'10001',
168+
'00000',
169+
'00000',
170+
'10001',
171+
'01110',
172+
'00000'
173+
];
174+
175+
var random =
176+
". .." +
177+
" . . " +
178+
". . ." +
179+
" . . " +
180+
" .. " +
181+
". . " +
182+
" . ." +
183+
".. .." ;
184+
185+
lcd.createChar(0, square);
186+
lcd.createChar(1, smiley);
187+
lcd.createChar(2, random);
188+
lcd.setCursor(5,2);
189+
lcd.print(new Buffer("\0\1\2\1\0")); // NOTE: when `.print`ing a string, 'ascii' turns \0 into a space
190+
````
191+
122192
##piezo
123193

124194
````javascript
@@ -164,6 +234,20 @@ setInterval(function(){
164234
}, 1000);
165235
````
166236

237+
##ping
238+
239+
See: <http://arduino.cc/en/Tutorial/Ping>
240+
241+
````javascript
242+
var range = new arduino.Ping({
243+
board: board
244+
});
245+
246+
range.on('read', function () {
247+
console.log("Distance to target (cm)", range.centimeters);
248+
});
249+
````
250+
167251
##servo
168252

169253
````javascript
@@ -213,6 +297,8 @@ What is implemented right now:
213297
* `02` digitalRead
214298
* `03` analogWrite
215299
* `04` analogRead
300+
* `97` ping
301+
* `98` servo
216302
* `99` debug
217303

218304
##pin

examples/adjustable-port.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var arduino = require('../');
2+
3+
var board = new arduino.Board({
4+
debug: true,
5+
device: "ACM"
6+
});
7+
8+
var led = new arduino.Led({
9+
board: board,
10+
pin: "13"
11+
});
12+
13+
board.on('ready', function(){
14+
led.blink();
15+
});

examples/rc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var rc = new arduino.RC({
1212

1313
board.on('ready', function(){
1414
setTimeout(function() {
15+
// alternative: rc.decimal("123456")
1516
rc.triState("0FFF0FFFFF0F");
1617
}, 1000);
1718
setTimeout(function() {

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
Sensor: require('./lib/sensor'),
99
Ping: require('./lib/ping'),
1010
PIR: require('./lib/pir'),
11+
LCD: require('./lib/lcd'),
1112
RC: require('./lib/rc'),
1213
IR: require('./lib/ir')
1314
};

lib/board.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var events = require('events'),
33
child = require('child_process'),
44
util = require('util'),
5-
colors = require('colors'),
5+
chalk = require('chalk'),
66
serial = require('serialport');
77

88
/*
@@ -12,6 +12,7 @@ var events = require('events'),
1212
var Board = function (options) {
1313
this.log('info', 'initializing');
1414
this.debug = options && options.debug || false;
15+
this.device = options && options.device || 'usb|ttyACM*|ttyS0';
1516
this.baudrate = options && options.baudrate || 115200;
1617
this.writeBuffer = [];
1718

@@ -28,7 +29,7 @@ var Board = function (options) {
2829

2930
self.log('info', 'binding serial events');
3031
self.serial.on('data', function(data){
31-
self.log('receive', data.toString().red);
32+
self.log('receive', chalk.red(data.toString()));
3233
self.emit('data', data);
3334
});
3435

@@ -72,7 +73,7 @@ util.inherits(Board, events.EventEmitter);
7273
Board.prototype.detect = function (callback) {
7374
this.log('info', 'attempting to find Arduino board');
7475
var self = this;
75-
child.exec('ls /dev | grep ACM', function(err, stdout, stderr){
76+
child.exec('ls /dev | grep -E "'+ self.device +'"', function(err, stdout, stderr){
7677
var usb = stdout.slice(0, -1).split('\n'),
7778
found = false,
7879
err = null,
@@ -131,7 +132,7 @@ Board.prototype.write = function (m) {
131132
this.log('write', m);
132133
this.serial.write('!' + m + '.');
133134
} else {
134-
this.log('info', 'serial not ready, buffering message: ' + m.red);
135+
this.log('info', 'serial not ready, buffering message: ' + chalk.red(m));
135136
this.writeBuffer.push(m);
136137
}
137138
}
@@ -180,7 +181,7 @@ Board.prototype.pinMode = function (pin, val) {
180181
Board.prototype.digitalWrite = function (pin, val) {
181182
pin = this.normalizePin(pin);
182183
val = this.normalizeVal(val);
183-
this.log('info', 'digitalWrite to pin ' + pin + ': ' + val.green);
184+
this.log('info', 'digitalWrite to pin ' + pin + ': ' + chalk.green(val));
184185
this.write('01' + pin + val);
185186
}
186187

@@ -196,7 +197,7 @@ Board.prototype.digitalRead = function (pin) {
196197
Board.prototype.analogWrite = function (pin, val) {
197198
pin = this.normalizePin(pin);
198199
val = this.normalizeVal(val);
199-
this.log('info', 'analogWrite to pin ' + pin + ': ' + val.green);
200+
this.log('info', 'analogWrite to pin ' + pin + ': ' + chalk.green(val));
200201
this.write('03' + pin + val);
201202
}
202203
Board.prototype.analogRead = function (pin) {
@@ -219,7 +220,7 @@ Board.prototype.delay = function (ms) {
219220
Board.prototype.log = function (/*level, message*/) {
220221
var args = [].slice.call(arguments);
221222
if (this.debug) {
222-
console.log(String(+new Date()).grey + ' duino '.blue + args.shift().magenta + ' ' + args.join(', '));
223+
console.log(chalk.gray(Date.now()) + chalk.blue(' duino ') + chalk.magenta(args.shift()) + ' ' + args.join(', '));
223224
}
224225
}
225226

0 commit comments

Comments
 (0)