My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd.cpp 49KB

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