Skip to content

Commit

Permalink
🎨 Macros for optional arguments (MarlinFirmware#21969)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed May 29, 2021
1 parent 61f2bb1 commit e75c3b6
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 351 deletions.
5 changes: 5 additions & 0 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@
#define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
#define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.

#define _OPTARG(A) , A
#define OPTARG(O,A) TERN_(O,DEFER4(_OPTARG)(A))
#define _OPTCODE(A) A;
#define OPTCODE(O,A) TERN_(O,DEFER4(_OPTCODE)(A))

// Macros to avoid 'f + 0.0' which is not always optimized away. Minus included for symmetry.
// Compiler flags -fno-signed-zeros -ffinite-math-only also cover 'f * 1.0', 'f - f', etc.
#define PLUS_TERN0(O,A) _TERN(_ENA_1(O),,+ (A)) // OPTION ? '+ (A)' : '<nul>'
Expand Down
4 changes: 1 addition & 3 deletions Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ class mesh_bed_leveling {
}

static float get_z(const xy_pos_t &pos
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
, const_float_t factor=1.0f
#endif
OPTARG(ENABLE_LEVELING_FADE_HEIGHT, const_float_t factor=1.0f)
) {
#if DISABLED(ENABLE_LEVELING_FADE_HEIGHT)
constexpr float factor = 1.0f;
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,11 @@
while (--segments) {
raw += diff;
planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, segment_xyz_mm
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)
);
}
planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, segment_xyz_mm
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)
);
return false; // Did not set current from destination
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/caselight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool CaseLight::on = CASE_LIGHT_DEFAULT_ON;
#if CASE_LIGHT_IS_COLOR_LED
#include "leds/leds.h"
constexpr uint8_t init_case_light[] = CASE_LIGHT_DEFAULT_COLOR;
LEDColor CaseLight::color = { init_case_light[0], init_case_light[1], init_case_light[2], TERN_(HAS_WHITE_LED, init_case_light[3]) };
LEDColor CaseLight::color = { init_case_light[0], init_case_light[1], init_case_light[2] OPTARG(HAS_WHITE_LED, init_case_light[3]) };
#endif

