# arduino_comm.py

import smbus
import time

class ArduinoI2C:
    def __init__(self, bus_id=1, address=0x50):
        self.bus = smbus.SMBus(bus_id)
        self.address = address

    def send_command(self, command):
        """
        Sends a single-byte command (0 to 255) to the Arduino.
        """
        try:
            if not (0 <= command <= 255):
                raise ValueError("Command must be between 0 and 255")
            self.bus.write_byte(self.address, command)
            print(f"Sent command: {command} to Arduino at address 0x{self.address:02X}")
        except Exception as e:
            print(f"Error sending command to Arduino: {e}")

    def read_response(self):
        """
        Reads a byte of response from the Arduino (optional).
        """
        try:
            response = self.bus.read_byte(self.address)
            print(f"Received response: {response}")
            return response
        except Exception as e:
            print(f"Error reading response from Arduino: {e}")
            return None
