My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ultralcd.cpp 47KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #ifdef LED_BACKLIGHT_TIMEOUT
  24. #include "../feature/leds/leds.h"
  25. #endif
  26. #if ENABLED(HOST_ACTION_COMMANDS)
  27. #include "../feature/host_actions.h"
  28. #endif
  29. // All displays share the MarlinUI class
  30. #include "ultralcd.h"
  31. MarlinUI ui;
  32. #if HAS_DISPLAY
  33. #include "../module/printcounter.h"
  34. #include "../MarlinCore.h"
  35. #include "../gcode/queue.h"
  36. #include "fontutils.h"
  37. #include "../sd/cardreader.h"
  38. #if ENABLED(EXTENSIBLE_UI)
  39. #define START_OF_UTF8_CHAR(C) (((C) & 0xC0u) != 0x80u)
  40. #endif
  41. #endif
  42. #if LCD_HAS_WAIT_FOR_MOVE
  43. bool MarlinUI::wait_for_move; // = false
  44. #endif
  45. #if HAS_SPI_LCD
  46. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  47. uint8_t MarlinUI::status_scroll_offset; // = 0
  48. constexpr uint8_t MAX_MESSAGE_LENGTH = _MAX(LONG_FILENAME_LENGTH, MAX_LANG_CHARSIZE * 2 * (LCD_WIDTH));
  49. #else
  50. constexpr uint8_t MAX_MESSAGE_LENGTH = MAX_LANG_CHARSIZE * (LCD_WIDTH);
  51. #endif
  52. #elif ENABLED(EXTENSIBLE_UI)
  53. constexpr uint8_t MAX_MESSAGE_LENGTH = 63;
  54. #endif
  55. #if EITHER(HAS_SPI_LCD, EXTENSIBLE_UI)
  56. uint8_t MarlinUI::alert_level; // = 0
  57. char MarlinUI::status_message[MAX_MESSAGE_LENGTH + 1];
  58. #endif
  59. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  60. MarlinUI::progress_t MarlinUI::progress_override; // = 0
  61. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  62. uint32_t MarlinUI::remaining_time;
  63. #endif
  64. #endif
  65. #if ENABLED(PCA9632_BUZZER) || USE_BEEPER
  66. #include "../libs/buzzer.h" // for BUZZ() macro
  67. #if ENABLED(PCA9632_BUZZER)
  68. #include "../feature/leds/pca9632.h"
  69. #endif
  70. void MarlinUI::buzz(const long duration, const uint16_t freq) {
  71. #if ENABLED(PCA9632_BUZZER)
  72. pca9632_buzz(duration, freq);
  73. #elif USE_BEEPER
  74. buzzer.tone(duration, freq);
  75. #endif
  76. }
  77. #endif
  78. #if HAS_SPI_LCD
  79. #if HAS_GRAPHICAL_LCD
  80. #include "dogm/ultralcd_DOGM.h"
  81. #endif
  82. #include "lcdprint.h"
  83. #include "../sd/cardreader.h"
  84. #include "../module/temperature.h"
  85. #include "../module/planner.h"
  86. #include "../module/motion.h"
  87. #if ENABLED(AUTO_BED_LEVELING_UBL)
  88. #include "../feature/bedlevel/bedlevel.h"
  89. #endif
  90. #if HAS_TRINAMIC_CONFIG
  91. #include "../feature/tmc_util.h"
  92. #endif
  93. #if HAS_ADC_BUTTONS
  94. #include "../module/thermistor/thermistors.h"
  95. #endif
  96. #if HAS_ENCODER_ACTION
  97. volatile uint8_t MarlinUI::buttons;
  98. #if HAS_SLOW_BUTTONS
  99. volatile uint8_t MarlinUI::slow_buttons;
  100. #endif
  101. #if ENABLED(TOUCH_BUTTONS)
  102. #include "../feature/touch/xpt2046.h"
  103. #endif
  104. #endif
  105. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  106. bool MarlinUI::defer_return_to_status;
  107. #endif
  108. uint8_t MarlinUI::lcd_status_update_delay = 1; // First update one loop delayed
  109. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  110. millis_t MarlinUI::next_filament_display; // = 0
  111. #endif
  112. millis_t MarlinUI::next_button_update_ms; // = 0
  113. #if HAS_GRAPHICAL_LCD
  114. bool MarlinUI::drawing_screen, MarlinUI::first_page; // = false
  115. #endif
  116. // Encoder Handling
  117. #if HAS_ENCODER_ACTION
  118. uint32_t MarlinUI::encoderPosition;
  119. volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
  120. #endif
  121. #if HAS_LCD_MENU
  122. #include "menu/menu.h"
  123. #include "../sd/cardreader.h"
  124. #if ENABLED(SDSUPPORT)
  125. #if ENABLED(SCROLL_LONG_FILENAMES)
  126. uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
  127. #endif
  128. const char * MarlinUI::scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll) {
  129. const char *outstr = theCard.longest_filename();
  130. if (theCard.longFilename[0]) {
  131. #if ENABLED(SCROLL_LONG_FILENAMES)
  132. if (doScroll) {
  133. for (uint8_t l = FILENAME_LENGTH; l--;)
  134. hash = ((hash << 1) | (hash >> 7)) ^ theCard.filename[l]; // rotate, xor
  135. static uint8_t filename_scroll_hash;
  136. if (filename_scroll_hash != hash) { // If the hash changed...
  137. filename_scroll_hash = hash; // Save the new hash
  138. filename_scroll_max = _MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
  139. filename_scroll_pos = 0; // Reset scroll to the start
  140. lcd_status_update_delay = 8; // Don't scroll right away
  141. }
  142. outstr += filename_scroll_pos;
  143. }
  144. #else
  145. theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge
  146. #endif
  147. }
  148. return outstr;
  149. }
  150. #endif
  151. screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR
  152. bool MarlinUI::screen_changed;
  153. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  154. bool MarlinUI::encoderRateMultiplierEnabled;
  155. millis_t MarlinUI::lastEncoderMovementMillis = 0;
  156. void MarlinUI::enable_encoder_multiplier(const bool onoff) {
  157. encoderRateMultiplierEnabled = onoff;
  158. lastEncoderMovementMillis = 0;
  159. }
  160. #endif
  161. #if EITHER(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION)
  162. int8_t MarlinUI::encoderDirection = ENCODERBASE;
  163. #endif
  164. #if ENABLED(TOUCH_BUTTONS)
  165. uint8_t MarlinUI::touch_buttons;
  166. uint8_t MarlinUI::repeat_delay;
  167. #endif
  168. bool MarlinUI::lcd_clicked;
  169. float move_menu_scale;
  170. bool MarlinUI::use_click() {
  171. const bool click = lcd_clicked;
  172. lcd_clicked = false;
  173. return click;
  174. }
  175. #if EITHER(AUTO_BED_LEVELING_UBL, G26_MESH_VALIDATION)
  176. bool MarlinUI::external_control; // = false
  177. void MarlinUI::wait_for_release() {
  178. while (button_pressed()) safe_delay(50);
  179. safe_delay(50);
  180. }
  181. #endif
  182. void _wrap_string(uint8_t &col, uint8_t &row, const char * const string, read_byte_cb_t cb_read_byte, bool wordwrap/*=false*/) {
  183. SETCURSOR(col, row);
  184. if (!string) return;
  185. auto _newline = [&col, &row]{
  186. col = 0; row++; // Move col to string len (plus space)
  187. SETCURSOR(0, row); // Simulate carriage return
  188. };
  189. uint8_t *p = (uint8_t*)string;
  190. wchar_t ch;
  191. if (wordwrap) {
  192. uint8_t *wrd = nullptr, c = 0;
  193. // find the end of the part
  194. for (;;) {
  195. if (!wrd) wrd = p; // Get word start /before/ advancing
  196. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  197. const bool eol = !ch; // zero ends the string
  198. // End or a break between phrases?
  199. if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
  200. if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
  201. // Past the right and the word is not too long?
  202. if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
  203. c += !eol; // +1 so the space will be printed
  204. col += c; // advance col to new position
  205. while (c) { // character countdown
  206. --c; // count down to zero
  207. wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
  208. lcd_put_wchar(ch); // character to the LCD
  209. }
  210. if (eol) break; // all done!
  211. wrd = nullptr; // set up for next word
  212. }
  213. else c++; // count word characters
  214. }
  215. }
  216. else {
  217. for (;;) {
  218. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  219. if (!ch) break;
  220. lcd_put_wchar(ch);
  221. col++;
  222. if (col >= LCD_WIDTH) _newline();
  223. }
  224. }
  225. }
  226. void MarlinUI::draw_select_screen_prompt(PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  227. const uint8_t plen = utf8_strlen_P(pref), slen = suff ? utf8_strlen_P(suff) : 0;
  228. uint8_t col = 0, row = 0;
  229. if (!string && plen + slen <= LCD_WIDTH) {
  230. col = (LCD_WIDTH - plen - slen) / 2;
  231. row = LCD_HEIGHT > 3 ? 1 : 0;
  232. }
  233. wrap_string_P(col, row, pref, true);
  234. if (string) {
  235. if (col) { col = 0; row++; } // Move to the start of the next line
  236. wrap_string(col, row, string);
  237. }
  238. if (suff) wrap_string_P(col, row, suff);
  239. }
  240. #endif // HAS_LCD_MENU
  241. void MarlinUI::init() {
  242. init_lcd();
  243. #if HAS_DIGITAL_BUTTONS
  244. #if BUTTON_EXISTS(EN1)
  245. SET_INPUT_PULLUP(BTN_EN1);
  246. #endif
  247. #if BUTTON_EXISTS(EN2)
  248. SET_INPUT_PULLUP(BTN_EN2);
  249. #endif
  250. #if BUTTON_EXISTS(ENC)
  251. SET_INPUT_PULLUP(BTN_ENC);
  252. #endif
  253. #if BUTTON_EXISTS(BACK)
  254. SET_INPUT_PULLUP(BTN_BACK);
  255. #endif
  256. #if BUTTON_EXISTS(UP)
  257. SET_INPUT(BTN_UP);
  258. #endif
  259. #if BUTTON_EXISTS(DWN)
  260. SET_INPUT(BTN_DWN);
  261. #endif
  262. #if BUTTON_EXISTS(LFT)
  263. SET_INPUT(BTN_LFT);
  264. #endif
  265. #if BUTTON_EXISTS(RT)
  266. SET_INPUT(BTN_RT);
  267. #endif
  268. #endif // !HAS_DIGITAL_BUTTONS
  269. #if HAS_SHIFT_ENCODER
  270. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  271. SET_OUTPUT(SR_DATA_PIN);
  272. SET_OUTPUT(SR_CLK_PIN);
  273. #elif defined(SHIFT_CLK)
  274. SET_OUTPUT(SHIFT_CLK);
  275. OUT_WRITE(SHIFT_LD, HIGH);
  276. #if defined(SHIFT_EN) && SHIFT_EN >= 0
  277. OUT_WRITE(SHIFT_EN, LOW);
  278. #endif
  279. SET_INPUT_PULLUP(SHIFT_OUT);
  280. #endif
  281. #endif // HAS_SHIFT_ENCODER
  282. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  283. SET_INPUT_PULLUP(SD_DETECT_PIN);
  284. #endif
  285. #if HAS_ENCODER_ACTION && HAS_SLOW_BUTTONS
  286. slow_buttons = 0;
  287. #endif
  288. update_buttons();
  289. TERN_(HAS_ENCODER_ACTION, encoderDiff = 0);
  290. }
  291. bool MarlinUI::get_blink() {
  292. static uint8_t blink = 0;
  293. static millis_t next_blink_ms = 0;
  294. millis_t ms = millis();
  295. if (ELAPSED(ms, next_blink_ms)) {
  296. blink ^= 0xFF;
  297. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  298. }
  299. return blink != 0;
  300. }
  301. ////////////////////////////////////////////
  302. ///////////// Keypad Handling //////////////
  303. ////////////////////////////////////////////
  304. #if BOTH(REPRAPWORLD_KEYPAD, HAS_ENCODER_ACTION)
  305. volatile uint8_t MarlinUI::keypad_buttons;
  306. #if HAS_LCD_MENU && !HAS_ADC_BUTTONS
  307. void lcd_move_x();
  308. void lcd_move_y();
  309. void lcd_move_z();
  310. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  311. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  312. ui.encoderPosition = dir;
  313. switch (axis) {
  314. case X_AXIS: lcd_move_x(); break;
  315. case Y_AXIS: lcd_move_y(); break;
  316. case Z_AXIS: lcd_move_z();
  317. default: break;
  318. }
  319. }
  320. #endif
  321. bool MarlinUI::handle_keypad() {
  322. #if HAS_ADC_BUTTONS
  323. #define ADC_MIN_KEY_DELAY 100
  324. if (keypad_buttons) {
  325. #if HAS_ENCODER_ACTION
  326. refresh(LCDVIEW_REDRAW_NOW);
  327. #if HAS_LCD_MENU
  328. if (encoderDirection == -(ENCODERBASE)) { // ADC_KEYPAD forces REVERSE_MENU_DIRECTION, so this indicates menu navigation
  329. if (RRK(EN_KEYPAD_UP)) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  330. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  331. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  332. else if (RRK(EN_KEYPAD_RIGHT)) { return_to_status(); quick_feedback(); }
  333. }
  334. else
  335. #endif
  336. {
  337. #if HAS_LCD_MENU
  338. if (RRK(EN_KEYPAD_UP)) encoderPosition -= ENCODER_PULSES_PER_STEP;
  339. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += ENCODER_PULSES_PER_STEP;
  340. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  341. else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
  342. #else
  343. if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= ENCODER_PULSES_PER_STEP;
  344. else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += ENCODER_PULSES_PER_STEP;
  345. #endif
  346. }
  347. #endif
  348. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  349. return true;
  350. }
  351. #else // !HAS_ADC_BUTTONS
  352. static uint8_t keypad_debounce = 0;
  353. if (!RRK( EN_KEYPAD_F1 | EN_KEYPAD_F2
  354. | EN_KEYPAD_F3 | EN_KEYPAD_DOWN
  355. | EN_KEYPAD_RIGHT | EN_KEYPAD_MIDDLE
  356. | EN_KEYPAD_UP | EN_KEYPAD_LEFT )
  357. ) {
  358. if (keypad_debounce > 0) keypad_debounce--;
  359. }
  360. else if (!keypad_debounce) {
  361. keypad_debounce = 2;
  362. const bool homed = all_axes_homed();
  363. #if HAS_LCD_MENU
  364. if (RRK(EN_KEYPAD_MIDDLE)) goto_screen(menu_move);
  365. #if DISABLED(DELTA) && Z_HOME_DIR < 0
  366. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  367. #endif
  368. if (homed) {
  369. #if ENABLED(DELTA) || Z_HOME_DIR != -1
  370. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  371. #endif
  372. if (RRK(EN_KEYPAD_F3)) _reprapworld_keypad_move(Z_AXIS, -1);
  373. if (RRK(EN_KEYPAD_LEFT)) _reprapworld_keypad_move(X_AXIS, -1);
  374. if (RRK(EN_KEYPAD_RIGHT)) _reprapworld_keypad_move(X_AXIS, 1);
  375. if (RRK(EN_KEYPAD_DOWN)) _reprapworld_keypad_move(Y_AXIS, 1);
  376. if (RRK(EN_KEYPAD_UP)) _reprapworld_keypad_move(Y_AXIS, -1);
  377. }
  378. #endif // HAS_LCD_MENU
  379. if (!homed && RRK(EN_KEYPAD_F1)) queue.inject_P(G28_STR);
  380. return true;
  381. }
  382. #endif // !ADC_KEYPAD
  383. return false;
  384. }
  385. #endif // REPRAPWORLD_KEYPAD
  386. /**
  387. * Status Screen
  388. *
  389. * This is very display-dependent, so the lcd implementation draws this.
  390. */
  391. #if ENABLED(LCD_PROGRESS_BAR)
  392. millis_t MarlinUI::progress_bar_ms; // = 0
  393. #if PROGRESS_MSG_EXPIRE > 0
  394. millis_t MarlinUI::expire_status_ms; // = 0
  395. #endif
  396. #endif
  397. void MarlinUI::status_screen() {
  398. TERN_(HAS_LCD_MENU, ENCODER_RATE_MULTIPLY(false));
  399. #if ENABLED(LCD_PROGRESS_BAR)
  400. //
  401. // HD44780 implements the following message blinking and
  402. // message expiration because Status Line and Progress Bar
  403. // share the same line on the display.
  404. //
  405. #if DISABLED(PROGRESS_MSG_ONCE) || (PROGRESS_MSG_EXPIRE > 0)
  406. #define GOT_MS
  407. const millis_t ms = millis();
  408. #endif
  409. // If the message will blink rather than expire...
  410. #if DISABLED(PROGRESS_MSG_ONCE)
  411. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  412. progress_bar_ms = ms;
  413. #endif
  414. #if PROGRESS_MSG_EXPIRE > 0
  415. // Handle message expire
  416. if (expire_status_ms) {
  417. // Expire the message if a job is active and the bar has ticks
  418. if (get_progress_percent() > 2 && !print_job_timer.isPaused()) {
  419. if (ELAPSED(ms, expire_status_ms)) {
  420. status_message[0] = '\0';
  421. expire_status_ms = 0;
  422. }
  423. }
  424. else {
  425. // Defer message expiration before bar appears
  426. // and during any pause (not just SD)
  427. expire_status_ms += LCD_UPDATE_INTERVAL;
  428. }
  429. }
  430. #endif // PROGRESS_MSG_EXPIRE
  431. #endif // LCD_PROGRESS_BAR
  432. #if HAS_LCD_MENU
  433. if (use_click()) {
  434. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  435. next_filament_display = millis() + 5000UL; // Show status message for 5s
  436. #endif
  437. goto_screen(menu_main);
  438. #if DISABLED(NO_LCD_REINIT)
  439. init_lcd(); // May revive the LCD if static electricity killed it
  440. #endif
  441. return;
  442. }
  443. #endif
  444. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  445. const int16_t old_frm = feedrate_percentage;
  446. int16_t new_frm = old_frm + int16_t(encoderPosition);
  447. // Dead zone at 100% feedrate
  448. if (old_frm == 100) {
  449. if (int16_t(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  450. new_frm -= ENCODER_FEEDRATE_DEADZONE;
  451. else if (int16_t(encoderPosition) < -(ENCODER_FEEDRATE_DEADZONE))
  452. new_frm += ENCODER_FEEDRATE_DEADZONE;
  453. else
  454. new_frm = old_frm;
  455. }
  456. else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100))
  457. new_frm = 100;
  458. LIMIT(new_frm, 10, 999);
  459. if (old_frm != new_frm) {
  460. feedrate_percentage = new_frm;
  461. encoderPosition = 0;
  462. #if BOTH(HAS_BUZZER, BEEP_ON_FEEDRATE_CHANGE)
  463. static millis_t next_beep;
  464. #ifndef GOT_MS
  465. const millis_t ms = millis();
  466. #endif
  467. if (ELAPSED(ms, next_beep)) {
  468. buzz(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
  469. next_beep = ms + 500UL;
  470. }
  471. #endif
  472. }
  473. #endif // ULTIPANEL_FEEDMULTIPLY
  474. draw_status_screen();
  475. }
  476. void MarlinUI::kill_screen(PGM_P lcd_error, PGM_P lcd_component) {
  477. init();
  478. status_printf_P(1, PSTR(S_FMT ": " S_FMT), lcd_error, lcd_component);
  479. TERN_(HAS_LCD_MENU, return_to_status());
  480. // RED ALERT. RED ALERT.
  481. #ifdef LED_BACKLIGHT_TIMEOUT
  482. leds.set_color(LEDColorRed());
  483. #ifdef NEOPIXEL_BKGD_LED_INDEX
  484. neo.set_pixel_color(NEOPIXEL_BKGD_LED_INDEX, 255, 0, 0, 0);
  485. neo.show();
  486. #endif
  487. #endif
  488. draw_kill_screen();
  489. }
  490. void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
  491. TERN_(HAS_LCD_MENU, refresh());
  492. #if HAS_ENCODER_ACTION
  493. if (clear_buttons) buttons = 0;
  494. next_button_update_ms = millis() + 500;
  495. #else
  496. UNUSED(clear_buttons);
  497. #endif
  498. #if HAS_BUZZER
  499. // Buzz and wait. Is the delay needed for buttons to settle?
  500. buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  501. #if HAS_LCD_MENU
  502. #if USE_BEEPER
  503. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  504. #else
  505. delay(10);
  506. #endif
  507. #endif
  508. #endif
  509. }
  510. ////////////////////////////////////////////
  511. /////////////// Manual Move ////////////////
  512. ////////////////////////////////////////////
  513. #if HAS_LCD_MENU
  514. int8_t manual_move_axis = (int8_t)NO_AXIS;
  515. millis_t manual_move_start_time = 0;
  516. #if IS_KINEMATIC
  517. bool MarlinUI::processing_manual_move = false;
  518. float manual_move_offset = 0;
  519. #endif
  520. #if E_MANUAL > 1
  521. int8_t MarlinUI::manual_move_e_index = 0;
  522. #endif
  523. /**
  524. * If the most recent manual move hasn't been fed to the planner yet,
  525. * and the planner can accept one, send a move immediately.
  526. */
  527. void MarlinUI::manage_manual_move() {
  528. if (processing_manual_move) return;
  529. if (manual_move_axis != (int8_t)NO_AXIS && ELAPSED(millis(), manual_move_start_time) && !planner.is_full()) {
  530. const feedRate_t fr_mm_s = manual_feedrate_mm_s[manual_move_axis];
  531. #if IS_KINEMATIC
  532. #if EXTRUDERS > 1
  533. const int8_t old_extruder = active_extruder;
  534. if (manual_move_axis == E_AXIS) active_extruder = manual_move_e_index;
  535. #endif
  536. // Set movement on a single axis
  537. destination = current_position;
  538. destination[manual_move_axis] += manual_move_offset;
  539. // Reset for the next move
  540. manual_move_offset = 0;
  541. manual_move_axis = (int8_t)NO_AXIS;
  542. // DELTA and SCARA machines use segmented moves, which could fill the planner during the call to
  543. // move_to_destination. This will cause idle() to be called, which can then call this function while the
  544. // previous invocation is being blocked. Modifications to manual_move_offset shouldn't be made while
  545. // processing_manual_move is true or the planner will get out of sync.
  546. processing_manual_move = true;
  547. prepare_internal_move_to_destination(fr_mm_s); // will set current_position from destination
  548. processing_manual_move = false;
  549. #if EXTRUDERS > 1
  550. active_extruder = old_extruder;
  551. #endif
  552. #else
  553. planner.buffer_line(current_position, fr_mm_s, manual_move_axis == E_AXIS ? manual_move_e_index : active_extruder);
  554. manual_move_axis = (int8_t)NO_AXIS;
  555. #endif
  556. }
  557. }
  558. #endif // HAS_LCD_MENU
  559. /**
  560. * Update the LCD, read encoder buttons, etc.
  561. * - Read button states
  562. * - Check the SD Card slot state
  563. * - Act on RepRap World keypad input
  564. * - Update the encoder position
  565. * - Apply acceleration to the encoder position
  566. * - Do refresh(LCDVIEW_CALL_REDRAW_NOW) on controller events
  567. * - Reset the Info Screen timeout if there's any input
  568. * - Update status indicators, if any
  569. *
  570. * Run the current LCD menu handler callback function:
  571. * - Call the handler only if lcdDrawUpdate != LCDVIEW_NONE
  572. * - Before calling the handler, LCDVIEW_CALL_NO_REDRAW => LCDVIEW_NONE
  573. * - Call the menu handler. Menu handlers should do the following:
  574. * - If a value changes, set lcdDrawUpdate to LCDVIEW_REDRAW_NOW and draw the value
  575. * (Encoder events automatically set lcdDrawUpdate for you.)
  576. * - if (should_draw()) { redraw }
  577. * - Before exiting the handler set lcdDrawUpdate to:
  578. * - LCDVIEW_CLEAR_CALL_REDRAW to clear screen and set LCDVIEW_CALL_REDRAW_NEXT.
  579. * - LCDVIEW_REDRAW_NOW to draw now (including remaining stripes).
  580. * - LCDVIEW_CALL_REDRAW_NEXT to draw now and get LCDVIEW_REDRAW_NOW on the next loop.
  581. * - LCDVIEW_CALL_NO_REDRAW to draw now and get LCDVIEW_NONE on the next loop.
  582. * - NOTE: For graphical displays menu handlers may be called 2 or more times per loop,
  583. * so don't change lcdDrawUpdate without considering this.
  584. *
  585. * After the menu handler callback runs (or not):
  586. * - Clear the LCD if lcdDrawUpdate == LCDVIEW_CLEAR_CALL_REDRAW
  587. * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually)
  588. *
  589. * This function is only called from the main thread.
  590. */
  591. LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  592. millis_t next_lcd_update_ms;
  593. void MarlinUI::update() {
  594. static uint16_t max_display_update_time = 0;
  595. millis_t ms = millis();
  596. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  597. static millis_t return_to_status_ms = 0;
  598. #define RESET_STATUS_TIMEOUT() (return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS)
  599. #else
  600. #define RESET_STATUS_TIMEOUT() NOOP
  601. #endif
  602. #ifdef LED_BACKLIGHT_TIMEOUT
  603. leds.update_timeout(powersupply_on);
  604. #endif
  605. #if HAS_LCD_MENU
  606. // Handle any queued Move Axis motion
  607. manage_manual_move();
  608. // Update button states for button_pressed(), etc.
  609. // If the state changes the next update may be delayed 300-500ms.
  610. update_buttons();
  611. // If the action button is pressed...
  612. static bool wait_for_unclick; // = false
  613. auto do_click = [&]{
  614. wait_for_unclick = true; // - Set debounce flag to ignore continous clicks
  615. lcd_clicked = !wait_for_user; // - Keep the click if not waiting for a user-click
  616. wait_for_user = false; // - Any click clears wait for user
  617. quick_feedback(); // - Always make a click sound
  618. };
  619. #if ENABLED(TOUCH_BUTTONS)
  620. if (touch_buttons) {
  621. RESET_STATUS_TIMEOUT();
  622. if (touch_buttons & (EN_A | EN_B)) { // Menu arrows, in priority
  623. if (ELAPSED(ms, next_button_update_ms)) {
  624. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * (ENCODER_PULSES_PER_STEP) * encoderDirection;
  625. if (touch_buttons & EN_A) encoderDiff *= -1;
  626. TERN_(AUTO_BED_LEVELING_UBL, if (external_control) ubl.encoder_diff = encoderDiff);
  627. next_button_update_ms = ms + repeat_delay; // Assume the repeat delay
  628. if (!wait_for_unclick) {
  629. next_button_update_ms += 250; // Longer delay on first press
  630. wait_for_unclick = true; // Avoid Back/Select click while repeating
  631. TERN_(HAS_BUZZER, buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ));
  632. }
  633. }
  634. }
  635. else if (!wait_for_unclick && (buttons & EN_C)) // OK button, if not waiting for a debounce release:
  636. do_click();
  637. }
  638. else // keep wait_for_unclick value
  639. #endif // TOUCH_BUTTONS
  640. {
  641. // Integrated LCD click handling via button_pressed
  642. if (!external_control && button_pressed()) {
  643. if (!wait_for_unclick) do_click(); // Handle the click
  644. }
  645. else
  646. wait_for_unclick = false;
  647. }
  648. if (LCD_BACK_CLICKED()) {
  649. quick_feedback();
  650. goto_previous_screen();
  651. }
  652. #endif // HAS_LCD_MENU
  653. if (ELAPSED(ms, next_lcd_update_ms) || TERN0(HAS_GRAPHICAL_LCD, drawing_screen)) {
  654. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL;
  655. #if ENABLED(TOUCH_BUTTONS)
  656. if (on_status_screen()) next_lcd_update_ms += (LCD_UPDATE_INTERVAL) * 2;
  657. TERN_(HAS_ENCODER_ACTION, touch_buttons = touch.read_buttons());
  658. #endif
  659. TERN_(LCD_HAS_STATUS_INDICATORS, update_indicators());
  660. #if HAS_ENCODER_ACTION
  661. TERN_(HAS_SLOW_BUTTONS, slow_buttons = read_slow_buttons()); // Buttons that take too long to read in interrupt context
  662. if (TERN0(REPRAPWORLD_KEYPAD, handle_keypad()))
  663. RESET_STATUS_TIMEOUT();
  664. const float abs_diff = ABS(encoderDiff);
  665. const bool encoderPastThreshold = (abs_diff >= (ENCODER_PULSES_PER_STEP));
  666. if (encoderPastThreshold || lcd_clicked) {
  667. if (encoderPastThreshold) {
  668. #if BOTH(HAS_LCD_MENU, ENCODER_RATE_MULTIPLIER)
  669. int32_t encoderMultiplier = 1;
  670. if (encoderRateMultiplierEnabled) {
  671. const float encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  672. if (lastEncoderMovementMillis) {
  673. // Note that the rate is always calculated between two passes through the
  674. // loop and that the abs of the encoderDiff value is tracked.
  675. const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000;
  676. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  677. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  678. #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
  679. SERIAL_ECHO_START();
  680. SERIAL_ECHOPAIR("Enc Step Rate: ", encoderStepRate);
  681. SERIAL_ECHOPAIR(" Multiplier: ", encoderMultiplier);
  682. SERIAL_ECHOPAIR(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
  683. SERIAL_ECHOPAIR(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
  684. SERIAL_EOL();
  685. #endif
  686. }
  687. lastEncoderMovementMillis = ms;
  688. } // encoderRateMultiplierEnabled
  689. #else
  690. constexpr int32_t encoderMultiplier = 1;
  691. #endif // ENCODER_RATE_MULTIPLIER
  692. encoderPosition += (encoderDiff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  693. encoderDiff = 0;
  694. }
  695. RESET_STATUS_TIMEOUT();
  696. refresh(LCDVIEW_REDRAW_NOW);
  697. #ifdef LED_BACKLIGHT_TIMEOUT
  698. leds.reset_timeout(ms);
  699. #endif
  700. }
  701. #endif
  702. // This runs every ~100ms when idling often enough.
  703. // Instead of tracking changes just redraw the Status Screen once per second.
  704. if (on_status_screen() && !lcd_status_update_delay--) {
  705. lcd_status_update_delay = 9
  706. #if HAS_GRAPHICAL_LCD
  707. + 3
  708. #endif
  709. ;
  710. max_display_update_time--;
  711. refresh(LCDVIEW_REDRAW_NOW);
  712. }
  713. #if BOTH(HAS_LCD_MENU, SCROLL_LONG_FILENAMES)
  714. // If scrolling of long file names is enabled and we are in the sd card menu,
  715. // cause a refresh to occur until all the text has scrolled into view.
  716. if (currentScreen == menu_media && !lcd_status_update_delay--) {
  717. lcd_status_update_delay = 4;
  718. if (++filename_scroll_pos > filename_scroll_max) {
  719. filename_scroll_pos = 0;
  720. lcd_status_update_delay = 12;
  721. }
  722. refresh(LCDVIEW_REDRAW_NOW);
  723. RESET_STATUS_TIMEOUT();
  724. }
  725. #endif
  726. // then we want to use 1/2 of the time only.
  727. uint16_t bbr2 = planner.block_buffer_runtime() >> 1;
  728. if ((should_draw() || drawing_screen) && (!bbr2 || bbr2 > max_display_update_time)) {
  729. // Change state of drawing flag between screen updates
  730. if (!drawing_screen) switch (lcdDrawUpdate) {
  731. case LCDVIEW_CALL_NO_REDRAW:
  732. refresh(LCDVIEW_NONE);
  733. break;
  734. case LCDVIEW_CLEAR_CALL_REDRAW:
  735. case LCDVIEW_CALL_REDRAW_NEXT:
  736. refresh(LCDVIEW_REDRAW_NOW);
  737. case LCDVIEW_REDRAW_NOW: // set above, or by a handler through LCDVIEW_CALL_REDRAW_NEXT
  738. case LCDVIEW_NONE:
  739. break;
  740. } // switch
  741. TERN_(HAS_ADC_BUTTONS, keypad_buttons = 0);
  742. #if HAS_GRAPHICAL_LCD
  743. #if ENABLED(LIGHTWEIGHT_UI)
  744. const bool in_status = on_status_screen(),
  745. do_u8g_loop = !in_status;
  746. lcd_in_status(in_status);
  747. if (in_status) status_screen();
  748. #else
  749. constexpr bool do_u8g_loop = true;
  750. #endif
  751. if (do_u8g_loop) {
  752. if (!drawing_screen) { // If not already drawing pages
  753. u8g.firstPage(); // Start the first page
  754. drawing_screen = first_page = true; // Flag as drawing pages
  755. }
  756. set_font(FONT_MENU); // Setup font for every page draw
  757. u8g.setColorIndex(1); // And reset the color
  758. run_current_screen(); // Draw and process the current screen
  759. first_page = false;
  760. // The screen handler can clear drawing_screen for an action that changes the screen.
  761. // If still drawing and there's another page, update max-time and return now.
  762. // The nextPage will already be set up on the next call.
  763. if (drawing_screen && (drawing_screen = u8g.nextPage())) {
  764. if (on_status_screen())
  765. NOLESS(max_display_update_time, millis() - ms);
  766. return;
  767. }
  768. }
  769. #else
  770. run_current_screen();
  771. #endif
  772. TERN_(HAS_LCD_MENU, lcd_clicked = false);
  773. // Keeping track of the longest time for an individual LCD update.
  774. // Used to do screen throttling when the planner starts to fill up.
  775. if (on_status_screen())
  776. NOLESS(max_display_update_time, millis() - ms);
  777. }
  778. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  779. // Return to Status Screen after a timeout
  780. if (on_status_screen() || defer_return_to_status)
  781. RESET_STATUS_TIMEOUT();
  782. else if (ELAPSED(ms, return_to_status_ms))
  783. return_to_status();
  784. #endif
  785. // Change state of drawing flag between screen updates
  786. if (!drawing_screen) switch (lcdDrawUpdate) {
  787. case LCDVIEW_CLEAR_CALL_REDRAW:
  788. clear_lcd(); break;
  789. case LCDVIEW_REDRAW_NOW:
  790. refresh(LCDVIEW_NONE);
  791. case LCDVIEW_NONE:
  792. case LCDVIEW_CALL_REDRAW_NEXT:
  793. case LCDVIEW_CALL_NO_REDRAW:
  794. default: break;
  795. } // switch
  796. } // ELAPSED(ms, next_lcd_update_ms)
  797. }
  798. #if HAS_ADC_BUTTONS
  799. typedef struct {
  800. uint16_t ADCKeyValueMin, ADCKeyValueMax;
  801. uint8_t ADCKeyNo;
  802. } _stADCKeypadTable_;
  803. #ifndef ADC_BUTTONS_VALUE_SCALE
  804. #define ADC_BUTTONS_VALUE_SCALE 1.0 // for the power voltage equal to the reference voltage
  805. #endif
  806. #ifndef ADC_BUTTONS_R_PULLUP
  807. #define ADC_BUTTONS_R_PULLUP 4.7 // common pull-up resistor in the voltage divider
  808. #endif
  809. #ifndef ADC_BUTTONS_LEFT_R_PULLDOWN
  810. #define ADC_BUTTONS_LEFT_R_PULLDOWN 0.47 // pull-down resistor for LEFT button voltage divider
  811. #endif
  812. #ifndef ADC_BUTTONS_RIGHT_R_PULLDOWN
  813. #define ADC_BUTTONS_RIGHT_R_PULLDOWN 4.7 // pull-down resistor for RIGHT button voltage divider
  814. #endif
  815. #ifndef ADC_BUTTONS_UP_R_PULLDOWN
  816. #define ADC_BUTTONS_UP_R_PULLDOWN 1.0 // pull-down resistor for UP button voltage divider
  817. #endif
  818. #ifndef ADC_BUTTONS_DOWN_R_PULLDOWN
  819. #define ADC_BUTTONS_DOWN_R_PULLDOWN 10.0 // pull-down resistor for DOWN button voltage divider
  820. #endif
  821. #ifndef ADC_BUTTONS_MIDDLE_R_PULLDOWN
  822. #define ADC_BUTTONS_MIDDLE_R_PULLDOWN 2.2 // pull-down resistor for MIDDLE button voltage divider
  823. #endif
  824. // Calculate the ADC value for the voltage divider with specified pull-down resistor value
  825. #define ADC_BUTTON_VALUE(r) int(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
  826. static constexpr uint16_t adc_button_tolerance = HAL_ADC_RANGE * 25 / 1024,
  827. adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
  828. static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
  829. // VALUE_MIN, VALUE_MAX, KEY
  830. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1 }, // F1
  831. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F2 }, // F2
  832. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F3 }, // F3
  833. { ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) - adc_button_tolerance,
  834. ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_LEFT }, // LEFT ( 272 ... 472)
  835. { ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) - adc_button_tolerance,
  836. ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_RIGHT }, // RIGHT (1948 ... 2148)
  837. { ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) - adc_button_tolerance,
  838. ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_UP }, // UP ( 618 ... 818)
  839. { ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) - adc_button_tolerance,
  840. ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_DOWN }, // DOWN (2686 ... 2886)
  841. { ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) - adc_button_tolerance,
  842. ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_MIDDLE }, // ENTER (1205 ... 1405)
  843. };
  844. uint8_t get_ADC_keyValue() {
  845. if (thermalManager.ADCKey_count >= 16) {
  846. const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
  847. thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
  848. thermalManager.ADCKey_count = 0;
  849. if (currentkpADCValue < adc_other_button)
  850. LOOP_L_N(i, ADC_KEY_NUM) {
  851. const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
  852. hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
  853. if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
  854. }
  855. }
  856. return 0;
  857. }
  858. #endif // HAS_ADC_BUTTONS
  859. #if HAS_ENCODER_ACTION
  860. /**
  861. * Read encoder buttons from the hardware registers
  862. * Warning: This function is called from interrupt context!
  863. */
  864. void MarlinUI::update_buttons() {
  865. const millis_t now = millis();
  866. if (ELAPSED(now, next_button_update_ms)) {
  867. #if HAS_DIGITAL_BUTTONS
  868. #if ANY_BUTTON(EN1, EN2, ENC, BACK)
  869. uint8_t newbutton = 0;
  870. #if BUTTON_EXISTS(EN1)
  871. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  872. #endif
  873. #if BUTTON_EXISTS(EN2)
  874. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  875. #endif
  876. #if BUTTON_EXISTS(ENC)
  877. if (BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  878. #endif
  879. #if BUTTON_EXISTS(BACK)
  880. if (BUTTON_PRESSED(BACK)) newbutton |= EN_D;
  881. #endif
  882. #else
  883. constexpr uint8_t newbutton = 0;
  884. #endif
  885. //
  886. // Directional buttons
  887. //
  888. #if ANY_BUTTON(UP, DWN, LFT, RT)
  889. const int8_t pulses = (ENCODER_PULSES_PER_STEP) * encoderDirection;
  890. if (false) {
  891. // for the else-ifs below
  892. }
  893. #if BUTTON_EXISTS(UP)
  894. else if (BUTTON_PRESSED(UP)) {
  895. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  896. next_button_update_ms = now + 300;
  897. }
  898. #endif
  899. #if BUTTON_EXISTS(DWN)
  900. else if (BUTTON_PRESSED(DWN)) {
  901. encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  902. next_button_update_ms = now + 300;
  903. }
  904. #endif
  905. #if BUTTON_EXISTS(LFT)
  906. else if (BUTTON_PRESSED(LFT)) {
  907. encoderDiff = -pulses;
  908. next_button_update_ms = now + 300;
  909. }
  910. #endif
  911. #if BUTTON_EXISTS(RT)
  912. else if (BUTTON_PRESSED(RT)) {
  913. encoderDiff = pulses;
  914. next_button_update_ms = now + 300;
  915. }
  916. #endif
  917. #endif // UP || DWN || LFT || RT
  918. buttons = (newbutton
  919. #if HAS_SLOW_BUTTONS
  920. | slow_buttons
  921. #endif
  922. #if BOTH(TOUCH_BUTTONS, HAS_ENCODER_ACTION)
  923. | (touch_buttons & TERN(HAS_ENCODER_WHEEL, ~(EN_A | EN_B), 0xFF))
  924. #endif
  925. );
  926. #elif HAS_ADC_BUTTONS
  927. buttons = 0;
  928. #endif
  929. #if HAS_ADC_BUTTONS
  930. if (keypad_buttons == 0) {
  931. const uint8_t b = get_ADC_keyValue();
  932. if (WITHIN(b, 1, 8)) keypad_buttons = _BV(b - 1);
  933. }
  934. #endif
  935. #if HAS_SHIFT_ENCODER
  936. /**
  937. * Set up Rotary Encoder bit values (for two pin encoders to indicate movement).
  938. * These values are independent of which pins are used for EN_A / EN_B indications.
  939. * The rotary encoder part is also independent of the LCD chipset.
  940. */
  941. uint8_t val = 0;
  942. WRITE(SHIFT_LD, LOW);
  943. WRITE(SHIFT_LD, HIGH);
  944. LOOP_L_N(i, 8) {
  945. val >>= 1;
  946. if (READ(SHIFT_OUT)) SBI(val, 7);
  947. WRITE(SHIFT_CLK, HIGH);
  948. WRITE(SHIFT_CLK, LOW);
  949. }
  950. #if ENABLED(REPRAPWORLD_KEYPAD)
  951. keypad_buttons = ~val;
  952. #else
  953. buttons = ~val;
  954. #endif
  955. #endif
  956. } // next_button_update_ms
  957. #if HAS_ENCODER_WHEEL
  958. static uint8_t lastEncoderBits;
  959. #define encrot0 0
  960. #define encrot1 2
  961. #define encrot2 3
  962. #define encrot3 1
  963. // Manage encoder rotation
  964. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; }
  965. uint8_t enc = 0;
  966. if (buttons & EN_A) enc |= B01;
  967. if (buttons & EN_B) enc |= B10;
  968. if (enc != lastEncoderBits) {
  969. switch (enc) {
  970. case encrot0: ENCODER_SPIN(encrot3, encrot1); break;
  971. case encrot1: ENCODER_SPIN(encrot0, encrot2); break;
  972. case encrot2: ENCODER_SPIN(encrot1, encrot3); break;
  973. case encrot3: ENCODER_SPIN(encrot2, encrot0); break;
  974. }
  975. if (external_control) {
  976. TERN_(AUTO_BED_LEVELING_UBL, ubl.encoder_diff = encoderDiff); // Make encoder rotation available to UBL G29 mesh editing.
  977. encoderDiff = 0; // Hide the encoder event from the current screen handler.
  978. }
  979. lastEncoderBits = enc;
  980. }
  981. #endif // HAS_ENCODER_WHEEL
  982. }
  983. #endif // HAS_ENCODER_ACTION
  984. #endif // HAS_SPI_LCD
  985. #if HAS_DISPLAY
  986. #if ENABLED(EXTENSIBLE_UI)
  987. #include "extui/ui_api.h"
  988. #endif
  989. ////////////////////////////////////////////
  990. /////////////// Status Line ////////////////
  991. ////////////////////////////////////////////
  992. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  993. void MarlinUI::advance_status_scroll() {
  994. // Advance by one UTF8 code-word
  995. if (status_scroll_offset < utf8_strlen(status_message))
  996. while (!START_OF_UTF8_CHAR(status_message[++status_scroll_offset]));
  997. else
  998. status_scroll_offset = 0;
  999. }
  1000. char* MarlinUI::status_and_len(uint8_t &len) {
  1001. char *out = status_message + status_scroll_offset;
  1002. len = utf8_strlen(out);
  1003. return out;
  1004. }
  1005. #endif
  1006. void MarlinUI::finish_status(const bool persist) {
  1007. #if !(ENABLED(LCD_PROGRESS_BAR) && (PROGRESS_MSG_EXPIRE > 0))
  1008. UNUSED(persist);
  1009. #endif
  1010. #if ENABLED(LCD_PROGRESS_BAR) || BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1011. const millis_t ms = millis();
  1012. #endif
  1013. #if ENABLED(LCD_PROGRESS_BAR)
  1014. progress_bar_ms = ms;
  1015. #if PROGRESS_MSG_EXPIRE > 0
  1016. expire_status_ms = persist ? 0 : ms + PROGRESS_MSG_EXPIRE;
  1017. #endif
  1018. #endif
  1019. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1020. next_filament_display = ms + 5000UL; // Show status message for 5s
  1021. #endif
  1022. #if BOTH(HAS_SPI_LCD, STATUS_MESSAGE_SCROLLING)
  1023. status_scroll_offset = 0;
  1024. #endif
  1025. TERN_(EXTENSIBLE_UI, ExtUI::onStatusChanged(status_message));
  1026. }
  1027. bool MarlinUI::has_status() { return (status_message[0] != '\0'); }
  1028. void MarlinUI::set_status(const char * const message, const bool persist) {
  1029. if (alert_level) return;
  1030. TERN_(HOST_PROMPT_SUPPORT, host_action_notify(message));
  1031. // Here we have a problem. The message is encoded in UTF8, so
  1032. // arbitrarily cutting it will be a problem. We MUST be sure
  1033. // that there is no cutting in the middle of a multibyte character!
  1034. // Get a pointer to the null terminator
  1035. const char* pend = message + strlen(message);
  1036. // If length of supplied UTF8 string is greater than
  1037. // our buffer size, start cutting whole UTF8 chars
  1038. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  1039. --pend;
  1040. while (!START_OF_UTF8_CHAR(*pend)) --pend;
  1041. };
  1042. // At this point, we have the proper cut point. Use it
  1043. uint8_t maxLen = pend - message;
  1044. strncpy(status_message, message, maxLen);
  1045. status_message[maxLen] = '\0';
  1046. finish_status(persist);
  1047. }
  1048. #include <stdarg.h>
  1049. void MarlinUI::status_printf_P(const uint8_t level, PGM_P const fmt, ...) {
  1050. if (level < alert_level) return;
  1051. alert_level = level;
  1052. va_list args;
  1053. va_start(args, fmt);
  1054. vsnprintf_P(status_message, MAX_MESSAGE_LENGTH, fmt, args);
  1055. va_end(args);
  1056. finish_status(level > 0);
  1057. }
  1058. void MarlinUI::set_status_P(PGM_P const message, int8_t level) {
  1059. if (level < 0) level = alert_level = 0;
  1060. if (level < alert_level) return;
  1061. alert_level = level;
  1062. TERN_(HOST_PROMPT_SUPPORT, host_action_notify(message));
  1063. // Since the message is encoded in UTF8 it must
  1064. // only be cut on a character boundary.
  1065. // Get a pointer to the null terminator
  1066. PGM_P pend = message + strlen_P(message);
  1067. // If length of supplied UTF8 string is greater than
  1068. // the buffer size, start cutting whole UTF8 chars
  1069. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  1070. --pend;
  1071. while (!START_OF_UTF8_CHAR(pgm_read_byte(pend))) --pend;
  1072. };
  1073. // At this point, we have the proper cut point. Use it
  1074. uint8_t maxLen = pend - message;
  1075. strncpy_P(status_message, message, maxLen);
  1076. status_message[maxLen] = '\0';
  1077. finish_status(level > 0);
  1078. }
  1079. void MarlinUI::set_alert_status_P(PGM_P const message) {
  1080. set_status_P(message, 1);
  1081. TERN_(HAS_LCD_MENU, return_to_status());
  1082. }
  1083. PGM_P print_paused = GET_TEXT(MSG_PRINT_PAUSED);
  1084. /**
  1085. * Reset the status message
  1086. */
  1087. void MarlinUI::reset_status(const bool no_welcome) {
  1088. PGM_P printing = GET_TEXT(MSG_PRINTING);
  1089. PGM_P welcome = GET_TEXT(WELCOME_MSG);
  1090. #if SERVICE_INTERVAL_1 > 0
  1091. static const char service1[] PROGMEM = { "> " SERVICE_NAME_1 "!" };
  1092. #endif
  1093. #if SERVICE_INTERVAL_2 > 0
  1094. static const char service2[] PROGMEM = { "> " SERVICE_NAME_2 "!" };
  1095. #endif
  1096. #if SERVICE_INTERVAL_3 > 0
  1097. static const char service3[] PROGMEM = { "> " SERVICE_NAME_3 "!" };
  1098. #endif
  1099. PGM_P msg;
  1100. if (printingIsPaused())
  1101. msg = print_paused;
  1102. #if ENABLED(SDSUPPORT)
  1103. else if (IS_SD_PRINTING())
  1104. return set_status(card.longest_filename(), true);
  1105. #endif
  1106. else if (print_job_timer.isRunning())
  1107. msg = printing;
  1108. #if SERVICE_INTERVAL_1 > 0
  1109. else if (print_job_timer.needsService(1)) msg = service1;
  1110. #endif
  1111. #if SERVICE_INTERVAL_2 > 0
  1112. else if (print_job_timer.needsService(2)) msg = service2;
  1113. #endif
  1114. #if SERVICE_INTERVAL_3 > 0
  1115. else if (print_job_timer.needsService(3)) msg = service3;
  1116. #endif
  1117. else if (!no_welcome)
  1118. msg = welcome;
  1119. else
  1120. return;
  1121. set_status_P(msg, -1);
  1122. }
  1123. #if ENABLED(SDSUPPORT)
  1124. extern bool wait_for_user, wait_for_heatup;
  1125. #endif
  1126. void MarlinUI::abort_print() {
  1127. #if ENABLED(SDSUPPORT)
  1128. wait_for_heatup = wait_for_user = false;
  1129. card.flag.abort_sd_printing = true;
  1130. #endif
  1131. #ifdef ACTION_ON_CANCEL
  1132. host_action_cancel();
  1133. #endif
  1134. TERN_(HOST_PROMPT_SUPPORT, host_prompt_open(PROMPT_INFO, PSTR("UI Aborted"), DISMISS_STR));
  1135. print_job_timer.stop();
  1136. set_status_P(GET_TEXT(MSG_PRINT_ABORTED));
  1137. TERN_(HAS_LCD_MENU, return_to_status());
  1138. }
  1139. #if ANY(PARK_HEAD_ON_PAUSE, SDSUPPORT)
  1140. #include "../gcode/queue.h"
  1141. #endif
  1142. void MarlinUI::pause_print() {
  1143. #if HAS_LCD_MENU
  1144. synchronize(GET_TEXT(MSG_PAUSING));
  1145. defer_status_screen();
  1146. #endif
  1147. TERN_(HOST_PROMPT_SUPPORT, host_prompt_open(PROMPT_PAUSE_RESUME, PSTR("UI Pause"), PSTR("Resume")));
  1148. set_status_P(print_paused);
  1149. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1150. TERN_(HAS_SPI_LCD, lcd_pause_show_message(PAUSE_MESSAGE_PARKING, PAUSE_MODE_PAUSE_PRINT)); // Show message immediately to let user know about pause in progress
  1151. queue.inject_P(PSTR("M25 P\nM24"));
  1152. #elif ENABLED(SDSUPPORT)
  1153. queue.inject_P(PSTR("M25"));
  1154. #elif defined(ACTION_ON_PAUSE)
  1155. host_action_pause();
  1156. #endif
  1157. }
  1158. void MarlinUI::resume_print() {
  1159. reset_status();
  1160. TERN_(PARK_HEAD_ON_PAUSE, wait_for_heatup = wait_for_user = false);
  1161. if (IS_SD_PAUSED()) queue.inject_P(M24_STR);
  1162. #ifdef ACTION_ON_RESUME
  1163. host_action_resume();
  1164. #endif
  1165. print_job_timer.start(); // Also called by M24
  1166. }
  1167. #if HAS_PRINT_PROGRESS
  1168. MarlinUI::progress_t MarlinUI::_get_progress() {
  1169. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  1170. const progress_t p = progress_override & PROGRESS_MASK;
  1171. #else
  1172. constexpr progress_t p = 0;
  1173. #endif
  1174. return (p
  1175. #if ENABLED(SDSUPPORT)
  1176. #if HAS_PRINT_PROGRESS_PERMYRIAD
  1177. ?: card.permyriadDone()
  1178. #else
  1179. ?: card.percentDone()
  1180. #endif
  1181. #endif
  1182. );
  1183. }
  1184. #endif
  1185. #else // !HAS_DISPLAY
  1186. //
  1187. // Send the status line as a host notification
  1188. //
  1189. void MarlinUI::set_status(const char * const message, const bool) {
  1190. #if ENABLED(HOST_PROMPT_SUPPORT)
  1191. host_action_notify(message);
  1192. #else
  1193. UNUSED(message);
  1194. #endif
  1195. }
  1196. void MarlinUI::set_status_P(PGM_P message, const int8_t) {
  1197. #if ENABLED(HOST_PROMPT_SUPPORT)
  1198. host_action_notify(message);
  1199. #else
  1200. UNUSED(message);
  1201. #endif
  1202. }
  1203. void MarlinUI::status_printf_P(const uint8_t, PGM_P const message, ...) {
  1204. #if ENABLED(HOST_PROMPT_SUPPORT)
  1205. host_action_notify(message);
  1206. #else
  1207. UNUSED(message);
  1208. #endif
  1209. }
  1210. #endif // !HAS_DISPLAY
  1211. #if ENABLED(SDSUPPORT)
  1212. void MarlinUI::media_changed(const uint8_t old_status, const uint8_t status) {
  1213. if (old_status == status) {
  1214. TERN_(EXTENSIBLE_UI, ExtUI::onMediaError()); // Failed to mount/unmount
  1215. return;
  1216. }
  1217. if (status) {
  1218. if (old_status < 2) {
  1219. TERN_(EXTENSIBLE_UI, ExtUI::onMediaInserted()); // ExtUI response
  1220. set_status_P(GET_TEXT(MSG_MEDIA_INSERTED));
  1221. }
  1222. }
  1223. else {
  1224. if (old_status < 2) {
  1225. TERN_(EXTENSIBLE_UI, ExtUI::onMediaRemoved()); // ExtUI response
  1226. #if PIN_EXISTS(SD_DETECT)
  1227. set_status_P(GET_TEXT(MSG_MEDIA_REMOVED));
  1228. TERN_(HAS_LCD_MENU, return_to_status());
  1229. #endif
  1230. }
  1231. }
  1232. #if PIN_EXISTS(SD_DETECT) && DISABLED(NO_LCD_REINIT)
  1233. init_lcd(); // Revive a noisy shared SPI LCD
  1234. #endif
  1235. refresh();
  1236. #if HAS_SPI_LCD || defined(LED_BACKLIGHT_TIMEOUT)
  1237. const millis_t ms = millis();
  1238. #endif
  1239. TERN_(HAS_SPI_LCD, next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL); // Delay LCD update for SD activity
  1240. #ifdef LED_BACKLIGHT_TIMEOUT
  1241. leds.reset_timeout(ms);
  1242. #endif
  1243. }
  1244. #endif // SDSUPPORT