Welcome

This documentation will guide you through the methods available in the Unicorn HAT python library.

Unicorn HAT is a Raspberry Pi add-on with 64 individually controllable RGB LEDs.

At A Glance

unicornhat.brightness(b=0.2)[source]
unicornhat.clear()[source]
unicornhat.get_brightness()[source]
unicornhat.get_index_from_xy(x, y)[source]
unicornhat.get_pixel(x, y)[source]
unicornhat.get_pixels()[source]
unicornhat.get_shape()[source]
unicornhat.off()[source]
unicornhat.rotation(r=0)[source]
unicornhat.set_all(r, g, b)[source]
unicornhat.set_layout(pixel_map=None)[source]
unicornhat.set_pixel(x, y, r, g, b)[source]
unicornhat.set_pixel_hsv(x, y, h, s, v)[source]
unicornhat.set_pixels(pixels)[source]
unicornhat.shade_pixels(shader)[source]
unicornhat.show()[source]

Brightness

unicornhat.brightness(b=0.2)[source]

Set the display brightness between 0.0 and 1.0

0.2 is highly recommended, UnicornHat can get painfully bright!

Parameters:b – Brightness from 0.0 to 1.0 (default 0.2)

Clear

unicornhat.clear()[source]

Clear the buffer

Get Brightness

unicornhat.get_brightness()[source]

Get the display brightness value

Returns a float between 0.0 and 1.0

Get Pixel

unicornhat.get_pixel(x, y)[source]

Get the RGB value of a single pixel

Parameters:
  • x – Horizontal position from 0 to 7
  • y – Veritcal position from 0 to 7

Get Pixels

unicornhat.get_pixels()[source]

Get the RGB value of all pixels in a 7x7x3 2d array of tuples

Get Shape

unicornhat.get_shape()[source]

Returns the shape (width, height) of the display

Turn Off

unicornhat.off()[source]

Clear the buffer and immediately update UnicornHat

Turns off all pixels.

Rotation

unicornhat.rotation(r=0)[source]

Set the display rotation

Parameters:r – Specify the rotation in degrees: 0, 90, 180 or 270

Set All

unicornhat.set_all(r, g, b)[source]

Set all pixels to a specific colour

Set Layout

unicornhat.set_layout(pixel_map=None)[source]

Set the layout to Unicorn HAT or Unicorn pHAT

Note: auto detection relies upon the HAT EEPROM. Your Unicorn HAT must be connected before boot to successfully auto detect.

Parameters:pixel_map – Choose the layout to set, can be either HAT, PHAT, PHAT_VERTICAL or AUTO

Set Pixel

unicornhat.set_pixel(x, y, r, g, b)[source]

Set a single pixel to RGB colour

Parameters:
  • x – Horizontal position from 0 to 7
  • y – Veritcal position from 0 to 7
  • r – Amount of red from 0 to 255
  • g – Amount of green from 0 to 255
  • b – Amount of blue from 0 to 255

Set Pixel HSV

unicornhat.set_pixel_hsv(x, y, h, s, v)[source]

Set a single pixel to a colour using HSV

Parameters:
  • x – Horizontal position from 0 to 7
  • y – Veritcal position from 0 to 7
  • h – Hue from 0.0 to 1.0 ( IE: degrees around hue wheel/360.0 )
  • s – Saturation from 0.0 to 1.0
  • v – Value (also known as brightness) from 0.0 to 1.0

Set Pixels

unicornhat.set_pixels(pixels)[source]

Set all pixels using an array of get_shape()

Shade Pixels

unicornhat.shade_pixels(shader)[source]

Set all pixels using a pixel shader style function

Parameters:pixels – A function which accepts the x and y positions of a pixel and returns values r, g and b

For example, this would be synonymous to clear:

set_pixels(lambda x, y: return 0,0,0)

Or perhaps we want to map red along the horizontal axis, and blue along the vertical:

set_pixels(lambda x, y: return (x/7.0) * 255, 0, (y/7.0) * 255)

Show

unicornhat.show()[source]

Update UnicornHat with the contents of the display buffer