user12039: I was looking at some macro use in an Arduino code. I believe what this macro does is it expands into a state that basically sets a pin to either input or output, but I'm a little confused about everything going on in this macro.
I do understand the point behind the do{...} while(0), but I'm sure what the ## are accomplishing. Also, what if anything do the underscores before DDR and PIN do? I would very appreciative of a simple clarification. Thank you!
#define MASK(PIN)  (1 << PIN)

/// set pin as input
#define _SET_INPUT(IO) do {DIO ##  IO ## _DDR &= ~MASK(DIO ## IO ## _PIN); } while (0)
/// set pin as output
#define _SET_OUTPUT(IO) do {DIO ##  IO ## _DDR |=  MASK(DIO ## IO ## _PIN); } while (0)


user4143: ## is the concatenation operator in macros. It joins two macros or pieces of text together when expanded.
#define _SET_INPUT(IO) do {DIO ##  IO ## _DDR &= ~MASK(DIO ## IO ## _PIN); } while (0)

_SET_INPUT(3);

woukd expand to:
do {DIO3_DDR &= ~MASK(DIO3_PIN); } while (0);

You can read more about how it works here: https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

12039: <p>I was looking at some macro use in an Arduino code. I believe what this macro does is it expands into a state that basically sets a pin to either input or output, but I'm a little confused about everything going on in this macro.</p>

<p>I do understand the point behind the do{...} while(0), but I'm sure what the ## are accomplishing. Also, what if anything do the underscores before DDR and PIN do? I would very appreciative of a simple clarification. Thank you!</p>

<pre><code>#define MASK(PIN)  (1 &lt;&lt; PIN)

/// set pin as input
#define _SET_INPUT(IO) do {DIO ##  IO ## _DDR &amp;= ~MASK(DIO ## IO ## _PIN); } while (0)
/// set pin as output
#define _SET_OUTPUT(IO) do {DIO ##  IO ## _DDR |=  MASK(DIO ## IO ## _PIN); } while (0)
</code></pre>
 4143: <p><code>##</code> is the concatenation operator in macros. It joins two macros or pieces of text together when expanded.</p>

<pre><code>#define _SET_INPUT(IO) do {DIO ##  IO ## _DDR &amp;= ~MASK(DIO ## IO ## _PIN); } while (0)

_SET_INPUT(3);
</code></pre>

<p>woukd expand to:</p>

<pre><code>do {DIO3_DDR &amp;= ~MASK(DIO3_PIN); } while (0);
</code></pre>

<p>You can read more about how it works here: <a href="https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html">https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html</a></p>
