Skip to content

Commit

Permalink
Apply REPEAT, RREPEAT, and loop macros (#16757)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored Feb 2, 2020
1 parent 0e72c31 commit 4716dac
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 221 deletions.
18 changes: 18 additions & 0 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,21 @@
// Repeat a macro passing 0...N-1 plus additional arguments.
#define REPEAT2_S(S,N,OP,V...) EVAL(_REPEAT2(S,SUB##S(N),OP,V))
#define REPEAT2(N,OP,V...) REPEAT2_S(0,N,OP,V)

// Use RREPEAT macros with REPEAT macros for nesting
#define _RREPEAT(_RPT_I,_RPT_N,_RPT_OP) \
_RPT_OP(_RPT_I) \
IF_ELSE(SUB1(_RPT_N)) \
( DEFER2(__RREPEAT)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP) ) \
( /* Do nothing */ )
#define __RREPEAT() _RREPEAT
#define _RREPEAT2(_RPT_I,_RPT_N,_RPT_OP,V...) \
_RPT_OP(_RPT_I,V) \
IF_ELSE(SUB1(_RPT_N)) \
( DEFER2(__RREPEAT2)()(ADD1(_RPT_I),SUB1(_RPT_N),_RPT_OP,V) ) \
( /* Do nothing */ )
#define __RREPEAT2() _RREPEAT2
#define RREPEAT_S(S,N,OP) EVAL(_RREPEAT(S,SUB##S(N),OP))
#define RREPEAT(N,OP) RREPEAT_S(0,N,OP)
#define RREPEAT2_S(S,N,OP,V...) EVAL(_RREPEAT2(S,SUB##S(N),OP,V))
#define RREPEAT2(N,OP,V...) RREPEAT2_S(0,N,OP,V)
48 changes: 10 additions & 38 deletions Marlin/src/feature/runout.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,56 +139,28 @@ class FilamentSensorBase {
#define INIT_RUNOUT_PIN(P) SET_INPUT(P)
#endif

INIT_RUNOUT_PIN(FIL_RUNOUT_PIN);
#if NUM_RUNOUT_SENSORS > 1
INIT_RUNOUT_PIN(FIL_RUNOUT2_PIN);
#if NUM_RUNOUT_SENSORS > 2
INIT_RUNOUT_PIN(FIL_RUNOUT3_PIN);
#if NUM_RUNOUT_SENSORS > 3
INIT_RUNOUT_PIN(FIL_RUNOUT4_PIN);
#if NUM_RUNOUT_SENSORS > 4
INIT_RUNOUT_PIN(FIL_RUNOUT5_PIN);
#if NUM_RUNOUT_SENSORS > 5
INIT_RUNOUT_PIN(FIL_RUNOUT6_PIN);
#endif
#endif
#endif
#endif
#endif
#define _INIT_RUNOUT(N) INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN);
REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _INIT_RUNOUT)
#undef _INIT_RUNOUT
}

// Return a bitmask of runout pin states
static inline uint8_t poll_runout_pins() {
return (
(READ(FIL_RUNOUT_PIN ) ? _BV(0) : 0)
#if NUM_RUNOUT_SENSORS > 1
| (READ(FIL_RUNOUT2_PIN) ? _BV(1) : 0)
#if NUM_RUNOUT_SENSORS > 2
| (READ(FIL_RUNOUT3_PIN) ? _BV(2) : 0)
#if NUM_RUNOUT_SENSORS > 3
| (READ(FIL_RUNOUT4_PIN) ? _BV(3) : 0)
#if NUM_RUNOUT_SENSORS > 4
| (READ(FIL_RUNOUT5_PIN) ? _BV(4) : 0)
#if NUM_RUNOUT_SENSORS > 5
| (READ(FIL_RUNOUT6_PIN) ? _BV(5) : 0)
#endif
#endif
#endif
#endif
#endif
);
#define _OR_RUNOUT(N) | (READ(FIL_RUNOUT##N##_PIN) ? _BV((N) - 1) : 0)
return (0 REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _OR_RUNOUT));
#undef _OR_RUNOUT
}

