Skip to content

Commit

Permalink
Merge branch 'gpiozero'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarr823 committed Dec 20, 2023
2 parents 23d73e1 + 0dac6cc commit 789cad4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion papertty/drivers/driver_it8951.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import time

from papertty.drivers.drivers_base import DisplayDriver
from papertty.drivers.drivers_base import GPIO

try:
import spidev
import RPi.GPIO as GPIO
except ImportError:
pass
except RuntimeError as e:
Expand Down
8 changes: 1 addition & 7 deletions papertty/drivers/drivers_4in2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@

from PIL import Image

from papertty.drivers.drivers_base import GPIO
from papertty.drivers.drivers_consts import EPD4in2const
from papertty.drivers.drivers_partial import WavesharePartial

try:
import RPi.GPIO as GPIO
except ImportError:
pass
except RuntimeError as e:
print(str(e))

# The driver works as follows:
#
# This driver is for the following display:
Expand Down
54 changes: 53 additions & 1 deletion papertty/drivers/drivers_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,64 @@
# using them
try:
import spidev
import RPi.GPIO as GPIO
import RPi.GPIO as rpiGPIO
except ImportError:
pass
except RuntimeError as e:
print(str(e))

# Optional dependency
try:
from gpiozero import OutputDevice, InputDevice
print("gpiozero found - using that instead of RPi.GPIO")
except ImportError:
print("gpiozero not found - defaulting to RPi.GPIO")
pass

class GPIO:

OUT = rpiGPIO.OUT
IN = rpiGPIO.IN
BCM = rpiGPIO.BCM
LOW = rpiGPIO.LOW
HIGH = rpiGPIO.HIGH

pins = {}

@staticmethod
def setmode(value):
rpiGPIO.setmode(value)

@staticmethod
def setwarnings(value):
rpiGPIO.setwarnings(value)

@staticmethod
def setup(pin, ioType):
try:
if ioType == GPIO.OUT:
GPIO.pins[str(pin)] = OutputDevice(pin)
else:
GPIO.pins[str(pin)] = InputDevice(pin)
except Exception as e:
GPIO.pins[str(pin)] = False
rpiGPIO.setup(pin, ioType)

@staticmethod
def input(pinNo):
pin = GPIO.pins[str(pinNo)]
if pin == False:
return rpiGPIO.input(pinNo)
else:
return pin.value

@staticmethod
def output(pinNo, value):
pin = GPIO.pins[str(pinNo)]
if pin == False:
rpiGPIO.output(pinNo, value)
else:
pin.on() if value == 1 else pin.off()

class DisplayDriver(ABC):
"""Abstract base class for a display driver - be it Waveshare e-Paper, PaPiRus, OLED..."""
Expand Down

0 comments on commit 789cad4

Please sign in to comment.