Created
February 25, 2021 16:31
-
-
Save Avotrix/abebad5ebe5bc5f2f449ae8dc2a7d669 to your computer and use it in GitHub Desktop.
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
// Load Mongoose OS API | |
load('api_timer.js'); | |
load('api_uart.js'); | |
load('api_sys.js'); | |
load('api_gpio.js'); | |
// Uart number used for this example | |
let uartNo = 0; | |
// Accumulated Rx data, will be echoed back to Tx | |
let rxAcc = ""; | |
let pin=16; | |
GPIO.set_mode(pin, GPIO.MODE_OUTPUT); | |
let value = false; | |
// Configure UART at 115200 baud | |
UART.setConfig(uartNo, { | |
baudRate: 115200, | |
}); | |
// Set dispatcher callback, it will be called whenver new Rx data or space in | |
// the Tx buffer becomes available | |
UART.setDispatcher(uartNo, function(uartNo, ud) { | |
let ra = UART.readAvail(uartNo); | |
if (ra > 0) { | |
// Received new data: print it immediately to the console, and also | |
// accumulate in the "rxAcc" variable which will be echoed back to UART later | |
print("_____________________________________"); | |
let data = UART.read(uartNo); | |
GPIO.toggle(pin); | |
print("Received UART data:", data); | |
rxAcc += data; | |
} | |
}, null); | |
// Enable Rx | |
UART.setRxEnabled(uartNo, true); | |
// Send UART data every second | |
Timer.set(10000 /* milliseconds */, true /* repeat */, function() { | |
print("--------_________------_________"); | |
value = !value; | |
UART.write( | |
uartNo, | |
"Hello UART! "+ "\n" | |
); | |
rxAcc = ""; | |
}, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment