AutoDeplete.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //! @file
  2. //! @author: Marek Bel
  3. //! @date Jan 3, 2019
  4. #include "AutoDeplete.h"
  5. #include "assert.h"
  6. //! @brief bit field marking depleted filaments
  7. //!
  8. //! binary 1 marks filament as depleted
  9. //! Zero initialized value means, that no filament is depleted.
  10. static uint8_t depleted;
  11. static const uint8_t filamentCount = 5;
  12. //! @return binary 1 for all filaments
  13. //! @par fCount number of filaments
  14. static constexpr uint8_t allDepleted(uint8_t fCount)
  15. {
  16. return fCount == 1 ? 1 : ((1 << (fCount - 1)) | allDepleted(fCount - 1));
  17. }
  18. //! @brief Is filament available for printing?
  19. //! @par filament Filament number to be checked
  20. //! @retval true Filament is available for printing.
  21. //! @retval false Filament is not available for printing.
  22. static bool loaded(uint8_t filament)
  23. {
  24. if (depleted & (1 << filament)) return false;
  25. return true;
  26. }
  27. //! @brief Mark filament as not available for printing.
  28. //! @par filament filament to be marked
  29. void ad_markDepleted(uint8_t filament)
  30. {
  31. assert(filament < filamentCount);
  32. if (filament < filamentCount)
  33. {
  34. depleted |= 1 << filament;
  35. }
  36. }
  37. //! @brief Mark filament as available for printing.
  38. //! @par filament filament to be marked
  39. void ad_markLoaded(uint8_t filament)
  40. {
  41. assert(filament < filamentCount);
  42. if (filament < filamentCount)
  43. {
  44. depleted &= ~(1 << filament);
  45. }
  46. }
  47. //! @brief Get alternative filament, which is not depleted
  48. //! @par filament filament
  49. //! @return Filament, if it is depleted, returns next available,
  50. //! if all filaments are depleted, returns filament function parameter.
  51. uint8_t ad_getAlternative(uint8_t filament)
  52. {
  53. assert(filament < filamentCount);
  54. for (uint8_t i = 0; i<filamentCount; ++i)
  55. {
  56. uint8_t nextFilament = (filament + i) % filamentCount;
  57. if (loaded(nextFilament)) return nextFilament;
  58. }
  59. return filament;
  60. }
  61. //! @brief Are all filaments depleted?
  62. //! @retval true All filaments are depleted.
  63. //! @retval false All filaments are not depleted.
  64. bool ad_allDepleted()
  65. {
  66. if (allDepleted(filamentCount) == depleted)
  67. {
  68. return true;
  69. }
  70. return false;
  71. }