From f97cd01cc0ae5c2b0e42bce35ac1aff138c2b593 Mon Sep 17 00:00:00 2001
From: Jevgeni Kiski <yozik04@gmail.com>
Date: Thu, 23 Jan 2020 13:13:28 +0200
Subject: [PATCH] OrangePi support
 https://github.com/flyte/pi-mqtt-gpio/issues/81

---
 README.md                        | 16 ++++++++++
 pi_mqtt_gpio/modules/orangepi.py | 52 ++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 pi_mqtt_gpio/modules/orangepi.py

diff --git a/README.md b/README.md
index 74cf1ab..b0cd31b 100644
--- a/README.md
+++ b/README.md
@@ -9,6 +9,7 @@ GPIO Modules
 ------------
 
 - Raspberry Pi GPIO (`raspberrypi`)
+- Orange Pi GPIO (`orangepi`)
 - PCF8574 IO chip (`pcf8574`)
 - PiFaceDigital 2 IO board (`piface2`)
 - Beaglebone GPIO (`beaglebone`)
@@ -151,6 +152,17 @@ sensor_inputs:
     
 ```
 
+### OrangePi boards
+
+You need to specify what OrangePi board you use
+
+```yaml
+gpio_modules:
+  - name: orangepi
+    module: orangepi
+    board: zero # Supported: ZERO, R1, ZEROPLUS, ZEROPLUS2H5, ZEROPLUS2H3, PCPCPLUS, ONE, LITE, PLUS2E, PC2, PRIME
+```
+
 #### SSL/TLS
 
 You may want to connect to a remote server, in which case it's a good idea to use an encrypted connection. If the server supports this, then you can supply the relevant config values for the [tls_set()](https://github.com/eclipse/paho.mqtt.python#tls_set) command.
@@ -215,6 +227,10 @@ gpio_modules:
     i2c_bus_num: 1
     chip_addr: 0x20
 
+  - name: orangepi
+    module: orangepi
+    board: r1
+
 digital_inputs:
   - name: button
     module: raspberrypi
diff --git a/pi_mqtt_gpio/modules/orangepi.py b/pi_mqtt_gpio/modules/orangepi.py
new file mode 100644
index 0000000..cea532f
--- /dev/null
+++ b/pi_mqtt_gpio/modules/orangepi.py
@@ -0,0 +1,52 @@
+from pi_mqtt_gpio.modules import GenericGPIO, PinDirection, PinPullup
+
+
+REQUIREMENTS = ("OrangePi.GPIO",)
+
+DIRECTIONS = None
+PULLUPS = None
+
+
+class GPIO(GenericGPIO):
+    """
+    Implementation of GPIO class for Raspberry Pi native GPIO.
+    """
+
+    def __init__(self, config):
+        global DIRECTIONS, PULLUPS
+        import OPi.GPIO as gpio
+
+        self.io = gpio
+        DIRECTIONS = {PinDirection.INPUT: gpio.IN, PinDirection.OUTPUT: gpio.OUT}
+
+        PULLUPS = {
+            PinPullup.OFF: gpio.PUD_OFF,
+            PinPullup.UP: gpio.PUD_UP,
+            PinPullup.DOWN: gpio.PUD_DOWN,
+        }
+
+        board = config["board"].upper()
+        if not hasattr(gpio, board):
+            raise AssertionError("%s board not found" % board)
+        gpio.setboard(getattr(gpio, board))
+        gpio.setmode(gpio.BCM)
+
+    def setup_pin(self, pin, direction, pullup, pin_config):
+        direction = DIRECTIONS[direction]
+
+        if pullup is None:
+            pullup = PULLUPS[PinPullup.OFF]
+        else:
+            pullup = PULLUPS[pullup]
+
+        initial = {None: -1, "low": 0, "high": 1}[pin_config.get("initial")]
+        self.io.setup(pin, direction, pull_up_down=pullup, initial=initial)
+
+    def set_pin(self, pin, value):
+        self.io.output(pin, value)
+
+    def get_pin(self, pin):
+        return self.io.input(pin)
+
+    def cleanup(self):
+        self.io.cleanup()
-- 
GitLab