mmu2_reporting.cpp 9.9 KB

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