I am trying to simplify writing my embedded programs in C. How would I create a macro that could accept any number or parameters up from 2?
I currently have this:
#define P_PIN_HIGH(pin_letter, pin_num) (PORT##pin_letter |= (1 << pin_num))
#define PIN_HIGH(...) P_PIN_HIGH(__VA_ARGS__)
And I can use this like so:
#define PIN_TEST A, 0 // Every pin gets defined this way.
PIN_HIGH(PIN_TEST); // Set this pin to high.
But, I would like to be able to pass any number of pins(they must have the same letter) to the macro, like so:
#define PIN_TEST A, 0
#define PIN_TEST1 A, 1
#define PIN_TEST2 A, 2
PIN_HIGH(PIN_TEST, PIN_TEST1, PIN_TEST2);
So, the compiled code, would look like this:
PORTA |= ((1<<0) | (1<<1) | (1<<2));
Is this doable?
#define MACRO(A, B, ...)and then inside the macro the part corresponding to...is obtained, again, with__VA_ARGS__.#define MACRO(PIN1, __VA_ARGS__).