jangxx / node-magichome

An incomplete implementation of the functionality of the "Magic Home" app. Partially a port of https://github.com/Danielhiversen/flux_led to Node.js
ISC License
124 stars 26 forks source link

Cannot change warmwhite values when using effectMode #36

Closed lynes1 closed 3 years ago

lynes1 commented 3 years ago

I want to change the amount of the warm light on my Ledstrip while using effectMode, but i getting the following error. Even though this.setColor(r,g,b) works flawlessly.

const { Control } = require('magic-home');
const control = new Control("192.168.0.234");

control.startEffectMode().then(effects => {
    effects.start(interval_function);
}).catch(err => {
    console.log("Error while connecting:", err.message);
});

function interval_function() {
    return this.setColorAndWarmWhite(255, 0, 0,255); 
}
TypeError: this.setColorAndWarmWhite is not a function
    at EffectInterface.interval_function [as _interval_function] (C:\Users\lamec\Desktop\Tisch\effect.js:19:15)
    at Timeout.setTimeout [as _onTimeout] (C:\Users\lamec\Desktop\Tisch\node_modules\magic-home\lib\EffectInterface.js:86:29)
    at ontimeout (timers.js:466:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:267:5)

Also the normal Mode works flawlessly with this.setColorAndWarmWhite(r,g,b)

const { Control } = require('magic-home');

let light = new Control("192.168.0.234");
light.setColorAndWarmWhite(255,0,0,255);
lynes1 commented 3 years ago

So i found out how to fix it, but i had to overwrite the lib by going into the file node_modules\magic-home\lib\EffectInterface.js and adding the function at line 107:

setColorAndWarmWhite(red, green, blue, warmWhite) {
        red = clamp(red, 0, 255);
        green = clamp(green, 0, 255);
        blue = clamp(blue, 0, 255);

        let mask = (this._options.apply_masks) ? 0xF0 : 0;

        let cmd_buf;
        if (this._options.cold_white_support) {
            cmd_buf = Buffer.from([ 0x31, red, green, blue, warmWhite, 0, mask, 0x0f ]);
        } else {
            cmd_buf = Buffer.from([ 0x31, red, green, blue, warmWhite, mask, 0x0f ]);
        }

        this._sendCommand(cmd_buf);
    }

I hope you will patch this soon so i dont have to edit the file each time i install the package new

jangxx commented 3 years ago

Yes you are correct, changing the warm white value (and also the cold white value by extension) is currently not supported in the EffectInterface. The EffectInterface is actually missing quite a lot of additions that have been made to the main Control class, since I was under the assumption that no one was actually using it. I even thought about removing it entirely, but since you're telling me that you actually do use it, I'm now thinking about completely reworking the internals and finally adding all those missing features. Give me a few days, I will work on it once I find the time.

lynes1 commented 3 years ago

Thank you.

jangxx commented 3 years ago

Okay, I started work on this now. I haven't written any documentation yet, but if you want to play around with it already, you can take a look at example/async_effect_test.js.

Basically, the new system uses async-await to make the control flow and synchronization much easier. You still provide an interval function which gets called repeatedly, but you can specify the entire effect in one "line" instead of using variables to keep track of where you are.

I'm probably going to finish this in the next couple of days (more testing and documentation), but the code should work and be feature complete right now, so let me know what you think in case you get around to it.

jangxx commented 3 years ago

Version 2.7.0 is now live with the new AsyncEffectInterface.

Not only does this now have all the features of the main Control class, the interface is also much nicer to work with, because you can do everything asynchronously.