Skip to content

Commit

Permalink
Merge pull request #47 from tessel/jon-ir-async
Browse files Browse the repository at this point in the history
Jon ir async
  • Loading branch information
kevinmehall committed May 15, 2014
2 parents bd1d4f4 + 0a41d2e commit 32d78af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 84 deletions.
40 changes: 0 additions & 40 deletions examples/duration_test.js

This file was deleted.

10 changes: 4 additions & 6 deletions examples/power-insignia.js → examples/infrared.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ var hardware = tessel.port('a');
// Import library and connect to module
var infrared = require('../index').use(hardware);

var counter = 0;

// When we're connected
infrared.on('ready', function() {
if (!err) {
console.log("Connected to IR!");

// Start turning tv on and off every 3 seconds
setInterval(powerTV, 3000);
// Start turning sending a signal every three seconds
setInterval(sendSignal, 3000);
}
else {
console.log("Err initializing: ", err.message );
Expand All @@ -26,7 +24,7 @@ infrared.on('data', function(data) {
console.log("Received RX Data: ", data);
});

var powerTV = function() {
var sendSignal = function() {

// Make a buffer off on/off durations (each duration is 16 bits)
var powerBuffer = new Buffer([0, 178, 255, 168, 0, 12, 255, 246, 0, 13, 255, 225, 0, 13, 255, 224, 0, 12, 255, 246, 0, 12, 255, 246, 0, 13, 255, 247, 0, 13, 255, 247, 0, 13, 255, 224, 0, 12, 255, 224, 0, 13, 255, 247, 0, 13, 255, 224, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 13, 255, 247, 0, 13, 255, 224, 0, 12, 255, 224, 0, 13, 255, 225, 0, 13, 255, 224, 0, 12, 255, 246, 0, 12, 255, 246, 0, 13, 255, 247, 0, 13, 255, 247, 0, 13, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 246, 0, 12, 255, 224, 0, 13, 255, 224, 0, 12, 255, 224, 0, 12, 255, 224, 0, 12]);
Expand All @@ -35,7 +33,7 @@ var powerTV = function() {
infrared.sendRawSignal(38, powerBuffer, function(err) {
if (err) console.log("Unable to send signal: ", err);
else {
console.log("TV Should be powered...");
console.log("Signal sent!");
}
});
};
56 changes: 18 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var MAX_SIGNAL_DURATION = 200;

var Infrared = function(hardware, callback) {

this.spi = hardware.SPI({clockSpeed : 1000, mode:2});
this.chipSelect = hardware.gpio(1);
this.reset = hardware.gpio(2);
this.irq = hardware.gpio(3);
this.spi = hardware.SPI({clockSpeed : 1000, mode:2, chipSelect:this.chipSelect});
this.transmitting = false;
this.listening = false;
this.chipSelect.output().high();
Expand Down Expand Up @@ -101,20 +101,20 @@ Infrared.prototype.setListening = function(set, callback) {

var cmd = set ? RX_START_CMD : RX_STOP_CMD;

var response = this._SPITransfer(new Buffer([cmd, 0x00, 0x00]));

self._validateResponse(response, [PACKET_CONF, cmd], function(valid) {
self.spi.transfer(new Buffer([cmd, 0x00, 0x00]), function listeningSet(err, response) {
self._validateResponse(response, [PACKET_CONF, cmd], function(valid) {

if (!valid) {
return callback && callback(new Error("Invalid response on setting rx on/off."));
}
else {
self.listening = set ? true : false;
if (!valid) {
return callback && callback(new Error("Invalid response on setting rx on/off."));
}
else {
self.listening = set ? true : false;

if (callback) {
callback();
if (callback) {
callback();
}
}
}
});
});
};

Expand All @@ -123,7 +123,7 @@ Infrared.prototype._fetchRXDurations = function(callback) {
// We have to pull chip select high in case we were in the middle of something else

// this.chipSelect.high();
this._SPITransfer(new Buffer([IR_RX_AVAIL_CMD, 0x00, 0x00, 0x00]), function(response) {
self.spi.transfer(new Buffer([IR_RX_AVAIL_CMD, 0x00, 0x00, 0x00]), function spiComplete(err, response) {
// DO something smarter than this eventually

self._validateResponse(response, [PACKET_CONF, IR_RX_AVAIL_CMD, 1], function(valid) {
Expand All @@ -142,7 +142,7 @@ Infrared.prototype._fetchRXDurations = function(callback) {

packet = new Buffer(packet);

self._SPITransfer(packet, function(response) {
self.spi.transfer(packet, function spiComplete(err, response) {

var fin = response[response.length-1];

Expand All @@ -155,7 +155,7 @@ Infrared.prototype._fetchRXDurations = function(callback) {
}

else {
// Remove the header echoes at the beginning
// Remove the header echoes at the beginning and stop bit
var buf = response.slice(rxHeader.length, response.length-1);

// Emit the buffer
Expand Down Expand Up @@ -196,14 +196,14 @@ Infrared.prototype.sendRawSignal = function(frequency, signalDurations, callback
var tx = this._constructTXPacket(frequency, signalDurations);

// Send it over
this._SPITransfer(tx, function(response) {
this.spi.transfer(tx, function spiComplete(err, response) {

self.transmitting = false;
var err;

// If there was an error already, set immediate on the callback
if (!self._validateResponse(response, [PACKET_CONF, IR_TX_CMD, frequency, signalDurations.length/2])) {
err = new Error("Invalid response from raw signal packet: " + response);
err = new Error("Invalid response from raw signal packet.");
}

if (callback) {
Expand Down Expand Up @@ -274,7 +274,7 @@ Infrared.prototype._establishCommunication = function(retries, callback){
Infrared.prototype.getFirmwareVersion = function(callback) {
var self = this;

self._SPITransfer(new Buffer([FIRMWARE_CMD, 0x00, 0x00]), function(response) {
self.spi.transfer(new Buffer([FIRMWARE_CMD, 0x00, 0x00]), function spiComplete(err, response) {
if (err) {
return callback(err, null);
}
Expand Down Expand Up @@ -314,26 +314,6 @@ Infrared.prototype._validateResponse = function(values, expected, callback) {
return res;
};

Infrared.prototype._SPITransfer = function(data, callback) {

// Pull Chip select down prior to transfer
this.chipSelect.low();

// Send over the data
var ret = this.spi.transferSync(data);

// Pull chip select back up
this.chipSelect.high();

// Call any callbacks
if (callback) {
callback(ret);
}

// Return the data
return ret;
};

exports.Infrared = Infrared;
exports.use = function (hardware, callback) {
return new Infrared(hardware, callback);
Expand Down

0 comments on commit 32d78af

Please sign in to comment.