aboutsummaryrefslogtreecommitdiff
path: root/unicorn_hat_sim.py
diff options
context:
space:
mode:
authorMartin Ashby <martin@martin-laptop.lan>2018-05-15 22:12:09 +0100
committerMartin Ashby <martin@martin-laptop.lan>2018-05-15 22:12:09 +0100
commit57e5ea1f424ea2567e10c3945d9273a92a547332 (patch)
tree8184c2c8bab04c1a15b28fcfa2c7b255223a98d5 /unicorn_hat_sim.py
downloadunicornpaint-57e5ea1f424ea2567e10c3945d9273a92a547332.tar.gz
unicornpaint-57e5ea1f424ea2567e10c3945d9273a92a547332.tar.bz2
unicornpaint-57e5ea1f424ea2567e10c3945d9273a92a547332.tar.xz
unicornpaint-57e5ea1f424ea2567e10c3945d9273a92a547332.zip
Basic should be working
Diffstat (limited to 'unicorn_hat_sim.py')
-rw-r--r--unicorn_hat_sim.py123
1 files changed, 123 insertions, 0 deletions
diff --git a/unicorn_hat_sim.py b/unicorn_hat_sim.py
new file mode 100644
index 0000000..9d7d1a0
--- /dev/null
+++ b/unicorn_hat_sim.py
@@ -0,0 +1,123 @@
+import sys
+import colorsys
+import pygame.gfxdraw
+
+try:
+ import pygame
+except ImportError:
+ print("To simulate a unicorn HAT on your computer, please pip install pygame")
+
+class UnicornHatSim(object):
+ def __init__(self, width, height, rotation_offset = 0):
+ # Compat with old library
+ self.AUTO = None
+ self.PHAT = None
+
+ # Set some defaults
+ self.rotation_offset = rotation_offset
+ self.rotation(0)
+ self.pixels = [(0, 0, 0)] * width * height
+ self.pixel_size = 15
+ self.width = width
+ self.height = height
+ self.window_width = width * self.pixel_size
+ self.window_height = height * self.pixel_size
+
+ # Init pygame and off we go
+ pygame.init()
+ pygame.display.set_caption("Unicorn HAT simulator")
+ 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)]
+
+ def get_pixels(self):
+ """Returns a 2d array of 3-tuple RGB values repsenting pixel colours """
+ px = []
+ for y in range(self.height):
+ row = []
+ for x in range(self.width):
+ i = (x * self.width) + y
+ row.append(self.pixels[i])
+ px.append(row)
+ return px
+
+ def draw(self):
+ for event in pygame.event.get(): # User did something
+ if event.type == pygame.QUIT:
+ print("Exiting...")
+ sys.exit()
+
+ for x in range(self.width):
+ for y in range(self.height):
+ self.draw_led(x, y)
+
+ def show(self):
+ self.clear()
+ self.draw()
+ pygame.display.flip()
+
+ def draw_led(self, x, y):
+ self.draw_gfxcircle(x,y)
+
+ def draw_gfxcircle(self, x, y):
+ p = self.pixel_size
+ w_x = int(x * p + self.pixel_size / 2)
+ w_y = int((self.height - 1 - y) * p + self.pixel_size / 2)
+ r = int(self.pixel_size / 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
+
+# SD hats works as expected
+unicornhat = UnicornHatSim(8,8)
+unicornphat = UnicornHatSim(8, 4)
+
+# Unicornhat HD seems to be the other way around (not that there's anything wrong with that), so we rotate it 180 deg
+unicornhathd = UnicornHatSim(16, 16, 180) \ No newline at end of file