Skip to content

Commit aaec737

Browse files
committed
Switch to StructArray to simplify pwm_regs and save memory.
This depends on adafruit/Adafruit_CircuitPython_Register#10
1 parent 35c9e79 commit aaec737

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

adafruit_pca9685.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838

3939
import time
4040

41-
from adafruit_register import i2c_struct
41+
from adafruit_register.i2c_struct import UnaryStruct
42+
from adafruit_register.i2c_struct_array import StructArray
4243
from adafruit_bus_device import i2c_device
4344

4445
class PWMChannel:
@@ -56,7 +57,7 @@ def frequency(self):
5657
def duty_cycle(self):
5758
"""16 bit value that dictates how much of one cycle is high (1) versus low (0). 0xffff will
5859
always be high, 0 will always be low and 0x7fff will be half high and then half low."""
59-
pwm = self._pca.pwm_regs[self._index].__get__(self._pca)
60+
pwm = self._pca.pwm_regs[self._index]
6061
if pwm[0] == 0x1000:
6162
return 0xffff
6263
return pwm[1] << 4
@@ -67,11 +68,11 @@ def duty_cycle(self, value):
6768
raise ValueError("Out of range")
6869

6970
if value == 0xffff:
70-
self._pca.pwm_regs[self._index].__set__(self._pca, (0x1000, 0))
71+
self._pca.pwm_regs[self._index] = (0x1000, 0)
7172
else:
7273
# Shift our value by four because the PCA9685 is only 12 bits but our value is 16
7374
value = (value + 1) >> 4
74-
self._pca.pwm_regs[self._index].__set__(self._pca, (0, value))
75+
self._pca.pwm_regs[self._index] = (0, value)
7576

7677
class PCAChannels: # pylint: disable=too-few-public-methods
7778
"""Lazily creates and caches channel objects as needed. Treat it like a sequence."""
@@ -100,24 +101,9 @@ class PCA9685:
100101
:param int reference_clock_speed: The frequency of the internal reference clock in Herz.
101102
"""
102103
# Registers:
103-
mode1_reg = i2c_struct.UnaryStruct(0x00, '<B')
104-
prescale_reg = i2c_struct.UnaryStruct(0xFE, '<B')
105-
pwm_regs = (i2c_struct.Struct(0x06, '<HH'), # PWM 0
106-
i2c_struct.Struct(0x0A, '<HH'), # PWM 1
107-
i2c_struct.Struct(0x0E, '<HH'), # PWM 2
108-
i2c_struct.Struct(0x12, '<HH'), # PWM 3
109-
i2c_struct.Struct(0x16, '<HH'), # PWM 4
110-
i2c_struct.Struct(0x1A, '<HH'), # PWM 5
111-
i2c_struct.Struct(0x1E, '<HH'), # PWM 6
112-
i2c_struct.Struct(0x22, '<HH'), # PWM 7
113-
i2c_struct.Struct(0x26, '<HH'), # PWM 8
114-
i2c_struct.Struct(0x2A, '<HH'), # PWM 9
115-
i2c_struct.Struct(0x2E, '<HH'), # PWM 10
116-
i2c_struct.Struct(0x32, '<HH'), # PWM 11
117-
i2c_struct.Struct(0x36, '<HH'), # PWM 12
118-
i2c_struct.Struct(0x3A, '<HH'), # PWM 13
119-
i2c_struct.Struct(0x3E, '<HH'), # PWM 14
120-
i2c_struct.Struct(0x42, '<HH')) # PWM 15
104+
mode1_reg = UnaryStruct(0x00, '<B')
105+
prescale_reg = UnaryStruct(0xFE, '<B')
106+
pwm_regs = StructArray(0x06, '<HH', 16)
121107

122108
def __init__(self, i2c_bus, *, address=0x40, reference_clock_speed=25000000):
123109
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)

0 commit comments

Comments
 (0)