Trying to understand while(count- -) {}

I have some questions about this function which is in SoftwareSerial.cpp and can be found here on github:

inline void DebugPulse(uint8_t pin, uint8_t count)
{
  volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));

  uint8_t val = *pport;
  while (count--)
  {
    *pport = val | digitalPinToBitMask(pin);
    *pport = val;
  }
}
  1. What is an inline?
  2. Won't the while loop continue indefinitely? If not, why? I realize that each time it loops, the value of count will be decremented by 1, but then it seems to me that the loop should run until the limit has been reached as to the lowest value permitted for the variable count at which point, it should throw a runtime error (if those were possible in arduino's that is) .

Actually I think I just figured out #2 ... the while condition only runs if the test is greater than zero?

'inline' is a hint to the compiler that the function is not used often so it can be inserted in the few places it is used rather than being called like a function.

The 'while' will stop when count reaches zero (== false). If count starts at 5, for example, the loop will run for count = 5, 4, 3, 2, and 1. Since the decrement (--) is after the value is tested, inside the loop I think you will see the values of 4, 3, 2, 1, and 0 for count.

Tells the compiler, add the compiled as individual lines of code, hence, they are placed in line with other code.


count- -
Decrement the count variable, when 0 it will be false.

Thank you, yes that makes sense ... I don't know why that wasn't obvious to me right away.

And inline ... that's interesting ... so what would be the net effect of declaring that function with just void vs inline void?

Runs if != 0. Any non-zero value converts to te boolean 'true' and zero converts to the boolean 'false'. Any "while (integerExpression)" is equivalent to "while ((integerExpression) != 0)"

The compiler expands this function directly to the caller instead of calling it.
This reduces function call overhead and increases execution speed, but increases the size of ROM consumed may increase.

In C, only 0 represents False and the others evaluate to True.
Also, 1 byte variable is subtracting 1 from 0x00 gives 0xFF.
(0b00000000 - 1 = 0b11111111)
This is 255 when interpreted as an unsigned integer.
If you continue more subtracting 1 from here, finaly it will become 0 and you will exit While.

OK I didn't know that ... so if the while condition is less than zero it would also run? so if the function was called with say ... pin 4 and count = -1 ... then the loop would run indefinitely?

-1 in a 1 byte signed variable is 0xFF.
So it ’s the same as my explanation above.
Think in hexadecimal.

Depends on the compiler. A smart compiler might see that a function is only called once and make it inline without the hint. Another compiler might ignore the 'inline' hint and decide to make the function callable.

OK, this is where I always go south when dealing with C ... you have to think in binary often in order to understand it.

Thank you for the answers, they make perfect sense.

Depends on the size of 'count'. If its an 8-bit integer it would count from -1 down to -128 and then roll over to 127, and then count down to 0. If its a 16-bit 'int' it would count down to -32768 and then roll over to 32767, and then count down to 0. No matter how big, if you count down long enough you will reach 0.

In terms of using this function for debugging ... the comments on this function say that you can use it for example to look at the signal on a scope ... but wouldn't even a large number for count cause this function to run rather quickly? I suppose you'd have to set up your scope to snap shot it.

The pulses would be going by at a few megahertz. If you set your scope to trigger you can capture and count the pulses to see where in the code you are.

Still is amazing to me that we can purchase electronics that run at those speeds for a couple of bucks.

Thanks again

The fastest pulse that a 16MHz Arduino can produce is 8MHz.
What the code produces is always slower than that.
This is a signal that can be sufficiently observed with an oscilloscope with some performance.

Also, it is desirable that debug signals don't affect the main program as much as possible.
Therefore, it uses the maximum performance of the processor and minimizes the CPU cost.

Could you give me a scenario where this function would be useful? I've got zero experience debugging serial communications and have never found myself needing to do so in spite of using serial communications for decades. But I am curious...

Im thinking about setting up the scope just to see this in action.

A short pulse can be used to trigger a logic analyzer to capture parallel outside data at time specific code.

Probably, Not a serial communication debug for users, developer debug of how well the library is working.
This may have been prepared by the developer to test the library.

In software serial communication, only the code controls the timing of the signal.
Using some techniques for achieve operate high baud rate.
I think use this signal to debug for optimize code.

It runs while tested expression returns true. 0 is false, any other number including negative is true