Example #1
0
        public void MimickAverage(Nanoleaf nanoleaf)
        {
            JArray    palette = null;
            Stopwatch timer   = null;
            Bitmap    bitmap  = null;

            while (true)
            {
                palette = new JArray();
                timer   = new Stopwatch();
                timer.Start();
                bitmap = ImageAnalysis.CaptureBitmap(1);

                if (bitmap == null)
                {
                    palette = PaletteController.RandomPalette(1, maxBrightness: 0);
                }
                else
                {
                    int paletteCount = 12;
                    var averages     = ImageAnalysis.ReduceColors(ImageAnalysis.GetAverageColors(bitmap, 1, 1), true, paletteCount);
                    throw new Exception();

                    foreach (LCColor color in averages)
                    {
                        palette.Add(new LCColor("rgb", color.r, color.g, color.b).RGBJO);
                    }
                }

                lightController.CreateCustom(nanoleaf, palette, 5, 50, animType: "random");

                timer.Stop();
                CalcAverage(timer.ElapsedMilliseconds);
            }
        }
        public Tuple <string, string> CreateTest(Nanoleaf nanoleaf, JArray palette, int transMin, int transMax, string animType = "flow")
        {
            dynamic write = new JObject();

            write.command   = "display";
            write.animType  = animType;
            write.colorType = "HSB";
            write.animData  = null;
            write.loop      = true;
            write.palette   = palette;
            dynamic transTime = new JObject();

            transTime.maxValue = transMax;
            transTime.minValue = transMin;
            write.transTime    = transTime;

            dynamic jsonObject = new JObject();

            jsonObject.write = write;

            string url_ex = "/effects";

            //SendLightDelegate putUpdate = PutAndForget;
            //FireAndForget(putUpdate, url_ex, jsonObject.ToString(Formatting.None)); Thread.Sleep(300);
            PutAndForget(nanoleaf, url_ex, jsonObject.ToString(Formatting.None));
            return(new Tuple <string, string>(url_ex, jsonObject.ToString(Formatting.None)));
        }
        public bool PutAndForget(Nanoleaf nanoleaf, string url_ex, string json)
        {
            WebRequest request = WebRequest.Create(nanoleaf.url + url_ex);

            request.ContentType = "application/json";
            request.Method      = "PUT";

            MemoryStream jsonContent = new MemoryStream(Encoding.ASCII.GetBytes(json));

            using (var content = request.GetRequestStream())
            {
                jsonContent.Seek(0, SeekOrigin.Begin);
                jsonContent.WriteTo(content);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(true);
                }
                else if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    return(false);
                }

                return(true);
            }
        }
        public Dictionary <string, string> CreateCustomRhythm(Nanoleaf nanoleaf, JArray palette, int brightness)
        {
            dynamic pluginRequest = new JObject();

            pluginRequest.command  = "request";
            pluginRequest.animName = "Paint Splatter";
            dynamic jsonObject = new JObject();

            jsonObject.write = pluginRequest;
            JObject effect     = JObject.FromObject(Put(nanoleaf, "/effects", jsonObject.ToString(Formatting.None)));
            JToken  body       = JToken.Parse(effect["body"].ToString());
            string  pluginUuid = body["pluginUuid"].ToString();

            //get the rhythm/plugin uuid of the effect to overwrite, do not need to send the 'animName' in this update
            dynamic write = new JObject();

            write.command       = "display";
            write.pluginType    = "rhythm";
            write.pluginUuid    = pluginUuid;
            write.animType      = "plugin";
            write.colorType     = "HSB";
            write.animData      = null;
            write.loop          = true;
            write.flowFactor    = 0;
            write.explodeFactor = 0.5;
            write.palette       = palette;

            jsonObject       = new JObject();
            jsonObject.write = write;

            string url_ex = "/effects";

            return(Put(nanoleaf, url_ex, jsonObject.ToString(Formatting.None)));
        }
        public Tuple <string, string> CreateTemp(Nanoleaf nanoleaf, List <Panel> panels, List <LCColor> colors)
        {
            dynamic write = new JObject();

            write.command  = "display";
            write.animType = "static";

            string animData = String.Format("{0} ", panels.Count);
            int    indexer  = 0;

            foreach (LCColor color in colors)
            {
                panels[indexer].Color = color;
                //string format is the panelId, frame, rgbwt string
                animData += String.Format("{0} {1} {2} ", panels[indexer].panelId, 10, color.RGBWT);
                indexer  += 1;
            }

            write.animData = animData;

            dynamic jsonObject = new JObject();

            jsonObject.write = write;

            string url_ex = "/effects";

            PutAndForget(nanoleaf, url_ex, jsonObject.ToString(Formatting.None));
            return(new Tuple <string, string>(url_ex, jsonObject.ToString(Formatting.None)));
        }
        public void RainbowShift(Nanoleaf nanoleaf, int delay, int gap, int brightness)
        {
            Dictionary <string, int> settings = new Dictionary <string, int>()
            {
                { "hue", 360 },
                { "sat", 100 },
                { "brightness", brightness }
            };

            SetState(nanoleaf, settings, delay);

            int value = 360;

            while (true)
            {
                settings = new Dictionary <string, int>()
                {
                    { "hue", value }
                };
                SetState(nanoleaf, settings, delay);
                value -= gap;
                Thread.Sleep(delay);
                if (value <= 0)
                {
                    value = 360 + value;
                }
            }
        }
