rpi-clickety
Version:
Hook up your 433Mhz remote control outlets to your raspberry pi!
90 lines (89 loc) • 3.68 kB
JavaScript
"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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var events_1 = require("events");
var pigpio_1 = __importDefault(require("pigpio"));
var State;
(function (State) {
State[State["SEEK"] = 0] = "SEEK";
State[State["BUFFERING"] = 1] = "BUFFERING";
})(State || (State = {}));
;
var Framer = /** @class */ (function (_super) {
__extends(Framer, _super);
function Framer(syncMarker, maxBufferLen) {
if (syncMarker === void 0) { syncMarker = 4000; }
if (maxBufferLen === void 0) { maxBufferLen = 64; }
var _this = _super.call(this) || this;
_this.syncLowDuration = 0;
_this.count = 0;
_this.state = State.SEEK;
_this.data = Buffer.alloc(maxBufferLen * 4);
_this.syncMarker = syncMarker;
_this.maxBufferLen = maxBufferLen;
_this.lastTick = pigpio_1.default.getTick();
return _this;
}
Framer.prototype.reset = function () {
this.state = State.SEEK;
this.count = 0;
};
Framer.prototype.onData = function (level, tick) {
//Compare amount of time between the last tick to the current tick
var duration = pigpio_1.default.tickDiff(this.lastTick, tick);
switch (this.state) {
case State.SEEK:
if (duration >= this.syncMarker) {
this.syncLowDuration = duration;
this.state = State.BUFFERING;
}
break;
case State.BUFFERING:
//If we get a duration that's close to the original long duration
//it might be the sync bit, signalling that we should process
//the buffered data.
if (Math.abs(duration - this.syncLowDuration) < 200) {
//Emit payload data along with sync bit information.
//Note that the high part of sync was the last recorded
this.emit('data', {
sync: {
high: this.data.readUInt32BE((this.count - 1) * 4),
low: this.syncLowDuration
},
data: Buffer.from(this.data).slice(0, ((this.count - 1) * 4))
});
//Treat this new long-low as a new sync
this.syncLowDuration = duration;
this.count = 0;
break;
}
//Only accumulate up to maxBufferLen items
//Otherwise start over as we've been buffering noise
if (this.count < this.maxBufferLen) {
this.data.writeUInt32BE(duration, this.count++ * 4);
}
else {
this.reset();
}
break;
}
this.lastTick = tick;
};
return Framer;
}(events_1.EventEmitter));
exports.default = Framer;