diff --git a/knightrider_multistrip.py b/knightrider_multistrip.py index f15d6d9..2b7d89d 100755 --- a/knightrider_multistrip.py +++ b/knightrider_multistrip.py @@ -2,12 +2,13 @@ import sk6812_multistrip as sk6812 import time +import sys num_modules = 16 interval = 1/60 -w = sk6812.SK6812("192.168.42.1", 2703) +w = sk6812.SK6812(sys.argv[1], 2703) w.set_fadestep(0.20/interval) #w.set_num_modules(num_modules) @@ -29,13 +30,17 @@ while True: set_colors[int(curModule[0])] = [0, 0, 0, 0] set_colors[int(curModule[1])] = [0, 0, 0, 0] - set_colors[num_modules-1-int(curModule[2])] = [0, 0, 0, 0] - set_colors[num_modules-1-int(curModule[3])] = [0, 0, 0, 0] + #set_colors[num_modules-1-int(curModule[1])] = [0, 0, 0, 0] + set_colors[int(curModule[2])] = [0, 0, 0, 0] + set_colors[int(curModule[3])] = [0, 0, 0, 0] + #set_colors[num_modules-1-int(curModule[3])] = [0, 0, 0, 0] set_colors[int(curModule[0])][0] = brightness set_colors[int(curModule[1])][1] = brightness - set_colors[num_modules-1-int(curModule[2])][2] = brightness - set_colors[num_modules-1-int(curModule[3])][3] = brightness + #set_colors[num_modules-1-int(curModule[1])][1] = brightness + set_colors[int(curModule[2])][2] = brightness + set_colors[int(curModule[3])][3] = brightness + #set_colors[num_modules-1-int(curModule[3])][3] = brightness for strip in range(8): for k in set_colors.keys(): diff --git a/rgbw_hsv_flame.py b/rgbw_hsv_flame.py index 0f50fec..5ecf048 100755 --- a/rgbw_hsv_flame.py +++ b/rgbw_hsv_flame.py @@ -10,7 +10,6 @@ from hsv2rgbw import hsv2rgbw import numpy as np s = sk6812.SK6812(sys.argv[1], 2703) -s.set_fadestep(10) phase = 0 nled = 16 @@ -71,6 +70,7 @@ loop = 0 intensity = MAXENERGY_ADD while True: + s.set_fadestep(10) if loop % 5 == 0: intensity = random.randint(MAXENERGY_ADD*3//4, MAXENERGY_ADD*5//4) diff --git a/rgbw_prime_sinus.py b/rgbw_prime_sinus.py index 79bf93f..fa0ffdc 100755 --- a/rgbw_prime_sinus.py +++ b/rgbw_prime_sinus.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import sk6812_multistrip as sk6812 import time @@ -19,21 +19,37 @@ strip = 0 tiltphase = 0 +seq = 0 +rxseq = 0 +lasttxtime = 0 + while True: - for i in range(nled): - for strip in range(nstrip): - x = 2*math.pi * i / nled - y = 2*math.pi * strip * 2*math.sin(tiltphase) / nstrip - r = scale * (127 + 127 * math.sin(y + x + 13*phase + 0 * math.pi/2)) - g = scale * (127 + 127 * math.sin(y + x + 23*phase + 1 * math.pi/2)) - b = scale * (127 + 127 * math.sin(y + x + 42*phase + 2 * math.pi/2)) - w = scale * 0.6 * (127 + 127 * math.sin(y + x + 5*phase + 3 * math.pi/2)) - s.set_color((strip+5)%nstrip, i, r, g, b, w) + rxseq = s.try_read_seq() + if rxseq != -1: + print(f"TX: {seq}, RX: {rxseq}") - s.commit() + now = time.time() - phase += 2*math.pi * interval / 41 - tiltphase += 2*math.pi * interval / 11 + if rxseq >= seq - 3 or (lasttxtime < now - 0.5): + lasttxtime = now + + for i in range(nled): + for strip in range(nstrip): + x = 2*math.pi * i / nled + y = 2*math.pi * strip * 2*math.sin(tiltphase) / nstrip + r = scale * (127 + 127 * math.sin(y + x + 283*phase + 0 * math.pi/2)) + g = scale * (127 + 127 * math.sin(y + x + 293*phase + 1 * math.pi/2)) + b = scale * (127 + 127 * math.sin(y + x + 307*phase + 2 * math.pi/2)) + w = scale * 0.6 * (127 + 127 * math.sin(y + x + 311*phase + 3 * math.pi/2)) + + s.set_color((strip+0)%nstrip, i, r, g, b, w) + + s.ack_request(seq) + seq += 1 + s.commit() + + phase += 2*math.pi * interval / (739*1) + tiltphase += 2*math.pi * interval / (471/2) time.sleep(interval) diff --git a/rgbw_sinus.py b/rgbw_sinus.py index 446efec..b0d4495 100755 --- a/rgbw_sinus.py +++ b/rgbw_sinus.py @@ -3,8 +3,9 @@ import sk6812_multistrip as sk6812 import time import math +import sys -s = sk6812.SK6812("192.168.42.1", 2703) +s = sk6812.SK6812(sys.argv[1], 2703) phase = 0 nled = 16 diff --git a/rundumleuchte.py b/rundumleuchte.py index ebab493..bc8dc7b 100755 --- a/rundumleuchte.py +++ b/rundumleuchte.py @@ -3,22 +3,23 @@ import sk6812_multistrip as sk6812 import time import math +import sys num_modules = 16 num_strips = 8 -interval = 1/30 +interval = 1/20 -w = sk6812.SK6812("10.42.7.145", 2703) -w.set_fadestep(0.80/interval) +w = sk6812.SK6812(sys.argv[1], 2703) -color = [255, 0, 255, 0] +color = [0x20, 0x40, 0x00, 0x10] cur_strip = 0 while True: + w.set_fadestep(int(0.80/interval)) for k in range(num_modules): - f = math.sin(math.pi * k / num_modules) + f = 1#math.sin(math.pi * k / num_modules) w.add_color(cur_strip, k, f*color[0], f*color[1], f*color[2], f*color[3]) for strip in range(8): diff --git a/sk6812_multistrip.py b/sk6812_multistrip.py index ea4ab69..ed7217f 100755 --- a/sk6812_multistrip.py +++ b/sk6812_multistrip.py @@ -12,6 +12,7 @@ class SK6812Command: FADE_COLOR = 1 ADD_COLOR = 2 SET_FADESTEP = 3 + ACK_REQUEST = 255 def __init__(self, action = SET_COLOR, strip = 0, module = 0, d0 = 0, d1 = 0, d2 = 0, d3 = 0): self.action = int(action) @@ -33,6 +34,7 @@ class SK6812: family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(host, port, 0, socket.SOCK_DGRAM)[0] self.__socket = socket.socket(family, socktype, proto) + self.__socket.settimeout(0.0) # nonblocking mode self.__socket.connect(sockaddr) def commit(self): @@ -62,6 +64,21 @@ class SK6812: # add a "add to color" command self.__commands.append(SK6812Command(SK6812Command.ADD_COLOR, strip, module, r, g, b, w)) + def ack_request(self, seq): + # add a "request for acknowledgement" command + self.__commands.append(SK6812Command(SK6812Command.ACK_REQUEST, 0, 0, (seq >> 8) & 0xFF, seq & 0xFF, 0, 0)) + + def try_read_seq(self): + try: + data = self.__socket.recv(2) + except socket.error: + return -1 + + if not data or len(data) != 2: + return -1 + else: + return struct.unpack(">H", data)[0] + if __name__ == "__main__": w = SK6812("192.168.2.222", 2703) w.set_fadestep(1);