RASCSI/python/ctrlboard/src/ctrlboard_hw/pca9554multiplexer.py
dependabot[bot] 52e4a92aec
Bump black from 22.8.0 to 24.3.0 in /python/web (#1444)
* Bump black from 22.8.0 to 24.3.0 in /python/web

Bumps [black](https://github.com/psf/black) from 22.8.0 to 24.3.0.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](https://github.com/psf/black/compare/22.8.0...24.3.0)

---
updated-dependencies:
- dependency-name: black
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>

* Github CI: Backend web test runner use python 3.9.19

* Reformat python sources with black

* Docker: Bump to python 3.9-slim image for pytest

* Bump pytest version to 8.1.1

* Bump more library versions, and freeze them

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Daniel Markstedt <daniel@mindani.net>
2024-03-22 00:19:13 -07:00

69 lines
2.3 KiB
Python

"""
Module for interfacting with the pca9554 multiplexer
"""
# pylint: disable=c-extension-no-member
import logging
import smbus
class PCA9554Multiplexer:
"""Class interfacing with the pca9554 multiplexer"""
PIN_ENABLED_AS_OUTPUT = 0
PIN_ENABLED_AS_INPUT = 1
def __init__(self, i2c_address):
"""Constructor for the pc9554 multiplexer interface class"""
self.i2c_address = i2c_address
try:
self.i2c_bus = smbus.SMBus(1)
if self.read_input_register() is None:
logging.error(
"PCA9554 initialization test on specified i2c address %s failed",
self.i2c_address,
)
self.i2c_bus = None
except IOError:
logging.error("Could not open the i2c bus.")
self.i2c_bus = None
def write_configuration_register_port(self, port_bit, bit_value):
"""Reconfigures the configuration register. Updates the specified
port bit with bit_value. Returns true if successful, false otherwise."""
try:
if (0 <= port_bit <= 8) and (0 <= bit_value <= 1):
configuration_register = self.i2c_bus.read_byte_data(self.i2c_address, 3)
if bit_value:
updated_configuration_register = configuration_register | (1 << port_bit)
else:
updated_configuration_register = configuration_register & (
0xFF - (1 << port_bit)
)
self.i2c_bus.write_byte_data(self.i2c_address, 3, updated_configuration_register)
return True
return False
except IOError:
return False
def read_input_register(self):
"""Reads the complete 8 bit input port register from pca9554"""
try:
return self.i2c_bus.read_byte_data(self.i2c_address, 0)
except IOError:
return None
def read_input_register_port(self, port_bit):
"""Reads the input port register and returns the logic level of one specific port in the
argument"""
try:
if 0 <= port_bit <= 8:
input_register = self.i2c_bus.read_byte_data(self.i2c_address, 0)
return (input_register >> port_bit) & 1
return None
except IOError:
return None