Last active
December 17, 2019 15:02
-
-
Save ElectricImpSampleCode/fc0130fb5d6b55b03e86 to your computer and use it in GitHub Desktop.
Electric Imp imp API uart.flush() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assign parallel aliases for the UART according to role | |
arduino <- hardware.uart57; | |
computer <- hardware.uart57; | |
function relay() { | |
// UART initially configured for Arduino input | |
local byte = arduino.read(); | |
if (byte != -1) { | |
// We have a valid byte to relay so reconfigure the UART | |
// for output to the computer. Remember: disable arduino first | |
server.log("Switching UART to computer..."); | |
arduino.disable(); | |
// Configure UART for computer connection and write byte | |
computer.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS); | |
computer.write(byte); | |
// With the byte sent to the computer, reassign the UART | |
// back to the data source, Arduino, and await next byte | |
server.log("Switching UART to Arduino"); | |
// Flush the FIFO first to make sure the byte has been sent | |
// before we disable the computer link | |
computer.flush(); | |
computer.disable(); | |
// Configure the UART to read from Arduino | |
arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, relay); | |
} | |
} | |
// Initialize by starting to read from Arduino | |
server.log("Switching UART to Arduino"); | |
arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, relay); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment