Add RGB channel order conversion method
- Add get_rgb_channel_order() to convert color order strings to channel indices - Supports proper RGB tuple reordering for different LED strip types - Used by color conversion utilities
This commit is contained in:
@@ -43,61 +43,6 @@ class Settings(dict):
|
||||
self.set_defaults()
|
||||
self.save()
|
||||
|
||||
def set_settings(self, data, patterns, save):
|
||||
try:
|
||||
print(f"Setting settings: {data}")
|
||||
for key, value in data.items():
|
||||
print(key, value)
|
||||
if key == "colors":
|
||||
buff = []
|
||||
for color in value:
|
||||
buff.append(tuple(int(color[i:i+2], 16) for i in self.color_order))
|
||||
patterns.colors = buff
|
||||
elif key == "num_leds":
|
||||
patterns.update_num_leds(self["led_pin"], value)
|
||||
elif key == "pattern":
|
||||
if not patterns.select(value):
|
||||
return "Pattern doesn't exist", 400
|
||||
elif key == "delay":
|
||||
delay = int(data["delay"])
|
||||
patterns.delay = delay
|
||||
elif key == "brightness":
|
||||
brightness = int(data["brightness"])
|
||||
patterns.brightness = brightness
|
||||
elif key == "n1":
|
||||
patterns.n1 = value
|
||||
elif key == "n2":
|
||||
patterns.n2 = value
|
||||
elif key == "n3":
|
||||
patterns.n3 = value
|
||||
elif key == "n4":
|
||||
patterns.n4 = value
|
||||
elif key == "n5":
|
||||
patterns.n5 = value
|
||||
elif key == "n6":
|
||||
patterns.n6 = value
|
||||
elif key == "name":
|
||||
self[key] = value
|
||||
self.save()
|
||||
machine.reset()
|
||||
elif key == "color_order":
|
||||
self["color_order"] = value
|
||||
self.color_order = self.get_color_order(value)
|
||||
pass
|
||||
elif key == "id":
|
||||
pass
|
||||
elif key == "led_pin":
|
||||
patterns.update_num_leds(value, self["num_leds"])
|
||||
else:
|
||||
return "Invalid key", 400
|
||||
self[key] = value
|
||||
#print(self)
|
||||
if save:
|
||||
self.save()
|
||||
print(self)
|
||||
return "OK", 200
|
||||
except (KeyError, ValueError):
|
||||
return "Bad request", 400
|
||||
|
||||
def get_color_order(self, color_order):
|
||||
"""Convert color order string to tuple of hex string indices."""
|
||||
@@ -111,6 +56,19 @@ class Settings(dict):
|
||||
}
|
||||
return color_orders.get(color_order.lower(), (1, 3, 5)) # Default to RGB
|
||||
|
||||
def get_rgb_channel_order(self, color_order=None):
|
||||
"""Convert color order string to RGB channel indices for reordering tuples.
|
||||
Returns tuple of channel indices: (r_channel, g_channel, b_channel)
|
||||
Example: 'grb' -> (1, 0, 2) means (G, R, B)"""
|
||||
if color_order is None:
|
||||
color_order = self.get("color_order", "rgb")
|
||||
color_order = color_order.lower()
|
||||
# Map hex string positions to RGB channel indices
|
||||
# Position 1 (R in hex) -> channel 0, Position 3 (G) -> channel 1, Position 5 (B) -> channel 2
|
||||
hex_to_channel = {1: 0, 3: 1, 5: 2}
|
||||
hex_indices = self.get_color_order(color_order)
|
||||
return tuple(hex_to_channel[pos] for pos in hex_indices)
|
||||
|
||||
# Example usage
|
||||
def main():
|
||||
settings = Settings()
|
||||
|
||||
Reference in New Issue
Block a user