My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ultralcd.cpp 45KB

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