Skip to content

Commit

Permalink
Merge pull request #18 from pimoroni/defer-setup
Browse files Browse the repository at this point in the history
Defer setup to avoid module import-time side-effects
  • Loading branch information
Gadgetoid authored Oct 18, 2017
2 parents 5d20344 + 551ba47 commit f415a4e
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions library/unicornhathd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import colorsys
import time


try:
import spidev
except ImportError:
sys.exit("This library requires the spidev module\nInstall with: sudo pip install spidev")
raise ImportError("This library requires the spidev module\nInstall with: sudo pip install spidev")

try:
import numpy
except ImportError:
exit("This library requires the numpy module\nInstall with: sudo pip install numpy")
raise ImportError("This library requires the numpy module\nInstall with: sudo pip install numpy")


__version__ = '0.0.2'

_spi = spidev.SpiDev()
_spi.open(0, 0)
_spi.max_speed_hz = 9000000
_SOF = 0x72
_DELAY = 1.0/120

Expand All @@ -34,6 +32,20 @@
_brightness = 0.5
_buf = numpy.zeros((16,16,3), dtype=int)

is_setup = False

def setup():
global _spi, _buf, is_setup

if is_setup:
return

_spi = spidev.SpiDev()
_spi.open(0, 0)
_spi.max_speed_hz = 9000000

is_setup = True

def brightness(b):
"""Set the display brightness between 0.0 and 1.0.
Expand Down Expand Up @@ -124,6 +136,7 @@ def off():

def show():
"""Output the contents of the buffer to Unicorn HAT HD."""
setup()
_spi.xfer2([_SOF] + (numpy.rot90(_buf,_rotation).reshape(768) * _brightness).astype(numpy.uint8).tolist())
time.sleep(_DELAY)

0 comments on commit f415a4e

Please sign in to comment.