Skip to content

Instantly share code, notes, and snippets.

@Gadgetoid
Created January 10, 2018 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gadgetoid/f826f89ecd7e6694d1012edbd5382eb2 to your computer and use it in GitHub Desktop.
Save Gadgetoid/f826f89ecd7e6694d1012edbd5382eb2 to your computer and use it in GitHub Desktop.
Unicorn HAT HD - Multiple Display Driver
#!/usr/bin/env python
import colorsys
import time
import RPi.GPIO as GPIO
try:
import numpy
except ImportError:
exit("This library requires the numpy module\nInstall with: sudo pip install numpy")
__version__ = '0.0.2'
PIN_CLK = 11
PINS_DAT = [10, 22, 27]
PIN_CS = 8
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_CS, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(PIN_CLK, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PINS_DAT, GPIO.OUT, initial=GPIO.LOW)
_SOF = 0x72
_DELAY = 1.0/120
WIDTH = 48
HEIGHT = 16
PHAT = None
HAT = None
PHAT_VERTICAL = None
AUTO = None
_rotation = 0
_brightness = 0.5
_buf = numpy.zeros((WIDTH,HEIGHT,3), dtype=int)
def spi_write(buf1, buf2, buf3):
GPIO.output(PIN_CS, GPIO.LOW)
spi_write_byte(_SOF, _SOF, _SOF)
for x in range(len(buf1)):
b1, b2, b3 = buf1[x], buf2[x], buf3[x]
spi_write_byte(b1, b2, b3)
time.sleep(0.0000001)
GPIO.output(PIN_CS, GPIO.HIGH)
def spi_write_byte(b1, b2, b3):
for x in range(8):
GPIO.output(PINS_DAT[0], b1 & 0b10000000)
GPIO.output(PINS_DAT[1], b2 & 0b10000000)
GPIO.output(PINS_DAT[2], b3 & 0b10000000)
GPIO.output(PIN_CLK, GPIO.HIGH)
b1 <<= 1
b2 <<= 1
b3 <<= 1
time.sleep(0.00000001)
GPIO.output(PIN_CLK, GPIO.LOW)
def brightness(b):
"""Set the display brightness between 0.0 and 1.0.
:param b: Brightness from 0.0 to 1.0 (default 0.5)
"""
global _brightness
_brightness = b
def rotation(r):
"""Set the display rotation in degrees.
Actual rotation will be snapped to the nearest 90 degrees.
"""
global _rotation
_rotation = int(round(r/90.0))
def get_rotation():
"""Returns the display rotation in degrees."""
return _rotation * 90
def set_layout(pixel_map=None):
"""Does nothing, for library compatibility with Unicorn HAT."""
pass
def set_all(r, g, b):
_buf[:] = r, g, b
def set_pixel(x, y, r, g, b):
"""Set a single pixel to RGB colour.
:param x: Horizontal position from 0 to 15
:param y: Veritcal position from 0 to 15
:param r: Amount of red from 0 to 255
:param g: Amount of green from 0 to 255
:param b: Amount of blue from 0 to 255
"""
_buf[x][y] = r, g, b
def set_pixel_hsv(x, y, h, s=1.0, v=1.0):
"""set a single pixel to a colour using HSV.
:param x: Horizontal position from 0 to 15
:param y: Veritcal position from 0 to 15
:param h: Hue from 0.0 to 1.0 ( IE: degrees around hue wheel/360.0 )
:param s: Saturation from 0.0 to 1.0
:param v: Value (also known as brightness) from 0.0 to 1.0
"""
r, g, b = [int(n*255) for n in colorsys.hsv_to_rgb(h, s, v)]
set_pixel(x, y, r, g, b)
def get_pixel(x, y):
return tuple(_buf[x][y])
def shade_pixels(shader):
for x in range(WIDTH):
for y in range(HEIGHT):
r, g, b = shader(x, y)
set_pixel(x, y, r, g, b)
def get_pixels():
return _buf
def get_shape():
"""Return the shape (width, height) of the display."""
return WIDTH, HEIGHT
def clear():
"""Clear the buffer."""
_buf.fill(0)
def off():
"""Clear the buffer and immediately update Unicorn HAT HD.
Turns off all pixels.
"""
clear()
show()
def show():
"""Output the contents of the buffer to Unicorn HAT HD."""
_buf1 = numpy.rot90(_buf[:16,:HEIGHT],1)
_buf2 = numpy.rot90(_buf[16:32,:HEIGHT],1)
_buf3 = numpy.rot90(_buf[32:48,:HEIGHT],1)
_buf1, _buf2, _buf3 = [(x.reshape(768) * _brightness).astype(numpy.uint8).tolist() for x in _buf1, _buf2, _buf3]
spi_write(_buf1, _buf2, _buf3)
time.sleep(_DELAY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment