from machine import ADC, Pin, PWM, I2C
from time import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd

# LCD configuración I2C
i2c = I2C(0, scl=Pin(21), sda=Pin(22), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)  # 0x27 suele ser la dirección I2C

# Servo configuración
servo = PWM(Pin(33), freq=50)

# Potenciómetro configuración
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB)
pot.width(ADC.WIDTH_10BIT)

def set_servo_angle(angle):
    min_us = 500
    max_us = 2400
    us = min_us + (max_us - min_us) * angle // 180
    servo.duty_ns(us * 1000)

lcd.clear()
lcd.putstr("Simulando...")

while True:
    valor = pot.read()
    angulo = int((valor / 1023) * 180)
    set_servo_angle(angulo)
    
    lcd.move_to(0, 1)
    lcd.putstr("Angulo: {:3d}".format(angulo))

    sleep(0.2)