-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add decorator for rpi.gpio and spidev
- Loading branch information
1 parent
cb5a61f
commit deb63ab
Showing
2 changed files
with
46 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters