from iowp.factory import IowFactory
from iowp.lcd import LCD2x16

def main():
    factory = IowFactory.get_instance()
    if len(factory.get_devices()) == 0:
        print("No IO-Warrior devices found.")
        return

    iow_dev = factory.get_iow_device(0)

    lcd = LCD2x16()
    iow_dev.add_special_mode_function_impl(lcd)

    print(iow_dev)

    try:
        print("Clearing LCD...")
        lcd.clear_lcd()

        print("Writing lines...")
        lcd.write_line(1, True, "Hello iowp!")
        lcd.write_line(2, True, "LCD 2x16 Ready")

        print("Testing special characters (smiley)...")
        smiley = [
            0b00000,
            0b01010,
            0b01010,
            0b00000,
            0b10001,
            0b01110,
            0b00000,
            0b00000
        ]
        lcd.set_special_char(0, smiley)
        lcd.set_cursor(1, 16)
        lcd.write_string(chr(0))
     
       
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()
