def read_register(self, address: int, register: int) -> int: """Reads register from io stream.""" try: if os.getenv("IS_I2C_ENABLED") == "true": reg = c_uint8(register) result = c_uint8() request = make_i2c_rdwr_data( # type: ignore [ (address, 0, 1, pointer(reg)), # write cmd register ( address, I2C_M_RD, 1, pointer(result), ), # read 1 byte as result ] ) fcntl.ioctl(self.io.fileno(), I2C_RDWR, request) byte_ = int(result.value) message = "Read register 0x{:02X}, value: 0x{:02X}".format( register, byte_ ) self.logger.debug(message) return byte_ elif os.getenv("IS_USB_I2C_ENABLED") == "true": device = self.io.get_port(address) # type: ignore byte_raw = device.read_from(register, readlen=1) byte = int(byte_raw[0]) return byte else: message = "Platform does not support i2c communication" raise ReadError(message) except (IOError, I2cIOError, I2cNackError) as e: message = "Unable to read register 0x{:02}".format(register) raise ReadError(message) from e
def read(self, device_addr: int, num_bytes: int) -> bytes: """Reads bytes from buffer. Returns 0x00 if buffer is empty.""" msg = "Reading {} bytes, buffer: {}".format(num_bytes, byte_str(self.buffer)) self.logger.debug(msg) # Check device address matches if device_addr != self.device_addr: message = "Address not found: 0x{:02X}".format(device_addr) raise ReadError(message) # Pop bytes from buffer and return bytes_ = [] while num_bytes > 0: # Check for empty buffer or pop byte from buffer if len(self.buffer) == 0: bytes_.append(0x00) else: bytes_.append(self.buffer.pop()) # Decrement num bytes to read num_bytes = num_bytes - 1 # Successfully read bytes return bytes(bytes_)
def read(self, address: int, num_bytes: int) -> bytes: """Reads bytes from io stream.""" try: if os.getenv("IS_I2C_ENABLED") == "true": fcntl.ioctl(self.io, I2C_SLAVE, address) return bytes(self.io.read(num_bytes)) elif os.getenv("IS_USB_I2C_ENABLED") == "true": device = self.io.get_port(address) # type: ignore bytes_ = device.read(readlen=num_bytes) return bytes(bytes_) else: message = "Platform does not support i2c communication" raise ReadError(message) except (IOError, I2cIOError, I2cNackError) as e: message = "Unable to read {} bytes".format(num_bytes) raise ReadError(message) from e
def read(self, address: int, num_bytes: int) -> bytes: """Reads bytes from io stream.""" try: fcntl.ioctl(self.io, I2C_SLAVE, address) return bytes(self.io.read(num_bytes)) except IOError as e: message = "Unable to read {} bytes".format(num_bytes) raise ReadError(message) from e
def read(self, device_addr: int, num_bytes: int) -> bytes: """Reads bytes from buffer. Returns 0x00 if buffer is empty.""" #msg = "Reading {} bytes, buffer: {}".format(num_bytes, byte_str(self.buffer)) #self.logger.debug(msg) # Check device address matches if device_addr != self.device_addr: message = "Address not found: 0x{:02X}".format(device_addr) raise ReadError(message) return self.get_read_response_bytes(num_bytes)
def read_register(self, device_addr: int, register_addr: int) -> int: """Reads register byte.""" # Check address matches if device_addr != self.device_addr: message = "Address not found: 0x{:02X}".format(device_addr) raise ReadError(message) # Check register within range if register_addr not in range(256): message = "Invalid register addrress: {}, must be 0-255".format( register_addr ) raise ReadError(message) # Read register value from register dict try: return self.registers[register_addr] except KeyError: message = "Register address not found: 0x{:02X}".format(register_addr) raise ReadError(message)
def read_register(self, address: int, register: int) -> int: """Reads register from io stream.""" try: reg = c_uint8(register) result = c_uint8() request = make_i2c_rdwr_data( # type: ignore [ (address, 0, 1, pointer(reg)), # write cmd register (address, I2C_M_RD, 1, pointer(result)), # read 1 byte as result ]) fcntl.ioctl(self.io.fileno(), I2C_RDWR, request) byte_ = int(result.value) message = "Read register 0x{:02X}, value: 0x{:02X}".format( register, byte_) self.logger.debug(message) return byte_ except IOError as e: message = "Unable to read register 0x{:02}".format(register) raise ReadError(message) from e