41
41
ADG729_DEFAULT_ADDR = 0x44
42
42
43
43
44
- class Adafruit_ADG72x :
44
+ class ADG72x :
45
45
"""
46
46
A driver for the ADG728/ADG729 analog multiplexers.
47
47
"""
@@ -56,19 +56,65 @@ def __init__(self, i2c: typing.Type[I2C], i2c_address: int = ADG728_DEFAULT_ADDR
56
56
:type i2c_address: int
57
57
"""
58
58
self .i2c_device = i2cdevice .I2CDevice (i2c , i2c_address )
59
+ self ._channels = []
59
60
60
- @property . setter
61
- def channels (self , bits : int ):
61
+ @property
62
+ def channel (self ):
62
63
"""
63
- Selects channels on the ADG72x chip based on the provided bits.
64
- Each bit in the 8-bit value 'bits' turns on a single channel;
65
- multiple channels can be enabled simultaneously.
64
+ Gets the list of currently set channels. Returns an empty list if no channels are active.
65
+ """
66
+ return self ._channels [0 ] if len (self ._channels ) == 1 else self ._channels
67
+
68
+ @channel .setter
69
+ def channel (self , channel : int ):
70
+ """
71
+ Selects a single channel on the ADG72x chip. Channel numbering starts at 1.
66
72
67
73
:param bits: 8-bit value representing the channels to be selected/deselected.
68
74
:type bits: int
69
75
"""
76
+ bits = 1 << (channel - 1 )
77
+ try :
78
+ with self .i2c_device as i2c :
79
+ i2c .write (bytes ([bits ]))
80
+ except Exception as error :
81
+ raise IOError ("Failed to select channel on the ADG72x" ) from error
82
+ self .channels = [channel ]
83
+
84
+ @property
85
+ def channels (self ):
86
+ """
87
+ Gets the list of currently set channels. Returns an empty list if no channels are active.
88
+ """
89
+ return self ._channels
90
+
91
+ @channels .setter
92
+ def channels (self , channels : typing .List [int ]):
93
+ """
94
+ Selects multiple channels on the ADG72x chip. Channel numbering starts at 1.
95
+
96
+ :param channels: A list of channel numbers to be selected.
97
+ :type channels: List[int]
98
+ """
99
+ bits = 0
100
+ for channel in channels :
101
+ bits |= 1 << (channel - 1 )
70
102
try :
71
103
with self .i2c_device as i2c :
72
104
i2c .write (bytes ([bits ]))
73
105
except Exception as error :
74
106
raise IOError ("Failed to select channels on the ADG72x" ) from error
107
+ self ._channels = channels # Update the cached list of active channels
108
+
109
+ def channels_off (self ):
110
+ """
111
+ Turns all channels off.
112
+ """
113
+ try :
114
+ with self .i2c_device as i2c :
115
+ i2c .write (bytes ([0 ])) # Write a byte with all bits cleared
116
+ except Exception as error :
117
+ raise IOError ("Failed to turn off channels on the ADG72x" ) from error
118
+ self ._channels = (
119
+ []
120
+ ) # Update the cached list to reflect that no channels are active
0 commit comments