Skip to content

Instantly share code, notes, and snippets.

@iwater
Created July 27, 2014 14:33
Show Gist options
  • Save iwater/2141841420452b71277b to your computer and use it in GitHub Desktop.
Save iwater/2141841420452b71277b to your computer and use it in GitHub Desktop.
use Espruino to control Sony TV
var get16BitsComplement = function (number) {
return number;
};
var factor = 1000;
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;
var generateIRCode = function (hexValue) {
var headerBuf = [];
headerBuf.push(headerBytes[0]);
headerBuf.push(headerBytes[1]);
// 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 = [];
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.push(oneOnDuration);
} else {
// Write the zero ON duration
bodyBuf.push(zeroOnDuration);
}
// Write the standard OFF duration
bodyBuf.push(offDuration);
}
bodyBuf[bodyBuf.length-1] = repeatDuration;
return [].concat(headerBuf, bodyBuf);
};
var generateIRCodeForSending = function (hexValue) {
return [].concat(generateIRCode(hexValue), generateIRCode(hexValue), generateIRCode(hexValue));
};
var pin = C1;
function sendCommand(cmd) {
var codes = generateIRCodeForSending(cmd);
analogWrite(A1, 0.5, { freq:38000 });
codes.forEach(function(code){
digitalPulse(pin, code > 0, Math.abs(code));
});
digitalPulse(pin,1,0);
pin.reset();
digitalRead(A1);
}
setWatch(function() {
sendCommand(0xA90);
print('finish');
}, BTN1, { repeat:true, edge:"rising", debounce:10 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment