mmu2_error_converter.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "mmu2_error_converter.h"
  2. #include "mmu2/error_codes.h"
  3. #include "mmu2/errors_list.h"
  4. #include "language.h"
  5. #include <stdio.h>
  6. namespace MMU2 {
  7. // @@TODO ideally compute the numbers by using some constexpr function, but since
  8. // the current avg-gcc doesn't support cycles in constexpr functions, it is hard to achieve.
  9. // So for now this has been hand-crafted.
  10. const uint16_t MMUErrorCodeIndex(uint16_t ec) {
  11. switch (ec) {
  12. case (uint16_t)ErrorCode::FINDA_DIDNT_SWITCH_ON:
  13. return 0;
  14. case (uint16_t)ErrorCode::FINDA_DIDNT_SWITCH_OFF:
  15. return 1;
  16. case (uint16_t)ErrorCode::FSENSOR_DIDNT_SWITCH_ON:
  17. return 2;
  18. case (uint16_t)ErrorCode::FSENSOR_DIDNT_SWITCH_OFF:
  19. return 3;
  20. case (uint16_t)ErrorCode::STALLED_PULLEY:
  21. return 4;
  22. case (uint16_t)ErrorCode::HOMING_SELECTOR_FAILED:
  23. return 5;
  24. case (uint16_t)ErrorCode::HOMING_IDLER_FAILED:
  25. return 6;
  26. case (uint16_t)ErrorCode::MMU_NOT_RESPONDING:
  27. return 25;
  28. case (uint16_t)ErrorCode::PROTOCOL_ERROR:
  29. return 26;
  30. case (uint16_t)ErrorCode::FILAMENT_ALREADY_LOADED:
  31. return 27;
  32. case (uint16_t)ErrorCode::INVALID_TOOL:
  33. return 28;
  34. case (uint16_t)ErrorCode::QUEUE_FULL:
  35. return 29;
  36. case (uint16_t)ErrorCode::VERSION_MISMATCH:
  37. return 30;
  38. case (uint16_t)ErrorCode::INTERNAL:
  39. return 31;
  40. }
  41. // // TMC-related errors - multiple of these can occur at once
  42. // // - in such a case we report the first which gets found/converted into Prusa-Error-Codes (usually the fact, that one TMC has an issue is serious enough)
  43. // // By carefully ordering the checks here we can prioritize the errors being reported to the user.
  44. if (ec & (uint16_t)ErrorCode::TMC_PULLEY_BIT) {
  45. if (ec & (uint16_t)ErrorCode::TMC_IOIN_MISMATCH)
  46. return 13;
  47. if (ec & (uint16_t)ErrorCode::TMC_RESET)
  48. return 16;
  49. if (ec & (uint16_t)ErrorCode::TMC_UNDERVOLTAGE_ON_CHARGE_PUMP)
  50. return 19;
  51. if (ec & (uint16_t)ErrorCode::TMC_SHORT_TO_GROUND)
  52. return 22;
  53. if (ec & (uint16_t)ErrorCode::TMC_OVER_TEMPERATURE_WARN)
  54. return 7;
  55. if (ec & (uint16_t)ErrorCode::TMC_OVER_TEMPERATURE_ERROR)
  56. return 10;
  57. } else if (ec & (uint16_t)ErrorCode::TMC_SELECTOR_BIT) {
  58. if (ec & (uint16_t)ErrorCode::TMC_IOIN_MISMATCH)
  59. return 14;
  60. if (ec & (uint16_t)ErrorCode::TMC_RESET)
  61. return 17;
  62. if (ec & (uint16_t)ErrorCode::TMC_UNDERVOLTAGE_ON_CHARGE_PUMP)
  63. return 20;
  64. if (ec & (uint16_t)ErrorCode::TMC_SHORT_TO_GROUND)
  65. return 23;
  66. if (ec & (uint16_t)ErrorCode::TMC_OVER_TEMPERATURE_WARN)
  67. return 8;
  68. if (ec & (uint16_t)ErrorCode::TMC_OVER_TEMPERATURE_ERROR)
  69. return 11;
  70. } else if (ec & (uint16_t)ErrorCode::TMC_IDLER_BIT) {
  71. if (ec & (uint16_t)ErrorCode::TMC_IOIN_MISMATCH)
  72. return 15;
  73. if (ec & (uint16_t)ErrorCode::TMC_RESET)
  74. return 18;
  75. if (ec & (uint16_t)ErrorCode::TMC_UNDERVOLTAGE_ON_CHARGE_PUMP)
  76. return 21;
  77. if (ec & (uint16_t)ErrorCode::TMC_SHORT_TO_GROUND)
  78. return 24;
  79. if (ec & (uint16_t)ErrorCode::TMC_OVER_TEMPERATURE_WARN)
  80. return 9;
  81. if (ec & (uint16_t)ErrorCode::TMC_OVER_TEMPERATURE_ERROR)
  82. return 12;
  83. }
  84. // // if nothing got caught, return a generic error
  85. // return FindError(ERR_OTHER);
  86. }
  87. void TranslateErr(uint16_t ec, char *dst, size_t dstSize) {
  88. uint16_t ei = MMUErrorCodeIndex(ec);
  89. // just to prevent the compiler from stripping the data structures from the final binary for now
  90. *dst = errorButtons[ei];
  91. snprintf(
  92. dst, dstSize, "%S %S",
  93. static_cast<const char * const>(pgm_read_ptr(&errorTitles[ei])),
  94. static_cast<const char * const>(pgm_read_ptr(&errorDescs[ei]))
  95. );
  96. }
  97. } // namespace MMU2