38
38
39
39
import time
40
40
41
- from adafruit_register import i2c_struct
41
+ from adafruit_register .i2c_struct import UnaryStruct
42
+ from adafruit_register .i2c_struct_array import StructArray
42
43
from adafruit_bus_device import i2c_device
43
44
44
45
class PWMChannel :
@@ -56,7 +57,7 @@ def frequency(self):
56
57
def duty_cycle (self ):
57
58
"""16 bit value that dictates how much of one cycle is high (1) versus low (0). 0xffff will
58
59
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 ]
60
61
if pwm [0 ] == 0x1000 :
61
62
return 0xffff
62
63
return pwm [1 ] << 4
@@ -67,11 +68,11 @@ def duty_cycle(self, value):
67
68
raise ValueError ("Out of range" )
68
69
69
70
if value == 0xffff :
70
- self ._pca .pwm_regs [self ._index ]. __set__ ( self . _pca , (0x1000 , 0 ) )
71
+ self ._pca .pwm_regs [self ._index ] = (0x1000 , 0 )
71
72
else :
72
73
# Shift our value by four because the PCA9685 is only 12 bits but our value is 16
73
74
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 )
75
76
76
77
class PCAChannels : # pylint: disable=too-few-public-methods
77
78
"""Lazily creates and caches channel objects as needed. Treat it like a sequence."""
@@ -100,24 +101,9 @@ class PCA9685:
100
101
:param int reference_clock_speed: The frequency of the internal reference clock in Herz.
101
102
"""
102
103
# 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 )
121
107
122
108
def __init__ (self , i2c_bus , * , address = 0x40 , reference_clock_speed = 25000000 ):
123
109
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
0 commit comments