My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ultralcd.cpp 49KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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(AUTO_BED_LEVELING_UBL)
  85. #include "../feature/bedlevel/bedlevel.h"
  86. #endif
  87. #if HAS_TRINAMIC
  88. #include "../feature/tmc_util.h"
  89. #endif
  90. #if HAS_ADC_BUTTONS
  91. #include "../module/thermistor/thermistors.h"
  92. #endif
  93. #if HAS_ENCODER_ACTION
  94. volatile uint8_t MarlinUI::buttons;
  95. #if HAS_SLOW_BUTTONS
  96. volatile uint8_t MarlinUI::slow_buttons;
  97. #endif
  98. #if ENABLED(TOUCH_BUTTONS)
  99. #include "../feature/touch/xpt2046.h"
  100. #endif
  101. #endif
  102. #if ENABLED(INIT_SDCARD_ON_BOOT)
  103. uint8_t lcd_sd_status;
  104. #endif
  105. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  106. bool MarlinUI::defer_return_to_status;
  107. #endif
  108. uint8_t MarlinUI::lcd_status_update_delay = 1; // First update one loop delayed
  109. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  110. millis_t MarlinUI::next_filament_display; // = 0
  111. #endif
  112. millis_t MarlinUI::next_button_update_ms; // = 0
  113. #if HAS_GRAPHICAL_LCD
  114. bool MarlinUI::drawing_screen, MarlinUI::first_page; // = false
  115. #endif
  116. // Encoder Handling
  117. #if HAS_ENCODER_ACTION
  118. uint32_t MarlinUI::encoderPosition;
  119. volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
  120. #endif
  121. #if HAS_LCD_MENU
  122. #include "menu/menu.h"
  123. #include "../sd/cardreader.h"
  124. #if ENABLED(SDSUPPORT)
  125. #if ENABLED(SCROLL_LONG_FILENAMES)
  126. uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
  127. #endif
  128. const char * MarlinUI::scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll) {
  129. const char *outstr = theCard.longest_filename();
  130. if (theCard.longFilename[0]) {
  131. #if ENABLED(SCROLL_LONG_FILENAMES)
  132. if (doScroll) {
  133. for (uint8_t l = FILENAME_LENGTH; l--;)
  134. hash = ((hash << 1) | (hash >> 7)) ^ theCard.filename[l]; // rotate, xor
  135. static uint8_t filename_scroll_hash;
  136. if (filename_scroll_hash != hash) { // If the hash changed...
  137. filename_scroll_hash = hash; // Save the new hash
  138. filename_scroll_max = _MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
  139. filename_scroll_pos = 0; // Reset scroll to the start
  140. lcd_status_update_delay = 8; // Don't scroll right away
  141. }
  142. outstr += filename_scroll_pos;
  143. }
  144. #else
  145. theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge
  146. #endif
  147. }
  148. return outstr;
  149. }
  150. #endif
  151. screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR
  152. bool MarlinUI::screen_changed;
  153. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  154. bool MarlinUI::encoderRateMultiplierEnabled;
  155. millis_t MarlinUI::lastEncoderMovementMillis = 0;
  156. void MarlinUI::enable_encoder_multiplier(const bool onoff) {
  157. encoderRateMultiplierEnabled = onoff;
  158. lastEncoderMovementMillis = 0;
  159. }
  160. #endif
  161. #if EITHER(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION)
  162. int8_t MarlinUI::encoderDirection = ENCODERBASE;
  163. #endif
  164. #if ENABLED(TOUCH_BUTTONS)
  165. uint8_t MarlinUI::touch_buttons;
  166. uint8_t MarlinUI::repeat_delay;
  167. #endif
  168. bool MarlinUI::lcd_clicked;
  169. float move_menu_scale;
  170. bool MarlinUI::use_click() {
  171. const bool click = lcd_clicked;
  172. lcd_clicked = false;
  173. return click;
  174. }
  175. #if EITHER(AUTO_BED_LEVELING_UBL, G26_MESH_VALIDATION)
  176. bool MarlinUI::external_control; // = false
  177. void MarlinUI::wait_for_release() {
  178. while (button_pressed()) safe_delay(50);
  179. safe_delay(50);
  180. }
  181. #endif
  182. void _wrap_string(uint8_t &col, uint8_t &row, const char * const string, read_byte_cb_t cb_read_byte, bool wordwrap/*=false*/) {
  183. SETCURSOR(col, row);
  184. if (!string) return;
  185. auto _newline = [&col, &row]() {
  186. col = 0; row++; // Move col to string len (plus space)
  187. SETCURSOR(0, row); // Simulate carriage return
  188. };
  189. uint8_t *p = (uint8_t*)string;
  190. wchar_t ch;
  191. if (wordwrap) {
  192. uint8_t *wrd = nullptr, c = 0;
  193. // find the end of the part
  194. for (;;) {
  195. if (!wrd) wrd = p; // Get word start /before/ advancing
  196. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  197. const bool eol = !ch; // zero ends the string
  198. // End or a break between phrases?
  199. if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
  200. if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
  201. // Past the right and the word is not too long?
  202. if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
  203. c += !eol; // +1 so the space will be printed
  204. col += c; // advance col to new position
  205. while (c) { // character countdown
  206. --c; // count down to zero
  207. wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
  208. lcd_put_wchar(ch); // character to the LCD
  209. }
  210. if (eol) break; // all done!
  211. wrd = nullptr; // set up for next word
  212. }
  213. else c++; // count word characters
  214. }
  215. }
  216. else {
  217. for (;;) {
  218. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  219. if (!ch) break;
  220. lcd_put_wchar(ch);
  221. col++;
  222. if (col >= LCD_WIDTH) _newline();
  223. }
  224. }
  225. }
  226. void MarlinUI::draw_select_screen_prompt(PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  227. const uint8_t plen = utf8_strlen_P(pref), slen = suff ? utf8_strlen_P(suff) : 0;
  228. uint8_t col = 0, row = 0;
  229. if (!string && plen + slen <= LCD_WIDTH) {
  230. col = (LCD_WIDTH - plen - slen) / 2;
  231. row = LCD_HEIGHT > 3 ? 1 : 0;
  232. }
  233. wrap_string_P(col, row, pref, true);
  234. if (string) {
  235. if (col) { col = 0; row++; } // Move to the start of the next line
  236. wrap_string(col, row, string);
  237. }
  238. if (suff) wrap_string_P(col, row, suff);
  239. }
  240. #endif // HAS_LCD_MENU
  241. void MarlinUI::init() {
  242. init_lcd();
  243. #if HAS_DIGITAL_BUTTONS
  244. #if BUTTON_EXISTS(EN1)
  245. SET_INPUT_PULLUP(BTN_EN1);
  246. #endif
  247. #if BUTTON_EXISTS(EN2)
  248. SET_INPUT_PULLUP(BTN_EN2);
  249. #endif
  250. #if BUTTON_EXISTS(ENC)
  251. SET_INPUT_PULLUP(BTN_ENC);
  252. #endif
  253. #if BUTTON_EXISTS(BACK)
  254. SET_INPUT_PULLUP(BTN_BACK);
  255. #endif
  256. #if BUTTON_EXISTS(UP)
  257. SET_INPUT(BTN_UP);
  258. #endif
  259. #if BUTTON_EXISTS(DWN)
  260. SET_INPUT(BTN_DWN);
  261. #endif
  262. #if BUTTON_EXISTS(LFT)
  263. SET_INPUT(BTN_LFT);
  264. #endif
  265. #if BUTTON_EXISTS(RT)
  266. SET_INPUT(BTN_RT);
  267. #endif
  268. #endif // !HAS_DIGITAL_BUTTONS
  269. #if HAS_SHIFT_ENCODER
  270. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  271. SET_OUTPUT(SR_DATA_PIN);
  272. SET_OUTPUT(SR_CLK_PIN);
  273. #elif defined(SHIFT_CLK)
  274. SET_OUTPUT(SHIFT_CLK);
  275. OUT_WRITE(SHIFT_LD, HIGH);
  276. #if defined(SHIFT_EN) && SHIFT_EN >= 0
  277. OUT_WRITE(SHIFT_EN, LOW);
  278. #endif
  279. SET_INPUT_PULLUP(SHIFT_OUT);
  280. #endif
  281. #endif // HAS_SHIFT_ENCODER
  282. #if ENABLED(SDSUPPORT)
  283. #if PIN_EXISTS(SD_DETECT)
  284. SET_INPUT_PULLUP(SD_DETECT_PIN);
  285. #endif
  286. #if ENABLED(INIT_SDCARD_ON_BOOT)
  287. lcd_sd_status = 2; // UNKNOWN
  288. #endif
  289. #endif
  290. #if HAS_ENCODER_ACTION
  291. #if HAS_SLOW_BUTTONS
  292. slow_buttons = 0;
  293. #endif
  294. #endif
  295. update_buttons();
  296. #if HAS_ENCODER_ACTION
  297. encoderDiff = 0;
  298. #endif
  299. }
  300. bool MarlinUI::get_blink() {
  301. static uint8_t blink = 0;
  302. static millis_t next_blink_ms = 0;
  303. millis_t ms = millis();
  304. if (ELAPSED(ms, next_blink_ms)) {
  305. blink ^= 0xFF;
  306. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  307. }
  308. return blink != 0;
  309. }
  310. ////////////////////////////////////////////
  311. ///////////// Keypad Handling //////////////
  312. ////////////////////////////////////////////
  313. #if ENABLED(REPRAPWORLD_KEYPAD) && HAS_ENCODER_ACTION
  314. volatile uint8_t MarlinUI::keypad_buttons;
  315. #if HAS_LCD_MENU && !HAS_ADC_BUTTONS
  316. void lcd_move_x();
  317. void lcd_move_y();
  318. void lcd_move_z();
  319. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  320. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  321. encoderPosition = dir;
  322. switch (axis) {
  323. case X_AXIS: lcd_move_x(); break;
  324. case Y_AXIS: lcd_move_y(); break;
  325. case Z_AXIS: lcd_move_z();
  326. default: break;
  327. }
  328. }
  329. #endif
  330. bool MarlinUI::handle_keypad() {
  331. #if HAS_ADC_BUTTONS
  332. #define ADC_MIN_KEY_DELAY 100
  333. if (keypad_buttons) {
  334. #if HAS_ENCODER_ACTION
  335. refresh(LCDVIEW_REDRAW_NOW);
  336. #if HAS_LCD_MENU
  337. if (encoderDirection == -(ENCODERBASE)) { // ADC_KEYPAD forces REVERSE_MENU_DIRECTION, so this indicates menu navigation
  338. if (RRK(EN_KEYPAD_UP)) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  339. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  340. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  341. else if (RRK(EN_KEYPAD_RIGHT)) { return_to_status(); quick_feedback(); }
  342. }
  343. else
  344. #endif
  345. {
  346. #if HAS_LCD_MENU
  347. if (RRK(EN_KEYPAD_UP)) encoderPosition -= ENCODER_PULSES_PER_STEP;
  348. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += ENCODER_PULSES_PER_STEP;
  349. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  350. else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
  351. #else
  352. if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= ENCODER_PULSES_PER_STEP;
  353. else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += ENCODER_PULSES_PER_STEP;
  354. #endif
  355. }
  356. #endif
  357. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  358. return true;
  359. }
  360. #else // !HAS_ADC_BUTTONS
  361. static uint8_t keypad_debounce = 0;
  362. if (!RRK( EN_KEYPAD_F1 | EN_KEYPAD_F2
  363. | EN_KEYPAD_F3 | EN_KEYPAD_DOWN
  364. | EN_KEYPAD_RIGHT | EN_KEYPAD_MIDDLE
  365. | EN_KEYPAD_UP | EN_KEYPAD_LEFT )
  366. ) {
  367. if (keypad_debounce > 0) keypad_debounce--;
  368. }
  369. else if (!keypad_debounce) {
  370. keypad_debounce = 2;
  371. const bool homed = all_axes_homed();
  372. #if HAS_LCD_MENU
  373. if (RRK(EN_KEYPAD_MIDDLE)) goto_screen(menu_move);
  374. #if DISABLED(DELTA) && Z_HOME_DIR < 0
  375. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  376. #endif
  377. if (homed) {
  378. #if ENABLED(DELTA) || Z_HOME_DIR != -1
  379. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  380. #endif
  381. if (RRK(EN_KEYPAD_F3)) _reprapworld_keypad_move(Z_AXIS, -1);
  382. if (RRK(EN_KEYPAD_LEFT)) _reprapworld_keypad_move(X_AXIS, -1);
  383. if (RRK(EN_KEYPAD_RIGHT)) _reprapworld_keypad_move(X_AXIS, 1);
  384. if (RRK(EN_KEYPAD_DOWN)) _reprapworld_keypad_move(Y_AXIS, 1);
  385. if (RRK(EN_KEYPAD_UP)) _reprapworld_keypad_move(Y_AXIS, -1);
  386. }
  387. #endif // HAS_LCD_MENU
  388. if (!homed && RRK(EN_KEYPAD_F1)) queue.inject_P(G28_STR);
  389. return true;
  390. }
  391. #endif // !ADC_KEYPAD
  392. return false;
  393. }
  394. #endif // REPRAPWORLD_KEYPAD
  395. /**
  396. * Status Screen
  397. *
  398. * This is very display-dependent, so the lcd implementation draws this.
  399. */
  400. #if ENABLED(LCD_PROGRESS_BAR)
  401. millis_t MarlinUI::progress_bar_ms; // = 0
  402. #if PROGRESS_MSG_EXPIRE > 0
  403. millis_t MarlinUI::expire_status_ms; // = 0
  404. #endif
  405. #endif
  406. void MarlinUI::status_screen() {
  407. #if HAS_LCD_MENU
  408. ENCODER_RATE_MULTIPLY(false);
  409. #endif
  410. #if ENABLED(LCD_PROGRESS_BAR)
  411. //
  412. // HD44780 implements the following message blinking and
  413. // message expiration because Status Line and Progress Bar
  414. // share the same line on the display.
  415. //
  416. #if DISABLED(PROGRESS_MSG_ONCE) || (PROGRESS_MSG_EXPIRE > 0)
  417. #define GOT_MS
  418. const millis_t ms = millis();
  419. #endif
  420. // If the message will blink rather than expire...
  421. #if DISABLED(PROGRESS_MSG_ONCE)
  422. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  423. progress_bar_ms = ms;
  424. #endif
  425. #if PROGRESS_MSG_EXPIRE > 0
  426. // Handle message expire
  427. if (expire_status_ms > 0) {
  428. // Expire the message if a job is active and the bar has ticks
  429. if (get_progress_percent() > 2 && !print_job_timer.isPaused()) {
  430. if (ELAPSED(ms, expire_status_ms)) {
  431. status_message[0] = '\0';
  432. expire_status_ms = 0;
  433. }
  434. }
  435. else {
  436. // Defer message expiration before bar appears
  437. // and during any pause (not just SD)
  438. expire_status_ms += LCD_UPDATE_INTERVAL;
  439. }
  440. }
  441. #endif // PROGRESS_MSG_EXPIRE
  442. #endif // LCD_PROGRESS_BAR
  443. #if HAS_LCD_MENU
  444. if (use_click()) {
  445. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  446. next_filament_display = millis() + 5000UL; // Show status message for 5s
  447. #endif
  448. goto_screen(menu_main);
  449. #if DISABLED(NO_LCD_REINIT)
  450. init_lcd(); // May revive the LCD if static electricity killed it
  451. #endif
  452. return;
  453. }
  454. #endif
  455. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  456. const int16_t old_frm = feedrate_percentage;
  457. int16_t new_frm = old_frm + int16_t(encoderPosition);
  458. // Dead zone at 100% feedrate
  459. if (old_frm == 100) {
  460. if (int16_t(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  461. new_frm -= ENCODER_FEEDRATE_DEADZONE;
  462. else if (int16_t(encoderPosition) < -(ENCODER_FEEDRATE_DEADZONE))
  463. new_frm += ENCODER_FEEDRATE_DEADZONE;
  464. else
  465. new_frm = old_frm;
  466. }
  467. else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100))
  468. new_frm = 100;
  469. LIMIT(new_frm, 10, 999);
  470. if (old_frm != new_frm) {
  471. feedrate_percentage = new_frm;
  472. encoderPosition = 0;
  473. #if HAS_BUZZER && ENABLED(BEEP_ON_FEEDRATE_CHANGE)
  474. static millis_t next_beep;
  475. #ifndef GOT_MS
  476. const millis_t ms = millis();
  477. #endif
  478. if (ELAPSED(ms, next_beep)) {
  479. buzz(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
  480. next_beep = ms + 500UL;
  481. }
  482. #endif
  483. }
  484. #endif // ULTIPANEL_FEEDMULTIPLY
  485. draw_status_screen();
  486. }
  487. void MarlinUI::kill_screen(PGM_P lcd_error, PGM_P lcd_component) {
  488. init();
  489. status_printf_P(1, PSTR(S_FMT ": " S_FMT), lcd_error, lcd_component);
  490. #if HAS_LCD_MENU
  491. return_to_status();
  492. #endif
  493. // RED ALERT. RED ALERT.
  494. #ifdef LED_BACKLIGHT_TIMEOUT
  495. leds.set_color(LEDColorRed());
  496. #ifdef NEOPIXEL_BKGD_LED_INDEX
  497. neo.set_pixel_color(NEOPIXEL_BKGD_LED_INDEX, 255, 0, 0, 0);
  498. neo.show();
  499. #endif
  500. #endif
  501. draw_kill_screen();
  502. }
  503. void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
  504. #if HAS_LCD_MENU
  505. refresh();
  506. #endif
  507. #if HAS_ENCODER_ACTION
  508. if (clear_buttons) buttons = 0;
  509. next_button_update_ms = millis() + 500;
  510. #else
  511. UNUSED(clear_buttons);
  512. #endif
  513. #if HAS_BUZZER
  514. // Buzz and wait. Is the delay needed for buttons to settle?
  515. buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  516. #if HAS_LCD_MENU
  517. #if USE_BEEPER
  518. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  519. #else
  520. delay(10);
  521. #endif
  522. #endif
  523. #endif
  524. }
  525. ////////////////////////////////////////////
  526. /////////////// Manual Move ////////////////
  527. ////////////////////////////////////////////
  528. #if HAS_LCD_MENU
  529. extern bool no_reentry; // Flag to prevent recursion into menu handlers
  530. int8_t manual_move_axis = (int8_t)NO_AXIS;
  531. millis_t manual_move_start_time = 0;
  532. #if IS_KINEMATIC
  533. bool MarlinUI::processing_manual_move = false;
  534. float manual_move_offset = 0;
  535. #endif
  536. #if E_MANUAL > 1
  537. int8_t MarlinUI::manual_move_e_index = 0;
  538. #endif
  539. /**
  540. * If the most recent manual move hasn't been fed to the planner yet,
  541. * and the planner can accept one, send a move immediately.
  542. */
  543. void MarlinUI::manage_manual_move() {
  544. if (processing_manual_move) return;
  545. if (manual_move_axis != (int8_t)NO_AXIS && ELAPSED(millis(), manual_move_start_time) && !planner.is_full()) {
  546. const feedRate_t fr_mm_s = MMM_TO_MMS(manual_feedrate_mm_m[manual_move_axis]);
  547. #if IS_KINEMATIC
  548. #if EXTRUDERS > 1
  549. const int8_t old_extruder = active_extruder;
  550. if (manual_move_axis == E_AXIS) active_extruder = manual_move_e_index;
  551. #endif
  552. // Set movement on a single axis
  553. destination = current_position;
  554. destination[manual_move_axis] += manual_move_offset;
  555. // Reset for the next move
  556. manual_move_offset = 0;
  557. manual_move_axis = (int8_t)NO_AXIS;
  558. // DELTA and SCARA machines use segmented moves, which could fill the planner during the call to
  559. // move_to_destination. This will cause idle() to be called, which can then call this function while the
  560. // previous invocation is being blocked. Modifications to manual_move_offset shouldn't be made while
  561. // processing_manual_move is true or the planner will get out of sync.
  562. processing_manual_move = true;
  563. prepare_internal_move_to_destination(fr_mm_s); // will set current_position from destination
  564. processing_manual_move = false;
  565. #if EXTRUDERS > 1
  566. active_extruder = old_extruder;
  567. #endif
  568. #else
  569. planner.buffer_line(current_position, fr_mm_s, manual_move_axis == E_AXIS ? manual_move_e_index : active_extruder);
  570. manual_move_axis = (int8_t)NO_AXIS;
  571. #endif
  572. }
  573. }
  574. #endif // HAS_LCD_MENU
  575. /**
  576. * Update the LCD, read encoder buttons, etc.
  577. * - Read button states
  578. * - Check the SD Card slot state
  579. * - Act on RepRap World keypad input
  580. * - Update the encoder position
  581. * - Apply acceleration to the encoder position
  582. * - Do refresh(LCDVIEW_CALL_REDRAW_NOW) on controller events
  583. * - Reset the Info Screen timeout if there's any input
  584. * - Update status indicators, if any
  585. *
  586. * Run the current LCD menu handler callback function:
  587. * - Call the handler only if lcdDrawUpdate != LCDVIEW_NONE
  588. * - Before calling the handler, LCDVIEW_CALL_NO_REDRAW => LCDVIEW_NONE
  589. * - Call the menu handler. Menu handlers should do the following:
  590. * - If a value changes, set lcdDrawUpdate to LCDVIEW_REDRAW_NOW and draw the value
  591. * (Encoder events automatically set lcdDrawUpdate for you.)
  592. * - if (should_draw()) { redraw }
  593. * - Before exiting the handler set lcdDrawUpdate to:
  594. * - LCDVIEW_CLEAR_CALL_REDRAW to clear screen and set LCDVIEW_CALL_REDRAW_NEXT.
  595. * - LCDVIEW_REDRAW_NOW to draw now (including remaining stripes).
  596. * - LCDVIEW_CALL_REDRAW_NEXT to draw now and get LCDVIEW_REDRAW_NOW on the next loop.
  597. * - LCDVIEW_CALL_NO_REDRAW to draw now and get LCDVIEW_NONE on the next loop.
  598. * - NOTE: For graphical displays menu handlers may be called 2 or more times per loop,
  599. * so don't change lcdDrawUpdate without considering this.
  600. *
  601. * After the menu handler callback runs (or not):
  602. * - Clear the LCD if lcdDrawUpdate == LCDVIEW_CLEAR_CALL_REDRAW
  603. * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually)
  604. *
  605. * This function is only called from the main thread.
  606. */
  607. LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  608. void MarlinUI::update() {
  609. static uint16_t max_display_update_time = 0;
  610. static millis_t next_lcd_update_ms;
  611. millis_t ms = millis();
  612. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  613. static millis_t return_to_status_ms = 0;
  614. #define RESET_STATUS_TIMEOUT() (return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS)
  615. #else
  616. #define RESET_STATUS_TIMEOUT() NOOP
  617. #endif
  618. #ifdef LED_BACKLIGHT_TIMEOUT
  619. leds.update_timeout(powersupply_on);
  620. #endif
  621. #if HAS_LCD_MENU
  622. // Handle any queued Move Axis motion
  623. manage_manual_move();
  624. // Update button states for button_pressed(), etc.
  625. // If the state changes the next update may be delayed 300-500ms.
  626. update_buttons();
  627. // If the action button is pressed...
  628. static bool wait_for_unclick; // = false
  629. #if ENABLED(TOUCH_BUTTONS)
  630. if (touch_buttons) {
  631. RESET_STATUS_TIMEOUT();
  632. if (buttons & (EN_A | EN_B)) { // Menu arrows, in priority
  633. if (ELAPSED(ms, next_button_update_ms)) {
  634. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * (ENCODER_PULSES_PER_STEP) * encoderDirection;
  635. if (buttons & EN_A) encoderDiff *= -1;
  636. #if ENABLED(AUTO_BED_LEVELING_UBL)
  637. if (external_control) ubl.encoder_diff = encoderDiff;
  638. #endif
  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 && DISABLED(TOUCH_BUTTONS)
  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(HOST_PROMPT_SUPPORT)
  1238. host_prompt_open(PROMPT_PAUSE_RESUME, PSTR("UI Pause"), PSTR("Resume"));
  1239. #endif
  1240. set_status_P(print_paused);
  1241. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1242. #if HAS_SPI_LCD
  1243. lcd_pause_show_message(PAUSE_MESSAGE_PAUSING, PAUSE_MODE_PAUSE_PRINT); // Show message immediately to let user know about pause in progress
  1244. #endif
  1245. queue.inject_P(PSTR("M25 P\nM24"));
  1246. #elif ENABLED(SDSUPPORT)
  1247. queue.inject_P(PSTR("M25"));
  1248. #elif defined(ACTION_ON_PAUSE)
  1249. host_action_pause();
  1250. #endif
  1251. }
  1252. void MarlinUI::resume_print() {
  1253. reset_status();
  1254. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1255. wait_for_heatup = wait_for_user = false;
  1256. #endif
  1257. if (IS_SD_PAUSED()) queue.inject_P(M24_STR);
  1258. #ifdef ACTION_ON_RESUME
  1259. host_action_resume();
  1260. #endif
  1261. print_job_timer.start(); // Also called by M24
  1262. }
  1263. #if HAS_PRINT_PROGRESS
  1264. MarlinUI::progress_t MarlinUI::_get_progress() {
  1265. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  1266. const progress_t p = progress_override & PROGRESS_MASK;
  1267. #else
  1268. constexpr progress_t p = 0;
  1269. #endif
  1270. return (p
  1271. #if ENABLED(SDSUPPORT)
  1272. #if HAS_PRINT_PROGRESS_PERMYRIAD
  1273. ?: card.permyriadDone()
  1274. #else
  1275. ?: card.percentDone()
  1276. #endif
  1277. #endif
  1278. );
  1279. }
  1280. #endif
  1281. #else // !HAS_DISPLAY
  1282. //
  1283. // Send the status line as a host notification
  1284. //
  1285. void MarlinUI::set_status(const char * const message, const bool) {
  1286. #if ENABLED(HOST_PROMPT_SUPPORT)
  1287. host_action_notify(message);
  1288. #else
  1289. UNUSED(message);
  1290. #endif
  1291. }
  1292. void MarlinUI::set_status_P(PGM_P message, const int8_t) {
  1293. #if ENABLED(HOST_PROMPT_SUPPORT)
  1294. host_action_notify(message);
  1295. #else
  1296. UNUSED(message);
  1297. #endif
  1298. }
  1299. void MarlinUI::status_printf_P(const uint8_t, PGM_P const message, ...) {
  1300. #if ENABLED(HOST_PROMPT_SUPPORT)
  1301. host_action_notify(message);
  1302. #else
  1303. UNUSED(message);
  1304. #endif
  1305. }
  1306. #endif // !HAS_DISPLAY