Skip to content

Commit 4589f15

Browse files
authored
Merge pull request #53 from DrRob/fix-duty-cycle
Make duty_cycle return the same value it was set to...
2 parents cdde512 + 99d4212 commit 4589f15

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

adafruit_pca9685.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def duty_cycle(self) -> int:
7878
pwm = self._pca.pwm_regs[self._index]
7979
if pwm[0] == 0x1000:
8080
return 0xFFFF
81+
if pwm[1] == 0x1000:
82+
return 0x0000
8183
return pwm[1] << 4
8284

8385
@duty_cycle.setter
@@ -86,10 +88,16 @@ def duty_cycle(self, value: int) -> None:
8688
raise ValueError(f"Out of range: value {value} not 0 <= value <= 65,535")
8789

8890
if value == 0xFFFF:
91+
# Special case for "fully on":
8992
self._pca.pwm_regs[self._index] = (0x1000, 0)
93+
elif value < 0x0010:
94+
# Special case for "fully off":
95+
self._pca.pwm_regs[self._index] = (0, 0x1000)
9096
else:
9197
# Shift our value by four because the PCA9685 is only 12 bits but our value is 16
92-
value = (value + 1) >> 4
98+
value = value >> 4
99+
# value should never be zero here because of the test for the "fully off" case
100+
# (the LEDn_ON and LEDn_OFF registers should never be set with the same values)
93101
self._pca.pwm_regs[self._index] = (0, value)
94102

95103

0 commit comments

Comments
 (0)