#************************************
# Raspberry Pi 5 UART Example
# Written for Design News
# Written by: Fred Eady
# Last Update: 08-20-2024
# Notes:
#************************************
# TXD = GPIO 14
# RXD = GPIO 15
#************************************

import serial
import time

# Define the LED ON and OFF commands
cmdA = b"A" #ON
cmdB = b"B" #OFF

# Open the Pi 5 serial port
serialPort = serial.Serial("/dev/ttyAMA0", 115200)
print("Serial Example..")
# Send command LED ON and wait for response
print("Sending LED ON Command..")
serialPort.write(cmdA)
responseON = serialPort.read(2)
if responseON == b'ON':
    print("Response from NUCLEO-G031K8 -> LED is ON\r\n")
else:
    print("Unrecognized Response from NUCLEO=G031K8!\r\n")

print("Executing 5 second delay..\r\n")
time.sleep(5)

# Send command LED OFF and wait for response
print("Sending LED OFF Command..")
serialPort.write(cmdB)
responseOFF = serialPort.read(3)
if responseOFF == b'OFF':
    print("Response from NUCLEO-G031K8 -> LED is OFF\r\n")
else:
    print("Unrecognized Response from NUCLEO=G031K8!\r\n")