Example #7
0
        public Mode(Nanoleaf nanoleaf, int mode)
        {
            this.lightController = new LightController();
            //get the number of light panels and double it
            switch (mode)
            {
            case 1:
                Screenshot(nanoleaf);
                break;

            case 2:
                MimickAverage(nanoleaf);
                break;

            case 3:
                MimickMostUsed(nanoleaf);
                break;

            case 4:
                MimickPosition(nanoleaf);
                break;

            case 5:
                StreamingMode(nanoleaf);
                break;

            case 6:
                FrameMode(nanoleaf);
                break;

            default:
                Screenshot(nanoleaf);
                break;
            }
        }
Example #8
0
        public void MimickMostUsed(Nanoleaf nanoleaf)
        {
            Stopwatch timer  = null;
            Bitmap    bitmap = null;

            while (true)
            {
                timer = new Stopwatch();
                timer.Start();
                bitmap = ImageAnalysis.CaptureBitmap(1);

                if (bitmap == null)
                {
                    JArray colors = PaletteController.RandomPalette(1, maxBrightness: 0);
                    lightController.CreateCustom(nanoleaf, colors, 10, 10);
                }
                else
                {
                    int paletteCount = 12;
                    var mostUsed     = ImageAnalysis.GetColorsFromImage(bitmap, paletteCount);

                    JArray palette = new JArray();
                    foreach (LCColor color in mostUsed)
                    {
                        LCColor lcc = new LCColor("rgb", color.r, color.g, color.b);
                        palette.Add(lcc);
                    }

                    lightController.CreateCustom(nanoleaf, palette, 5, 50, animType: "random");
                }

                timer.Stop();
                CalcAverage(timer.ElapsedMilliseconds);
            }
        }
