Created
July 26, 2014 15:20
-
-
Save iwater/1563f6f5c7f8e2de1587 to your computer and use it in GitHub Desktop.
use Tessel to control Sony TV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tessel = require('tessel'); | |
var sony = require('./sony'); | |
var infraredlib = require('ir-attx4'); | |
var infrared = infraredlib.use(tessel.port['A']); | |
// When we're connected | |
infrared.on('ready', function() { | |
if (!err) { | |
console.log("Connected to IR!"); | |
// Start sending a signal every three seconds | |
var timer = setInterval(function() { | |
// Make a buffer of on/off durations (each duration is 16 bits) | |
var powerBuffer = sony.generateIRCodeForSending(0xA90); | |
// Sony signal at 40 kHz | |
// https://github.com/shirriff/Arduino-IRremote/blob/master/IRremote.cpp @line 96 | |
infrared.sendRawSignal(40, powerBuffer, function(err) { | |
if (err) { | |
console.log("Unable to send signal: ", err); | |
} else { | |
console.log("Signal sent!"); | |
} | |
}); | |
}, 3000); // Every 3 seconds | |
} else { | |
console.log(err); | |
} | |
}); | |
// If we get data, print it out | |
infrared.on('data', function(data) { | |
console.log("Received RX Data: ", data); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// because this bug, you must write a negative number to buffer as it's complement using writeUIntXX | |
// https://forums.tessel.io/t/write-negative-number-to-buffer-got-zero/642 | |
var get16BitsComplement = function (number) { | |
return number < 0 ? (65536 + number) : number; | |
}; | |
// Tessel IR module lib using duration factor 50 | |
var factor = 50; | |
var headerBytes = [2400 / factor, get16BitsComplement(-600 / factor)]; | |
var oneOnDuration = 1200 / factor; | |
var zeroOnDuration = 600 / factor; | |
var offDuration = get16BitsComplement(-600 / factor); | |
var repeatDuration = get16BitsComplement(-25700 / factor); | |
var bodyLen = 12; | |
function generateIRCode(hexValue) { | |
var headerBuf = new Buffer(4); | |
headerBuf.writeUInt16BE(headerBytes[0], 0); | |
headerBuf.writeUInt16BE(headerBytes[1], 2); | |
// multiply by 4 b/c we're sending int16s (2 8-byte words) for each duration | |
// and there is both an on and an off duration | |
var bodyBuf = new Buffer(bodyLen * 2 * 2); | |
for (var i = 0; i < bodyLen; i++) { | |
// If the next bit is a 1 | |
if ((hexValue >> (bodyLen - i - 1)) & 1) { | |
// Write the one ON duration | |
bodyBuf.writeUInt16BE(oneOnDuration, i * 4); | |
} else { | |
// Write the zero ON duration | |
bodyBuf.writeUInt16BE(zeroOnDuration, i * 4); | |
} | |
// Write the standard OFF duration | |
bodyBuf.writeUInt16BE(offDuration, (i * 4) + 2); | |
} | |
bodyBuf.writeUInt16BE(repeatDuration, bodyBuf.length - 2); | |
return Buffer.concat([headerBuf, bodyBuf]); | |
}; | |
var generateIRCodeForSending = exports.generateIRCodeForSending = function (hexValue) { | |
return Buffer.concat([generateIRCode(hexValue), generateIRCode(hexValue), generateIRCode(hexValue)]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
because tessel ir lib will update to hidden factor, when using whith new lib change factor to number 1