wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
463 stars 73 forks source link

The right way to detect when the TX Led is ON or OFF #48

Closed tawjaw closed 4 years ago

tawjaw commented 4 years ago

Hi @urish How can I detect when the TX Led is ON or OFF? My guess is that whenever there's transmission on USART then the LED should be on. But how to switch it off when there's nothing being transmitted?

urish commented 4 years ago

Yes, the recommended method is to listen for onByteTransmit callback of the AVRUSART instance. Whenever you get a byte, you switch the LED on for the next browser frame. You could use a flag that will be set to true in onByteTransmit, then check it and turn the LED on in requestAnimationFrame iff that flag is true, and set the flag to false so that next animation frame will turn the LED off (unless there's been another onByteTransmit call in the interim).

Ideally, you could use the baud rate (as outlined in #16) to calculate the exact duty cycle of the LED (each byte would be about 10 symbols, and you can derive the time-per-symbol from the baud rate), but I believe for most purposes briefly turning it on whenever there is a new byte would suffice.

tawjaw commented 3 years ago

Thanks @urish I just saw this for some reason!

urish commented 3 years ago

You're welcome :-)