UNPKG

rpi-clickety

Version:

Hook up your 433Mhz remote control outlets to your raspberry pi!

121 lines (120 loc) 4.88 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var pigpio_1 = require("pigpio"); var events_1 = require("events"); var Framer_1 = __importDefault(require("./Framer")); var PulseBuffer_1 = __importDefault(require("./PulseBuffer")); var Receiver = /** @class */ (function (_super) { __extends(Receiver, _super); function Receiver(pin) { var _this = _super.call(this) || this; _this._listening = false; _this.receiver = new pigpio_1.Gpio(pin, { mode: pigpio_1.Gpio.INPUT }); _this.framer = new Framer_1.default(); _this.framer.on('data', _this.decodeBuffer.bind(_this)); return _this; } Receiver.prototype.decodeBuffer = function (_a) { var e_1, _b; var sync = _a.sync, data = _a.data; var timings = PulseBuffer_1.default.from(data); //Calculate base pulse length using the sync bit timings. //This should be about 32x the pulse length. var pulseLength = Math.trunc((sync.low + sync.high) / 32); var pulseTolerance = 0.6 * pulseLength; var pulseLong = 3 * pulseLength; var pulseLongTolerance = 3 * pulseTolerance; var bitLen = 4 * pulseLength; var bitTolerance = 4 * pulseTolerance; var code = 0; try { for (var timings_1 = __values(timings), timings_1_1 = timings_1.next(); !timings_1_1.done; timings_1_1 = timings_1.next()) { var _c = __read(timings_1_1.value, 2), high = _c[0], low = _c[1]; code <<= 1; //Validation: Combined, they should equal to ~4x pulseLength within some tolerance if (Math.abs((high + low) - bitLen) > bitTolerance) { return; } //high var always refers to high duration as either pulse train //starts with high val. If high is a long pulse, it's a 1 //bit. Otherwise zero (do nothing). if (Math.abs(high - pulseLong) < pulseLongTolerance) { code |= 1; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (timings_1_1 && !timings_1_1.done && (_b = timings_1.return)) _b.call(timings_1); } finally { if (e_1) throw e_1.error; } } timings.addPulse(sync.high, sync.low); //Append sync pulse return this.emit('code', { code: code, raw: timings.buffer.toString('base64'), info: { pulseLength: pulseLength, sync: sync }, }); }; Receiver.prototype.listenStop = function () { if (!this._listening) return; this.receiver.removeAllListeners(); this.receiver.disableAlert(); this.framer.reset(); this._listening = false; }; Receiver.prototype.listenStart = function () { if (this._listening) return; this.receiver.on('alert', this.framer.onData.bind(this.framer)); this.receiver.enableAlert(); this._listening = true; }; return Receiver; }(events_1.EventEmitter)); exports.default = Receiver;