-
Notifications
You must be signed in to change notification settings - Fork 51
/
lib.py
40 lines (29 loc) · 1.1 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
# Copyright (c) 2017-18 Richard Hull and contributors
# See LICENSE.rst for details.
import luma.core.error
__all__ = ["rpi_gpio", "spidev"]
def __spidev__(self): # pragma: no cover
# 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: # pragma: no cover
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
return GPIO
except RuntimeError as e:
if str(e) in ['This module can only be run on a Raspberry Pi!',
'Module not imported correctly!']:
raise luma.core.error.UnsupportedPlatform(
'GPIO access not available')
def rpi_gpio(Class):
setattr(Class, __rpi_gpio__.__name__, __rpi_gpio__)
return Class
def spidev(Class):
setattr(Class, __spidev__.__name__, __spidev__)
return Class