// Return a bitmask of runout flag states (1 bits always indicates runout)
static inline uint8_t poll_runout_states() {
return poll_runout_pins() ^ uint8_t(
return (poll_runout_pins()
#if DISABLED(FIL_RUNOUT_INVERTING)
_BV(NUM_RUNOUT_SENSORS) - 1
#else
0
^ uint8_t(_BV(NUM_RUNOUT_SENSORS) - 1)
#endif
);
}

#undef INIT_RUNOUT_PIN
};

#if ENABLED(FILAMENT_MOTION_SENSOR)
Expand Down
9 changes: 2 additions & 7 deletions Marlin/src/gcode/calibrate/M666.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,8 @@
SERIAL_ECHOPAIR(" Y2:", endstops.y2_endstop_adj);
#endif
#if ENABLED(Z_MULTI_ENDSTOPS)
SERIAL_ECHOPAIR(" Z2:", endstops.z2_endstop_adj);
#if NUM_Z_STEPPER_DRIVERS >= 3
SERIAL_ECHOPAIR(" Z3:", endstops.z3_endstop_adj);
#if NUM_Z_STEPPER_DRIVERS >= 4
SERIAL_ECHOPAIR(" Z4:", endstops.z4_endstop_adj);
#endif
#endif
#define _ECHO_ZADJ(N) SERIAL_ECHOPAIR(" Z" STRINGIFY(N) ":", endstops.z##N##_endstop_adj);
REPEAT_S(2, INCREMENT(NUM_Z_STEPPER_DRIVERS), _ECHO_ZADJ)
#endif
SERIAL_EOL();
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/config/M301.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void GcodeSuite::M301() {
SERIAL_ECHO_START();
#if ENABLED(PID_PARAMS_PER_HOTEND)
SERIAL_ECHOPAIR(" e:", e); // specify extruder in serial output
#endif // PID_PARAMS_PER_HOTEND
#endif
SERIAL_ECHOPAIR(" p:", PID_PARAM(Kp, e),
" i:", unscalePID_i(PID_PARAM(Ki, e)),
" d:", unscalePID_d(PID_PARAM(Kd, e)));
Expand Down
54 changes: 9 additions & 45 deletions Marlin/src/lcd/menu/menu_advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,35 +228,17 @@ void menu_cancelobject();
#if ENABLED(PID_AUTOTUNE_MENU)
#define DEFINE_PIDTEMP_FUNCS(N) \
_DEFINE_PIDTEMP_BASE_FUNCS(N); \
void lcd_autotune_callback_E##N() { _lcd_autotune(N); } //
void lcd_autotune_callback_E##N() { _lcd_autotune(N); }
#else
#define DEFINE_PIDTEMP_FUNCS(N) _DEFINE_PIDTEMP_BASE_FUNCS(N); //
#define DEFINE_PIDTEMP_FUNCS(N) _DEFINE_PIDTEMP_BASE_FUNCS(N);
#endif

#if HOTENDS
DEFINE_PIDTEMP_FUNCS(0);
#if HOTENDS > 1 && ENABLED(PID_PARAMS_PER_HOTEND)
DEFINE_PIDTEMP_FUNCS(1);
#if HOTENDS > 2
DEFINE_PIDTEMP_FUNCS(2);
#if HOTENDS > 3
DEFINE_PIDTEMP_FUNCS(3);
#if HOTENDS > 4
DEFINE_PIDTEMP_FUNCS(4);
#if HOTENDS > 5
DEFINE_PIDTEMP_FUNCS(5);
#if HOTENDS > 6
DEFINE_PIDTEMP_FUNCS(6);
#if HOTENDS > 7
DEFINE_PIDTEMP_FUNCS(7);
#endif // HOTENDS > 7
#endif // HOTENDS > 6
#endif // HOTENDS > 5
#endif // HOTENDS > 4
#endif // HOTENDS > 3
#endif // HOTENDS > 2
#endif // HOTENDS > 1 && PID_PARAMS_PER_HOTEND
#endif // HOTENDS
REPEAT_S(1, HOTENDS, DEFINE_PIDTEMP_FUNCS)
#endif
#endif

#define SHOW_MENU_ADVANCED_TEMPERATURE ((ENABLED(AUTOTEMP) && HAS_TEMP_HOTEND) || EITHER(PID_AUTOTUNE_MENU, PID_EDIT_MENU))

Expand Down Expand Up @@ -320,33 +302,15 @@ void menu_cancelobject();
#if ENABLED(PID_AUTOTUNE_MENU)
#define PID_EDIT_MENU_ITEMS(N) \
_PID_EDIT_MENU_ITEMS(N); \
EDIT_ITEM_FAST_N(int3, N, MSG_PID_AUTOTUNE_E, &autotune_temp[N], 150, heater_maxtemp[N] - 15, []{ _lcd_autotune(MenuItemBase::itemIndex); })
EDIT_ITEM_FAST_N(int3, N, MSG_PID_AUTOTUNE_E, &autotune_temp[N], 150, heater_maxtemp[N] - 15, []{ _lcd_autotune(MenuItemBase::itemIndex); });
#else
#define PID_EDIT_MENU_ITEMS(N) _PID_EDIT_MENU_ITEMS(N)
#define PID_EDIT_MENU_ITEMS(N) _PID_EDIT_MENU_ITEMS(N);
#endif

PID_EDIT_MENU_ITEMS(0);
#if HOTENDS > 1 && ENABLED(PID_PARAMS_PER_HOTEND)
PID_EDIT_MENU_ITEMS(1);
#if HOTENDS > 2
PID_EDIT_MENU_ITEMS(2);
#if HOTENDS > 3
PID_EDIT_MENU_ITEMS(3);
#if HOTENDS > 4
PID_EDIT_MENU_ITEMS(4);
#if HOTENDS > 5
PID_EDIT_MENU_ITEMS(5);
#if HOTENDS > 6
PID_EDIT_MENU_ITEMS(6);
#if HOTENDS > 7
PID_EDIT_MENU_ITEMS(7);
#endif // HOTENDS > 7
#endif // HOTENDS > 6
#endif // HOTENDS > 5
#endif // HOTENDS > 4
#endif // HOTENDS > 3
#endif // HOTENDS > 2
#endif // HOTENDS > 1 && PID_PARAMS_PER_HOTEND
REPEAT_S(1, HOTENDS, PID_EDIT_MENU_ITEMS)
#endif

END_MENU();
}
Expand Down
86 changes: 24 additions & 62 deletions Marlin/src/module/configuration_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,7 @@ void MarlinSettings::reset() {
false
#endif
;
for (uint8_t q = 0; q < COUNT(planner.filament_size); q++)
LOOP_L_N(q, COUNT(planner.filament_size))
planner.filament_size[q] = DEFAULT_NOMINAL_FILAMENT_DIA;

#endif
Expand Down Expand Up @@ -2730,7 +2730,7 @@ void MarlinSettings::reset() {
//

#if ENABLED(ADVANCED_PAUSE_FEATURE)
for (uint8_t e = 0; e < EXTRUDERS; e++) {
LOOP_L_N(e, EXTRUDERS) {
fc_settings[e].unload_length = FILAMENT_CHANGE_UNLOAD_LENGTH;
fc_settings[e].load_length = FILAMENT_CHANGE_FAST_LOAD_LENGTH;
}
Expand Down Expand Up @@ -2842,36 +2842,17 @@ void MarlinSettings::reset() {
SERIAL_ECHOLNPGM(" Disabled");
}

CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 D", LINEAR_UNIT(planner.filament_size[0]));
#if EXTRUDERS > 1
#if EXTRUDERS == 1
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T1 D", LINEAR_UNIT(planner.filament_size[1]));
#if EXTRUDERS > 2
SERIAL_ECHOLNPAIR(" M200 D", LINEAR_UNIT(planner.filament_size[0]));
#elif EXTRUDERS
LOOP_L_N(i, EXTRUDERS) {
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T2 D", LINEAR_UNIT(planner.filament_size[2]));
#if EXTRUDERS > 3
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T3 D", LINEAR_UNIT(planner.filament_size[3]));
#if EXTRUDERS > 4
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T4 D", LINEAR_UNIT(planner.filament_size[4]));
#if EXTRUDERS > 5
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T5 D", LINEAR_UNIT(planner.filament_size[5]));
#if EXTRUDERS > 6
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T6 D", LINEAR_UNIT(planner.filament_size[6]));
#if EXTRUDERS > 7
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(" M200 T7 D", LINEAR_UNIT(planner.filament_size[7]));
#endif // EXTRUDERS > 7
#endif // EXTRUDERS > 6
#endif // EXTRUDERS > 5
#endif // EXTRUDERS > 4
#endif // EXTRUDERS > 3
#endif // EXTRUDERS > 2
#endif // EXTRUDERS > 1
SERIAL_ECHOPGM(" M200");
if (i) SERIAL_ECHOPAIR_P(SP_T_STR, int(i));
SERIAL_ECHOLNPAIR(" D", LINEAR_UNIT(planner.filament_size[i]));
}
#endif

if (!parser.volumetric_enabled)
CONFIG_ECHO_MSG(" M200 D0");
Expand All @@ -2893,7 +2874,7 @@ void MarlinSettings::reset() {
);
#if ENABLED(DISTINCT_E_FACTORS)
CONFIG_ECHO_START();
for (uint8_t i = 0; i < E_STEPPERS; i++) {
LOOP_L_N(i, E_STEPPERS) {
SERIAL_ECHOLNPAIR_P(
PSTR(" M203 T"), (int)i
, SP_E_STR, VOLUMETRIC_UNIT(planner.settings.max_feedrate_mm_s[E_AXIS_N(i)])
Expand All @@ -2913,7 +2894,7 @@ void MarlinSettings::reset() {
);
#if ENABLED(DISTINCT_E_FACTORS)
CONFIG_ECHO_START();
for (uint8_t i = 0; i < E_STEPPERS; i++)
LOOP_L_N(i, E_STEPPERS)
SERIAL_ECHOLNPAIR_P(
PSTR(" M201 T"), (int)i
, SP_E_STR, VOLUMETRIC_UNIT(planner.settings.max_acceleration_mm_per_s2[E_AXIS_N(i)])
Expand Down Expand Up @@ -3022,8 +3003,8 @@ void MarlinSettings::reset() {
#if ENABLED(MESH_BED_LEVELING)

if (leveling_is_valid()) {
for (uint8_t py = 0; py < GRID_MAX_POINTS_Y; py++) {
for (uint8_t px = 0; px < GRID_MAX_POINTS_X; px++) {
LOOP_L_N(py, GRID_MAX_POINTS_Y) {
LOOP_L_N(px, GRID_MAX_POINTS_X) {
CONFIG_ECHO_START();
SERIAL_ECHOPAIR_P(PSTR(" G29 S3 I"), (int)px, PSTR(" J"), (int)py);
SERIAL_ECHOLNPAIR_F_P(SP_Z_STR, LINEAR_UNIT(mbl.z_values[px][py]), 5);
Expand All @@ -3047,8 +3028,8 @@ void MarlinSettings::reset() {
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)

if (leveling_is_valid()) {
for (uint8_t py = 0; py < GRID_MAX_POINTS_Y; py++) {
for (uint8_t px = 0; px < GRID_MAX_POINTS_X; px++) {
LOOP_L_N(py, GRID_MAX_POINTS_Y) {
LOOP_L_N(px, GRID_MAX_POINTS_X) {
CONFIG_ECHO_START();
SERIAL_ECHOPAIR(" G29 W I", (int)px, " J", (int)py);
SERIAL_ECHOLNPAIR_F_P(SP_Z_STR, LINEAR_UNIT(z_values[px][py]), 5);
Expand All @@ -3063,7 +3044,7 @@ void MarlinSettings::reset() {
#if ENABLED(EDITABLE_SERVO_ANGLES)

CONFIG_ECHO_HEADING("Servo Angles:");
for (uint8_t i = 0; i < NUM_SERVOS; i++) {
LOOP_L_N(i, NUM_SERVOS) {
switch (i) {
#if ENABLED(SWITCHING_EXTRUDER)
case SWITCHING_EXTRUDER_SERVO_NR:
Expand Down Expand Up @@ -3146,7 +3127,7 @@ void MarlinSettings::reset() {
#if HOTENDS && HAS_LCD_MENU

CONFIG_ECHO_HEADING("Material heatup parameters:");
for (uint8_t i = 0; i < COUNT(ui.preheat_hotend_temp); i++) {
LOOP_L_N(i, COUNT(ui.preheat_hotend_temp)) {
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(
" M145 S", (int)i
Expand Down Expand Up @@ -3200,7 +3181,7 @@ void MarlinSettings::reset() {

#if HAS_USER_THERMISTORS
CONFIG_ECHO_HEADING("User thermistors:");
for (uint8_t i = 0; i < USER_THERMISTORS; i++)
LOOP_L_N(i, USER_THERMISTORS)
thermalManager.log_user_thermistor(i, true);
#endif

Expand Down Expand Up @@ -3596,29 +3577,10 @@ void MarlinSettings::reset() {
say_M603(forReplay);
SERIAL_ECHOLNPAIR("L", LINEAR_UNIT(fc_settings[0].load_length), " U", LINEAR_UNIT(fc_settings[0].unload_length));
#else
#define _ECHO_603(N) do{ say_M603(forReplay); SERIAL_ECHOLNPAIR("T" STRINGIFY(N) " L", LINEAR_UNIT(fc_settings[N].load_length), " U", LINEAR_UNIT(fc_settings[N].unload_length)); }while(0)
_ECHO_603(0);
_ECHO_603(1);
#if EXTRUDERS > 2
_ECHO_603(2);
#if EXTRUDERS > 3
_ECHO_603(3);
#if EXTRUDERS > 4
_ECHO_603(4);
#if EXTRUDERS > 5
_ECHO_603(5);
#if EXTRUDERS > 6
_ECHO_603(6);
#if EXTRUDERS > 7
_ECHO_603(7);
#endif // EXTRUDERS > 7
#endif // EXTRUDERS > 6
#endif // EXTRUDERS > 5
#endif // EXTRUDERS > 4
#endif // EXTRUDERS > 3
#endif // EXTRUDERS > 2
#endif // EXTRUDERS == 1
#endif // ADVANCED_PAUSE_FEATURE
#define _ECHO_603(N) do{ say_M603(forReplay); SERIAL_ECHOLNPAIR("T" STRINGIFY(N) " L", LINEAR_UNIT(fc_settings[N].load_length), " U", LINEAR_UNIT(fc_settings[N].unload_length)); }while(0);
REPEAT(EXTRUDERS, _ECHO_603)
#endif
#endif

#if EXTRUDERS > 1
CONFIG_ECHO_HEADING("Tool-changing:");
Expand Down
23 changes: 3 additions & 20 deletions Marlin/src/module/endstops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,35 +480,18 @@ void _O2 Endstops::report_states() {
#if NUM_RUNOUT_SENSORS == 1
print_es_state(READ(FIL_RUNOUT_PIN) != FIL_RUNOUT_INVERTING, PSTR(MSG_FILAMENT_RUNOUT_SENSOR));
#else
#define _CASE_RUNOUT(N) case N: pin = FIL_RUNOUT##N##_PIN; break;
for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
pin_t pin;
switch (i) {
default: continue;
case 1: pin = FIL_RUNOUT_PIN; break;
case 2: pin = FIL_RUNOUT2_PIN; break;
#if NUM_RUNOUT_SENSORS >= 3
case 3: pin = FIL_RUNOUT3_PIN; break;
#if NUM_RUNOUT_SENSORS >= 4
case 4: pin = FIL_RUNOUT4_PIN; break;
#if NUM_RUNOUT_SENSORS >= 5
case 5: pin = FIL_RUNOUT5_PIN; break;
#if NUM_RUNOUT_SENSORS >= 6
case 6: pin = FIL_RUNOUT6_PIN; break;
#if NUM_RUNOUT_SENSORS >= 7
case 7: pin = FIL_RUNOUT7_PIN; break;
#if NUM_RUNOUT_SENSORS >= 8
case 8: pin = FIL_RUNOUT8_PIN; break;
#endif
#endif
#endif
#endif
#endif
#endif
REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _CASE_RUNOUT)
}
SERIAL_ECHOPGM(MSG_FILAMENT_RUNOUT_SENSOR);
if (i > 1) SERIAL_CHAR(' ', '0' + i);
print_es_state(extDigitalRead(pin) != FIL_RUNOUT_INVERTING);
}
#undef _CASE_RUNOUT
#endif
#endif
#if ENABLED(BLTOUCH)
Expand Down
Loading

0 comments on commit 4716dac

Please sign in to comment.