PWMPin change events added.
authorBenjamin Braatz <bb@bbraatz.eu>
Wed, 28 Jul 2021 10:47:33 +0000 (12:47 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Wed, 28 Jul 2021 10:47:33 +0000 (12:47 +0200)
controlpi_plugins/_pigpio.py
controlpi_plugins/gpio.py
controlpi_plugins/pcf8574.py

index 92b2538d610dcdfa9b4cb2b5c013dc0f953a6f1f..48fbc445f561e504a165813371ba822897c3141e 100644 (file)
@@ -1,9 +1,3 @@
-"""Helper to instantiate only one pigpio instance.
-
-…
-
-TODO: documentation, doctests
-"""
 import pigpio  # type: ignore
 
 
index 8f842a46b6d4b7876094349f18e7837867d77106..053c0573e3cd5cb9275c084fb18315d082d3e345 100644 (file)
@@ -1,9 +1,3 @@
-"""Plugins for GPIO pins.
-
-…
-
-TODO: documentation, doctests
-"""
 import asyncio
 import pigpio  # type: ignore
 
@@ -12,11 +6,6 @@ from controlpi_plugins._pigpio import _get_pigpio_pi  # type: ignore
 
 
 class OutputPin(BasePlugin):
-    """… plugin.
-
-    Do this and that.
-    """
-
     CONF_SCHEMA = {'properties': {'pin': {'type': 'integer',
                                           'minimum': 0, 'maximum': 31}},
                    'required': ['pin']}
@@ -59,11 +48,6 @@ class OutputPin(BasePlugin):
 
 
 class HackPin(BasePlugin):
-    """… plugin.
-
-    Do this and that.
-    """
-
     CONF_SCHEMA = {'properties': {'pin': {'type': 'integer',
                                           'minimum': 0, 'maximum': 31}},
                    'required': ['pin']}
@@ -104,11 +88,6 @@ class HackPin(BasePlugin):
 
 
 class InputPin(BasePlugin):
-    """… plugin.
-
-    Do this and that.
-    """
-
     CONF_SCHEMA = {'properties': {'pin': {'type': 'integer',
                                           'minimum': 0, 'maximum': 31},
                                   'glitch filter': {'type': 'integer',
@@ -159,13 +138,14 @@ class InputPin(BasePlugin):
 
 
 class PWMPin(BasePlugin):
-    """… plugin.
-
-    Do this and that.
-    """
-
     CONF_SCHEMA = {'properties': {'pin': {'type': 'integer',
-                                          'minimum': 0, 'maximum': 31}},
+                                          'minimum': 0, 'maximum': 31},
+                                  'freq': {'type': 'integer',
+                                           'minimum': 0,
+                                           'maximum': 125000000},
+                                  'duty': {'type': 'integer',
+                                           'minimum': 0,
+                                           'maximum': 1000000}},
                    'required': ['pin']}
 
     async def _receive(self, message: Message) -> None:
@@ -176,20 +156,43 @@ class PWMPin(BasePlugin):
         elif message['command'] == 'set freq':
             pi = _get_pigpio_pi()
             assert isinstance(message['new freq'], int)
-            self._freq: int = message['new freq']
-            pi.hardware_PWM(self.conf['pin'], self._freq, self._duty)
+            if message['new freq'] != self._freq:
+                self._freq: int = message['new freq']
+                pi.hardware_PWM(self.conf['pin'], self._freq, self._duty)
+                await self.bus.send(Message(self.name, {'event': 'changed',
+                                                        'freq': self._freq}))
+            else:
+                await self.bus.send(Message(self.name, {'freq': self._freq}))
         elif message['command'] == 'set duty':
             pi = _get_pigpio_pi()
             assert isinstance(message['new duty'], int)
-            self._duty: int = message['new duty']
-            pi.hardware_PWM(self.conf['pin'], self._freq, self._duty)
+            if message['new duty'] != self._duty:
+                self._duty: int = message['new duty']
+                pi.hardware_PWM(self.conf['pin'], self._freq, self._duty)
+                await self.bus.send(Message(self.name, {'event': 'changed',
+                                                        'duty': self._duty}))
+            else:
+                await self.bus.send(Message(self.name, {'duty': self._duty}))
 
     def process_conf(self) -> None:
         """Configure pin and register bus client."""
         pi = _get_pigpio_pi()
         self._freq = 0
+        if 'freq' in self.conf:
+            self._freq = self.conf['freq']
         self._duty = 0
-        sends = [MessageTemplate({'freq': {'type': 'integer',
+        if 'duty' in self.conf:
+            self._duty = self.conf['duty']
+        pi.hardware_PWM(self.conf['pin'], self._freq, self._duty)
+        sends = [MessageTemplate({'event': {'const': 'changed'},
+                                  'freq': {'type': 'integer',
+                                           'minimum': 0,
+                                           'maximum': 125000000}}),
+                 MessageTemplate({'event': {'const': 'changed'},
+                                  'duty': {'type': 'integer',
+                                           'minimum': 0,
+                                           'maximum': 1000000}}),
+                 MessageTemplate({'freq': {'type': 'integer',
                                            'minimum': 0,
                                            'maximum': 125000000}}),
                  MessageTemplate({'duty': {'type': 'integer',
@@ -214,3 +217,4 @@ class PWMPin(BasePlugin):
 
     async def run(self) -> None:
         """Run no code proactively."""
+        pass
index 4fdac103fb992c4b826fe7a5207ed7b1d0c97422..3b1ea5109eb2c4eb850d8bff1edfeb5daa64ae70 100644 (file)
@@ -1,9 +1,3 @@
-"""Plugins for I2C I/O cards.
-
-…
-
-TODO: documentation, doctests
-"""
 import asyncio
 import pigpio  # type: ignore
 
@@ -14,11 +8,6 @@ from controlpi_plugins._pigpio import _get_pigpio_pi  # type: ignore
 
 
 class OutputCard(BasePlugin):
-    """… plugin.
-
-    Do this and that.
-    """
-
     CONF_SCHEMA = {'properties':
                    {'address': {'type': 'integer'},
                     'pins':
@@ -97,11 +86,6 @@ class OutputCard(BasePlugin):
 
 
 class InputCard(BasePlugin):
-    """… plugin.
-
-    Do this and that.
-    """
-
     CONF_SCHEMA = {'properties':
                    {'address': {'type': 'integer'},
                     'interrupt pin': {'type': 'integer'},