def verify(self, address: int, channel: int) -> None: """Verifies if mux at address is set to correct channel.""" #self.logger.debug("Verifying mux connection") # Check mux exists if address not in self.connections: message = "Mux 0x{:02X} has never been set".format(address) raise MuxError(message, logger=self.logger) # Convert channel to channel byte channel_byte = 0x01 << channel if self.connections[address] != channel_byte: message = "Mux channel mismatch, stored: 0x{:02X}, received: 0x{:02X}".format( self.connections[address], channel_byte) raise MuxError(message, logger=self.logger)
def set_mux(self, mux: int, channel: int, retry: bool = True) -> None: """Sets mux to channel""" with self.i2c_lock: channel_byte = 0x01 << channel self.logger.debug( "Setting mux 0x{:02X} to channel {}, writing: [0x{:02X}]". format(mux, channel, channel_byte)) try: self.io.write(mux, bytes([channel_byte])) except WriteError as e: raise MuxError("Unable to set mux", logger=self.logger) from e
def set(self, address: int, channel_byte: int) -> None: """Sets mux at address to channel.""" #message = "Setting addr 0x{:02X} to 0x{:02X}".format(address, channel_byte) #self.logger.debug(message) # Verify valid channel byte: if channel_byte not in self.valid_channel_bytes: message = "Unable to set mux, invalid channel byte: 0x{:02X}".format( channel_byte) raise MuxError(message) # Set mux to channel self.connections[address] = channel_byte
def write(self, address: int, bytes_: bytes) -> None: """Writes bytes to buffer.""" # Check if writing to mux if address == self.mux_address: # Check if mux command valid if len(bytes_) > 1: raise MuxError( "Unable to set mux, only 1 command byte is allowed") # Set mux to channel self.mux_simulator.set(self.mux_address, bytes_[0]) # type: ignore # Check if writing to device elif address == self.device_addr: # Verify mux connection if self.mux_address != None: address = self.mux_address # type: ignore channel = self.mux_channel self.mux_simulator.verify(address, channel) # type: ignore # Get response bytes response_bytes = self.get_write_response_bytes(bytes_) # Verify known write bytes if response_bytes == None: raise WriteError("Unknown write bytes: {}".format( byte_str(bytes_))) # Write response bytes to buffer response_byte_string = byte_str(response_bytes) # type: ignore self.logger.debug( "Response bytes: {}".format(response_byte_string)) for byte in response_bytes: # type: ignore self.buffer.insert(0, byte) self.logger.debug("Buffer: {}".format(byte_str(self.buffer))) # Check for invalid address else: message = "Address not found: 0x{:02X}".format(address) raise WriteError(message)