Skip to content

Make duty_cycle return the same value it was set to... #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion adafruit_pca9685.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def duty_cycle(self) -> int:
pwm = self._pca.pwm_regs[self._index]
if pwm[0] == 0x1000:
return 0xFFFF
if pwm[1] == 0x1000:
return 0x0000
return pwm[1] << 4

@duty_cycle.setter
Expand All @@ -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)


Expand Down