Example #9
0
        static void AddLight()
        {
            Ping_all();

            Nanoleaf nanoleaf = null;
            int      attempts = 1;

            while (nanoleaf == null && attempts < 10)
            {
                attempts++;
                Log.Write("Hold the on-off button down for 5-7 seconds until the LED starts flashing in a pattern"); //pairing stays active for about 30s
                Thread.Sleep(/*5000*/ 100);                                                                          //sleep for 5 seconds for the user to press and hold the button
                Log.Write("waited");

                Parallel.ForEach(lights, (light) =>
                {
                    Log.Write("attempt: " + attempts);
                    nanoleaf = new LightController().SelectCorrectLight(light);
                });

                if (nanoleaf == null)
                {
                    Thread.Sleep(1000);
                    Log.Write("slept");
                }
            }
        }
        public Dictionary <string, string> Off(Nanoleaf nanoleaf)
        {
            Dictionary <string, int> settings = new Dictionary <string, int>()
            {
                { "on", 0 }
            };

            return(SetState(nanoleaf, settings, 1000));
        }
        public Dictionary <string, string> StartExistingEffect(Nanoleaf nanoleaf, string effect_name)
        {
            dynamic jsonObject = new JObject();

            jsonObject.select = effect_name;
            string url_ex = "/effects";

            return(Put(nanoleaf, url_ex, jsonObject.ToString(Formatting.None)));
        }
        public Dictionary <string, string> Put(Nanoleaf nanoleaf, string url_ex, string json)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            WebRequest request = WebRequest.Create(nanoleaf.url + url_ex);

            request.ContentType = "application/json";
            request.Method      = "PUT";

            MemoryStream jsonContent = new MemoryStream(Encoding.ASCII.GetBytes(json));

            using (var content = request.GetRequestStream())
            {
                jsonContent.Seek(0, SeekOrigin.Begin);
                jsonContent.WriteTo(content);
            }

            Dictionary <string, string> put = new Dictionary <string, string>();

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    WebHeaderCollection header = response.Headers;

                    var    encoding = ASCIIEncoding.ASCII;
                    string body     = "";
                    using (var reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        body = reader.ReadToEnd();
                    }

                    put.Add("body", body);
                }
                else if (response.StatusCode != HttpStatusCode.NoContent)
                {
                    throw new Exception();
                }

                put.Add("json", json);
            }

            watch.Stop();
            long ms = watch.ElapsedMilliseconds;

            put.Add("ms", ms.ToString());
            return(put);
        }
        public Dictionary <string, string> SetState(Nanoleaf nanoleaf, Dictionary <string, int> settings, int delay)
        {
            dynamic jsonObject = new JObject();

            foreach (KeyValuePair <string, int> pair in settings)
            {
                switch (pair.Key)
                {
                case "on":
                    jsonObject.on = pair.Value == 1;
                    break;

                case "hue":
                    jsonObject.hue = pair.Value;
                    break;

                case "sat":
                    jsonObject.sat = pair.Value;
                    break;

                case "saturation":
                    jsonObject.sat = pair.Value;
                    break;

                case "brightness":
                    jsonObject.brightness = pair.Value;
                    break;

                case "ct":
                    jsonObject.ct = pair.Value;
                    break;

                case "probability":
                    jsonObject.duration = pair.Value;
                    break;

                default:
                    jsonObject.on = true;
                    break;
                }
            }

            string url_ex = "/state";

            return(Put(nanoleaf, url_ex, jsonObject.ToString(Newtonsoft.Json.Formatting.None)));
        }
        public Nanoleaf SelectCorrectLight(Nanoleaf nanoleaf)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            WebRequest request = WebRequest.Create(nanoleaf.url + "/new");

            request.ContentType = "application/json";
            request.Method      = "POST";

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        Log.Error("Sync response failed");
                        return(null);
                    }
                    else
                    {
                        WebHeaderCollection header = response.Headers;

                        var    encoding = ASCIIEncoding.ASCII;
                        string body     = "";
                        using (var reader = new StreamReader(response.GetResponseStream(), encoding))
                        {
                            body = reader.ReadToEnd();
                            nanoleaf.auth_token = JObject.Parse(body)["auth_token"].ToString();
                            return(nanoleaf);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
                return(null);
            }
        }
        public Dictionary <string, string> Get(Nanoleaf nanoleaf, string url_ex)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            WebRequest request = WebRequest.Create(nanoleaf.url + url_ex);

            request.ContentType = "application/json";
            request.Method      = "GET";

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception();
                }
                else
                {
                    WebHeaderCollection header = response.Headers;

                    var    encoding = ASCIIEncoding.ASCII;
                    string body     = "";
                    using (var reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        body = reader.ReadToEnd();
                    }

                    watch.Stop();
                    long ms = watch.ElapsedMilliseconds;
                    Dictionary <string, string> get = new Dictionary <string, string>()
                    {
                        { "body", body },
                        { "ms", ms.ToString() }
                    };
                    return(get);
                }
            }
        }
