forked from andreeuuetoa/litsimaja
Add TKinter Visualizer Emulator(Slow but works)
This commit is contained in:
124
pyleds/lib/strip/FakeStrip.py
Normal file
124
pyleds/lib/strip/FakeStrip.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import atexit
|
||||
from lib.Color import Color
|
||||
|
||||
|
||||
class _LedData(object):
|
||||
|
||||
def __init__(self, channel, size):
|
||||
self.size = size
|
||||
self.channel = channel
|
||||
self._led = {}
|
||||
for i in range(size):
|
||||
self._led[i] = 0
|
||||
|
||||
def __getitem__(self, pos):
|
||||
"""Return the 24-bit RGB color value at the provided position or slice
|
||||
of positions.
|
||||
"""
|
||||
# Handle if a slice of positions are passed in by grabbing all the values
|
||||
# and returning them in a list.
|
||||
if isinstance(pos, slice):
|
||||
return [self._led[n] for n in range(*pos.indices(self.size))]
|
||||
else:
|
||||
return self._led[pos]
|
||||
|
||||
def __setitem__(self, pos, value):
|
||||
"""Set the 24-bit RGB color value at the provided position or slice of
|
||||
positions.
|
||||
"""
|
||||
# Handle if a slice of positions are passed in by setting the appropriate
|
||||
# LED data values to the provided values.
|
||||
if isinstance(pos, slice):
|
||||
index = 0
|
||||
for n in range(*pos.indices(self.size)):
|
||||
self._led[n] = value[index]
|
||||
index += 1
|
||||
# Else assume the passed in value is a number to the position.
|
||||
else:
|
||||
self._led[pos] = value
|
||||
|
||||
|
||||
class FakeStrip(object):
|
||||
_brightness: int
|
||||
|
||||
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
|
||||
brightness=255, channel=0, strip_type=None, gamma=None):
|
||||
|
||||
if gamma is None:
|
||||
# Support gamma in place of strip_type for back-compat with
|
||||
# previous version of forked library
|
||||
if type(strip_type) is list and len(strip_type) == 256:
|
||||
gamma = strip_type
|
||||
# strip_type = None
|
||||
else:
|
||||
gamma = list(range(256))
|
||||
|
||||
# Initialize the channel in use
|
||||
self._channel = None
|
||||
self._gamma = gamma
|
||||
self._brightness = brightness
|
||||
|
||||
# Grab the led data array.
|
||||
self._led_data = _LedData(self._channel, num)
|
||||
|
||||
# Substitute for __del__, traps an exit condition and cleans up properly
|
||||
atexit.register(self._cleanup)
|
||||
|
||||
def _cleanup(self):
|
||||
# Clean up memory used by the library when not needed anymore.
|
||||
if self._channel is not None:
|
||||
self._channel = None
|
||||
|
||||
def setGamma(self, gamma):
|
||||
if type(gamma) is list and len(gamma) == 256:
|
||||
self._gamma = gamma
|
||||
|
||||
def begin(self):
|
||||
"""Initialize library, must be called once before other functions are called."""
|
||||
return
|
||||
|
||||
def show(self):
|
||||
"""Update the display with the data from the LED buffer."""
|
||||
return
|
||||
|
||||
def getBrightness(self) -> int:
|
||||
return self._brightness
|
||||
|
||||
def setBrightness(self, brightness: int):
|
||||
"""Scale each LED in the buffer by the provided brightness.
|
||||
A brightness of 0 is the darkest and 255 is the brightest.
|
||||
"""
|
||||
self._brightness = brightness
|
||||
|
||||
def numPixels(self):
|
||||
"""Return the number of pixels in the display."""
|
||||
return self._led_data.size
|
||||
|
||||
def setPixelColor(self, n, color):
|
||||
"""Set LED at position n to the provided 24-bit color value (in RGB order).
|
||||
"""
|
||||
self._led_data[n] = color
|
||||
|
||||
def setPixelColorRGB(self, n, red, green, blue):
|
||||
"""Set LED at position n to the provided red, green, and blue color.
|
||||
Each color component should be a value from 0 to 255 (where 0 is the
|
||||
lowest intensity and 255 is the highest intensity).
|
||||
"""
|
||||
self.setPixelColor(n, Color(red, green, blue))
|
||||
|
||||
def getPixels(self):
|
||||
"""Return an object which allows access to the LED display data as if
|
||||
it were a sequence of 24-bit RGB values.
|
||||
"""
|
||||
return self._led_data
|
||||
|
||||
def getPixelColor(self, n):
|
||||
"""Get the 24-bit RGB color value for the LED at position n."""
|
||||
return self._led_data[n]
|
||||
|
||||
def getPixelColorRGB(self, n):
|
||||
c = lambda: None
|
||||
setattr(c, 'r', self._led_data[n] >> 16 & 0xff)
|
||||
setattr(c, 'g', self._led_data[n] >> 8 & 0xff)
|
||||
setattr(c, 'b', self._led_data[n] & 0xff)
|
||||
return c
|
||||
73
pyleds/lib/strip/TkinterStrip.py
Normal file
73
pyleds/lib/strip/TkinterStrip.py
Normal file
@@ -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
|
||||
79
pyleds/lib/strip/WindowStrip.py
Normal file
79
pyleds/lib/strip/WindowStrip.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user