Skip to content

Commit

Permalink
add decorator for rpi.gpio and spidev
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Mar 7, 2017
1 parent cb5a61f commit deb63ab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
42 changes: 42 additions & 0 deletions luma/core/lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from functools import wraps

import luma.core.error


__all__ = ["rpi_gpio", "spidev"]


def __spidev__(self):
# spidev cant compile on macOS, so use a similar technique to
# initialize (mainly so the tests run unhindered)
import spidev
return spidev.SpiDev()


def __rpi_gpio__(self):
# RPi.GPIO _really_ doesn't like being run on anything other than
# a Raspberry Pi... this is imported here so we can swap out the
# implementation for a mock
try:
import RPi.GPIO
return RPi.GPIO
except RuntimeError as e:
if str(e) == 'This module can only be run on a Raspberry Pi!':
raise luma.core.error.UnsupportedPlatform(
'GPIO access not available')


def rpi_gpio(f):
@wraps(f)
def wrapper(*args, **kwds):
f.__rpi_gpio__ = classmethod(__rpi_gpio__)
return f(*args, **kwds)
return wrapper


def spidev(f):
@wraps(f)
def wrapper(*args, **kwds):
f.__spidev__ = classmethod(__spidev__)
return f(*args, **kwds)
return wrapper
22 changes: 4 additions & 18 deletions luma/core/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import luma.core.error

from luma.core import lib


__all__ = ["i2c", "spi"]

Expand Down Expand Up @@ -111,6 +113,8 @@ def cleanup(self):
self._bus.close()


@lib.spidev
@lib.rpi_gpio
class spi(object):
"""
Wraps an `SPI <https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus>`_
Expand Down Expand Up @@ -166,24 +170,6 @@ def __init__(self, spi=None, gpio=None, port=0, device=0,
self._gpio.output(self._bcm_RST, self._gpio.LOW) # Reset device
self._gpio.output(self._bcm_RST, self._gpio.HIGH) # Keep RESET pulled high

def __rpi_gpio__(self):
# RPi.GPIO _really_ doesn't like being run on anything other than
# a Raspberry Pi... this is imported here so we can swap out the
# implementation for a mock
try:
import RPi.GPIO
return RPi.GPIO
except RuntimeError as e:
if str(e) == 'This module can only be run on a Raspberry Pi!':
raise luma.core.error.UnsupportedPlatform(
'GPIO access not available')

def __spidev__(self):
# spidev cant compile on macOS, so use a similar technique to
# initialize (mainly so the tests run unhindered)
import spidev
return spidev.SpiDev()

def command(self, *cmd):
"""
Sends a command or sequence of commands through to the SPI device.
Expand Down

0 comments on commit deb63ab

Please sign in to comment.