import smbus class PCA9534D: def __init__(self, i2c_bus=1, device_address=0x20): self.bus = smbus.SMBus(i2c_bus) self.device_address = device_address self.CONFIG_REG = 0x03 self.OUTPUT_REG = 0x01 self.PINS = { 'AN': 0, 'PWM': 1, 'INT': 2, 'RST': 3 } # Configurar todos los pines como salidas for pin in self.PINS.values(): self._set_pin_direction(pin, output=True) def _set_pin_direction(self, pin, output=True): # Leer la configuración actual current_config = self.bus.read_byte_data(self.device_address, self.CONFIG_REG) if output: new_config = current_config & ~(1 << pin) # Poner el bit en 0 para salida else: new_config = current_config | (1 << pin) # Poner el bit en 1 para entrada self.bus.write_byte_data(self.device_address, self.CONFIG_REG, new_config) def _set_pin_state(self, pin, high=True): # Leer el estado actual del registro de salida current_output = self.bus.read_byte_data(self.device_address, self.OUTPUT_REG) if high: new_output = current_output | (1 << pin) # Poner el bit en 1 else: new_output = current_output & ~(1 << pin) # Poner el bit en 0 self.bus.write_byte_data(self.device_address, self.OUTPUT_REG, new_output) def set_an_high(self): self._set_pin_state(self.PINS['AN'], high=True) def set_an_low(self): self._set_pin_state(self.PINS['AN'], high=False) def set_pwm_high(self): self._set_pin_state(self.PINS['PWM'], high=True) def set_pwm_low(self): self._set_pin_state(self.PINS['PWM'], high=False) def set_int_high(self): self._set_pin_state(self.PINS['INT'], high=True) def set_int_low(self): self._set_pin_state(self.PINS['INT'], high=False) def set_rst_high(self): self._set_pin_state(self.PINS['RST'], high=True) def set_rst_low(self): self._set_pin_state(self.PINS['RST'], high=False)