mmu2_reporting.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "mmu2.h"
  2. #include "mmu2_reporting.h"
  3. #include "mmu2_error_converter.h"
  4. #include "mmu2/error_codes.h"
  5. #include "mmu2/buttons.h"
  6. #include "ultralcd.h"
  7. #include "Filament_sensor.h"
  8. #include "language.h"
  9. #include "temperature.h"
  10. #include "sound.h"
  11. namespace MMU2 {
  12. const char * const ProgressCodeToText(uint16_t pc); // we may join progress convertor and reporter together
  13. void BeginReport(CommandInProgress cip, uint16_t ec) {
  14. custom_message_type = CustomMsg::MMUProgress;
  15. lcd_setstatuspgm( ProgressCodeToText(ec) );
  16. }
  17. void EndReport(CommandInProgress cip, uint16_t ec) {
  18. // clear the status msg line - let the printed filename get visible again
  19. custom_message_type = CustomMsg::Status;
  20. }
  21. static void ReportErrorHookDynamicRender(void)
  22. {
  23. lcd_set_cursor(3, 2);
  24. lcd_printf_P(PSTR("%d"), mmu2.FindaDetectsFilament());
  25. lcd_set_cursor(8, 2);
  26. lcd_printf_P(PSTR("%d"), fsensor.getFilamentPresent());
  27. lcd_set_cursor(11, 2);
  28. lcd_print("?>?"); // This is temporary until below TODO is resolved
  29. // TODO, see lcdui_print_extruder(void)
  30. //if (MMU2::mmu2.get_current_tool() == MMU2::FILAMENT_UNKNOWN)
  31. // lcd_printf_P(_N(" ?>%u"), tmp_extruder + 1);
  32. //else
  33. // lcd_printf_P(_N(" %u>%u"), MMU2::mmu2.get_current_tool() + 1, tmp_extruder + 1);
  34. // Print active extruder temperature
  35. lcd_set_cursor(16, 2);
  36. lcd_printf_P(PSTR("%d"), (int)(degHotend(0) + 0.5));
  37. }
  38. static void ReportErrorHookStaticRender(uint16_t ec) {
  39. //! Show an error screen
  40. //! When an MMU error occurs, the LCD content will look like this:
  41. //! |01234567890123456789|
  42. //! |MMU FW update needed| <- title/header of the error: max 20 characters
  43. //! |prusa3d.com/ERR04504| <- URL 20 characters
  44. //! |FI:1 FS:1 5>3 t201°| <- status line, t is thermometer symbol
  45. //! |>Retry >Done >MoreW| <- buttons
  46. const uint8_t ei = PrusaErrorCodeIndex(ec);
  47. bool two_choices = false;
  48. // Read and determine what operations should be shown on the menu
  49. // Note: uint16_t is used here to avoid compiler warning. uint8_t is only half the size of void*
  50. const uint8_t button_operation = PrusaErrorButtons(ei);
  51. const uint8_t button_op_right = BUTTON_OP_RIGHT(button_operation);
  52. const uint8_t button_op_middle = BUTTON_OP_MIDDLE(button_operation);
  53. // Check if the menu should have three or two choices
  54. if (button_op_right == (uint8_t)ButtonOperations::NoOperation){
  55. // Two operations not specified, the error menu should only show two choices
  56. two_choices = true;
  57. }
  58. lcd_clear();
  59. lcd_update_enable(false);
  60. // Print title and header
  61. lcd_printf_P(PSTR("%.20S\nprusa3d.com/ERR04%hu"), _T(PrusaErrorTitle(ei)), PrusaErrorCode(ei) );
  62. // Render static characters in third line
  63. lcd_set_cursor(0, 2);
  64. lcd_printf_P(PSTR("FI: FS: > %c %c"), LCD_STR_THERMOMETER[0], LCD_STR_DEGREE[0]);
  65. // Render the choices
  66. lcd_show_choices_prompt_P(two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE, _T(PrusaErrorButtonTitle(button_op_middle)), _T(two_choices ? PrusaErrorButtonMore() : PrusaErrorButtonTitle(button_op_right)), two_choices ? 10 : 7, two_choices ? nullptr : _T(PrusaErrorButtonMore()));
  67. }
  68. static uint8_t ReportErrorHookMonitor(uint16_t ec) {
  69. const uint8_t ei = PrusaErrorCodeIndex(ec);
  70. bool two_choices = false;
  71. static int8_t enc_dif = 0;
  72. // Read and determine what operations should be shown on the menu
  73. // Note: uint16_t is used here to avoid compiler warning. uint8_t is only half the size of void*
  74. const uint8_t button_operation = PrusaErrorButtons(ei);
  75. const uint8_t button_op_right = BUTTON_OP_RIGHT(button_operation);
  76. const uint8_t button_op_middle = BUTTON_OP_MIDDLE(button_operation);
  77. // Check if the menu should have three or two choices
  78. if (button_op_right == (uint8_t)ButtonOperations::NoOperation){
  79. // Two operations not specified, the error menu should only show two choices
  80. two_choices = true;
  81. }
  82. static int8_t current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  83. static int8_t choice_selected = -1;
  84. // Check if knob was rotated
  85. if (abs(enc_dif - lcd_encoder_diff) >= ENCODER_PULSES_PER_STEP) {
  86. if (two_choices == false) { // third_choice is not nullptr, safe to dereference
  87. if (enc_dif > lcd_encoder_diff && current_selection != LCD_LEFT_BUTTON_CHOICE) {
  88. // Rotating knob counter clockwise
  89. current_selection--;
  90. } else if (enc_dif < lcd_encoder_diff && current_selection != LCD_RIGHT_BUTTON_CHOICE) {
  91. // Rotating knob clockwise
  92. current_selection++;
  93. }
  94. } else {
  95. if (enc_dif > lcd_encoder_diff && current_selection != LCD_LEFT_BUTTON_CHOICE) {
  96. // Rotating knob counter clockwise
  97. current_selection = LCD_LEFT_BUTTON_CHOICE;
  98. } else if (enc_dif < lcd_encoder_diff && current_selection != LCD_MIDDLE_BUTTON_CHOICE) {
  99. // Rotating knob clockwise
  100. current_selection = LCD_MIDDLE_BUTTON_CHOICE;
  101. }
  102. }
  103. // Update '>' render only
  104. lcd_set_cursor(0, 3);
  105. lcd_print(current_selection == LCD_LEFT_BUTTON_CHOICE ? '>': ' ');
  106. if (two_choices == false)
  107. {
  108. lcd_set_cursor(7, 3);
  109. lcd_print(current_selection == LCD_MIDDLE_BUTTON_CHOICE ? '>': ' ');
  110. lcd_set_cursor(13, 3);
  111. lcd_print(current_selection == LCD_RIGHT_BUTTON_CHOICE ? '>': ' ');
  112. } else {
  113. lcd_set_cursor(10, 3);
  114. lcd_print(current_selection == LCD_MIDDLE_BUTTON_CHOICE ? '>': ' ');
  115. }
  116. // Consume rotation event and make feedback sound
  117. enc_dif = lcd_encoder_diff;
  118. Sound_MakeSound(e_SOUND_TYPE_EncoderMove);
  119. }
  120. // Check if knob was clicked and consume the event
  121. if (lcd_clicked()) {
  122. Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
  123. choice_selected = current_selection;
  124. // Reset current_selection
  125. current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  126. }
  127. // return to loop()
  128. if (choice_selected == -1) { // No selection, continue monitoring
  129. return 0;
  130. }
  131. if ((two_choices && choice_selected == LCD_MIDDLE_BUTTON_CHOICE) // Two choices and middle button selected
  132. || (!two_choices && choice_selected == LCD_RIGHT_BUTTON_CHOICE)) // Three choices and right most button selected
  133. {
  134. // 'More' show error description
  135. lcd_show_fullscreen_message_and_wait_P(_T(PrusaErrorDesc(ei)));
  136. current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  137. choice_selected = -1;
  138. return 1;
  139. // Return back to the choice menu
  140. } else if(choice_selected == LCD_MIDDLE_BUTTON_CHOICE) {
  141. SetButtonResponse((ButtonOperations)button_op_right);
  142. current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  143. choice_selected = -1;
  144. return 2;
  145. } else {
  146. SetButtonResponse((ButtonOperations)button_op_middle);
  147. current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  148. choice_selected = -1;
  149. return 2;
  150. }
  151. }
  152. enum class ReportErrorHookStates : uint8_t {
  153. RENDER_ERROR_SCREEN = 0,
  154. MONITOR_SELECTION = 1,
  155. };
  156. enum ReportErrorHookStates ReportErrorHookState;
  157. void ReportErrorHook(CommandInProgress cip, uint16_t ec) {
  158. switch ((uint8_t)ReportErrorHookState)
  159. {
  160. case (uint8_t)ReportErrorHookStates::RENDER_ERROR_SCREEN:
  161. // START
  162. ReportErrorHookStaticRender(ec);
  163. ReportErrorHookState = ReportErrorHookStates::MONITOR_SELECTION;
  164. // Fall through
  165. case (uint8_t)ReportErrorHookStates::MONITOR_SELECTION:
  166. ReportErrorHookDynamicRender(); // Render dynamic characters
  167. switch (ReportErrorHookMonitor(ec))
  168. {
  169. case 0:
  170. // No choice selected, return to loop()
  171. break;
  172. case 1:
  173. // More button selected, change state
  174. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  175. break;
  176. case 2:
  177. // Exit error screen and enable lcd updates
  178. lcd_set_custom_characters();
  179. lcd_update_enable(true);
  180. lcd_return_to_status();
  181. // Reset the state in case a new error is reported
  182. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  183. break;
  184. default:
  185. break;
  186. }
  187. return; // Always return to loop() to let MMU trigger a call to ReportErrorHook again
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. void ReportProgressHook(CommandInProgress cip, uint16_t ec) {
  194. custom_message_type = CustomMsg::MMUProgress;
  195. lcd_setstatuspgm( _T(ProgressCodeToText(ec)) );
  196. }
  197. } // namespace MMU2