from i2c_handler import I2CSlaveHandler, Packet import neopixel class LedDistanceHandler(I2CSlaveHandler): def __init__(self, i2c_id, sda, scl, slave_addr): super().__init__(i2c_id, sda, scl, slave_addr) self.last_led_color = (0, 0, 0) # RGB tuple self.np = neopixel.NeoPixel(machine.Pin(23), 1) def process_packet(self, packet: Packet): cmd = packet[0] # Assume LED color command is 0x10, RGB in bytes 2,3,4 if cmd == 0x10: # Set LED Color r = packet[2] g = packet[3] b = packet[4] self.last_led_color = (r, g, b) print(f"Set LED color to RGB({r},{g},{b})") # TODO: Set the hardware LED to this color self.np[0] = self.last_led_color self.np.write() else: print(f"Unknown command 0x{cmd:02X}") self.response_packet = None def main(): handler = LedDistanceHandler(i2c_id=0, sda=0, scl=1, slave_addr=0x41) print("LED/Distance handler I2C slave started") try: handler.handle() except KeyboardInterrupt: handler.deinit() print("I2C slave stopped") if __name__ == "__main__": main()