AutoDeplete.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. static constexpr uint8_t allDepleted(uint8_t fCount)
  13. {
  14. return fCount == 1 ? 1 : ((1 << (fCount - 1)) | allDepleted(fCount - 1));
  15. }
  16. //! @brief Is filament available for printing?
  17. //! @par filament Filament number to be checked
  18. //! @retval true Filament is available for printing.
  19. //! @retval false Filament is not available for printing.
  20. static bool loaded(uint8_t filament)
  21. {
  22. if (depleted & (1 << filament)) return false;
  23. return true;
  24. }
  25. //! @brief Mark filament as not available for printing.
  26. //! @par filament filament to be marked
  27. void ad_markDepleted(uint8_t filament)
  28. {
  29. assert(filament < filamentCount);
  30. if (filament < filamentCount)
  31. {
  32. depleted |= 1 << filament;
  33. }
  34. }
  35. //! @brief Mark filament as available for printing.
  36. //! @par filament filament to be marked
  37. void ad_markLoaded(uint8_t filament)
  38. {
  39. assert(filament < filamentCount);
  40. if (filament < filamentCount)
  41. {
  42. depleted &= ~(1 << filament);
  43. }
  44. }
  45. //! @brief Get alternative filament, which is not depleted
  46. //! @par filament filament
  47. //! @return Filament, if it is depleted, returns next available,
  48. //! if all filaments are depleted, returns filament function parameter.
  49. uint8_t ad_getAlternative(uint8_t filament)
  50. {
  51. assert(filament < filamentCount);
  52. for (uint8_t i = 0; i<filamentCount; ++i)
  53. {
  54. uint8_t nextFilament = (filament + i) % filamentCount;
  55. if (loaded(nextFilament)) return nextFilament;
  56. }
  57. return filament;
  58. }
  59. //! @brief Are all filaments depleted?
  60. //! @retval true All filaments are depleted.
  61. //! @retval false All filaments are not depleted.
  62. bool ad_allDepleted()
  63. {
  64. if (allDepleted(filamentCount) == depleted)
  65. {
  66. return true;
  67. }
  68. return false;
  69. }