Skip to main content

Pimoroni UnicornHAT simulator (pHAT, HAT and HAT HD)

Project description

Unicorn HAT (HD) simulator

v1.0.4

Unicorn HAT (HD) simulator - Originally by Jannis Hermanns <jannis@gmail.com>, modified by Mark Pitman <mark.pitman@gmail.com> and adjusted by me (Matt Brash <matt@brash.ca>)

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 example set_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

Usage

If you want your code to run on your computer as well as your Pi, you could do something like this:

  1. pip install unicornhatsimulator (or pip2 or pip3 depending on your setup)
  2. 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).

Demo

(Note that this gif has a low framerate, the simulator runs nice and smooth in real life)

Demo

TODO

  • 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) and get_pixels
  • add non-standard set_pixel_size(p) to increase or decrease dot sizes

Disclaimer

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unicornhatsimulator-1.0.4.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unicornhatsimulator-1.0.4-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file unicornhatsimulator-1.0.4.tar.gz.

File metadata

  • Download URL: unicornhatsimulator-1.0.4.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for unicornhatsimulator-1.0.4.tar.gz
Algorithm Hash digest
SHA256 a7d69994a855263422b35b5c349d58279b16f9ddeedd350628b348d086a9e319
MD5 b0652ad2bc3ad8f43df48efc737843b2
BLAKE2b-256 42eecfedb6f042ee208ab1429f05897ff286bc5e868dfe636ef1572efd8cb065

See more details on using hashes here.

File details

Details for the file unicornhatsimulator-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for unicornhatsimulator-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3baab427015df35ac6ee3b313d2efd51f5665c7b81c212aafbd0d34b6167fc3c
MD5 f5b67577538efba08a0c16973e3fb7e5
BLAKE2b-256 ebd51b1a291817a43fbab83a291caf1ed9dbec6f3382a27ddfa3a38e1b2b3080

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page