/ adafruit_tca9548a.py
adafruit_tca9548a.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2018 Carter Nelson for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 ``Adafruit_TCA9548A`` 24 ==================================================== 25 26 CircuitPython driver for the TCA9548A I2C Multiplexer. 27 28 * Author(s): Carter Nelson 29 30 Implementation Notes 31 -------------------- 32 33 **Hardware:** 34 35 * TCA9548A I2C Multiplexer: https://www.adafruit.com/product/2717 36 37 **Software and Dependencies:** 38 39 * Adafruit CircuitPython firmware for the supported boards: 40 https://github.com/adafruit/circuitpython/releases 41 * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice 42 """ 43 44 # imports 45 from micropython import const 46 47 _DEFAULT_ADDRESS = const(0x70) 48 49 __version__ = "0.0.0-auto.0" 50 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TCA9548A.git" 51 52 53 class TCA9548A_Channel: 54 """Helper class to represent an output channel on the TCA9548A and take care 55 of the necessary I2C commands for channel switching. This class needs to 56 behave like an I2CDevice.""" 57 58 def __init__(self, tca, channel): 59 self.tca = tca 60 self.channel_switch = bytearray([1 << channel]) 61 62 def try_lock(self): 63 """Pass thru for try_lock.""" 64 while not self.tca.i2c.try_lock(): 65 pass 66 self.tca.i2c.writeto(self.tca.address, self.channel_switch) 67 return True 68 69 def unlock(self): 70 """Pass thru for unlock.""" 71 self.tca.i2c.writeto(self.tca.address, b"\x00") 72 return self.tca.i2c.unlock() 73 74 def readfrom_into(self, address, buffer, **kwargs): 75 """Pass thru for readfrom_into.""" 76 if address == self.tca.address: 77 raise ValueError("Device address must be different than TCA9548A address.") 78 return self.tca.i2c.readfrom_into(address, buffer, **kwargs) 79 80 def writeto(self, address, buffer, **kwargs): 81 """Pass thru for writeto.""" 82 if address == self.tca.address: 83 raise ValueError("Device address must be different than TCA9548A address.") 84 return self.tca.i2c.writeto(address, buffer, **kwargs) 85 86 def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs): 87 """Pass thru for writeto_then_readfrom.""" 88 # In linux, at least, this is a special kernel function call 89 if address == self.tca.address: 90 raise ValueError("Device address must be different than TCA9548A address.") 91 92 if hasattr(self.tca.i2c, "writeto_then_readfrom"): 93 self.tca.i2c.writeto_then_readfrom(address, buffer_out, buffer_in, **kwargs) 94 else: 95 self.tca.i2c.writeto( 96 address, 97 buffer_out, 98 start=kwargs.get("out_start", 0), 99 end=kwargs.get("out_end", None), 100 stop=False, 101 ) 102 self.tca.i2c.readfrom_into( 103 address, 104 buffer_in, 105 start=kwargs.get("in_start", 0), 106 end=kwargs.get("in_end", None), 107 ) 108 109 110 class TCA9548A: 111 """Class which provides interface to TCA9548A I2C multiplexer.""" 112 113 def __init__(self, i2c, address=_DEFAULT_ADDRESS): 114 self.i2c = i2c 115 self.address = address 116 self.channels = [None] * 8 117 118 def __len__(self): 119 return 8 120 121 def __getitem__(self, key): 122 if not 0 <= key <= 7: 123 raise IndexError("Channel must be an integer in the range: 0-7") 124 if self.channels[key] is None: 125 self.channels[key] = TCA9548A_Channel(self, key) 126 return self.channels[key]