From 4b3c6fee7b401135e3195a1a4ea398579059f99a Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Mon, 5 Dec 2016 12:30:00 -0800 Subject: [PATCH] Overload DEFINE_HEATER to take variable arguments DEFINE_HEATER used to take 3 arguments. Today it takes 4. Soon it might take 5. The transition from 3 to 4 was painful while old config files had not caught up to the new parameters. Let's avoid the pain again in the future by making this macro overloadable to accept any correct number of arguments while doing the right thing. Also let's accept 5 or 6 parameters so new configs will work with today's "older" code. --- config_wrapper.h | 17 +++++++++++++++-- heater-avr.c | 12 ++++++------ heater-lpc.c | 12 ++++++------ heater-stm32.c | 12 ++++++------ heater.h | 6 +++--- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/config_wrapper.h b/config_wrapper.h index f52193e..137957b 100644 --- a/config_wrapper.h +++ b/config_wrapper.h @@ -7,10 +7,23 @@ */ #include "arduino.h" -#ifndef DEFINE_HEATER - #define DEFINE_HEATER(...) +#ifndef DEFINE_HEATER_ACTUAL + #define DEFINE_HEATER_ACTUAL(...) #endif +// Heater definition helpers. This collection of macros turns any DEFINE_HEATER(...) macro +// expansion into a DEFINE_HEATER_ACTUAL(a,b,c,d), i.e. a macro with four arguments. Currently +// we expand 3 to 6 arguments, but we ignore the 6th arg in this code. This allows +// us to use this "old" code with some "newer" configs which come at us from the future, so long +// as the newer configs add parameters at the end of the list rather than in the middle. +// From http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments +#define GET_MACRO(_1, _2, _3, _4, _5, _6, NAME, ...) NAME +#define DEFINE_HEATER(...) GET_MACRO(__VA_ARGS__, DHTR_6, DHTR_5, DHTR_4, DHTR_3)(__VA_ARGS__) +#define DHTR_3(name, pin, pwm) DEFINE_HEATER_ACTUAL(name, pin, 0, pwm, 100) +#define DHTR_4(name, pin, invert, pwm) DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, 100) +#define DHTR_5(name, pin, invert, pwm, max_pwm) DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, max_pwm) +#define DHTR_6(name, pin, invert, pwm, max_pwm, arg6) DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, max_pwm) + /** This wrapper config header is used to allow makefiles and test scripts to replace or augment the user's 'config.h' file in a controlled manner. A diff --git a/heater-avr.c b/heater-avr.c index 7bc8a0d..aa09664 100644 --- a/heater-avr.c +++ b/heater-avr.c @@ -32,9 +32,9 @@ typedef struct { #define HEATER_MAX_VALUE(dummy) #endif -#undef DEFINE_HEATER +#undef DEFINE_HEATER_ACTUAL /// \brief helper macro to fill heater definition struct from config.h -#define DEFINE_HEATER(name, pin, invert, pwm, max_value) { \ +#define DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, max_value) { \ &(pin ## _WPORT), \ pin ## _PIN, \ invert ? 1 : 0, \ @@ -45,7 +45,7 @@ static const heater_definition_t heaters[NUM_HEATERS] = { #include "config_wrapper.h" }; -#undef DEFINE_HEATER +#undef DEFINE_HEATER_ACTUAL /// \brief initialise heater subsystem @@ -208,12 +208,12 @@ void heater_init() { } // set all heater pins to output - #undef DEFINE_HEATER - #define DEFINE_HEATER(name, pin, invert, pwm, max_value) \ + #undef DEFINE_HEATER_ACTUAL + #define DEFINE_HEATER_ACTUAL(name, pin, invert, ...) \ SET_OUTPUT(pin); \ WRITE(pin, invert ? 1 : 0); #include "config_wrapper.h" - #undef DEFINE_HEATER + #undef DEFINE_HEATER_ACTUAL pid_init(); } diff --git a/heater-lpc.c b/heater-lpc.c index 46736d0..2b1fd30 100644 --- a/heater-lpc.c +++ b/heater-lpc.c @@ -83,8 +83,8 @@ typedef struct { #define HEATER_MAX_VALUE(dummy) #endif -#undef DEFINE_HEATER -#define DEFINE_HEATER(name, pin, invert, pwm, max_value) \ +#undef DEFINE_HEATER_ACTUAL +#define DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, max_value) \ { \ { pwm && pin ## _TIMER ? \ &(pin ## _TIMER->MR[pin ## _MATCH]) : \ @@ -96,7 +96,7 @@ typedef struct { static const heater_definition_t heaters[NUM_HEATERS] = { #include "config_wrapper.h" }; -#undef DEFINE_HEATER +#undef DEFINE_HEATER_ACTUAL /** Initialise heater subsystem. @@ -143,8 +143,8 @@ void heater_init() { PIO1_9 CT16B1_MAT0 0x1 --- */ // Auto-generate pin setup. - #undef DEFINE_HEATER - #define DEFINE_HEATER(name, pin, invert, pwm, max_value) \ + #undef DEFINE_HEATER_ACTUAL + #define DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, max_value) \ if (pwm && pin ## _TIMER) { \ uint32_t freq; \ \ @@ -184,7 +184,7 @@ void heater_init() { WRITE(pin, invert ? 1 : 0); \ } #include "config_wrapper.h" - #undef DEFINE_HEATER + #undef DEFINE_HEATER_ACTUAL pid_init(); } diff --git a/heater-stm32.c b/heater-stm32.c index 81aad6c..5551c5c 100644 --- a/heater-stm32.c +++ b/heater-stm32.c @@ -71,8 +71,8 @@ typedef struct { uint8_t uses_pwm; } heater_definition_t; -#undef DEFINE_HEATER -#define DEFINE_HEATER(name, pin, invert, pwm) \ +#undef DEFINE_HEATER_ACTUAL +#define DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, ...) \ { \ { pwm && pin ## _TIMER ? \ &(pin ## _TIMER-> EXPANDER(CCR, pin ## _CHANNEL,)) : \ @@ -83,7 +83,7 @@ typedef struct { static const heater_definition_t heaters[NUM_HEATERS] = { #include "config_wrapper.h" }; -#undef DEFINE_HEATER +#undef DEFINE_HEATER_ACTUAL /** Initialise heater subsystem. @@ -154,8 +154,8 @@ void heater_init() { */ // Auto-generate pin setup. - #undef DEFINE_HEATER - #define DEFINE_HEATER(name, pin, invert, pwm) \ + #undef DEFINE_HEATER_ACTUAL + #define DEFINE_HEATER_ACTUAL(name, pin, invert, pwm, ...) \ if (pwm && pin ## _TIMER) { \ uint32_t freq; \ uint8_t macro_mask; \ @@ -208,7 +208,7 @@ void heater_init() { } #include "config_wrapper.h" - #undef DEFINE_HEATER + #undef DEFINE_HEATER_ACTUAL pid_init(); } diff --git a/heater.h b/heater.h index c78af97..da5b29a 100644 --- a/heater.h +++ b/heater.h @@ -29,15 +29,15 @@ #endif -#undef DEFINE_HEATER -#define DEFINE_HEATER(name, pin, invert, pwm, ...) HEATER_ ## name, +#undef DEFINE_HEATER_ACTUAL +#define DEFINE_HEATER_ACTUAL(name, ...) HEATER_ ## name, typedef enum { #include "config_wrapper.h" NUM_HEATERS, HEATER_noheater } heater_t; -#undef DEFINE_HEATER +#undef DEFINE_HEATER_ACTUAL /** This struct holds the runtime heater data.