forked from pvx/litsimaja
parent
2ffdf0c0af
commit
84e2837f4b
7 changed files with 257 additions and 11 deletions
@ -0,0 +1,5 @@ |
||||
.idea/ |
||||
.git/ |
||||
__pycache__/ |
||||
*.pyc |
||||
*.swp |
@ -0,0 +1,52 @@ |
||||
Color = lambda r, g, b: (r << 16) | (g << 8) | b |
||||
|
||||
|
||||
def Color_to_list(color): |
||||
rgb = [(color >> 16) & 255, (color >> 8) & 255, color & 255] |
||||
w = (color >> 24) & 255 |
||||
if w: |
||||
rgb.append(w) |
||||
return rgb |
||||
|
||||
|
||||
def Color_to_hex(color): |
||||
s = [(color >> 16) & 255, (color >> 8) & 255, color & 255] |
||||
return "#"+''.join(map(lambda y: ("0" + hex(int(y))[2:])[-2:], s)) |
||||
|
||||
|
||||
def Color_to_rgb(color): |
||||
return (color >> 16) & 255, (color >> 8) & 255, color & 255 |
||||
|
||||
|
||||
def hex_to_rgb(value): |
||||
value = value.lstrip('#') if value[0] == "#" else value |
||||
lv = len(value) |
||||
return [int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)] |
||||
|
||||
|
||||
def to_Color(value): |
||||
if type(value) == str: |
||||
r, g, b = hex_to_rgb(value) |
||||
return Color(r, g, b) |
||||
if type(value) == int: |
||||
if 0 <= value <= 16777215: |
||||
return value |
||||
else: |
||||
raise ValueError("RGB Int exceeded the lower and upper limits") |
||||
if type(value) == list or type(value) == tuple: |
||||
out = [] |
||||
for n, code in enumerate(value): |
||||
if not str(code).isnumeric(): |
||||
raise ValueError("RGB color must be numeric") |
||||
if n > 4: |
||||
raise ValueError("RGB contains more than 4 values") |
||||
_code = int(code) |
||||
if _code < 0: |
||||
print(f"RGB value under 0") |
||||
out.append(0) |
||||
elif _code > 255: |
||||
print(f"RGB value over 255") |
||||
out.append(255) |
||||
else: |
||||
out.append(_code) |
||||
return Color(out[0], out[1], out[2]) |
@ -0,0 +1,73 @@ |
||||
from threading import Thread |
||||
from tkinter import Tk, Label |
||||
from .FakeStrip import FakeStrip |
||||
from lib.Color import Color_to_list |
||||
from time import sleep |
||||
import numpy as np |
||||
from PIL import Image, ImageTk |
||||
|
||||
|
||||
class Visualizer(Thread): |
||||
|
||||
def __init__(self, x, y, multi): |
||||
Thread.__init__(self) |
||||
self.x, self.y = x, y |
||||
self.multi = multi |
||||
self.root = None |
||||
self.panel = None |
||||
self.first_loop = True |
||||
self.start() |
||||
sleep(0.04) |
||||
|
||||
def run(self): |
||||
self.root = Tk() |
||||
self.root.title("RGB Visualizer") |
||||
self.panel = Label(self.root) |
||||
self.root.geometry(f'{int(self.x * self.multi)}x{int(self.y * self.multi)}') |
||||
self.panel.pack() |
||||
self.root.mainloop() |
||||
|
||||
def print_image(self, frame): |
||||
self.panel.configure(image=frame) |
||||
self.panel.frame = frame |
||||
|
||||
|
||||
class TkinterStrip(FakeStrip): |
||||
|
||||
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False, |
||||
brightness=255, channel=0, strip_type=None, gamma=None): |
||||
super().__init__(num, pin, freq_hz, dma, invert, brightness, channel, strip_type, gamma) |
||||
self.viz = Visualizer(95, 50, 6) |
||||
# self.ms = time() * 1000.0 |
||||
self.show() |
||||
|
||||
def show(self): |
||||
"""Update the display with the data from the LED buffer.""" |
||||
bp = [49, 99, 194, 244, 290] |
||||
x = self.viz.x |
||||
y = self.viz.y |
||||
|
||||
data = np.zeros((y, x, 3), dtype=np.uint8) |
||||
|
||||
for s in range(self.numPixels()): |
||||
x1 = y1 = 0 |
||||
if s < 49: |
||||
x1 = x / 2 + s - 1 |
||||
elif s < 99: |
||||
x1 = x - 1 |
||||
y1 = (s - 49) |
||||
elif s < 194: |
||||
x1 = x + (99 - s) - 1 |
||||
y1 = y - 1 |
||||
elif s < 244: |
||||
y1 = y + (194 - s) - 1 |
||||
elif s < 290: |
||||
x1 = s - 244 |
||||
x1 = int(x1) |
||||
y1 = int(y1) |
||||
data[y1, x1] = Color_to_list(self.getPixelColor(s)) |
||||
img = Image.fromarray(data, 'RGB').resize((int(self.viz.x * self.viz.multi), int(self.viz.y * self.viz.multi))) |
||||
self.viz.print_image(ImageTk.PhotoImage(img)) |
||||
# ms = time() * 1000.0 |
||||
# print(ms - self.ms) |
||||
# self.ms = ms |
@ -0,0 +1,79 @@ |
||||
from threading import Thread |
||||
from tkinter import Tk, Canvas |
||||
from lib.strip.FakeStrip import FakeStrip |
||||
import time |
||||
|
||||
|
||||
class Visualizer(Thread): |
||||
|
||||
def __init__(self): |
||||
Thread.__init__(self) |
||||
self.x, self.y = 1000, 500 |
||||
self.first_loop = True |
||||
self.start() |
||||
time.sleep(0.01) |
||||
|
||||
def run(self): |
||||
self.root = Tk() |
||||
self.canvas = Canvas(self.root, width=self.x, height=self.y) |
||||
self.root.geometry(f'{self.x}x{self.y}') |
||||
self.canvas.grid() |
||||
self.root.mainloop() |
||||
|
||||
|
||||
class WindowStrip(FakeStrip): |
||||
|
||||
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False, |
||||
brightness=255, channel=0, strip_type=None, gamma=None): |
||||
super().__init__(num, pin, freq_hz, dma, invert, brightness, channel, strip_type, gamma) |
||||
self.viz = Visualizer() |
||||
self.show() |
||||
|
||||
def show(self): |
||||
"""Update the display with the data from the LED buffer.""" |
||||
size = 10.5 |
||||
bp = [47, 97, 191, 242, 289] |
||||
x = self.viz.x |
||||
y = self.viz.y |
||||
first_loop = self.viz.first_loop |
||||
|
||||
for s in range(self.numPixels()): |
||||
rgb_int = self.getPixelColor(s) |
||||
r = rgb_int & 255 |
||||
g = (rgb_int >> 8) & 255 |
||||
b = (rgb_int >> 16) & 255 |
||||
|
||||
if not first_loop: |
||||
self.viz.canvas.itemconfig(s + 1, fill="#%02x%02x%02x" % (r, g, b)) |
||||
else: |
||||
x1 = 0 |
||||
y1 = 0 |
||||
x2 = 0 |
||||
y2 = 0 |
||||
if s < bp[0]: |
||||
x1 = (x / 2) + s * size |
||||
y1 = 0 |
||||
x2 = x1 + size |
||||
y2 = size |
||||
elif s < bp[1]: |
||||
x1 = x - size |
||||
y1 = -(bp[0] - s) * size |
||||
x2 = x1 + size |
||||
y2 = y1 + size |
||||
elif s < bp[2]: |
||||
x1 = x + (bp[1] - s) * size |
||||
y1 = y |
||||
x2 = x1 - size |
||||
y2 = y1 - size |
||||
elif s < bp[3]: |
||||
x1 = 0 |
||||
y1 = y + (bp[2] - s) * size |
||||
x2 = x1 + size |
||||
y2 = y1 - size |
||||
elif s < bp[4]: |
||||
x1 = size * - (bp[3] - s) |
||||
y1 = 0 |
||||
x2 = x1 + size |
||||
y2 = y1 + size |
||||
self.viz.canvas.create_rectangle(x1, y1, x2, y2, fill="#%02x%02x%02x" % (r, g, b), outline="") |
||||
self.viz.first_loop = False |
Loading…
Reference in new issue