void CaseLight::update(const bool sflag) {
Expand Down
6 changes: 1 addition & 5 deletions Marlin/src/feature/fwretract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ void FWRetract::reset() {
* Note: Auto-retract will apply the set Z hop in addition to any Z hop
* included in the G-code. Use M207 Z0 to to prevent double hop.
*/
void FWRetract::retract(const bool retracting
#if HAS_MULTI_EXTRUDER
, bool swapping/*=false*/
#endif
) {
void FWRetract::retract(const bool retracting OPTARG(HAS_MULTI_EXTRUDER, bool swapping/*=false*/)) {
// Prevent two retracts or recovers in a row
if (retracted[active_extruder] == retracting) return;

Expand Down
6 changes: 1 addition & 5 deletions Marlin/src/feature/fwretract.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ class FWRetract {
#endif
}

static void retract(const bool retracting
#if HAS_MULTI_EXTRUDER
, bool swapping = false
#endif
);
static void retract(const bool retracting OPTARG(HAS_MULTI_EXTRUDER, bool swapping = false));

static void M207();
static void M207_report(const bool forReplay=false);
Expand Down
4 changes: 1 addition & 3 deletions Marlin/src/feature/leds/leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ void LEDLights::setup() {
}

void LEDLights::set_color(const LEDColor &incol
#if ENABLED(NEOPIXEL_LED)
, bool isSequence/*=false*/
#endif
OPTARG(NEOPIXEL_LED, bool isSequence/*=false*/)
) {

#if ENABLED(NEOPIXEL_LED)
Expand Down
71 changes: 14 additions & 57 deletions Marlin/src/feature/leds/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,21 @@
*/
typedef struct LEDColor {
uint8_t r, g, b
#if HAS_WHITE_LED
, w
#if ENABLED(NEOPIXEL_LED)
, i
#endif
#endif
OPTARG(HAS_WHITE_LED, w)
OPTARG(NEOPIXEL_LED, i)
;

LEDColor() : r(255), g(255), b(255)
#if HAS_WHITE_LED
, w(255)
#if ENABLED(NEOPIXEL_LED)
, i(NEOPIXEL_BRIGHTNESS)
#endif
#endif
OPTARG(HAS_WHITE_LED, w(255))
OPTARG(NEOPIXEL_LED, i(NEOPIXEL_BRIGHTNESS))
{}

LEDColor(uint8_t r, uint8_t g, uint8_t b
#if HAS_WHITE_LED
, uint8_t w=0
#if ENABLED(NEOPIXEL_LED)
, uint8_t i=NEOPIXEL_BRIGHTNESS
#endif
#endif
) : r(r), g(g), b(b)
#if HAS_WHITE_LED
, w(w)
#if ENABLED(NEOPIXEL_LED)
, i(i)
#endif
#endif
{}
LEDColor(uint8_t r, uint8_t g, uint8_t b OPTARG(HAS_WHITE_LED, uint8_t w=0) OPTARG(NEOPIXEL_LED, uint8_t i=NEOPIXEL_BRIGHTNESS))
: r(r), g(g), b(b) OPTARG(HAS_WHITE_LED, w(w)) OPTARG(NEOPIXEL_LED, i(i)) {}

LEDColor(const uint8_t (&rgbw)[4]) : r(rgbw[0]), g(rgbw[1]), b(rgbw[2])
#if HAS_WHITE_LED
, w(rgbw[3])
#if ENABLED(NEOPIXEL_LED)
, i(NEOPIXEL_BRIGHTNESS)
#endif
#endif
OPTARG(HAS_WHITE_LED, w(rgbw[3]))
OPTARG(NEOPIXEL_LED, i(NEOPIXEL_BRIGHTNESS))
{}

LEDColor& operator=(const uint8_t (&rgbw)[4]) {
Expand Down Expand Up @@ -111,15 +86,7 @@ typedef struct LEDColor {
/**
* Color helpers and presets
*/
#if HAS_WHITE_LED
#if ENABLED(NEOPIXEL_LED)
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W, I)
#else
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B, W)
#endif
#else
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B)
#endif
#define MakeLEDColor(R,G,B,W,I) LEDColor(R, G, B OPTARG(HAS_WHITE_LED, W) OPTARG(NEOPIXEL_LED, I))

#define LEDColorOff() LEDColor( 0, 0, 0)
#define LEDColorRed() LEDColor(255, 0, 0)
Expand Down Expand Up @@ -147,25 +114,15 @@ class LEDLights {
static void setup(); // init()

static void set_color(const LEDColor &color
#if ENABLED(NEOPIXEL_LED)
, bool isSequence=false
#endif
OPTARG(NEOPIXEL_LED, bool isSequence=false)
);

static inline void set_color(uint8_t r, uint8_t g, uint8_t b
#if HAS_WHITE_LED
, uint8_t w=0
#endif
#if ENABLED(NEOPIXEL_LED)
, uint8_t i=NEOPIXEL_BRIGHTNESS
, bool isSequence=false
#endif
OPTARG(HAS_WHITE_LED, uint8_t w=0)
OPTARG(NEOPIXEL_LED, uint8_t i=NEOPIXEL_BRIGHTNESS)
OPTARG(NEOPIXEL_LED, bool isSequence=false)
) {
set_color(MakeLEDColor(r, g, b, w, i)
#if ENABLED(NEOPIXEL_LED)
, isSequence
#endif
);
set_color(MakeLEDColor(r, g, b, w, i) OPTARG(NEOPIXEL_LED, isSequence));
}

static inline void set_off() { set_color(LEDColorOff()); }
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/feature/leds/pca9632.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ static void PCA9632_WriteRegister(const byte addr, const byte regadd, const byte
}

static void PCA9632_WriteAllRegisters(const byte addr, const byte regadd, const byte vr, const byte vg, const byte vb
#if ENABLED(PCA9632_RGBW)
, const byte vw
#endif
OPTARG(PCA9632_RGBW, const byte vw)
) {
#if DISABLED(PCA9632_NO_AUTO_INC)
uint8_t data[4];
Expand Down Expand Up @@ -143,9 +141,7 @@ void PCA9632_set_led_color(const LEDColor &color) {
;

PCA9632_WriteAllRegisters(PCA9632_ADDRESS,PCA9632_PWM0, color.r, color.g, color.b
#if ENABLED(PCA9632_RGBW)
, color.w
#endif
OPTARG(PCA9632_RGBW, color.w)
);
PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_LEDOUT, LEDOUT);
}
Expand Down
12 changes: 3 additions & 9 deletions Marlin/src/feature/tmc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,9 @@ class TMCStorage {
}

struct {
#if ENABLED(HAS_STEALTHCHOP)
bool stealthChop_enabled = false;
#endif
#if ENABLED(HYBRID_THRESHOLD)
uint8_t hybrid_thrs = 0;
#endif
#if ENABLED(USE_SENSORLESS)
int16_t homing_thrs = 0;
#endif
OPTCODE(HAS_STEALTHCHOP, bool stealthChop_enabled = false)
OPTCODE(HYBRID_THRESHOLD, uint8_t hybrid_thrs = 0)
OPTCODE(USE_SENSORLESS, int16_t homing_thrs = 0)
} stored;
};

Expand Down
15 changes: 4 additions & 11 deletions Marlin/src/gcode/bedlevel/G26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,8 @@ typedef struct {
thermalManager.setTargetBed(bed_temp);

// Wait for the temperature to stabilize
if (!thermalManager.wait_for_bed(true
#if G26_CLICK_CAN_CANCEL
, true
#endif
)
) return G26_ERR;
if (!thermalManager.wait_for_bed(true OPTARG(G26_CLICK_CAN_CANCEL, true)))
return G26_ERR;
}

#else
Expand All @@ -352,11 +348,8 @@ typedef struct {
thermalManager.setTargetHotend(hotend_temp, active_extruder);

// Wait for the temperature to stabilize
if (!thermalManager.wait_for_hotend(active_extruder, true
#if G26_CLICK_CAN_CANCEL
, true
#endif
)) return G26_ERR;
if (!thermalManager.wait_for_hotend(active_extruder, true OPTARG(G26_CLICK_CAN_CANCEL, true)))
return G26_ERR;

#if HAS_WIRED_LCD
ui.reset_status();
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/gcode/motion/G2_G3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ void plan_arc(
#endif

if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, 0
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)
)) break;
}

Expand All @@ -266,9 +264,7 @@ void plan_arc(
#endif

planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, 0
#if ENABLED(SCARA_FEEDRATE_SCALING)
, inv_duration
#endif
OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)
);

TERN_(AUTO_BED_LEVELING_UBL, raw[l_axis] = start_L);
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/gcode/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ char GCodeQueue::injected_commands[64]; // = { 0 }


void GCodeQueue::RingBuffer::commit_command(bool skip_ok
#if HAS_MULTI_SERIAL
, serial_index_t serial_ind/*=-1*/
#endif
OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind/*=-1*/)
) {
commands[index_w].skip_ok = skip_ok;
TERN_(HAS_MULTI_SERIAL, commands[index_w].port = serial_ind);
Expand All @@ -100,9 +98,7 @@ void GCodeQueue::RingBuffer::commit_command(bool skip_ok
* Return false for a full buffer, or if the 'command' is a comment.
*/
bool GCodeQueue::RingBuffer::enqueue(const char *cmd, bool skip_ok/*=true*/
#if HAS_MULTI_SERIAL
, serial_index_t serial_ind/*=-1*/
#endif
OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind/*=-1*/)
) {
if (*cmd == ';' || length >= BUFSIZE) return false;
strcpy(commands[index_w].buffer, cmd);
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/gcode/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,11 @@ class GCodeQueue {
void advance_pos(uint8_t &p, const int inc) { if (++p >= BUFSIZE) p = 0; length += inc; }

void commit_command(bool skip_ok
#if HAS_MULTI_SERIAL
, serial_index_t serial_ind = serial_index_t()
#endif
OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind = serial_index_t())
);

bool enqueue(const char *cmd, bool skip_ok = true
#if HAS_MULTI_SERIAL
, serial_index_t serial_ind = serial_index_t()
#endif
OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind = serial_index_t())
);

void ok_to_send();
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,9 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
// Tell ui.update() to start a move to current_position after a short delay.
//
void ManualMove::soon(const AxisEnum move_axis
#if MULTI_E_MANUAL
, const int8_t eindex/*=-1*/
#endif
OPTARG(MULTI_E_MANUAL, const int8_t eindex/*=active_extruder*/)
) {
#if MULTI_E_MANUAL
if (move_axis == E_AXIS) e_index = eindex >= 0 ? eindex : active_extruder;
#endif
TERN_(MULTI_E_MANUAL, if (move_axis == E_AXIS) e_index = eindex);
start_time = millis() + (menu_scale < 0.99f ? 0UL : 250UL); // delay for bigger moves
axis = move_axis;
//SERIAL_ECHOLNPAIR("Post Move with Axis ", AS_CHAR(axis_codes[axis]), " soon.");
Expand Down
6 changes: 1 addition & 5 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@
static bool constexpr processing = false;
#endif
static void task();
static void soon(const AxisEnum axis
#if MULTI_E_MANUAL
, const int8_t eindex=-1
#endif
);
static void soon(const AxisEnum axis OPTARG(MULTI_E_MANUAL, const int8_t eindex=active_extruder));
};

#endif
Expand Down
12 changes: 4 additions & 8 deletions Marlin/src/lcd/menu/menu_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,13 @@ void lcd_move_z() { _lcd_move_xyz(GET_TEXT(MSG_MOVE_Z), Z_AXIS); }

#if E_MANUAL

static void lcd_move_e(TERN_(MULTI_E_MANUAL, const int8_t eindex=-1)) {
static void lcd_move_e(TERN_(MULTI_E_MANUAL, const int8_t eindex=active_extruder)) {
if (ui.use_click()) return ui.goto_previous_screen_no_defer();
if (ui.encoderPosition) {
if (!ui.manual_move.processing) {
const float diff = float(int32_t(ui.encoderPosition)) * ui.manual_move.menu_scale;
TERN(IS_KINEMATIC, ui.manual_move.offset, current_position.e) += diff;
ui.manual_move.soon(E_AXIS
#if MULTI_E_MANUAL
, eindex
#endif
);
ui.manual_move.soon(E_AXIS OPTARG(MULTI_E_MANUAL, eindex));
ui.refresh(LCDVIEW_REDRAW_NOW);
}
ui.encoderPosition = 0;
Expand Down Expand Up @@ -139,7 +135,7 @@ void _goto_manual_move(const_float_t scale) {
ui.goto_screen(_manual_move_func_ptr);
}

void _menu_move_distance(const AxisEnum axis, const screenFunc_t func, const int8_t eindex=-1) {
void _menu_move_distance(const AxisEnum axis, const screenFunc_t func, const int8_t eindex=active_extruder) {
_manual_move_func_ptr = func;
START_MENU();
if (LCD_HEIGHT >= 4) {
Expand Down Expand Up @@ -188,7 +184,7 @@ void _menu_move_distance(const AxisEnum axis, const screenFunc_t func, const int
#if E_MANUAL

inline void _goto_menu_move_distance_e() {
ui.goto_screen([]{ _menu_move_distance(E_AXIS, []{ lcd_move_e(TERN_(MULTI_E_MANUAL, active_extruder)); }, -1); });
ui.goto_screen([]{ _menu_move_distance(E_AXIS, []{ lcd_move_e(); }); });
}

inline void _menu_move_distance_e_maybe() {
Expand Down
Loading

0 comments on commit e75c3b6

Please sign in to comment.