mmu2_reporting.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 * 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( _T(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. lcd_setstatuspgm(MSG_WELCOME); // should be seen only when the printer is not printing a file
  20. custom_message_type = CustomMsg::Status;
  21. }
  22. /**
  23. * @brief Renders any characters that will be updated live on the MMU error screen.
  24. *Currently, this is FINDA and Filament Sensor status and Extruder temperature.
  25. */
  26. extern void ReportErrorHookDynamicRender(void){
  27. // beware - this optimization abuses the fact, that FindaDetectsFilament returns 0 or 1 and '0' is followed by '1' in the ASCII table
  28. lcd_putc_at(3, 2, mmu2.FindaDetectsFilament() + '0');
  29. lcd_putc_at(8, 2, fsensor.getFilamentPresent() + '0');
  30. // print active/changing filament slot
  31. lcd_set_cursor(10, 2);
  32. lcdui_print_extruder();
  33. // Print active extruder temperature
  34. lcd_set_cursor(16, 2);
  35. lcd_printf_P(PSTR("%3d"), (int)(degHotend(0) + 0.5));
  36. }
  37. /**
  38. * @brief Renders any characters that are static on the MMU error screen i.e. they don't change.
  39. * @param[in] ei Error code index
  40. */
  41. static void ReportErrorHookStaticRender(uint8_t ei) {
  42. //! Show an error screen
  43. //! When an MMU error occurs, the LCD content will look like this:
  44. //! |01234567890123456789|
  45. //! |MMU FW update needed| <- title/header of the error: max 20 characters
  46. //! |prusa3d.com/ERR04504| <- URL 20 characters
  47. //! |FI:1 FS:1 5>3 t201°| <- status line, t is thermometer symbol
  48. //! |>Retry >Done >W| <- buttons
  49. bool two_choices = false;
  50. // Read and determine what operations should be shown on the menu
  51. const uint8_t button_operation = PrusaErrorButtons(ei);
  52. const uint8_t button_op_right = BUTTON_OP_RIGHT(button_operation);
  53. const uint8_t button_op_middle = BUTTON_OP_MIDDLE(button_operation);
  54. // Check if the menu should have three or two choices
  55. if (button_op_right == (uint8_t)ButtonOperations::NoOperation){
  56. // Two operations not specified, the error menu should only show two choices
  57. two_choices = true;
  58. }
  59. lcd_set_custom_characters_nextpage();
  60. lcd_update_enable(false);
  61. lcd_clear();
  62. // Print title and header
  63. lcd_printf_P(PSTR("%.20S\nprusa3d.com/ERR04%hu"), _T(PrusaErrorTitle(ei)), PrusaErrorCode(ei) );
  64. ReportErrorHookSensorLineRender();
  65. // Render the choices
  66. //@todo convert MSG_BTN_MORE to PROGMEM_N1
  67. 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 ? 18 : 9, two_choices ? nullptr : _T(PrusaErrorButtonMore()));
  68. }
  69. extern void ReportErrorHookSensorLineRender()
  70. {
  71. // Render static characters in third line
  72. lcd_set_cursor(0, 2);
  73. lcd_printf_P(PSTR("FI: FS: > %c %c"), LCD_STR_THERMOMETER[0], LCD_STR_DEGREE[0]);
  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 = lcd_encoder_diff;
  89. if (lcd_encoder_diff == 0)
  90. {
  91. // lcd_update_enable(true) was called outside ReportErrorHookMonitor
  92. // It will set lcd_encoder_diff to 0, sync enc_dif
  93. enc_dif = 0;
  94. }
  95. // Read and determine what operations should be shown on the menu
  96. const uint8_t button_operation = PrusaErrorButtons(ei);
  97. const uint8_t button_op_right = BUTTON_OP_RIGHT(button_operation);
  98. const uint8_t button_op_middle = BUTTON_OP_MIDDLE(button_operation);
  99. // Check if the menu should have three or two choices
  100. if (button_op_right == (uint8_t)ButtonOperations::NoOperation){
  101. // Two operations not specified, the error menu should only show two choices
  102. two_choices = true;
  103. }
  104. static int8_t current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  105. static int8_t choice_selected = -1;
  106. // Check if knob was rotated
  107. if (abs(enc_dif - lcd_encoder_diff) >= ENCODER_PULSES_PER_STEP) {
  108. if (two_choices == false) { // third_choice is not nullptr, safe to dereference
  109. if (enc_dif > lcd_encoder_diff && current_selection != LCD_LEFT_BUTTON_CHOICE) {
  110. // Rotating knob counter clockwise
  111. current_selection--;
  112. } else if (enc_dif < lcd_encoder_diff && current_selection != LCD_RIGHT_BUTTON_CHOICE) {
  113. // Rotating knob clockwise
  114. current_selection++;
  115. }
  116. } else {
  117. if (enc_dif > lcd_encoder_diff && current_selection != LCD_LEFT_BUTTON_CHOICE) {
  118. // Rotating knob counter clockwise
  119. current_selection = LCD_LEFT_BUTTON_CHOICE;
  120. } else if (enc_dif < lcd_encoder_diff && current_selection != LCD_MIDDLE_BUTTON_CHOICE) {
  121. // Rotating knob clockwise
  122. current_selection = LCD_MIDDLE_BUTTON_CHOICE;
  123. }
  124. }
  125. // Update '>' render only
  126. //! @brief Button menu
  127. //!
  128. //! @code{.unparsed}
  129. //! |01234567890123456789|
  130. //! | |
  131. //! | |
  132. //! | |
  133. //! |>(left) |
  134. //! ----------------------
  135. //! Three choices
  136. //! |>(left)>(mid)>(righ)|
  137. //! ----------------------
  138. //! Two choices
  139. //! ----------------------
  140. //! |>(left) >(mid) |
  141. //! ----------------------
  142. //! @endcode
  143. //
  144. lcd_set_cursor(0, 3);
  145. lcd_print(current_selection == LCD_LEFT_BUTTON_CHOICE ? '>': ' ');
  146. if (two_choices == false)
  147. {
  148. lcd_set_cursor(9, 3);
  149. lcd_print(current_selection == LCD_MIDDLE_BUTTON_CHOICE ? '>': ' ');
  150. lcd_set_cursor(18, 3);
  151. lcd_print(current_selection == LCD_RIGHT_BUTTON_CHOICE ? '>': ' ');
  152. } else {
  153. // More button for two button screen
  154. lcd_set_cursor(18, 3);
  155. lcd_print(current_selection == LCD_MIDDLE_BUTTON_CHOICE ? '>': ' ');
  156. }
  157. // Consume rotation event and make feedback sound
  158. enc_dif = lcd_encoder_diff;
  159. Sound_MakeSound(e_SOUND_TYPE_EncoderMove);
  160. }
  161. // Check if knob was clicked and consume the event
  162. if (lcd_clicked()) {
  163. Sound_MakeSound(e_SOUND_TYPE_ButtonEcho);
  164. choice_selected = current_selection;
  165. } else {
  166. // continue monitoring
  167. return ret;
  168. }
  169. if ((two_choices && choice_selected == LCD_MIDDLE_BUTTON_CHOICE) // Two choices and middle button selected
  170. || (!two_choices && choice_selected == LCD_RIGHT_BUTTON_CHOICE)) // Three choices and right most button selected
  171. {
  172. // 'More' show error description
  173. lcd_show_fullscreen_message_and_wait_P(_T(PrusaErrorDesc(ei)));
  174. ret = 1;
  175. } else if(choice_selected == LCD_MIDDLE_BUTTON_CHOICE) {
  176. SetButtonResponse((ButtonOperations)button_op_right);
  177. ret = 2;
  178. } else {
  179. SetButtonResponse((ButtonOperations)button_op_middle);
  180. ret = 2;
  181. }
  182. // Reset static variables to their default value
  183. current_selection = two_choices ? LCD_LEFT_BUTTON_CHOICE : LCD_MIDDLE_BUTTON_CHOICE;
  184. choice_selected = -1;
  185. return ret;
  186. }
  187. enum class ReportErrorHookStates : uint8_t {
  188. RENDER_ERROR_SCREEN = 0,
  189. MONITOR_SELECTION = 1,
  190. DISMISS_ERROR_SCREEN = 2,
  191. };
  192. enum ReportErrorHookStates ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  193. void ReportErrorHook(uint16_t ec) {
  194. if (mmu2.MMUCurrentErrorCode() == ErrorCode::OK && mmu2.MMULastErrorSource() == MMU2::ErrorSourceMMU) {
  195. // If the error code suddenly changes to OK, that means
  196. // a button was pushed on the MMU and the LCD should
  197. // dismiss the error screen until MMU raises a new error
  198. ReportErrorHookState = ReportErrorHookStates::DISMISS_ERROR_SCREEN;
  199. }
  200. const uint8_t ei = PrusaErrorCodeIndex(ec);
  201. switch ((uint8_t)ReportErrorHookState) {
  202. case (uint8_t)ReportErrorHookStates::RENDER_ERROR_SCREEN:
  203. ReportErrorHookStaticRender(ei);
  204. ReportErrorHookState = ReportErrorHookStates::MONITOR_SELECTION;
  205. IncrementMMUFails();
  206. // check if it is a "power" failure - we consider TMC-related errors as power failures
  207. if( (uint16_t)ec & 0x7e00 ){ // @@TODO can be optimized to uint8_t operation
  208. // TMC-related errors are from 0x8200 higher
  209. // we can increment a power error at this spot
  210. mmu2.IncrementTMCFailures();
  211. }
  212. [[fallthrough]];
  213. case (uint8_t)ReportErrorHookStates::MONITOR_SELECTION:
  214. mmu2.is_mmu_error_monitor_active = true;
  215. ReportErrorHookDynamicRender(); // Render dynamic characters
  216. switch (ReportErrorHookMonitor(ei)) {
  217. case 0:
  218. // No choice selected, return to loop()
  219. break;
  220. case 1:
  221. // More button selected, change state
  222. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  223. break;
  224. case 2:
  225. // Exit error screen and enable lcd updates
  226. lcd_set_custom_characters();
  227. lcd_update_enable(true);
  228. lcd_return_to_status();
  229. // Reset the state in case a new error is reported
  230. mmu2.is_mmu_error_monitor_active = false;
  231. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  232. break;
  233. default:
  234. break;
  235. }
  236. return; // Always return to loop() to let MMU trigger a call to ReportErrorHook again
  237. break;
  238. case (uint8_t)ReportErrorHookStates::DISMISS_ERROR_SCREEN:
  239. lcd_set_custom_characters();
  240. lcd_update_enable(true);
  241. lcd_return_to_status();
  242. // Reset the state in case a new error is reported
  243. mmu2.is_mmu_error_monitor_active = false;
  244. ReportErrorHookState = ReportErrorHookStates::RENDER_ERROR_SCREEN;
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. void ReportProgressHook(CommandInProgress cip, uint16_t ec) {
  251. if (cip != CommandInProgress::NoCommand) {
  252. custom_message_type = CustomMsg::MMUProgress;
  253. lcd_setstatuspgm( _T(ProgressCodeToText(ec)) );
  254. } else {
  255. // If there is no command in progress we can display other
  256. // useful information such as the name of the SD file
  257. // being printed
  258. custom_message_type = CustomMsg::Status;
  259. }
  260. }
  261. void IncrementLoadFails(){
  262. eeprom_increment_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL);
  263. eeprom_increment_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT);
  264. }
  265. void IncrementMMUFails(){
  266. eeprom_increment_byte((uint8_t *)EEPROM_MMU_FAIL);
  267. eeprom_increment_word((uint16_t *)EEPROM_MMU_FAIL_TOT);
  268. }
  269. } // namespace MMU2