Skip to content

Commit 4d42872

Browse files
committed
adding example
1 parent a6a1652 commit 4d42872

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

adafruit_adg72x.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
ADG729_DEFAULT_ADDR = 0x44
4242

4343

44-
class Adafruit_ADG72x:
44+
class ADG72x:
4545
"""
4646
A driver for the ADG728/ADG729 analog multiplexers.
4747
"""
@@ -56,19 +56,65 @@ def __init__(self, i2c: typing.Type[I2C], i2c_address: int = ADG728_DEFAULT_ADDR
5656
:type i2c_address: int
5757
"""
5858
self.i2c_device = i2cdevice.I2CDevice(i2c, i2c_address)
59+
self._channels = []
5960

60-
@property.setter
61-
def channels(self, bits: int):
61+
@property
62+
def channel(self):
6263
"""
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.
6672
6773
:param bits: 8-bit value representing the channels to be selected/deselected.
6874
:type bits: int
6975
"""
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)
70102
try:
71103
with self.i2c_device as i2c:
72104
i2c.write(bytes([bits]))
73105
except Exception as error:
74106
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

examples/adg72x_simpletest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
21
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
32
#
43
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
import board
7+
import adafruit_adg72x
8+
9+
i2c = board.I2C()
10+
switch = adafruit_adg72x.ADG72x(i2c)
11+
12+
count = 0
13+
14+
while True:
15+
print(f"Selecting channel {count}")
16+
switch.channel = count
17+
count = (count + 1) % 8
18+
time.sleep(1)

0 commit comments

Comments
 (0)