-
-
Notifications
You must be signed in to change notification settings - Fork 8
Individual LED Control #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7deed9b
7220b68
987a90a
78d9770
1407723
13bac75
017d0b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Kevsoft.WLED; | ||
|
||
public class MatrixResponse | ||
{ | ||
/// <summary> The number of LEDs in the width of the matrix </summary> | ||
[JsonPropertyName("w")] | ||
public int Width { get; set; } | ||
|
||
/// <summary> The number of LEDs in the Height of the matrix </summary> | ||
[JsonPropertyName("h")] | ||
public int Height { get; set; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Kevsoft.WLED; | ||
|
||
public sealed class SingleLedRequest | ||
{ | ||
/// <summary> The position of the LED in the segment </summary> | ||
public int LedPosition { get; set; } | ||
|
||
/// <summary> The color of the LED as HEX (e.g. FF0000 for red) </summary> | ||
public string Color { get; set; } = string.Empty; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ public async Task<string[]> GetPalettes() | |
var message = await _client.GetAsync("json/pal"); | ||
|
||
message.EnsureSuccessStatusCode(); | ||
|
||
return (await message.Content.ReadFromJsonAsync<string[]>())!; | ||
} | ||
|
||
|
@@ -73,7 +73,7 @@ public async Task Post(WLedRootRequest request) | |
var result = await _client.PostAsync("/json", content); | ||
result.EnsureSuccessStatusCode(); | ||
} | ||
|
||
public async Task Post(StateRequest request) | ||
{ | ||
var stateString = JsonSerializer.Serialize(request); | ||
|
@@ -82,4 +82,74 @@ public async Task Post(StateRequest request) | |
var result = await _client.PostAsync("/json/state", content); | ||
result.EnsureSuccessStatusCode(); | ||
} | ||
} | ||
|
||
public async Task Post(List<SingleLedRequest> ledList) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do with some tests around this Post method to prove out all the logic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll Look into this. Something in this repository keeps tripping the "warning as an error" thing for venerable nugets on my computer, even though im not seeing that flag on the projects, so may need to nudge a few of those along the way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think i got a test added now. |
||
{ | ||
// Eliminate duplicate positions | ||
ledList = ledList.GroupBy(x => x.LedPosition).Select(x => x.Last()).ToList(); | ||
|
||
List<object> list = []; | ||
int counter = 0; | ||
|
||
//Attempt to group colors together to reduce the number of packets sent as there is a 256 color at a time limit | ||
foreach (IGrouping<string, SingleLedRequest>? leds in ledList.GroupBy(x => x.Color)) | ||
{ | ||
if (counter >=255) | ||
{ | ||
await Post(new StateRequest { On = true, Segments = [new() { Id = 0, IndividualLedControl = [.. list] }] }); | ||
list = []; | ||
counter = 0; | ||
} | ||
// If there is only one LED in the group, add it to the list | ||
if (leds.Count() == 1) | ||
{ | ||
list.Add(leds.First().LedPosition); | ||
list.Add(leds.First().Color); | ||
counter++; | ||
continue; | ||
} | ||
|
||
// If there are multiple LEDs in the group, find the sequential LED's and group them up | ||
// to make the next step easier | ||
List<List<int>> grouped = leds.Select(x => x.LedPosition).OrderBy(x => x) | ||
.Aggregate(new List<List<int>> { new() }, | ||
(acc, curr) => | ||
{ | ||
if (!acc.Last().Any() || curr - acc.Last().Last() == 1) | ||
acc.Last().Add(curr); | ||
else | ||
acc.Add([curr]); | ||
return acc; | ||
}); | ||
|
||
foreach (List<int> group in grouped) | ||
{ | ||
//Another round of sending the colors if we are at the limit | ||
if (counter >= 255) | ||
{ | ||
await Post(new StateRequest { On = true, Segments = [new() { Id = 0, IndividualLedControl = [.. list] }] }); | ||
list = []; | ||
counter = 0; | ||
} | ||
|
||
// If there is only one LED in the group, add it to the list | ||
if (group.Count == 1) | ||
{ | ||
list.Add(group.First()); | ||
list.Add(leds.First().Color); | ||
counter++; | ||
continue; | ||
} | ||
|
||
//And if there are multiple LED's, Add them to the list, but when displaying max | ||
//is not displayed so add 1 to the max to get it to display properly | ||
list.Add(group.Min()); | ||
list.Add(group.Max() + 1); | ||
list.Add(leds.First().Color); | ||
counter++; | ||
} | ||
} | ||
//And finally send the last packet | ||
await Post(new StateRequest { On = true, Segments = [new() { Id = 0, IndividualLedControl = [.. list] }] }); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this have a type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple different things that can be in this list. Ends up being a mix of ints and strings, but there's not a pattern to how they are ordered - and Wled didn't respond to them all sent as strings im my testing. For what i set up - I'm ignoring the RGB option and going with just hex, but the hex string could be a list of 3 ints
just the Hex Values,
coordinate followed by hex color,
2 ints for start and end of a range followed by the hex color.