LED kinda works, but there's much to do
This commit is contained in:
@@ -3,20 +3,21 @@ import colorsys
|
||||
import pygame.gfxdraw
|
||||
import time
|
||||
import pygame
|
||||
import numpy
|
||||
|
||||
class UnicornHat(object):
|
||||
def __init__(self, width, height, rotation_offset = 0):
|
||||
def __init__(self, width, height):
|
||||
# Compat with old library
|
||||
|
||||
# Set some defaults
|
||||
self.rotation_offset = rotation_offset
|
||||
self.rotation(0)
|
||||
self.pixels = [(0, 0, 0)] * width * height
|
||||
self.pixel_size = 25
|
||||
self.pixel_size = 20
|
||||
self.height = height
|
||||
self.width = width
|
||||
self.window_width = width * self.pixel_size
|
||||
self.window_height = height * self.pixel_size
|
||||
self.pixels = numpy.zeros((self.height,self.width,3), dtype=int)
|
||||
|
||||
self.window_width = self.width * self.pixel_size
|
||||
self.window_height = self.height * self.pixel_size
|
||||
|
||||
# Init pygame and off we go
|
||||
pygame.init()
|
||||
@@ -24,9 +25,15 @@ class UnicornHat(object):
|
||||
self.screen = pygame.display.set_mode([self.window_width, self.window_height])
|
||||
self.clear()
|
||||
|
||||
|
||||
def set_pixel(self, x, y, r, g, b):
|
||||
i = (x * self.width) + y
|
||||
self.pixels[i] = [int(r), int(g), int(b)]
|
||||
self.pixels[x][y] = int(r), int(g), int(b)
|
||||
|
||||
|
||||
def set_matrix(self, matrix):
|
||||
self.pixels = matrix
|
||||
self.show()
|
||||
|
||||
|
||||
def draw(self):
|
||||
for event in pygame.event.get(): # User did something
|
||||
@@ -34,9 +41,21 @@ class UnicornHat(object):
|
||||
print("Exiting...")
|
||||
sys.exit()
|
||||
|
||||
for x in range(self.width):
|
||||
for y in range(self.height):
|
||||
self.draw_led(x, y)
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
self.draw_led(i,j)
|
||||
|
||||
|
||||
def draw_led(self,i, j):
|
||||
p = self.pixel_size
|
||||
w_x = int(j * p + p / 2)
|
||||
#w_y = int((self.height - 1 - y) * p + p / 2)
|
||||
w_y = int(i * p + p / 2)
|
||||
r = int(p / 4)
|
||||
color = self.pixels[i,j,:]
|
||||
pygame.gfxdraw.aacircle(self.screen, w_x, w_y, r, color)
|
||||
pygame.gfxdraw.filled_circle(self.screen, w_x, w_y, r, color)
|
||||
|
||||
|
||||
def show(self):
|
||||
self.clear()
|
||||
@@ -44,58 +63,35 @@ class UnicornHat(object):
|
||||
pygame.display.flip()
|
||||
#time.sleep(5)
|
||||
|
||||
def draw_led(self, x, y):
|
||||
p = self.pixel_size
|
||||
w_x = int(x * p + p / 2)
|
||||
w_y = int((self.height - 1 - y) * p + p / 2)
|
||||
r = int(p / 4)
|
||||
color = self.pixels[self.index(x, y)]
|
||||
pygame.gfxdraw.aacircle(self.screen, w_x, w_y, r, color)
|
||||
pygame.gfxdraw.filled_circle(self.screen, w_x, w_y, r, color)
|
||||
|
||||
def get_shape(self):
|
||||
return (self.width, self.height)
|
||||
|
||||
|
||||
def brightness(self, *args):
|
||||
pass
|
||||
|
||||
|
||||
def rotation(self, r):
|
||||
self._rotation = int(round(r/90.0)) % 3
|
||||
|
||||
|
||||
def clear(self):
|
||||
self.screen.fill((0, 0, 0))
|
||||
|
||||
|
||||
def get_rotation(self):
|
||||
return self._rotation * 90
|
||||
|
||||
|
||||
def set_layout(self, *args):
|
||||
pass
|
||||
|
||||
def set_pixel_hsv(self, x, y, h, s=1.0, v=1.0):
|
||||
r, g, b = [int(n*255) for n in colorsys.hsv_to_rgb(h, s, v)]
|
||||
self.set_pixel(x, y, r, g, b)
|
||||
|
||||
def off(self):
|
||||
print("Closing window")
|
||||
#pygame.quit()
|
||||
|
||||
def index(self, x, y):
|
||||
# Offset to match device rotation
|
||||
rot = (self.get_rotation() + self.rotation_offset) % 360
|
||||
|
||||
if rot == 0:
|
||||
xx = x
|
||||
yy = y
|
||||
elif rot == 90:
|
||||
xx = self.height - 1 - y
|
||||
yy = x
|
||||
elif rot == 180:
|
||||
xx = self.width - 1 - x
|
||||
yy = self.height - 1 - y
|
||||
elif rot == 270:
|
||||
xx = y
|
||||
yy = self.width - 1 - x
|
||||
return (xx * self.width) + yy
|
||||
|
||||
|
||||
"""
|
||||
@@ -105,11 +101,10 @@ class UnicornHat(object):
|
||||
|
||||
# Unicornhat HD seems to be the other way around (not that there's anything wrong with that), so we rotate it 180°
|
||||
# unicornhathd = UnicornHatSim(16, 16, 180)
|
||||
twohats = UnicornHatSim(16, 32, 180)
|
||||
|
||||
|
||||
for i in range(16):
|
||||
twohats.set_pixel(i,i, 200,200,200)
|
||||
twohats.show()
|
||||
time.sleep(1)
|
||||
"""
|
||||
# twohats = UnicornHat(32, 16)
|
||||
#
|
||||
# for i in range(16):
|
||||
# twohats.set_pixel(i,i, 200,200,200)
|
||||
# twohats.show()
|
||||
# time.sleep(1)
|
||||
|
@@ -26,7 +26,7 @@ class UnicornHat(object):
|
||||
|
||||
self.rotation = 1
|
||||
self.brightness = 0.5
|
||||
self.buffer = numpy.zeros((self.WIDTH,self.HEIGHT,3), dtype=int)
|
||||
self.buffer = numpy.zeros((self.HEIGHT,self.WIDTH,3), dtype=int)
|
||||
|
||||
|
||||
|
||||
@@ -85,6 +85,12 @@ class UnicornHat(object):
|
||||
"""
|
||||
self.buffer[x][y] = r, g, b
|
||||
|
||||
|
||||
def set_matrix(self, matrix):
|
||||
self.buffer = matrix
|
||||
self.show()
|
||||
|
||||
|
||||
def set_pixel_hsv(self, x, y, h, s=1.0, v=1.0):
|
||||
"""set a single pixel to a colour using HSV.
|
||||
:param x: Horizontal position from 0 to 15
|
||||
|
Reference in New Issue
Block a user