pimote
Version:
Control Energenie Switches from a Raspberry Pi
85 lines (70 loc) • 1.87 kB
JavaScript
// Generated by CoffeeScript 2.6.1
(function() {
var BITS, ENABLE, OnOff, Pimote, rpio,
indexOf = [].indexOf;
rpio = require('rpio');
BITS = [11, 15, 16, 13, 18, 22];
ENABLE = BITS[5];
OnOff = Object.freeze({
on: [0b1101, 0b1111, 0b0111, 0b1011, 0b0011],
off: [0b1100, 0b1110, 0b0110, 0b1010, 0b0010]
});
Pimote = class Pimote {
constructor() {
throw new Error("This is a static class");
}
static init() {
var bit, i, len;
rpio.init({
gpiomem: true,
mapping: 'physical'
});
for (i = 0, len = BITS.length; i < len; i++) {
bit = BITS[i];
rpio.open(bit, rpio.OUTPUT, rpio.LOW);
}
}
static switchOn(socket = 0) {
this.init();
this.enable(OnOff.on[socket]);
return this.close();
}
static switchOff(socket = 0) {
this.init();
this.enable(OnOff.off[socket]);
return this.close();
}
static pair(socket) {
if (indexOf.call([1, 2, 3, 4], socket) < 0) {
throw "Socket 1 to 4 only";
}
this.init();
this.enable(OnOff.on[socket], 5000);
return this.close();
}
static enable(bitmap, timer = 250) {
var bit, i, len, mask, ref;
mask = 0b1000;
ref = BITS.slice(0, 4);
for (i = 0, len = ref.length; i < len; i++) {
bit = ref[i];
rpio.write(bit, bitmap & mask ? rpio.HIGH : rpio.LOW);
mask >>= 1;
}
rpio.msleep(100);
rpio.write(ENABLE, rpio.HIGH);
rpio.msleep(timer);
return rpio.write(ENABLE, rpio.LOW);
}
static close() {
var bit, i, len, results;
results = [];
for (i = 0, len = BITS.length; i < len; i++) {
bit = BITS[i];
results.push(rpio.close(bit));
}
return results;
}
};
module.exports = Pimote;
}).call(this);