From 366bdf24ee1d9896ce36867917dc699389226676 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Wed, 28 Jul 2021 12:47:33 +0200 Subject: [PATCH] PWMPin change events added. --- controlpi_plugins/_pigpio.py | 6 ---- controlpi_plugins/gpio.py | 68 +++++++++++++++++++----------------- controlpi_plugins/pcf8574.py | 16 --------- 3 files changed, 36 insertions(+), 54 deletions(-) diff --git a/controlpi_plugins/_pigpio.py b/controlpi_plugins/_pigpio.py index 92b2538..48fbc44 100644 --- a/controlpi_plugins/_pigpio.py +++ b/controlpi_plugins/_pigpio.py @@ -1,9 +1,3 @@ -"""Helper to instantiate only one pigpio instance. - -… - -TODO: documentation, doctests -""" import pigpio # type: ignore diff --git a/controlpi_plugins/gpio.py b/controlpi_plugins/gpio.py index 8f842a4..053c057 100644 --- a/controlpi_plugins/gpio.py +++ b/controlpi_plugins/gpio.py @@ -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 diff --git a/controlpi_plugins/pcf8574.py b/controlpi_plugins/pcf8574.py index 4fdac10..3b1ea51 100644 --- a/controlpi_plugins/pcf8574.py +++ b/controlpi_plugins/pcf8574.py @@ -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'}, -- 2.34.1