Exemple #1
0
 /// <summary> Creates a new color object from hexadecimal values. (ex. #0000ff) </summary>
 public Color(string hexColor)
 {
     byte[] bytes = Utilis.ToByteArray(hexColor);
     Red   = bytes[0];
     Green = bytes[1];
     Blue  = bytes[2];
 }
Exemple #2
0
        /// <summary>
        /// Sets the light a custom pattern.
        /// Use an array of Color objects to assign a list of colors the light will cycle through.
        /// </summary>
        /// <param name="colors"> The array of colors that the light will cycle through. </param>
        /// <param name="transition"> The transition type (Gradual, Strobe, Jump). </param>
        /// <param name="speed"> How quick the light will cycle through the pattern, from 0 to 100. </param>
        public async Task SetCustomPatternAsync(Color[] colors, TransitionType transition, byte speed)
        {
            List <byte> data      = new List <byte>();
            bool        firstbyte = true;

            for (int i = 0; i < colors.Length; i++)
            {
                if (firstbyte == true)
                {
                    data.Add(0x51);
                    firstbyte = false;
                }
                else
                {
                    data.Add(0);
                }

                data.AddRange(new byte[] { colors[i].Red, colors[i].Green, colors[i].Blue });
            }

            for (int i = 0; i < 16 - colors.Length; i++)
            {
                data.AddRange(new byte[] { 0, 1, 2, 3 });
            }

            data.AddRange(new byte[] { 0x00, Utilis.SpeedToDelay(speed), Convert.ToByte(transition), 0xff, 0x0f });

            byte[] dataReady = data.ToArray();
            await SendDataAsync(dataReady);

            //Populate field.
            Mode = LightMode.Custom;
        }
Exemple #3
0
        /* TODO.
         * /// <summary> Sets the brightness of the light. </summary>
         * /// <param name="brightness"> The level of brightness, from 0 to 100. </param>
         * public async Task SetBrightnessAsync(byte brightness)
         * {
         *  if (brightness > 100) throw new MagicHomeException("Brightness value cannot be more than 100");
         *  if (Mode == LightMode.Color)
         *      await SetColorAsync(
         *          Convert.ToByte(Color.Red * (brightness / Brightness)),
         *          Convert.ToByte(Color.Green * (brightness / Brightness)),
         *          Convert.ToByte(Color.Blue * (brightness / Brightness))
         *          );
         *  else if (Mode == LightMode.WarmWhite)
         *      await SetWarmWhiteAsync(Convert.ToByte(WarmWhite * brightness / Brightness));
         *  UpdateBrightness();
         * }*/

        /// <summary> Sets a preset pattern. </summary>
        /// <param name="speed"> The speed of the pattern from 0 to 100. </param>
        public async Task SetPresetPatternAsync(PresetPattern pattern, byte speed)
        {
            byte delay = Utilis.SpeedToDelay(speed);

            await SendDataAsync(new byte[] { 0x61, Convert.ToByte(pattern), delay, 0x0f });

            //Populate field.
            Mode = LightMode.Preset;
        }
Exemple #4
0
 /// <summary> Updates the brightness property of this instance based on it's colors or warm white value. </summary>
 private void UpdateBrightness()
 {
     if (Mode == LightMode.Color)
     {
         Brightness = Utilis.DetermineBrightness(Color.Red, Color.Green, Color.Blue);
     }
     else if (Mode == LightMode.WarmWhite)
     {
         Brightness = Utilis.DetermineBrightness(WarmWhite, WarmWhite, WarmWhite);
     }
 }
Exemple #5
0
        /// <summary> Sends a request to get the light's status.
        /// Updates this instance with current bulbs's mode, time, status, protocol, color, brightness.
        ///
        /// This operation usually takes between 80 and 500 milliseconds.
        /// </summary>
        public async Task RefreshAsync()
        {
            //Send request for status.
            if (Protocol == LedProtocol.LEDENET)
            {
                await SendDataAsync(0x81, 0x8a, 0x8b);
            }
            else if (Protocol == LedProtocol.LEDENET_ORIGINAL)
            {
                await SendDataAsync(0xef, 0x01, 0x77);
            }

            //Read and process the response.
            var dataRaw = await ReadDataAsync();

            string[] dataHex = new string[14];
            for (int i = 0; i < dataHex.Length; i++)
            {
                dataHex[i] = dataRaw[i].ToString("X");
            }

            //Check if it uses checksum.
            if (Protocol == LedProtocol.LEDENET_ORIGINAL)
            {
                if (dataHex[1] == "01")
                {
                    UseCsum = false;
                }
            }

            //Check power state.
            if (dataHex[2] == "23")
            {
                Power = true;
            }
            else if (dataHex[2] == "24")
            {
                Power = false;
            }

            //Check light mode.
            Mode = Utilis.DetermineMode(dataHex[3], dataHex[9]);

            //Handle color property.
            switch (Mode)
            {
            case LightMode.Color:
                Color     = new Color(dataRaw[6], dataRaw[7], dataRaw[8]);
                WarmWhite = 0;
                break;

            case LightMode.WarmWhite:
                Color     = Colors.Empty;
                WarmWhite = dataRaw[9];
                break;

            case LightMode.Preset:
            case LightMode.Unknown:
            case LightMode.Custom:
                Color     = Colors.Empty;
                WarmWhite = 0;
                break;
            }

            UpdateBrightness();

            //Send request to get the time of the light.
            Time = await GetTimeAsync();
        }