Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extended Analog support

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information...
commit c64e413076b57b272877bac4df9f08ae8bc76a2a 1 parent 763fa16
@rwaldron rwaldron authored
Showing with 55 additions and 3 deletions.
  1. +31 −3 lib/firmata.js
  2. +24 −0 test/firmata.test.js
View
34 lib/firmata.js
@@ -42,6 +42,7 @@ var PIN_MODE = 0xF4,
QUERY_FIRMWARE = 0x79,
REPORT_VERSION = 0xF9,
ANALOG_MESSAGE = 0xE0,
+ EXTENDED_ANALOG = 0x6F,
CAPABILITY_QUERY = 0x6B,
CAPABILITY_RESPONSE = 0x6C,
PIN_STATE_QUERY = 0x6D,
@@ -448,7 +449,7 @@ var Board = function(port, options, callback) {
else if (board.currentBuffer[0] !== START_SYSEX) {
// Identify command on first byte
cmd = board.currentBuffer[0] < 240 ? board.currentBuffer[0] & 0xF0 : board.currentBuffer[0];
-
+
// Check if it is not a valid command
if (cmd !== REPORT_VERSION && cmd !== ANALOG_MESSAGE && cmd !== DIGITAL_MESSAGE) {
// console.log("OUT OF SYNC - CMD: "+cmd);
@@ -471,7 +472,7 @@ var Board = function(port, options, callback) {
MIDI_RESPONSE[cmd](board);
board.currentBuffer.length = 0;
} else {
- // A bad serial read must have happened.
+ // A bad serial read must have happened.
// Reseting the buffer will allow recovery.
board.currentBuffer.length = 0;
}
@@ -548,8 +549,35 @@ Board.prototype.analogRead = function (pin, callback) {
*/
Board.prototype.analogWrite = function (pin, value) {
+ var data = [];
+
this.pins[pin].value = value;
- this.sp.write(new Buffer([ANALOG_MESSAGE | pin, value & 0x7F, (value >> 7) & 0x7F]));
+
+ if (pin > 15) {
+ data[0] = START_SYSEX;
+ data[1] = EXTENDED_ANALOG;
+ data[2] = pin;
+ data[3] = value & 0x7F;
+ data[4] = (value >> 7) & 0x7F;
+
+ if (value > 0x00004000) {
+ data[data.length] = (value >> 14) & 0x7F;
+ }
+
+ if (value > 0x00200000) {
+ data[data.length] = (value >> 21) & 0x7F;
+ }
+
+ if (value > 0x10000000) {
+ data[data.length] = (value >> 28) & 0x7F;
+ }
+
+ data[data.length] = END_SYSEX;
+ } else {
+ data.push(ANALOG_MESSAGE | pin, value & 0x7F, (value >> 7) & 0x7F);
+ }
+
+ this.sp.write(new Buffer(data));
};
/**
View
24 test/firmata.test.js
@@ -305,6 +305,30 @@ describe('board', function () {
done();
});
+ it('should be able to write a value to an extended analog output', function (done) {
+ var length = board.pins.length;
+
+ board.pins[46] = {
+ supportedModes: [0, 1, 4],
+ mode: 4,
+ value: 0,
+ report: 1,
+ analogChannel: 127
+ };
+
+
+ board.analogWrite(46, 180);
+ should.deepEqual(serialPort.lastWrite, [0xF0, 0x6F, 46, 52, 1, 0xF7]);
+
+ board.analogWrite(46, 0);
+ should.deepEqual(serialPort.lastWrite, [0xF0, 0x6F, 46, 0, 0, 0xF7]);
+
+ // Restore to original length
+ board.pins.length = length;
+
+ done();
+ });
+
it('should be able to send an i2c config', function (done) {
board.sendI2CConfig(1);
should.deepEqual(serialPort.lastWrite, [0xF0, 0x78, 1 & 0xFF, (1 >> 8) & 0xFF, 0xF7]);
Please sign in to comment.
Something went wrong with that request. Please try again.