|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] |
| 4 | + * |
| 5 | + * Based on Sprinter and grbl. |
| 6 | + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +#include "../../inc/MarlinConfig.h" |
| 24 | + |
| 25 | +#if HAS_CHAMELEON |
| 26 | + |
| 27 | +#include "chameleon.h" |
| 28 | +//#include "../../MarlinCore.h" |
| 29 | +#include "../../module/planner.h" |
| 30 | +#include "../../module/stepper.h" |
| 31 | +#include "../../lcd/marlinui.h" |
| 32 | + |
| 33 | +//#define DEBUG_OUT ENABLED(DEBUG_CHAMELEON) |
| 34 | +#include "../../core/debug_out.h" |
| 35 | + |
| 36 | +Chameleon chameleon; |
| 37 | + |
| 38 | +struct E_Step { |
| 39 | + float extrude; |
| 40 | + feedRate_t feedRate; |
| 41 | +}; |
| 42 | + |
| 43 | +// Change tools the Chameleon 3D way |
| 44 | +void Chameleon::tool_change(const uint8_t index) { |
| 45 | + const uint8_t switches = active_extruder ^ index; |
| 46 | + if (!switches) return; |
| 47 | + |
| 48 | + ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1)); |
| 49 | + |
| 50 | + #ifdef CHAMELEON_TOOLCHANGE_Y |
| 51 | + current_position.y = CHAMELEON_TOOLCHANGE_Y; |
| 52 | + #endif |
| 53 | + #ifdef CHAMELEON_TOOLCHANGE_Z |
| 54 | + NOLESS(current_position.z, CHAMELEON_TOOLCHANGE_Z); |
| 55 | + line_to_current_position(5000); |
| 56 | + #endif |
| 57 | + if (switches & 0x1) { |
| 58 | + current_position.x = CHAMELEON_BUTTON1_X + (X_CENTER > (CHAMELEON_BUTTON1_X)) ? 10 : -10; |
| 59 | + } |
| 60 | + else { |
| 61 | + #if EXTRUDERS > 2 |
| 62 | + if (switches & 0x2) |
| 63 | + current_position.x = CHAMELEON_BUTTON2_X + (X_CENTER > (CHAMELEON_BUTTON2_X)) ? 10 : -10; |
| 64 | + #endif |
| 65 | + } |
| 66 | + line_to_current_position(5000); |
| 67 | + |
| 68 | + static constexpr E_Step ramming_sequence[] PROGMEM = { CHAMELEON_RAMMING_SEQUENCE }; |
| 69 | + execute_extruder_sequence(ramming_sequence, COUNT(ramming_sequence)); |
| 70 | + |
| 71 | + toggle(switches); |
| 72 | + |
| 73 | + active_extruder = index; |
| 74 | + |
| 75 | + ui.reset_status(); |
| 76 | +} |
| 77 | + |
| 78 | +// Execute a list of E moves |
| 79 | +void Chameleon::execute_extruder_sequence(const E_Step * const sequence, const uint8_t steps) { |
| 80 | + planner.synchronize(); |
| 81 | + stepper.enable_extruder(); |
| 82 | + |
| 83 | + const E_Step *step = sequence; |
| 84 | + |
| 85 | + for (uint8_t i = 0; i < steps; ++i) { |
| 86 | + const float es = pgm_read_float(&(step->extrude)); |
| 87 | + const uint16_t fr_or_ms = pgm_read_float(&(step->feedRate)); |
| 88 | + |
| 89 | + DEBUG_ECHO_MSG("Move E", es, " @ ", fr_or_ms); |
| 90 | + |
| 91 | + if (es) { |
| 92 | + current_position.e += es; |
| 93 | + line_to_current_position(MMM_TO_MMS(fr_or_ms)); |
| 94 | + planner.synchronize(); |
| 95 | + } |
| 96 | + else |
| 97 | + safe_delay(fr_or_ms); |
| 98 | + |
| 99 | + step++; |
| 100 | + } |
| 101 | + |
| 102 | + // Reset E to 0 for the new extruder tool |
| 103 | + current_position.e = 0; |
| 104 | + sync_plan_position_e(); |
| 105 | + |
| 106 | + stepper.disable_extruder(); |
| 107 | +} |
| 108 | + |
| 109 | +// Bump the switches needed to change feeders |
| 110 | +void Chameleon::toggle(const uint8_t switches) { |
| 111 | + if (switches & 0x1) { |
| 112 | + current_position.x = CHAMELEON_BUTTON1_X; |
| 113 | + line_to_current_position(2000); |
| 114 | + current_position.x += (X_CENTER > (CHAMELEON_BUTTON1_X)) ? 10 : -10; |
| 115 | + line_to_current_position(2000); |
| 116 | + } |
| 117 | + #if EXTRUDERS > 2 |
| 118 | + if (switches & 0x2) { |
| 119 | + current_position.x = CHAMELEON_BUTTON2_X; |
| 120 | + line_to_current_position(2000); |
| 121 | + current_position.x += (X_CENTER > (CHAMELEON_BUTTON2_X)) ? 10 : -10; |
| 122 | + line_to_current_position(2000); |
| 123 | + } |
| 124 | + #endif |
| 125 | + planner.synchronize(); |
| 126 | +} |
| 127 | + |
| 128 | +#endif // HAS_CHAMELEON |
0 commit comments