From a309ec1b0b7d46c3d7c483d2aecab039c2eae88f Mon Sep 17 00:00:00 2001 From: Marek Bel Date: Thu, 3 Jan 2019 22:58:25 +0100 Subject: [PATCH] Add filament auto deplete engine for multi-material prints with MMUv2. --- Firmware/AutoDeplete.cpp | 78 ++++++++++++++++++++++++++++++++++++++++ Firmware/AutoDeplete.h | 17 +++++++++ 2 files changed, 95 insertions(+) create mode 100644 Firmware/AutoDeplete.cpp create mode 100644 Firmware/AutoDeplete.h diff --git a/Firmware/AutoDeplete.cpp b/Firmware/AutoDeplete.cpp new file mode 100644 index 00000000..bfc09012 --- /dev/null +++ b/Firmware/AutoDeplete.cpp @@ -0,0 +1,78 @@ +//! @file +//! @author: Marek Bel +//! @date Jan 3, 2019 + +#include "AutoDeplete.h" +#include "assert.h" + +//! @brief bit field marking depleted filaments +//! +//! binary 1 marks filament as depleted +//! Zero initialized value means, that no filament is depleted. +static uint8_t depleted; +static const uint8_t filamentCount = 5; + +static constexpr uint8_t allDepleted(uint8_t fCount) +{ + return fCount == 1 ? 1 : ((1 << (fCount - 1)) | allDepleted(fCount - 2)); +} + +//! @brief Is filament available for printing? +//! @par filament Filament number to be checked +//! @retval true Filament is available for printing. +//! @retval false Filament is not available for printing. +static bool loaded(uint8_t filament) +{ + if (depleted & (1 << filament)) return false; + return true; +} + +//! @brief Mark filament as not available for printing. +//! @par filament filament to be marked +void ad_markDepleted(uint8_t filament) +{ + assert(filament < filamentCount); + if (filament < filamentCount) + { + depleted |= 1 << filament; + } +} + +//! @brief Mark filament as available for printing. +//! @par filament filament to be marked +void ad_markLoaded(uint8_t filament) +{ + assert(filament < filamentCount); + if (filament < filamentCount) + { + depleted &= ~(1 << filament); + } +} + +//! @brief Get alternative filament, which is not depleted +//! @par filament filament +//! @return Filament, if it is depleted, returns next available, +//! if all filaments are depleted, returns filament function parameter. +uint8_t ad_getAlternative(uint8_t filament) +{ + assert(filament < filamentCount); + if (ad_allDepleted()) return filament; + for (uint8_t i = 0; i + +void ad_markDepleted(uint8_t filament); +void ad_markLoaded(uint8_t filament); +uint8_t ad_getAlternative(uint8_t filament); +bool ad_allDepleted(); + +#endif /* AUTODEPLETE_H */