mschuler / Lifx.Lib

Portable Class Library (PCL) to control LIFX Bulbs
MIT License
1 stars 1 forks source link

Bug in reading light state #1

Open rscott78 opened 8 years ago

rscott78 commented 8 years ago

Hey, thanks for the work in your library. I went through about a dozen other C# Lifx libraries that were all garbage - finally found yours!

I've come across what I believe is a bug. When the light state is polled, the hue value is lost. In LightStatus.cs, the hue value will be a large number - say for example 32000. When the HsvColor gets set, there's a statement _hue = Math.Min(360d, Math.Max(hue, 0)); which essentially says that it can't be higher than 360. The result is that the hue is always 360.

mschuler commented 8 years ago

Hey, thank you for opening the first issue :smile:

Where does this large value comes from?

rscott78 commented 8 years ago

It comes from the API I suppose. Their documentation says it can be that high. I mostly solved the problem doing this - it basically scales it to a possible value of 65535.0. I say mostly because it seems like there was still a bug in there, but I don't recall what it was atm. :)


        internal override void SetPayload(byte[] payload) {
            _hue = BitConverter.ToUInt16(payload, 0);

            // 
            //GetUInt16(payload, 0, 360);

            //_saturation = GetUInt16(payload, 2, 100);
            //_brightness = GetUInt16(payload, 4);

            _saturation = BitConverter.ToUInt16(payload, 2);
            _brightness = BitConverter.ToUInt16(payload, 4);

            _kelvin = BitConverter.ToUInt16(payload, 6);
            _power = BitConverter.ToUInt16(payload, 10);
            _name = payload.ToUtf8String(12, 32);
            _bitmask = BitConverter.ToUInt64(payload, 44);
        }

        internal override void Apply(Bulb bulb) {
            bulb.Name = _name;
            bulb.IsPowerOn = _power != 0;

            var realHue = (_hue / 65535.0) * 360.0;
            var realBright = _brightness / 65535.0;
            var realSat = _saturation / 65535.0;

            bulb.Color = new HsvColor(realHue, realSat, realBright, _kelvin);

            //bulb.Color = new HsvColor(realHue, _saturation / 100.0, _brightness / 255.0, _kelvin);
        }