diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp index eb05267872..4add62ff72 100644 --- a/Marlin/src/Marlin.cpp +++ b/Marlin/src/Marlin.cpp @@ -62,6 +62,10 @@ #include "feature/digipot/digipot.h" #endif +#if ENABLED(MIXING_EXTRUDER) + #include "feature/mixing.h" +#endif + #if ENABLED(BEZIER_CURVE_SUPPORT) #include "module/planner_bezier.h" #endif @@ -186,13 +190,6 @@ millis_t max_inactive_time = 0, AdvancedPauseMenuResponse advanced_pause_menu_response; #endif -#if ENABLED(MIXING_EXTRUDER) - float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0. - #if MIXING_VIRTUAL_TOOLS > 1 - float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS]; - #endif -#endif - #ifdef CHDK millis_t chdkHigh = 0; bool chdkActive = false; @@ -302,45 +299,6 @@ void suicide() { #endif -#if ENABLED(MIXING_EXTRUDER) - - void normalize_mix() { - float mix_total = 0.0; - for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mix_total += RECIPROCAL(mixing_factor[i]); - // Scale all values if they don't add up to ~1.0 - if (!NEAR(mix_total, 1.0)) { - SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling."); - for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= mix_total; - } - } - - #if ENABLED(DIRECT_MIXING_IN_G1) - // Get mixing parameters from the GCode - // The total "must" be 1.0 (but it will be normalized) - // If no mix factors are given, the old mix is preserved - void gcode_get_mix() { - const char* mixing_codes = "ABCDHI"; - byte mix_bits = 0; - for (uint8_t i = 0; i < MIXING_STEPPERS; i++) { - if (parser.seenval(mixing_codes[i])) { - SBI(mix_bits, i); - float v = parser.value_float(); - NOLESS(v, 0.0); - mixing_factor[i] = RECIPROCAL(v); - } - } - // If any mixing factors were included, clear the rest - // If none were included, preserve the last mix - if (mix_bits) { - for (uint8_t i = 0; i < MIXING_STEPPERS; i++) - if (!TEST(mix_bits, i)) mixing_factor[i] = 0.0; - normalize_mix(); - } - } - #endif - -#endif - /************************************************** ***************** GCode Handlers ***************** **************************************************/ @@ -362,16 +320,6 @@ void quickstop_stepper() { SYNC_PLAN_POSITION_KINEMATIC(); } -#if ENABLED(MIXING_EXTRUDER) - #include "gcode/feature/mixing/M163.h" - #if MIXING_VIRTUAL_TOOLS > 1 - #include "gcode/feature/mixing/M164.h" - #endif - #if ENABLED(DIRECT_MIXING_IN_G1) - #include "gcode/feature/mixing/M165.h" - #endif -#endif - #include "gcode/control/M999.h" #include "gcode/control/T.h" @@ -957,12 +905,7 @@ void setup() { #endif #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1 - // Initialize mixing to 100% color 1 - for (uint8_t i = 0; i < MIXING_STEPPERS; i++) - mixing_factor[i] = (i == 0) ? 1.0 : 0.0; - for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++) - for (uint8_t i = 0; i < MIXING_STEPPERS; i++) - mixing_virtual_tool_mix[t][i] = mixing_factor[i]; + mixing_tools_init(); #endif #if ENABLED(BLTOUCH) diff --git a/Marlin/src/Marlin.h b/Marlin/src/Marlin.h index ba37ccd598..97e80c5db7 100644 --- a/Marlin/src/Marlin.h +++ b/Marlin/src/Marlin.h @@ -223,13 +223,6 @@ extern millis_t max_inactive_time, stepper_inactive_time; extern int lpq_len; #endif -#if ENABLED(MIXING_EXTRUDER) - extern float mixing_factor[MIXING_STEPPERS]; - #if MIXING_VIRTUAL_TOOLS > 1 - extern float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS]; - #endif -#endif - void calculate_volumetric_multipliers(); bool pin_is_protected(const int8_t pin); diff --git a/Marlin/src/feature/mixing.cpp b/Marlin/src/feature/mixing.cpp new file mode 100644 index 0000000000..7300c3e28a --- /dev/null +++ b/Marlin/src/feature/mixing.cpp @@ -0,0 +1,79 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../inc/MarlinConfig.h" + +#if ENABLED(MIXING_EXTRUDER) + +float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0. + +#if MIXING_VIRTUAL_TOOLS > 1 + + float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS]; + + void mixing_tools_init() { + // Initialize mixing to 100% color 1 + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + mixing_factor[i] = (i == 0) ? 1.0 : 0.0; + for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++) + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + mixing_virtual_tool_mix[t][i] = mixing_factor[i]; + } + +#endif // MIXING_VIRTUAL_TOOLS > 1 + +void normalize_mix() { + float mix_total = 0.0; + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mix_total += RECIPROCAL(mixing_factor[i]); + // Scale all values if they don't add up to ~1.0 + if (!NEAR(mix_total, 1.0)) { + SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling."); + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= mix_total; + } +} + +#if ENABLED(DIRECT_MIXING_IN_G1) + // Get mixing parameters from the GCode + // The total "must" be 1.0 (but it will be normalized) + // If no mix factors are given, the old mix is preserved + void gcode_get_mix() { + const char mixing_codes[] = { 'A', 'B', 'C', 'D', 'H', 'I' }; + byte mix_bits = 0; + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) { + if (parser.seenval(mixing_codes[i])) { + SBI(mix_bits, i); + float v = parser.value_float(); + NOLESS(v, 0.0); + mixing_factor[i] = RECIPROCAL(v); + } + } + // If any mixing factors were included, clear the rest + // If none were included, preserve the last mix + if (mix_bits) { + for (uint8_t i = 0; i < MIXING_STEPPERS; i++) + if (!TEST(mix_bits, i)) mixing_factor[i] = 0.0; + normalize_mix(); + } + } +#endif + +#endif // MIXING_EXTRUDER diff --git a/Marlin/src/feature/mixing.h b/Marlin/src/feature/mixing.h new file mode 100644 index 0000000000..fff240c0e0 --- /dev/null +++ b/Marlin/src/feature/mixing.h @@ -0,0 +1,41 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef __MIXING_H__ +#define __MIXING_H__ + +#include "../inc/MarlinConfig.h" + +extern float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0. + +#if MIXING_VIRTUAL_TOOLS > 1 + extern float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS]; + void mixing_tools_init(); +#endif + +void normalize_mix(); + +#if ENABLED(DIRECT_MIXING_IN_G1) + void gcode_get_mix(); +#endif + +#endif // __MIXING_H__ diff --git a/Marlin/src/gcode/feature/mixing/M163.h b/Marlin/src/gcode/feature/mixing/M163.cpp similarity index 87% rename from Marlin/src/gcode/feature/mixing/M163.h rename to Marlin/src/gcode/feature/mixing/M163.cpp index 5dd8df4815..721928289e 100644 --- a/Marlin/src/gcode/feature/mixing/M163.h +++ b/Marlin/src/gcode/feature/mixing/M163.cpp @@ -20,6 +20,13 @@ * */ +#include "../../../inc/MarlinConfig.h" + +#if ENABLED(MIXING_EXTRUDER) + +#include "../../gcode.h" +#include "../../../feature/mixing.h" + /** * M163: Set a single mix factor for a mixing extruder * This is called "weight" by some systems. @@ -28,7 +35,7 @@ * P[float] The mix value * */ -void gcode_M163() { +void GcodeSuite::M163() { const int mix_index = parser.intval('S'); if (mix_index < MIXING_STEPPERS) { float mix_value = parser.floatval('P'); @@ -36,3 +43,5 @@ void gcode_M163() { mixing_factor[mix_index] = RECIPROCAL(mix_value); } } + +#endif // MIXING_EXTRUDER diff --git a/Marlin/src/gcode/feature/mixing/M164.h b/Marlin/src/gcode/feature/mixing/M164.cpp similarity index 83% rename from Marlin/src/gcode/feature/mixing/M164.h rename to Marlin/src/gcode/feature/mixing/M164.cpp index 07176766db..138b374fd9 100644 --- a/Marlin/src/gcode/feature/mixing/M164.h +++ b/Marlin/src/gcode/feature/mixing/M164.cpp @@ -20,13 +20,20 @@ * */ +#include "../../../inc/MarlinConfig.h" + +#if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1 + +#include "../../gcode.h" +#include "../../../feature/mixing.h" + /** * M164: Store the current mix factors as a virtual tool. * * S[index] The virtual tool to store * */ -void gcode_M164() { +void GcodeSuite::M164() { const int tool_index = parser.intval('S'); if (tool_index < MIXING_VIRTUAL_TOOLS) { normalize_mix(); @@ -34,3 +41,5 @@ void gcode_M164() { mixing_virtual_tool_mix[tool_index][i] = mixing_factor[i]; } } + +#endif // MIXING_EXTRUDER && MIXING_VIRTUAL_TOOLS > 1 diff --git a/Marlin/src/gcode/feature/mixing/M165.h b/Marlin/src/gcode/feature/mixing/M165.cpp similarity index 86% rename from Marlin/src/gcode/feature/mixing/M165.h rename to Marlin/src/gcode/feature/mixing/M165.cpp index 4c75891295..feaa8248ee 100644 --- a/Marlin/src/gcode/feature/mixing/M165.h +++ b/Marlin/src/gcode/feature/mixing/M165.cpp @@ -20,6 +20,13 @@ * */ +#include "../../../inc/MarlinConfig.h" + +#if ENABLED(DIRECT_MIXING_IN_G1) + +#include "../../gcode.h" +#include "../../../feature/mixing.h" + /** * M165: Set multiple mix factors for a mixing extruder. * Factors that are left out will be set to 0. @@ -33,4 +40,6 @@ * I[factor] Mix factor for extruder stepper 6 * */ -void gcode_M165() { gcode_get_mix(); } +void GcodeSuite::M165() { gcode_get_mix(); } + +#endif // DIRECT_MIXING_IN_G1 diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 065bf1fe26..ee68d5a77e 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -116,9 +116,6 @@ void GcodeSuite::dwell(millis_t time) { // // Placeholders for non-migrated codes // -extern void gcode_M163(); -extern void gcode_M164(); -extern void gcode_M165(); extern void gcode_M999(); extern void gcode_T(uint8_t tmp_extruder); @@ -462,18 +459,12 @@ void GcodeSuite::process_next_command() { #endif #if ENABLED(MIXING_EXTRUDER) - case 163: // M163: Set a component weight for mixing extruder - gcode_M163(); - break; + case 163: M163(); break; // M163: Set a component weight for mixing extruder #if MIXING_VIRTUAL_TOOLS > 1 - case 164: // M164: Save current mix as a virtual extruder - gcode_M164(); - break; + case 164: M164(); break; // M164: Save current mix as a virtual extruder #endif #if ENABLED(DIRECT_MIXING_IN_G1) - case 165: // M165: Set multiple mix weights - gcode_M165(); - break; + case 165: M165(); break; // M165: Set multiple mix weights #endif #endif diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index 1dc983c334..f906f769ea 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -80,6 +80,10 @@ #include "../feature/baricuda.h" #endif +#if ENABLED(MIXING_EXTRUDER) + #include "../feature/mixing.h" +#endif + Planner planner; // public: diff --git a/Marlin/src/module/tool_change.cpp b/Marlin/src/module/tool_change.cpp index b8fab7cc80..68b8d82eaa 100644 --- a/Marlin/src/module/tool_change.cpp +++ b/Marlin/src/module/tool_change.cpp @@ -42,6 +42,10 @@ #include "../feature/snmm.h" #endif +#if ENABLED(MIXING_EXTRUDER) + #include "../feature/mixing.h" +#endif + #if ENABLED(SWITCHING_EXTRUDER) #if EXTRUDERS > 3