File size: 3,780 Bytes
1512437 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #!/usr/bin/python
import logging
#import smbus
from pymlab.sensors import Device, SimpleBus
LOGGER = logging.getLogger(__name__)
class I2CHub(Device):
"""Python library for I2CHUB02A MLAB module with TCA9548A i2c bus expander.
Example:
.. code-block:: python
#!/usr/bin/python
# Python library for I2CHUB02A MLAB module with TCA9548A i2c bus expander.
# Example code
# Program takes two arguments
import time
import I2CHUB02
import sys
# Example of example use:
# sudo ./I2CHUB02_Example.py 5
hub = I2CHUB02.i2chub(int(sys.argv[1]),eval(sys.argv[2]))
print "Get initial I2CHUB setup:"
print "I2CHUB channel setup:", bin(hub.status());
#print "Setup I2CHUB to channel configuration: ", bin(hub.ch0 |hub.ch2 | hub.ch3 | hub.ch7);
#hub.setup(hub.ch0 |hub.ch2 | hub.ch3 | hub.ch7);
#this connect the channels O and 7 on the I2CHUB02A togeather with master bus.
print "Setup I2CHUB to channel configuration: ", bin(eval(sys.argv[3]));
hub.setup(eval(sys.argv[3]));
time.sleep(0.1);
print "final I2C hub channel status: ", bin(hub.status());
"""
ch0 = 0b00000001
ch1 = 0b00000010
ch2 = 0b00000100
ch3 = 0b00001000
ch4 = 0b00010000
ch5 = 0b00100000
ch6 = 0b01000000
ch7 = 0b10000000
ALL_CHANNELS = 0b11111111
def __init__(self, parent = None, address=0x70, **kwargs):
Device.__init__(self, parent, address, **kwargs)
self.channels = {}
for channel in range(8):
self.channels[channel] = SimpleBus(
self,
None,
channel = channel,
)
self._status = None
def __getitem__(self, key):
return self.channels[key]
@property
def status(self):
if self._status is None:
self._status = self.get_status()
return self._status
def get_named_devices(self):
if self.name is None:
result = {}
else:
result = { self.name: self, }
for channel in self.channels.itervalues():
result.update(channel.get_named_devices())
return result
def add_child(self, device):
if device.channel is None:
raise ValueError("Device doesn't have a channel.")
self.channels[device.channel].add_child(device)
def route(self, child = None):
if self.routing_disabled:
return False
if child is None:
return Device.route(self)
LOGGER.debug("Routing multiplexer to %r" % (child, ))
if child.channel is None:
LOGGER.error("Child doesn't have a channel: %r" % (child, ))
return False
self.routing_disabled = True
mask = 1 << child.channel
if (self.status != mask):
self.setup(mask)
self.routing_disabled = False
#self.setup(child.channel)
return True
def initialize(self):
Device.initialize(self)
for channel, bus in self.channels.iteritems():
self.setup(1 << channel)
bus.initialize()
self.setup(self.ALL_CHANNELS)
def setup(self, i2c_channel_setup):
self._status = i2c_channel_setup
if self.bus is None:
raise Exception(
"Cannot setup device %s, because it doesn't have a bus (none of its parents is a bus)." % (
self.name
)
)
self.bus.write_byte(self.address, i2c_channel_setup)
return -1
def get_status(self):
self.route()
self._status = self.bus.read_byte(self.address)
return self._status
|