Open Will-Mann-16 opened 3 years ago
If anyone wants it, my code for the Accessory Controller for HomeBridge - pendant on a solution: `const colorsys = require('colorsys');
const LED_COUNT = 300;
module.exports = api => { api.registerAccessory('Lightstrip', LightstripAccessory); }
function rgb2Int(r, g, b) { return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff); }
class LightstripAccessory{ constructor(log, config, api){ this.log = log; this.config = config; this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.log.debug('Lightstrip Accessory Plugin Loading...')
this.informationService = new this.Service.AccessoryInformation();
this.informationService.setCharacteristic(this.Characteristic.Manufacturer, "CHINLEY");
this.informationService.setCharacteristic(this.Characteristic.Model, "LED Strip Light SMD5050");
this.lightService = new this.Service.Lightbulb(this.name);
this.controller = require('rpi-ws281x-native');
this.controller.init(LED_COUNT);
this.activeData = [];
this.on = false;
this.hue = 0;
this.saturation = 0;
this.brightness = 0;
this.lightService.getCharacteristic(this.Characteristic.On).on('get', this.handleGetOn.bind(this)).on('set', this.handleSetOn.bind(this));
this.lightService.getCharacteristic(this.Characteristic.Hue).on('get', this.handleGetHue.bind(this)).on('set', this.handleSetHue.bind(this));
this.lightService.getCharacteristic(this.Characteristic.Saturation).on('get', this.handleGetSaturation.bind(this)).on('set', this.handleSetSaturation.bind(this));
this.lightService.getCharacteristic(this.Characteristic.Brightness).on('get', this.handleGetBrightness.bind(this)).on('set', this.handleSetBrightness.bind(this));
// process.on('SIGINT', function () {
// this.controller.reset();
// process.nextTick(function () { process.exit(0); });
// });
this.log.debug('Lightstrip Accessory Plugin Loaded')
}
getServices() {
return [
this.informationService,
this.lightService,
];
}
render(){
if(this.on){
let { r, g, b } = colorsys.hsvToRgb(this.hue, this.saturation, this.brightness);
for(let i = 0; i < LED_COUNT; i++){
this.activeData[i] = rgb2Int(r, g, b);
}
} else {
this.activeData = [];
}
this.controller.render(activeData);
}
handleGetOn(callback){
callback(null, this.on);
}
handleSetOn(value, callback){
this.on = value;
this.render();
callback(null);
}
handleGetHue(callback){
callback(null, this.hue);
}
handleSetHue(value, callback){
this.hue = value;
this.render();
callback(null);
}
handleGetSaturation(callback){
callback(null, this.saturation);
}
handleSetSaturation(value, callback){
this.saturation = value;
this.render();
callback(null);
}
handleGetBrightness(callback){
callback(null, this.brightness);
}
handleSetBrightness(value, callback){
this.brightness = value;
this.render();
callback(null);
}
}`
Hello everyone, I was trying to setup this package on my RP4 with HomeBridge to see if I could control my LED strip from my iPhone. No luck so far, as I'm getting this error:
[9/21/2020, 00:12:13] [Lightstrip] Initializing Lightstrip accessory... Can't open /dev/mem: Operation not permitted error initializing -5 mmap() failed [9/21/2020, 00:12:13] TypeError: Init Error at EventEmitter.ws281x.init (/home/pi/Documents/strip-controller/homebridge-lightstrip/node_modules/rpi-ws281x-native/lib/ws281x-native.js:138:14) at new LightstripAccessory (/home/pi/Documents/strip-controller/homebridge-lightstrip/index.js:31:25) at /usr/local/lib/node_modules/homebridge/src/server.ts:355:50 at Array.forEach (<anonymous>) at Server._loadAccessories (/usr/local/lib/node_modules/homebridge/src/server.ts:328:29) at Server.start (/usr/local/lib/node_modules/homebridge/src/server.ts:154:12) at cli (/usr/local/lib/node_modules/homebridge/src/cli.ts:80:10) at Object.<anonymous> (/usr/local/lib/node_modules/homebridge/bin/homebridge:17:22) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) [9/21/2020, 00:12:13] Got SIGTERM, shutting down Homebridge... [9/21/2020, 00:12:18] [HB Supervisor] Homebridge Process Ended. Code: 1
I've setup HomeBridge with a superuser, and I've tested the package/code with the rainbow file, and it works, so I don't exactly know what's going on here. Any ideas? I would be eternally grateful.