PhilipsHue / PhilipsHueSDK-Java-MultiPlatform-Android

The Software Development Kit for Philips Hue Java Mulfi-Platform and Android (beta)
273 stars 214 forks source link

Strobe effect not working #11

Closed snehalpachigar closed 9 years ago

snehalpachigar commented 9 years ago

Hello, I am trying to get the strobe light effect with my hue bulbs, but for reason the result that I get are not satisfactory. I don't see a smooth strobe effect. Sometimes light gets stuck at a state and lags in giving the strobe effect. Every time the behavior is random. Is it difficult to have strobe effect? Here is the code that I use.

public void strobe(final int timeInMillis, final int speed) {

    Thread t1 = new Thread() {

        @Override
        public void run() {

            long startTime = SystemClock.uptimeMillis();
            while ((SystemClock.uptimeMillis() - startTime) < timeInMillis) { // Strobe for the amount of time specified. 
                if (flag) { // True initially
                    flag = false;
                    lightState.setOn(true);
                } else {
                    flag = true;
                    lightState.setOn(false);
                }
                lightState.setTransitionTime(1); // max 65500
                lightState.setBrightness(254);
                Log.e("TAG", "State is" + lightState.validateState());
                bridge.updateLightState(light1, lightState, listener);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    t1.start();
}

Am I doing something wrong? I do get the log that the state is validated, but light bridge doesnt update everytime as I want. Why am I not getting expected results?

SteveyO commented 9 years ago

Hi,

At a quick glance of you code I can see a few improvements:-

I have cleaned up the if/then else condition a bit, but of course this has no bearing on the performance:- while ((System.currentTimeMillis() - startTime) < timeInMillis) { // Strobe for the amount of time specified. flag=!flag; lightState.setOn(flag); lightState.setTransitionTime(1); // max 65500 if (flag) lightState.setBrightness(254); // Only send brightness when turning light on.

        bridge.updateLightState(light1, lightState, lightListener);              
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {}
    }

This works a bit better, but not completely smooth. I will get back to you as soon as I find a better way.