From 090842de914369a3c0ec69ceb8a2aee4b40a5682 Mon Sep 17 00:00:00 2001 From: DrRob <1513453+DrRob@users.noreply.github.com> Date: Tue, 2 May 2023 22:01:26 +0100 Subject: [PATCH 1/2] Make duty_cycle return the same value it was set to, albeit with the least significant four bits set to zero (the device only uses the most significant twelve bits and doesn't store the remaining four, so these will be lost). Also set the special bit for "fully off" instead of setting the LEDn_ON and LEDn_OFF registers to the same value, as instructed in the datasheet. --- adafruit_pca9685.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/adafruit_pca9685.py b/adafruit_pca9685.py index 6622de2..2352c2f 100755 --- a/adafruit_pca9685.py +++ b/adafruit_pca9685.py @@ -77,6 +77,8 @@ def duty_cycle(self) -> int: pwm = self._pca.pwm_regs[self._index] if pwm[0] == 0x1000: return 0xFFFF + elif pwm[1] == 0x1000: + return 0x0000 return pwm[1] << 4 @duty_cycle.setter @@ -85,10 +87,16 @@ def duty_cycle(self, value: int) -> None: raise ValueError(f"Out of range: value {value} not 0 <= value <= 65,535") if value == 0xFFFF: + # Special case for "fully on": self._pca.pwm_regs[self._index] = (0x1000, 0) + elif value < 0x0010: + # Special case for "fully off": + self._pca.pwm_regs[self._index] = (0, 0x1000) else: # Shift our value by four because the PCA9685 is only 12 bits but our value is 16 - value = (value + 1) >> 4 + value = value >> 4 + # value should never be zero here because of the test for the "fully off" case + # (the LEDn_ON and LEDn_OFF registers should never be set with the same values) self._pca.pwm_regs[self._index] = (0, value) From 99d4212deac529577a046752ff8a3feea145f1c3 Mon Sep 17 00:00:00 2001 From: DrRob <1513453+DrRob@users.noreply.github.com> Date: Tue, 2 May 2023 22:28:03 +0100 Subject: [PATCH 2/2] Oops, fixed a pylint failure. --- adafruit_pca9685.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_pca9685.py b/adafruit_pca9685.py index 2352c2f..18d8ee0 100755 --- a/adafruit_pca9685.py +++ b/adafruit_pca9685.py @@ -77,7 +77,7 @@ def duty_cycle(self) -> int: pwm = self._pca.pwm_regs[self._index] if pwm[0] == 0x1000: return 0xFFFF - elif pwm[1] == 0x1000: + if pwm[1] == 0x1000: return 0x0000 return pwm[1] << 4