Git Product home page Git Product logo

unicornhatsimulator's Introduction

Unicorn HAT (HD) simulator

v1.0.4

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

unicornhatsimulator's People

Contributors

misterbrash avatar

Stargazers

fr1550n avatar

Watchers

James Cloos avatar  avatar

unicornhatsimulator's Issues

Runs too fast + mistake in pypi.org documentation

This runs WAY too fast on my machine. Your implementation of pygame needs to have a delay added to get a consistent framerate. I recommend this change:

def show(self):
    self.screen.fill((0, 0, 0))
    self.draw()
    clock = pygame.time.Clock()
    pygame.display.flip()
    clock.tick(100)

Also, your documentation on https://pypi.org/project/unicornhatsimulator/ uses the wrong module name to launch the simulator. Your description says to add this to the code to the script you want to launch:

from unicorn_hat_sim import unicornhathd as unicorn

But "unicorn_hat_sim" is not the right module title. It needs to say:

from unicornhatsimulator import unicornhathd as unicorn

You should also point out that the alias name "unicorn" should be changed to match whatever alias is used in the script in question. Not every unicornhathd script uses "unicorn" as the alias.

Love this module, BTW! I am doing some graphic work on my machine, which makes development on the unicorn hat HD much easier to test. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.