public void ApplyInput() { // If the ID changed the image layouts must change as well if (!LedLayout.Id.Equals(InputId)) { foreach (var imageLayout in Model.DeviceLayout.LedImageLayouts) { foreach (var ledImage in imageLayout.LedImages.Where(l => l.Id.Equals(LedLayout.Id))) { ledImage.Id = InputId; } } } LedLayout.Id = InputId; LedLayout.DescriptiveX = InputX; LedLayout.DescriptiveY = InputY; LedLayout.DescriptiveWidth = InputWidth; LedLayout.DescriptiveHeight = InputHeight; // Apply custom shape data if (InputShape == Shape.Custom) { LedLayout.DescriptiveShape = InputShapeData; } else { LedLayout.DescriptiveShape = InputShape.ToString(); } // If LED image exists, update it if (_ledImage != null) { _ledImage.Image = InputImage; NotifyOfPropertyChange(() => LedImagePath); } // Create a new LED image and add it to the layout else { var ledImage = new LedImage { Id = LedLayout.Id, Image = InputImage }; // Find the current layout var layout = Model.DeviceLayout.LedImageLayouts.FirstOrDefault(l => l.Layout != null && l.Layout.Equals(_layoutViewModel.EditorViewModel.SelectedImageLayout)); // If missing, create it if (layout == null) { layout = new LedImageLayout { Layout = _layoutViewModel.EditorViewModel.SelectedImageLayout }; Model.DeviceLayout.LedImageLayouts.Add(layout); } layout.LedImages.Add(ledImage); UpdateLedImage(ledImage); } _layoutViewModel.UpdateLeds(); }
protected LedEffect(uint ledCount, string name) { this.Name = name; this.LedCount = ledCount; Rndm = new Random(); LastRenderTime = DateTime.UtcNow.AddDays(-1); Image = new LedImage(ledCount); FirstFrame = true; }
public static byte[] ToByteArray(this LedImage image) { if (image == null) { throw new ArgumentNullException(nameof(image)); } var result = new byte[image.Leds.Count * 3]; for (int i = 0; i < image.Leds.Count; i++) { result[i * 3 + 0] = image.Leds[i].G; result[i * 3 + 1] = image.Leds[i].R; result[i * 3 + 2] = image.Leds[i].B; } return(result); }
/// <summary> /// Applies the given layout. /// </summary> /// <param name="layoutPath">The file containing the layout.</param> /// <param name="imageLayout">The name of the layout used to get the images of the leds.</param> /// <param name="imageBasePath">The path images for this device are collected in.</param> protected void ApplyLayoutFromFile(string layoutPath, string imageLayout, string imageBasePath) { DeviceLayout layout = DeviceLayout.Load(layoutPath); if (layout != null) { LedImageLayout ledImageLayout = layout.LedImageLayouts.FirstOrDefault(x => string.Equals(x.Layout, imageLayout, StringComparison.OrdinalIgnoreCase)); InternalSize = new Size(layout.Width, layout.Height); if (layout.Leds != null) { foreach (LedLayout layoutLed in layout.Leds) { if (Enum.TryParse(layoutLed.Id, true, out LogitechLedIds ledId)) { LogitechLedId id = new LogitechLedId(this, ledId); if (!LedMapping.TryGetValue(id, out Led led)) { led = InitializeLed(id, new Rectangle()); } led.LedRectangle.Location.X = layoutLed.X; led.LedRectangle.Location.Y = layoutLed.Y; led.LedRectangle.Size.Width = layoutLed.Width; led.LedRectangle.Size.Height = layoutLed.Height; led.Shape = layoutLed.Shape; led.ShapeData = layoutLed.ShapeData; LedImage image = ledImageLayout?.LedImages.FirstOrDefault(x => x.Id == layoutLed.Id); led.Image = (!string.IsNullOrEmpty(image?.Image)) ? new Uri(Path.Combine(imageBasePath, image.Image), UriKind.Absolute) : new Uri(Path.Combine(imageBasePath, "Missing.png"), UriKind.Absolute); } } } } }
public void UpdateLedImage(LedImage ledImage) { _ledImage = ledImage; InputImage = _ledImage?.Image; NotifyOfPropertyChange(() => LedImagePath); }
public override byte[] Render() { if (TimePassed((int)_interval * 1000)) { var httpClient = new HttpClient(); HttpResponseMessage res; try { var httpTask = httpClient.GetAsync(_apiUrl); httpTask.Wait(); res = httpTask.Result; httpClient.Dispose(); } catch (Exception) { httpClient.Dispose(); Image.SetAll(20, 0, 0); return(Image.ToByteArray()); } if (res.IsSuccessStatusCode) { try { var readTask = res.Content.ReadAsStringAsync(); readTask.Wait(); var jsonString = readTask.Result; var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; var ledImageDto = JsonSerializer.Deserialize <LedImageDto>(jsonString, options); var ledImage = LedImage.FromDto(ledImageDto); if (ledImage.TransitionTime != 0) { if (_ledImageTo == null) { _ledImageTo = ledImage; for (int i = 0; i < ledImage.Leds.Count; i++) { Image.Leds[i] = ledImage.Leds[i]; } } else { if (!_ledImageTo.Equals(ledImage)) { _ledImageFrom = _ledImageTo; _ledImageTo = ledImage; _activeTransition = true; _transitionProgress = 0.0f; } } } else { for (int i = 0; i < ledImage.Leds.Count; i++) { Image.Leds[i] = ledImage.Leds[i]; } } } catch (Exception ex) { Logger.LogError("Error during rendering WebEffect", ex); } } else { Image.SetAll(20, 0, 0); } if (!_activeTransition) { return(Image.ToByteArray()); } } if (_activeTransition) { _transitionProgress += (float)5 / _ledImageTo.TransitionTime; if (_transitionProgress > 1.0f) { _transitionProgress = 1.0f; _activeTransition = false; } for (int i = 0; i < LedCount; i++) { var oldC = _ledImageFrom.Leds[i]; var newC = _ledImageTo.Leds[i]; var resC = RgbColor.Transition(oldC, newC, _transitionProgress); Image.Leds[i] = resC; } return(Image.ToByteArray()); } return(null); }