Firmware2/Marlin/src/feature/mixing.h

120 lines
3.6 KiB
C
Raw Normal View History

/**
* 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 <http://www.gnu.org/licenses/>.
*
*/
2018-10-16 10:38:57 +02:00
#pragma once
2017-09-18 07:06:16 +02:00
#include "../inc/MarlinConfig.h"
2018-10-16 10:38:57 +02:00
#ifdef __AVR__
#define MIXER_ACCU_SIGNED
2019-02-03 06:30:26 +01:00
typedef uint8_t mixer_comp_t;
2018-10-16 10:38:57 +02:00
typedef int8_t mixer_accu_t;
#define COLOR_A_MASK 0x80
#define COLOR_MASK 0x7F
2018-10-16 10:38:57 +02:00
#else
2019-02-03 06:30:26 +01:00
typedef uint_fast16_t mixer_comp_t;
2018-10-16 10:38:57 +02:00
typedef uint_fast16_t mixer_accu_t;
#define COLOR_A_MASK 0x8000
#define COLOR_MASK 0x7FFF
2017-09-18 07:06:16 +02:00
#endif
2018-10-16 10:38:57 +02:00
#ifndef MIXING_VIRTUAL_TOOLS
#define MIXING_VIRTUAL_TOOLS 1
#endif
2017-09-18 07:06:16 +02:00
2018-10-16 10:38:57 +02:00
#ifdef RETRACT_SYNC_MIXING
#define NR_MIXING_VIRTUAL_TOOLS (MIXING_VIRTUAL_TOOLS + 1)
#define MIXER_AUTORETRACT_TOOL MIXING_VIRTUAL_TOOLS
2018-11-01 20:41:03 +01:00
#if NR_MIXING_VIRTUAL_TOOLS > 254
#error "MIXING_VIRTUAL_TOOLS must be <= 254!"
#endif
2018-10-16 10:38:57 +02:00
#else
#define NR_MIXING_VIRTUAL_TOOLS (MIXING_VIRTUAL_TOOLS)
#if NR_MIXING_VIRTUAL_TOOLS > 255
2018-11-01 20:41:03 +01:00
#error "MIXING_VIRTUAL_TOOLS must be <= 255!"
#endif
2017-09-18 07:06:16 +02:00
#endif
2018-10-16 10:38:57 +02:00
#define MIXER_STEPPER_LOOP(VAR) \
for (uint_fast8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
2019-02-03 06:30:26 +01:00
#define MIXER_BLOCK_FIELD mixer_comp_t b_color[MIXING_STEPPERS]
2018-10-16 10:38:57 +02:00
#define MIXER_POPULATE_BLOCK() mixer.populate_block(block->b_color)
2019-02-03 06:30:26 +01:00
#define MIXER_STEPPER_SETUP() mixer.stepper_setup(current_block->b_color)
2018-10-16 10:38:57 +02:00
2019-02-03 06:30:26 +01:00
/**
* @brief Mixer class
* @details Contains data and behaviors for a Mixing Extruder
*/
2018-10-16 10:38:57 +02:00
class Mixer {
public:
2019-02-03 06:30:26 +01:00
static void init();
static void reset_vtools();
2018-10-16 10:38:57 +02:00
// Used up to Planner level
static void normalize(const uint8_t tool_index);
2019-02-03 06:30:26 +01:00
FORCE_INLINE static uint8_t get_current_vtool() { return selected_vtool; }
FORCE_INLINE static void T(const uint_fast8_t c) { selected_vtool = c; }
FORCE_INLINE static void set_collector(const uint8_t c, const float f) { collector[c] = f; }
2018-10-16 10:38:57 +02:00
// Used when dealing with blocks
2019-02-03 06:30:26 +01:00
FORCE_INLINE static void populate_block(mixer_comp_t b_color[]) {
uint_fast8_t j = get_current_vtool();
2018-10-16 10:38:57 +02:00
MIXER_STEPPER_LOOP(i) b_color[i] = color[j][i];
}
2019-02-03 06:30:26 +01:00
FORCE_INLINE static void stepper_setup(mixer_comp_t b_color[]) { MIXER_STEPPER_LOOP(i) s_color[i] = b_color[i]; }
2018-10-16 10:38:57 +02:00
// Used in Stepper
2019-02-03 06:30:26 +01:00
FORCE_INLINE static uint8_t get_stepper() { return runner; }
FORCE_INLINE static uint8_t get_next_stepper() {
2018-10-16 10:38:57 +02:00
do {
if (--runner < 0) runner = MIXING_STEPPERS - 1;
accu[runner] += s_color[runner];
if (
#ifdef MIXER_ACCU_SIGNED
accu[runner] < 0
#else
accu[runner] & COLOR_A_MASK
#endif
) {
accu[runner] &= COLOR_MASK;
return runner;
}
} while( true );
}
private:
// Used up to Planner level
2019-02-03 06:30:26 +01:00
static uint_fast8_t selected_vtool;
static float collector[MIXING_STEPPERS];
static mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
2018-10-16 10:38:57 +02:00
// Used in Stepper
2019-02-03 06:30:26 +01:00
static int_fast8_t runner;
static mixer_comp_t s_color[MIXING_STEPPERS];
static mixer_accu_t accu[MIXING_STEPPERS];
2018-10-16 10:38:57 +02:00
};
extern Mixer mixer;