Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import atexit
- from struct import Struct
- from socket import socket, AF_INET, SOCK_DGRAM
- from itertools import chain
- WLED_ADDR = ("192.168.0.215", 21324)
- sock = socket(AF_INET, SOCK_DGRAM)
- atexit.register(sock.close)
- def set_leds(colors, addr=WLED_ADDR):
- if len(colors) > 490:
- raise ValueError("Too many LEDs")
- leds = chain.from_iterable(colors)
- data_len = len(colors) * 3
- proto = 2
- delay = 10
- # Protokoll 2 == RGB
- # Protokoll, Timeout, [Rn,Gn,Bn], ...
- st = Struct(f"!BB{data_len}B")
- sock.sendto(st.pack(proto, delay, *leds), WLED_ADDR)
- def set_level(value, /, min_value=0, max_value=140, leds=72):
- relative = (value - min_value) / (max_value - min_value)
- on_count = round(relative * leds)
- off_count = leds - on_count
- r = min(255, max(0, int(relative * 255)))
- g = 0
- b = 255 - r
- color = (r , g , b)
- colors = [color] * on_count + [(0, 0, 0)] * off_count
- set_leds(colors)
- if __name__ == "__main__":
- try:
- while True:
- set_level(float(input("Value: ")))
- except KeyboardInterrupt:
- pass
- except ValueError:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement