From 7b751595cb21cdaf40781128d644081dcc527d7e Mon Sep 17 00:00:00 2001
From: "tomas.petrauskas" <tomas16108@gmail.com>
Date: Tue, 21 Jul 2020 13:21:24 +0300
Subject: [PATCH] FR: PCF8575 Module

---
 README.md                       |  1 +
 pi_mqtt_gpio/modules/pcf8575.py | 41 +++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 pi_mqtt_gpio/modules/pcf8575.py

diff --git a/README.md b/README.md
index 2d8776b..21e9dd5 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,7 @@ GPIO Modules
 - Raspberry Pi GPIO (`raspberrypi`)
 - Orange Pi GPIO (`orangepi`)
 - PCF8574 IO chip (`pcf8574`)
+- PCF8575 IO chip (`pcf8575`)
 - PiFaceDigital 2 IO board (`piface2`)
 - Beaglebone GPIO (`beaglebone`)
 
diff --git a/pi_mqtt_gpio/modules/pcf8575.py b/pi_mqtt_gpio/modules/pcf8575.py
new file mode 100644
index 0000000..cda201e
--- /dev/null
+++ b/pi_mqtt_gpio/modules/pcf8575.py
@@ -0,0 +1,41 @@
+from __future__ import absolute_import
+
+from pi_mqtt_gpio.modules import GenericGPIO, PinDirection, PinPullup
+
+
+REQUIREMENTS = ("pcf8575",)
+CONFIG_SCHEMA = {
+    "i2c_bus_num": {"type": "integer", "required": True, "empty": False},
+    "chip_addr": {"type": "integer", "required": True, "empty": False},
+}
+
+PULLUPS = None
+
+
+class GPIO(GenericGPIO):
+    """
+    Implementation of GPIO class for the pcf8575 IO expander chip.
+    """
+
+    def __init__(self, config):
+        global PULLUPS
+        PULLUPS = {PinPullup.UP: True, PinPullup.DOWN: False}
+        from pcf8575 import pcf8575
+
+        self.io = pcf8575(config["i2c_bus_num"], config["chip_addr"])
+
+    def setup_pin(self, pin, direction, pullup, pin_config):
+        if direction == PinDirection.INPUT and pullup is not None:
+            self.io.port[pin] = PULLUPS[pullup]
+        initial = pin_config.get("initial")
+        if initial is not None:
+            if initial == "high":
+                self.set_pin(pin, True)
+            elif initial == "low":
+                self.set_pin(pin, False)
+
+    def set_pin(self, pin, value):
+        self.io.port[pin] = value
+
+    def get_pin(self, pin):
+        return self.io.port[pin]
-- 
GitLab