Unicorn HAT (HD) simulator - Originally by Jannis Hermanns <[email protected]>, modified by Mark Pitman <[email protected]> and adjusted by me (Matt Brash <[email protected]>)
Simulates a Unicorn HAT HD (and should work for the 8x8 HAT and the 8x4 PHAT as well) using pygame. The version you see here includes the following edits:
- Added
set_all(r, g, b)
to set the entire buffer to one colour - Modified the
clear()
function to act like the real HAT (clears the buffer, not the screen) - Added
get_pixel(x, y)
to return a tuple of the r, g, b values of a pixel - Added
get_pixels()
which returns the entire buffer as an array of arrays of arrays ([y][x][r, g, b]) - Modified
set_pixel()
to accept a colour string, for exampleset_pixel(x, y, "red")
see set_pixel.md - Added the non-standard
set_pixel_size(value)
which is only for the simulator to increase or decrease the dot size - Fixed the rotation code to rotate counter-clockwise correctly
- Commented the code thoroughly
- Size the initial window based on the size of the HAT and pixel size
If you want your code to run on your computer as well as your Pi, you could do something like this:
pip install unicornhatsimulator
(orpip2
orpip3
depending on your setup)- Adjust your
import unicornhathd
statement as follows:
'''
Note: The alias "as unicorn" can be whatever you want (or nothing at all).
For example: as u
'''
try:
import unicornhathd as unicorn
print("unicorn hat hd detected")
except ImportError:
from unicornhatsimulator import unicornhathd as unicorn
You can choose from import unicornhathd
(16x16), import unicornhat
(8x8) and import unicornphat
(8x4).
(Note that this gif has a low framerate, the simulator runs nice and smooth in real life)
- find a python person to show me how this would be done properly. For example - leaving the window open at the end.
- fix/check rotation
- add a proper LED glow effect so it looks more like a real unicorn HAT
- publish via pip (now online at pypi)
- add
get_pixel(x, y)
andget_pixels
- add non-standard
set_pixel_size(p)
to increase or decrease dot sizes
I am not a pygame
programmer. I'm barely a python
programmer. I found the original code for the simulator and tried to modify it to match the current state of the Pimoroni UnicornhatHD library. This was in order to have my students be able to work on their projects in Replit when they don't have access to their physical raspberry pi's from class.
Feel free to leave suggestions (issues, bugs) but do not expect a timely response.
~ M. Brash