"""
This is a simple program that runs in a loop, looking for character data over the
UART0 line which happens to be hooked up to the MicroUSB to UART circuit on these
dev kits.
"""
import os
import random
import serial
import time

ESP32_PORT= '/dev/cu.SLAB_USBtoUART' if os.uname().sysname == "Darwin" else "/dev/ttyUSB0"
SENTENCES = [
    "The little green frog hopped to the pond.\n",
    "The cheese was late to the party.\n",
    "Don't let your dreams just be dreams.\n"
]


def main():
    print(f"Connecting to {ESP32_PORT}....")
    try:
        with serial.Serial(ESP32_PORT, 115200, timeout=1) as serial_conn:
            while True:
                if serial_conn.is_open:
                    serial_conn.write(random.choice(SENTENCES).encode('ascii'))
                    time.sleep(0.5)
                    if serial_conn.in_waiting:
                        print(f"Found {serial_conn.in_waiting} bytes waiting in input buffer!")
                        print(serial_conn.readlines())
                    else:
                        print("No data received.")
    except KeyboardInterrupt:
        print("Exiting program")

    exit(0)

if __name__ == "__main__":
    main()
