2

A lot of hardware comes with a Tx/Rx status LED, but what if your hardware doesn't and you want to see if data is transmitting? If you just set the LED to the value of the line it may go on and off before you even see it (<1/100th of a sec.).

My question is, how do I write an interrupt driven function to drive the LED state? I've searched the internet and found nothing and I could use a counter with a modulus, but that seems clunky. Any other ideas?

PS - I'd like to use this in either Arduino or Mbed, but I doubt it makes a difference to the question as to which one...

tshepang
  • 12,111
  • 21
  • 91
  • 136
Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
  • 2
    So your basic problem is that you want to set a minimum on-time for the LED. Do you also want to ensure that it then stays off for a certain period of time so there is a sense of "motion," or would you prefer that the LED stay on continuously so long as another character was transmitted before the minimum on-time expired? As a side-note, a minimum on-time is now considered a good security practice, as there was actually research a few years ago into sniffing network traffic by recording the lights on switch panels. – Sam Skuce Feb 25 '13 at 23:17
  • Wow I had no idea, but an "ideal" situation would be when traffic occurs LED = !LED every .1 sec or so – Eric Fossum Feb 25 '13 at 23:25
  • Actually, to be sure it's an actual ON/OFF signal, maybe I want it to do something else. I'll be honest, I don't know what's best... You seem knowledgeable, what do you recommend? – Eric Fossum Feb 25 '13 at 23:27
  • @SamSkuce: have you got a reference to said traffic sniffing? Sounds interesting! – Martin Thompson Feb 26 '13 at 10:08
  • @MartinThompson, http://it.slashdot.org/story/02/03/06/1221224/led-lights-friend-or-foe is the Slashdot story from 2002 (I'm amazed I remembered something that long ago), and http://applied-math.org/optical_tempest.pdf is the paper. – Sam Skuce Feb 26 '13 at 14:51

2 Answers2

6
void receive_or_transmit_interrupt()
{
     g_traffic = true;
     /* other stuff. */
}

void timer_that_fires_every_100_milliseconds()
{
     if ( led == ON)
     {
          led = OFF;
          g_traffic = false;
     }
     else if ( g_traffic )
     {
          led = ON;
     }
}

If you don't want the timer to always be firing even when there's not traffic, you could change the receive_or_transmit_interrupt to enable the timer, and the timer could disable itself when it turns off the LED.

Sam Skuce
  • 1,666
  • 14
  • 20
  • Ahh okay, So this will toggle every .1 sec only if there's traffic (1->0, 0->1) and not toggle if steady (1->1). – Eric Fossum Feb 25 '13 at 23:40
  • Right. It also assumes that nothing else is using that LED, and it kind of assumes that the LED begins in the 'OFF' state, although if that assumption was wrong, it would fix itself after the first traffic indicator. – Sam Skuce Feb 25 '13 at 23:42
  • Well hey, I appreciate the coding head start! I just couldn't visualize the end result. – Eric Fossum Feb 26 '13 at 00:12
  • This seams incomplete as it does not address how to implement the said interrupts. Where I can see using ISR(UART_RX_vect) and ISR(UART_TX_vect), except they are only double buffered(small). So they would only trip quickly and once, on the completion of the initial byte. Or is that it TX's UDRn buffer is filled between, ready for next and completly empty. After UART_UDRE_vect but before UART_TX_vect. This would not be the case for Rx. Also the interrupts(); is needed to enable the ISR() vectors. – mpflaga Feb 26 '13 at 20:59
  • @mpflaga, the OP didn't ask how to implement interrupts, he was asking for a general algorithm to implement a minimum on-time for a traffic LED - Imagine my code as functions called from whatever interrupt handlers he implemented. He also said this may be on an Mbed or Arduino, so whose interrupt syntax and handling would I use anyway? – Sam Skuce Feb 26 '13 at 22:23
  • With Arduino 0.22 and greater the core library began using interrupts for the Serial Rx and Tx. So defining the Rx and Tx vectors elsewhere in a project would break the existing vectors for the Serial object, if not have a linker error. The above would need to be merged into the existing vectors in HardwareSerial.cpp, as to maintain original functionality. Granted this is a bit specific. But shows care is needed to work within the existing eco's. – mpflaga Feb 27 '13 at 15:42
1

A simple way is to switch the LED on in the Tx/Rx interrupt and initiate a timer of say 200ms (long enough to perceive). You then switch the LED off in the timer ISR.

That way the indicator is extinguished 200 ms after the last tx/rx activity. If the activity is sustained, the indicator will remain illuminated.

If the tx/rx bursts are intermittent with greater than 200ms gaps, the indicator will flicker. So the states of off, on and flickering give a broad indication of the data activity.

Clifford
  • 88,407
  • 13
  • 85
  • 165