mmu2_error_converter.cpp 3.8 KB

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