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.
This commit is contained in:
Phil Hord 2016-12-05 12:30:00 -08:00 committed by Nico Tonnhofer
parent ab2c355821
commit 4b3c6fee7b
5 changed files with 36 additions and 23 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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.