Reuse single instance of pigpio.pi()
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Tue, 12 Jan 2021 17:19:13 +0000 (18:19 +0100)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Tue, 12 Jan 2021 17:19:13 +0000 (18:19 +0100)
graphit_pin/__init__.py
graphit_pin/gpio.py
graphit_pin/pcf8574.py

index 34d0b74e91e8cd123d432caf364d7256dcd2b6f6..ade938acdb11c642009192c01b3c6dfb7a05a232 100644 (file)
@@ -11,3 +11,12 @@ from .composition import InvertingPin, SwitchPin, GuardedPin, TimerPin,\
                          OrAggregatePin, AndAggregatePin
 from .gpio import GPIOInputPin, GPIOOutputPin
 from .pcf8574 import PCF8574Input, PCF8574Output
+
+import pigpio
+
+_pigpio_pi
+
+def get_pigpio_pi():
+    if _pigipio_pi is None:
+        _pigpio_pi = pigpio.pi()
+    return _pigpio_pi
index b14baddacd004607b9c8b56adcfeecc38ec0c1c5..0b2fb2e4e8aceaf6a1ab82ce42ecf6ec4997463e 100644 (file)
@@ -2,13 +2,14 @@ import asyncio
 import pigpio
 import graphit_event
 
+from . import get_pigpio_pi
 from .interface import PinInterface
 
 
 class GPIOInputPin(PinInterface, graphit_event.EventEmitterMixin):
     def __init__(self, pin: int, glitch: int = 5000, up: bool = False) -> None:
         self._pin = pin
-        pi = pigpio.pi()
+        pi = get_pigpio_pi()
         pi.set_mode(self._pin, pigpio.INPUT)
         pi.set_glitch_filter(self._pin, glitch)
         pi.set_pull_up_down(self._pin,
@@ -44,7 +45,7 @@ class GPIOInputPin(PinInterface, graphit_event.EventEmitterMixin):
 class GPIOOutputPin(PinInterface, graphit_event.EventEmitterMixin):
     def __init__(self, pin: int) -> None:
         self._pin = pin
-        pi = pigpio.pi()
+        pi = get_pigpio_pi()
         pi.set_mode(self._pin, pigpio.OUTPUT)
         self._value = bool(pi.read(self._pin))
 
@@ -56,7 +57,7 @@ class GPIOOutputPin(PinInterface, graphit_event.EventEmitterMixin):
     def value(self, value: bool) -> None:
         if self._value != value:
             self._value = value
-            pigpio.pi().write(self._pin, int(value))
+            get_pigpio_pi().write(self._pin, int(value))
             self._emit('change', self._value)
 
     @property
index aa8497f1515978d630ec100855a267adb43208c7..2d81876132249feddd8699ec5e4a2882f7269a40 100644 (file)
@@ -1,7 +1,7 @@
-import pigpio
 import graphit_event
 from typing import Callable
 
+from . import get_pigpio_pi
 from .interface import PinInterface
 
 PCF_ADDRESSES = tuple(range(32, 40)) + tuple(range(56, 64))
@@ -25,7 +25,7 @@ class PCF8574Input(graphit_event.EventEmitterMixin):
         assert address in PCF_ADDRESSES, 'Invalid PCF8574(A) I²C address'
         self._address = address
         self._interrupt = interrupt
-        pi = pigpio.pi()
+        pi = get_pigpio_pi()
         self._handle = pi.i2c_open(1, self._address)
         self._values = pi.i2c_read_byte(self._handle)
         self._pins = tuple(PCF8574InputPin(self, i) for i in range(0, 8))
@@ -39,7 +39,7 @@ class PCF8574Input(graphit_event.EventEmitterMixin):
     def close(self) -> None:
         self._interrupt.off(self._int_handle)
         try:
-            pigpio.pi().i2c_close(self._handle)
+            get_pigpio_pi().i2c_close(self._handle)
         except AttributeError:
             pass
 
@@ -81,14 +81,14 @@ class PCF8574Output(graphit_event.EventEmitterMixin):
     def __init__(self, address: int) -> None:
         assert address in PCF_ADDRESSES, 'Invalid PCF8574(A) I²C address'
         self._address = address
-        pi = pigpio.pi()
+        pi = get_pigpio_pi()
         self._handle = pi.i2c_open(1, self._address)
         self._values = pi.i2c_read_byte(self._handle)
         self._pins = tuple(PCF8574OutputPin(self, i) for i in range(0, 8))
 
     def close(self) -> None:
         try:
-            pigpio.pi().i2c_close(self._handle)
+            get_pigpio_pi().i2c_close(self._handle)
         except AttributeError:
             pass
 
@@ -109,7 +109,7 @@ class PCF8574Output(graphit_event.EventEmitterMixin):
         value = not value
         oldValues = self._values
         self._values = (oldValues & (0xFF ^ (1 << pin))) | (int(value) << pin)
-        pigpio.pi().i2c_write_byte(self._handle, self._values)
+        get_pigpio_pi().i2c_write_byte(self._handle, self._values)
         emitDiff(self._emit, oldValues, self._values)