Example #16
0
        public void Screenshot(Nanoleaf nanoleaf)
        {
            List <LCColor>   palette      = null;
            Bitmap           bitmap       = null;
            string           body         = lightController.Get(nanoleaf, "/panelLayout/layout")["body"];
            List <Panel>     panels       = Panel.GetPanels(body);
            int              paletteCount = panels.Count;
            Tuple <int, int> gcd          = ImageAnalysis.DivideScreen(paletteCount);

            palette = new List <LCColor>();
            bitmap  = ImageAnalysis.CaptureBitmap(0);

            if (bitmap == null)
            {
                palette.Add(new LCColor(Color.Black));
            }
            else
            {
                //palette = ImageAnalysis.ReduceColors(ImageAnalysis.GetAverageColors(bitmap, gcd.Item1, gcd.Item2), true, paletteCount);
                palette = ImageAnalysis.GetAverageColors(bitmap, gcd.Item1, gcd.Item2, borderReduction: 300);
            }

            lightController.CreateCustom(nanoleaf, PaletteController.ToJArray(palette));
        }
        public Tuple <string, string> CreateCustom(Nanoleaf nanoleaf, JArray palette, int transMin = 5, int transMax = 5, string animType = "flow")
        {
            dynamic write = new JObject();

            write.command   = "display";
            write.animType  = animType;
            write.colorType = "HSB";
            write.animData  = null;
            write.loop      = true;
            write.palette   = palette;
            dynamic transTime = new JObject();

            transTime.maxValue = transMax;
            transTime.minValue = transMin;
            write.transTime    = transTime;

            dynamic jsonObject = new JObject();

            jsonObject.write = write;

            string url_ex = "/effects";

            return(PutAndForget(nanoleaf, url_ex, jsonObject.ToString(Formatting.None)));
        }
Example #18
0
 public void MimickPosition(Nanoleaf nanoleaf)
 {
 }
Example #19
0
 public void FrameMode(Nanoleaf nanoleaf)
 {
     string       body         = lightController.Get(nanoleaf, "/panelLayout/layout")["body"];
     List <Panel> panels       = Panel.GetPanels(body);
     int          paletteCount = panels.Count;
 }
Example #20
0
        public void StreamingMode(Nanoleaf nanoleaf)
        {
            //UdpClient udp = new UdpClient();
            ////udp.EnableBroadcast = true;  //This was suggested in a now deleted answer
            //IPEndPoint groupEP = new IPEndPoint(ConvertIP("192.168.1.6"), 60221);
            //string str4 = "2 55 1 255 0 0 0 1 10 1 0 255 0 0 1 ";
            //byte[] sendBytes4 = Encoding.ASCII.GetBytes(str4);
            //udp.Send(sendBytes4, sendBytes4.Length, groupEP);
            //udp.Close();

            //get panel layout
            bool         first  = true;
            string       body   = lightController.Get(nanoleaf, "/panelLayout/layout")["body"];
            List <Panel> panels = Panel.GetPanels(body);

            List <LCColor> palette = null;
            Stopwatch      timer   = null;
            Bitmap         bitmap  = null;

            int paletteCount     = panels.Count;
            Tuple <int, int> gcd = ImageAnalysis.DivideScreen(paletteCount);

            while (true)
            {
                timer = new Stopwatch();
                timer.Start();

                palette = new List <LCColor>();
                bitmap  = ImageAnalysis.CaptureBitmap(0);

                if (bitmap == null)
                {
                    palette.Add(new LCColor(Color.Black));
                }
                else
                {
                    //palette = ImageAnalysis.ReduceColors(ImageAnalysis.GetAverageColors(bitmap, gcd.Item1, gcd.Item2), true, paletteCount);
                    palette = ImageAnalysis.GetAverageColors(bitmap, gcd.Item1, gcd.Item2, borderReduction: 300);
                }

                if (timer.ElapsedMilliseconds < 100)
                {
                    if (100 - timer.ElapsedMilliseconds > 0)
                    {
                        Thread.Sleep(100 - Convert.ToInt32(timer.ElapsedMilliseconds));
                    }
                }

                //TODO: change the position of some of the panels on shuffle, not all
                bool shuffle = true;
                if (shuffle && times.Count % 100 == 0)
                {
                    panels = Panel.Shuffle(panels);
                }

                lightController.CreateTemp(nanoleaf, panels, palette);

                timer.Stop();
                long ms = timer.ElapsedMilliseconds;

                if (!first)
                {
                    CalcAverage(ms);
                }
                first = false;
            }
        }
        public Dictionary <string, string> RandomStatic(Nanoleaf nanoleaf)
        {
            Dictionary <string, int> settings = PaletteController.Random().HSBtoDict();

            return(SetState(nanoleaf, settings, 1000));
        }
 public void FireAndForget(Nanoleaf nanoleaf, Delegate d, params object[] args)
 {
     ThreadPool.QueueUserWorkItem(callback => PutAndForget(nanoleaf, args[0].ToString(), args[1].ToString()));
 }