mmu2_reporting.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  22. * @brief Renders any characters that will be updated live on the MMU error screen.
  23. *Currently, this is FINDA and Filament Sensor status and Extruder temperature.
  24. */
  25. static void ReportErrorHookDynamicRender(void)
  26. {
  27. lcd_set_cursor(3, 2);
  28. lcd_printf_P(PSTR("%d"), mmu2.FindaDetectsFilament());
  29. lcd_set_cursor(8, 2);
  30. lcd_printf_P(PSTR("%d"), fsensor.getFilamentPresent());
  31. lcd_set_cursor(11, 2);
  32. lcd_print("?>?"); // This is temporary until below TODO is resolved
  33. // TODO, see lcdui_print_extruder(void)
  34. //if (MMU2::mmu2.get_current_tool() == MMU2::FILAMENT_UNKNOWN)
  35. // lcd_printf_P(_N(" ?>%u"), tmp_extruder + 1);
  36. //else
  37. // lcd_printf_P(_N(" %u>%u"), MMU2::mmu2.get_current_tool() + 1, tmp_extruder + 1);
  38. // Print active extruder temperature
  39. lcd_set_cursor(16, 2);
  40. lcd_printf_P(PSTR("%d"), (int)(degHotend(0) + 0.5));
  41. }
  42. /**
  43. * @brief Renders any characters that are static on the MMU error screen i.e. they don't change.
  44. * @param[in] ei Error code index
  45. */
  46. static void ReportErrorHookStaticRender(uint8_t ei) {
  47. //! Show an error screen
  48. //! When an MMU error occurs, the LCD content will look like this:
  49. //! |01234567890123456789|
  50. //! |MMU FW update needed| <- title/header of the error: max 20 characters
  51. //! |prusa3d.com/ERR04504| <- URL 20 characters
  52. //! |FI:1 FS:1 5>3 t201°| <- status line, t is thermometer symbol
  53. //! |>Retry >Done >MoreW| <- buttons
  54. bool two_choices = false;
  55. // Read and determine what operations should be shown on the menu
  56. const uint8_t button_operation = PrusaErrorButtons(ei);
  57. const uint8_t button_op_right = BUTTON_OP_RIGHT(button_operation);
  58. const uint8_t button_op_middle = BUTTON_OP_MIDDLE(button_operation);
  59. // Check if the menu should have three or two choices
  60. if (button_op_right == (uint8_t)ButtonOperations::NoOperation){
  61. // Two operations not specified, the error menu should only show two choices
  62. two_choices = true;
  63. }
  64. lcd_set_custom_characters_nextpage();
  65. lcd_update_enable(false);
  66. lcd_clear();
  67. // Print title and header
  68. lcd_printf_P(PSTR("%.20S\nprusa3d.com/ERR04%hu"), _T(PrusaErrorTitle(ei)), PrusaErrorCode(ei) );
  69. // Render static characters in third line
  70. lcd_set_cursor(0, 2);
  71. lcd_printf_P(PSTR("FI: FS: > %c %c"), LCD_STR_THERMOMETER[0], LCD_STR_DEGREE[0]);
  72. // Render the choices
  73. 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()));
  74. }
  75. /**
  76. * @brief Monitors the LCD button selection without blocking MMU communication
  77. * @param[in] ei Error code index
  78. * @return 0 if there is no knob click --
  79. * 1 if user clicked 'More' and firmware should render
  80. * the error screen when ReportErrorHook is called next --
  81. * 2 if the user selects an operation and we would like
  82. * to exit the error screen. The MMU will raise the menu
  83. * again if the error is not solved.
  84. */
  85. static uint8_t ReportErrorHookMonitor(uint8_t ei) {
  86. uint8_t ret = 0;
  87. bool two_choices = false;
  88. static int8_t enc_dif = 0;
  89. // Read and determine what operations should be shown on the menu
  90. const uint8_t button_operation = PrusaErrorButtons(ei);
  91. const uint8_t button_op_right = BUTTON_OP_RIGHT(button_operation);
  92. const uint8_t button_op_middle = BUTTON_OP_MIDDLE(button_operation);
  93. // Check if the menu should have three or two choices
  94. if (button_op_right == (uint8_t)ButtonOperations::NoOperation){
  95. // Two operations not specified, the error menu should only show two choices
  96. two_choices = true;
  97. }
  98. static int8_t current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  99. static int8_t choice_selected = -1;
  100. // Check if knob was rotated
  101. if (abs(enc_dif - lcd_encoder_diff) >= ENCODER_PULSES_PER_STEP) {
  102. if (two_choices == false) { // third_choice is not nullptr, safe to dereference
  103. if (enc_dif > lcd_encoder_diff && current_selection != LCD_LEFT_BUTTON_CHOICE) {
  104. // Rotating knob counter clockwise
  105. current_selection--;
  106. } else if (enc_dif < lcd_encoder_diff && current_selection != LCD_RIGHT_BUTTON_CHOICE) {
  107. // Rotating knob clockwise
  108. current_selection++;
  109. }
  110. } else {
  111. if (enc_dif > lcd_encoder_diff && current_selection != LCD_LEFT_BUTTON_CHOICE) {
  112. // Rotating knob counter clockwise
  113. current_selection = LCD_LEFT_BUTTON_CHOICE;
  114. } else if (enc_dif < lcd_encoder_diff && current_selection != LCD_MIDDLE_BUTTON_CHOICE) {
  115. // Rotating knob clockwise
  116. current_selection = LCD_MIDDLE_BUTTON_CHOICE;
  117. }
  118. }
  119. // Update '>' render only
  120. lcd_set_cursor(0, 3);
  121. lcd_print(current_selection == LCD_LEFT_BUTTON_CHOICE ? '>': ' ');
  122. if (two_choices == false)
  123. {
  124. lcd_set_cursor(7, 3);
  125. lcd_print(current_selection == LCD_MIDDLE_BUTTON_CHOICE ? '>': ' ');
  126. lcd_set_cursor(13, 3);
  127. lcd_print(current_selection == LCD_RIGHT_BUTTON_CHOICE ? '>': ' ');
  128. } else {
  129. lcd_set_cursor(10, 3);
  130. lcd_print(current_selection == LCD_MIDDLE_BUTTON_CHOICE ? '>': ' ');
  131. }
  132. // Consume rotation event and make feedback sound
  133. enc_dif = lcd_encoder_diff;
  134. Sound_MakeSound(e_SOUND_TYPE_EncoderMove);
  135. }
  136. // Check if knob was clicked and consume the event
  137. if (lcd_clicked()) {
  138. Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
  139. choice_selected = current_selection;
  140. } else {
  141. // continue monitoring
  142. return ret;
  143. }
  144. if ((two_choices && choice_selected == LCD_MIDDLE_BUTTON_CHOICE) // Two choices and middle button selected
  145. || (!two_choices && choice_selected == LCD_RIGHT_BUTTON_CHOICE)) // Three choices and right most button selected
  146. {
  147. // 'More' show error description
  148. lcd_show_fullscreen_message_and_wait_P(_T(PrusaErrorDesc(ei)));
  149. ret = 1;
  150. } else if(choice_selected == LCD_MIDDLE_BUTTON_CHOICE) {
  151. SetButtonResponse((ButtonOperations)button_op_right);
  152. ret = 2;
  153. } else {
  154. SetButtonResponse((ButtonOperations)button_op_middle);
  155. ret = 2;
  156. }
  157. // Reset static variables to their default value
  158. current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  159. choice_selected = -1;
  160. return ret;
  161. }
  162. enum class ReportErrorHookStates : uint8_t {
  163. RENDER_ERROR_SCREEN = 0,
  164. MONITOR_SELECTION = 1,
  165. DISMISS_ERROR_SCREEN = 2,
  166. };
  167. enum ReportErrorHookStates ReportErrorHookState;
  168. /**
  169. * @brief Render MMU error screen on the LCD. This must be non-blocking
  170. * and allow the MMU and printer to communicate with each other.
  171. * @param[in] ec Error code
  172. */
  173. void ReportErrorHook(uint16_t ec) {
  174. if (mmu2.MMUCurrentErrorCode() == ErrorCode::OK)
  175. {
  176. // If the error code suddenly changes to OK, that means
  177. // a button was pushed on the MMU and the LCD should
  178. // dismiss the error screen until MMU raises a new error
  179. ReportErrorHookState = ReportErrorHookStates::DISMISS_ERROR_SCREEN;
  180. }
  181. const uint8_t ei = PrusaErrorCodeIndex(ec);
  182. switch ((uint8_t)ReportErrorHookState)
  183. {
  184. case (uint8_t)ReportErrorHookStates::RENDER_ERROR_SCREEN:
  185. ReportErrorHookStaticRender(ei);
  186. ReportErrorHookState = ReportErrorHookStates::MONITOR_SELECTION;
  187. // Fall through
  188. case (uint8_t)ReportErrorHookStates::MONITOR_SELECTION:
  189. mmu2.is_mmu_error_monitor_active = true;
  190. ReportErrorHookDynamicRender(); // Render dynamic characters
  191. switch (ReportErrorHookMonitor(ei))
  192. {
  193. case 0:
  194. // No choice selected, return to loop()
  195. break;
  196. case 1:
  197. // More button selected, change state
  198. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  199. break;
  200. case 2:
  201. // Exit error screen and enable lcd updates
  202. lcd_set_custom_characters();
  203. lcd_update_enable(true);
  204. lcd_return_to_status();
  205. // Reset the state in case a new error is reported
  206. mmu2.is_mmu_error_monitor_active = false;
  207. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  208. break;
  209. default:
  210. break;
  211. }
  212. return; // Always return to loop() to let MMU trigger a call to ReportErrorHook again
  213. break;
  214. case (uint8_t)ReportErrorHookStates::DISMISS_ERROR_SCREEN:
  215. lcd_set_custom_characters();
  216. lcd_update_enable(true);
  217. lcd_return_to_status();
  218. // Reset the state in case a new error is reported
  219. mmu2.is_mmu_error_monitor_active = false;
  220. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. void ReportProgressHook(CommandInProgress cip, uint16_t ec) {
  227. custom_message_type = CustomMsg::MMUProgress;
  228. lcd_setstatuspgm( _T(ProgressCodeToText(ec)) );
  229. }
  230. } // namespace MMU2