i2c: support selecting multiple or no channels

closes #1054
pull/1082/head
Robert Jördens 2018-06-15 19:35:01 +02:00
parent 40baa8ecba
commit f9910ab242
1 changed files with 12 additions and 7 deletions

View File

@ -48,23 +48,28 @@ class PCA9548:
self.address = address
@kernel
def set(self, channel):
"""Select one channel.
def select(self, mask):
"""Enable/disable channels.
Selecting multiple channels at the same time is not supported by this
driver.
:param channel: channel number (0-7)
:param mask: Bit mask of enabled channels
"""
i2c_start(self.busno)
try:
if not i2c_write(self.busno, self.address):
raise I2CError("PCA9548 failed to ack address")
if not i2c_write(self.busno, 1 << channel):
if not i2c_write(self.busno, mask):
raise I2CError("PCA9548 failed to ack control word")
finally:
i2c_stop(self.busno)
@kernel
def set(self, channel):
"""Enable one channel.
:param channel: channel number (0-7)
"""
self.select(1 << channel)
@kernel
def readback(self):
i2c_start(self.busno)