From abf793e7030e83510aa4b348c8833cd16be08420 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 16 May 2021 17:51:51 +0200 Subject: [PATCH] Added get_channels() method to PCA9685 driver --- platypush/plugins/pwm/pca9685.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/platypush/plugins/pwm/pca9685.py b/platypush/plugins/pwm/pca9685.py index f33969251..aa406dc4c 100644 --- a/platypush/plugins/pwm/pca9685.py +++ b/platypush/plugins/pwm/pca9685.py @@ -32,6 +32,7 @@ class PwmPca9685Plugin(Plugin): MIN_PWM_VALUE = 0 MAX_PWM_VALUE = 0xffff + N_CHANNELS = 16 def __init__(self, frequency: float, step_value: float = 0.1, step_duration: float = 0.05, **kwargs): """ @@ -48,13 +49,23 @@ class PwmPca9685Plugin(Plugin): @staticmethod def _convert_percent_to_duty_cycle(value: float) -> int: """ - Convert a duty cycle percentage value to a PCA9685 value between 0 and `0xffff`. + Convert a duty cycle percentage value to a PCA9685 value between 0 and ``0xffff``. :param value: Duty cycle value, between 0 and 1. - :return: Duty cycle 16-bit value, between 0 and `0xffff`. + :return: Duty cycle 16-bit value, between 0 and ``0xffff``. """ return int(value * 65535) + @staticmethod + def _convert_duty_cycle_to_percent(value: int) -> float: + """ + Convert a PCA9685 duty cycle value value to a percentage value. + + :param value: Duty cycle 16-bit value, between 0 and ``0xffff``. + :return: Duty cycle percentage, between 0 and 1. + """ + return value / 65535 + @action def execute(self, channels: Dict[int, float], frequency: Optional[float] = None, step_value: Optional[float] = None, step_duration: Optional[float] = None): @@ -97,6 +108,22 @@ class PwmPca9685Plugin(Plugin): time.sleep(step_duration) + @action + def get_channels(self) -> Dict[int, float]: + """ + Get the current duty cycle value of the channels. + + :return: A map in the format ``channel_index -> value``, where ``value`` is the duty cycle of the associated + channel, as a percentage value between 0 and 1. + """ + if not self._pca: + return {i: 0 for i in range(self.N_CHANNELS)} + + return { + i: self._convert_duty_cycle_to_percent(channel.duty_cycle) + for i, channel in enumerate(self._pca.channels) + } + @action def deinit(self): """