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 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. #if ENABLED(ULTRA_LCD)
  24. #include <stdarg.h>
  25. #include "ultralcd.h"
  26. #include "lcdprint.h"
  27. #include "../sd/cardreader.h"
  28. #include "../module/temperature.h"
  29. #include "../module/planner.h"
  30. #include "../module/stepper.h"
  31. #include "../module/motion.h"
  32. #include "../module/probe.h"
  33. #include "../module/printcounter.h"
  34. #include "../gcode/gcode.h"
  35. #include "../gcode/queue.h"
  36. #include "../module/configuration_store.h"
  37. #include "../module/tool_change.h"
  38. #include "../Marlin.h"
  39. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  40. #include "../feature/pause.h"
  41. #endif
  42. #if ENABLED(POWER_LOSS_RECOVERY)
  43. #include "../feature/power_loss_recovery.h"
  44. #if HAS_LCD_MENU
  45. void menu_job_recovery();
  46. #endif
  47. #endif
  48. #if ENABLED(PRINTCOUNTER) && ENABLED(LCD_INFO_MENU)
  49. #include "../libs/duration_t.h"
  50. #endif
  51. #if ENABLED(FILAMENT_LCD_DISPLAY)
  52. #include "../feature/filwidth.h"
  53. #endif
  54. #if ENABLED(BLTOUCH)
  55. #include "../module/endstops.h"
  56. #endif
  57. #if HAS_LEVELING
  58. #include "../feature/bedlevel/bedlevel.h"
  59. #endif
  60. #if DISABLED(LCD_USE_I2C_BUZZER)
  61. #include "../libs/buzzer.h"
  62. #endif
  63. // Buttons
  64. volatile uint8_t buttons;
  65. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  66. uint8_t lcd_sd_status;
  67. #endif
  68. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  69. uint8_t status_scroll_offset = 0;
  70. #if LONG_FILENAME_LENGTH > CHARSIZE * 2 * (LCD_WIDTH)
  71. #define MAX_MESSAGE_LENGTH LONG_FILENAME_LENGTH
  72. #else
  73. #define MAX_MESSAGE_LENGTH CHARSIZE * 2 * (LCD_WIDTH)
  74. #endif
  75. #else
  76. #define MAX_MESSAGE_LENGTH CHARSIZE * (LCD_WIDTH)
  77. #endif
  78. char lcd_status_message[MAX_MESSAGE_LENGTH + 1];
  79. uint8_t lcd_status_update_delay = 1, // First update one loop delayed
  80. lcd_status_message_level; // Higher level blocks lower level
  81. #if ENABLED(FILAMENT_LCD_DISPLAY) && ENABLED(SDSUPPORT)
  82. millis_t previous_lcd_status_ms = 0;
  83. #endif
  84. #if HAS_LCD_MENU && ENABLED(SDSUPPORT) && ENABLED(SCROLL_LONG_FILENAMES)
  85. uint8_t filename_scroll_pos, filename_scroll_max;
  86. #endif
  87. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  88. uint8_t progress_bar_percent;
  89. #endif
  90. millis_t next_button_update_ms;
  91. #if HAS_GRAPHICAL_LCD
  92. bool drawing_screen, first_page; // = false
  93. #endif
  94. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  95. bool encoderRateMultiplierEnabled;
  96. #endif
  97. #if ENABLED(REVERSE_MENU_DIRECTION)
  98. int8_t encoderDirection = 1;
  99. #endif
  100. #if HAS_LCD_MENU
  101. #include "menu/menu.h"
  102. screenFunc_t currentScreen = lcd_status_screen;
  103. // Encoder Handling
  104. volatile int8_t encoderDiff; // Updated in lcd_buttons_update, added to encoderPosition every LCD update
  105. uint32_t encoderPosition;
  106. millis_t lastEncoderMovementMillis = 0;
  107. bool lcd_clicked, wait_for_unclick;
  108. float move_menu_scale;
  109. bool use_click() {
  110. const bool click = lcd_clicked;
  111. lcd_clicked = false;
  112. return click;
  113. }
  114. #endif
  115. void lcd_init() {
  116. lcd_implementation_init();
  117. #if ENABLED(NEWPANEL)
  118. #if BUTTON_EXISTS(EN1)
  119. SET_INPUT_PULLUP(BTN_EN1);
  120. #endif
  121. #if BUTTON_EXISTS(EN2)
  122. SET_INPUT_PULLUP(BTN_EN2);
  123. #endif
  124. #if BUTTON_EXISTS(ENC)
  125. SET_INPUT_PULLUP(BTN_ENC);
  126. #endif
  127. #if ENABLED(REPRAPWORLD_KEYPAD) && DISABLED(ADC_KEYPAD)
  128. SET_OUTPUT(SHIFT_CLK);
  129. OUT_WRITE(SHIFT_LD, HIGH);
  130. SET_INPUT_PULLUP(SHIFT_OUT);
  131. #endif
  132. #if BUTTON_EXISTS(UP)
  133. SET_INPUT(BTN_UP);
  134. #endif
  135. #if BUTTON_EXISTS(DWN)
  136. SET_INPUT(BTN_DWN);
  137. #endif
  138. #if BUTTON_EXISTS(LFT)
  139. SET_INPUT(BTN_LFT);
  140. #endif
  141. #if BUTTON_EXISTS(RT)
  142. SET_INPUT(BTN_RT);
  143. #endif
  144. #else // !NEWPANEL
  145. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  146. SET_OUTPUT(SR_DATA_PIN);
  147. SET_OUTPUT(SR_CLK_PIN);
  148. #elif defined(SHIFT_CLK)
  149. SET_OUTPUT(SHIFT_CLK);
  150. OUT_WRITE(SHIFT_LD, HIGH);
  151. OUT_WRITE(SHIFT_EN, LOW);
  152. SET_INPUT_PULLUP(SHIFT_OUT);
  153. #endif // SR_LCD_2W_NL
  154. #endif // !NEWPANEL
  155. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  156. SET_INPUT_PULLUP(SD_DETECT_PIN);
  157. lcd_sd_status = 2; // UNKNOWN
  158. #endif
  159. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  160. slow_buttons = 0;
  161. #endif
  162. lcd_buttons_update();
  163. #if HAS_LCD_MENU
  164. encoderDiff = 0;
  165. #endif
  166. }
  167. bool lcd_blink() {
  168. static uint8_t blink = 0;
  169. static millis_t next_blink_ms = 0;
  170. millis_t ms = millis();
  171. if (ELAPSED(ms, next_blink_ms)) {
  172. blink ^= 0xFF;
  173. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  174. }
  175. return blink != 0;
  176. }
  177. ////////////////////////////////////////////
  178. ///////////// Keypad Handling //////////////
  179. ////////////////////////////////////////////
  180. #if ENABLED(REPRAPWORLD_KEYPAD)
  181. volatile uint8_t buttons_reprapworld_keypad;
  182. #endif
  183. #if ENABLED(ADC_KEYPAD)
  184. #define KEYPAD_HOME EN_REPRAPWORLD_KEYPAD_F1
  185. #define KEYPAD_EN_C EN_REPRAPWORLD_KEYPAD_MIDDLE
  186. inline bool handle_adc_keypad() {
  187. #define ADC_MIN_KEY_DELAY 100
  188. if (buttons_reprapworld_keypad) {
  189. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  190. if (encoderDirection == -1) { // side effect which signals we are inside a menu
  191. if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_DOWN) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  192. else if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_UP) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  193. else if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_LEFT) { menu_action_back(); lcd_quick_feedback(true); }
  194. else if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_RIGHT) { lcd_return_to_status(); lcd_quick_feedback(true); }
  195. }
  196. else {
  197. if (buttons_reprapworld_keypad & (EN_REPRAPWORLD_KEYPAD_DOWN|EN_REPRAPWORLD_KEYPAD_UP|EN_REPRAPWORLD_KEYPAD_RIGHT)) {
  198. if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_DOWN) encoderPosition += ENCODER_PULSES_PER_STEP;
  199. else if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_UP) encoderPosition -= ENCODER_PULSES_PER_STEP;
  200. else if (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_RIGHT) encoderPosition = 0;
  201. }
  202. }
  203. #if ENABLED(ADC_KEYPAD_DEBUG)
  204. SERIAL_PROTOCOLLNPAIR("buttons_reprapworld_keypad = ", (uint32_t)buttons_reprapworld_keypad);
  205. SERIAL_PROTOCOLLNPAIR("encoderPosition = ", (uint32_t)encoderPosition);
  206. #endif
  207. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  208. return true;
  209. }
  210. return false;
  211. }
  212. #elif ENABLED(REPRAPWORLD_KEYPAD)
  213. #define REPRAPWORLD_BTN_OFFSET 0 // bit offset into buttons for shift register values
  214. #define BLEN_REPRAPWORLD_KEYPAD_F3 0
  215. #define BLEN_REPRAPWORLD_KEYPAD_F2 1
  216. #define BLEN_REPRAPWORLD_KEYPAD_F1 2
  217. #define BLEN_REPRAPWORLD_KEYPAD_DOWN 3
  218. #define BLEN_REPRAPWORLD_KEYPAD_RIGHT 4
  219. #define BLEN_REPRAPWORLD_KEYPAD_MIDDLE 5
  220. #define BLEN_REPRAPWORLD_KEYPAD_UP 6
  221. #define BLEN_REPRAPWORLD_KEYPAD_LEFT 7
  222. #define EN_REPRAPWORLD_KEYPAD_F3 (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_F3))
  223. #define EN_REPRAPWORLD_KEYPAD_F2 (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_F2))
  224. #define EN_REPRAPWORLD_KEYPAD_F1 (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_F1))
  225. #define EN_REPRAPWORLD_KEYPAD_DOWN (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_DOWN))
  226. #define EN_REPRAPWORLD_KEYPAD_RIGHT (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_RIGHT))
  227. #define EN_REPRAPWORLD_KEYPAD_MIDDLE (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_MIDDLE))
  228. #define EN_REPRAPWORLD_KEYPAD_UP (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_UP))
  229. #define EN_REPRAPWORLD_KEYPAD_LEFT (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_LEFT))
  230. #define REPRAPWORLD_KEYPAD_MOVE_Z_DOWN (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_F3)
  231. #define REPRAPWORLD_KEYPAD_MOVE_Z_UP (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_F2)
  232. #define REPRAPWORLD_KEYPAD_MOVE_Y_DOWN (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_DOWN)
  233. #define REPRAPWORLD_KEYPAD_MOVE_X_RIGHT (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_RIGHT)
  234. #define REPRAPWORLD_KEYPAD_MOVE_Y_UP (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_UP)
  235. #define REPRAPWORLD_KEYPAD_MOVE_X_LEFT (buttons_reprapworld_keypad & EN_REPRAPWORLD_KEYPAD_LEFT)
  236. #define KEYPAD_HOME EN_REPRAPWORLD_KEYPAD_MIDDLE
  237. #define KEYPAD_EN_C EN_REPRAPWORLD_KEYPAD_F1
  238. #define REPRAPWORLD_KEYPAD_MOVE_HOME (buttons_reprapworld_keypad & KEYPAD_HOME)
  239. #define REPRAPWORLD_KEYPAD_MOVE_MENU (buttons_reprapworld_keypad & KEYPAD_EN_C)
  240. #define REPRAPWORLD_KEYPAD_PRESSED (buttons_reprapworld_keypad & ( \
  241. EN_REPRAPWORLD_KEYPAD_F3 | \
  242. EN_REPRAPWORLD_KEYPAD_F2 | \
  243. EN_REPRAPWORLD_KEYPAD_F1 | \
  244. EN_REPRAPWORLD_KEYPAD_DOWN | \
  245. EN_REPRAPWORLD_KEYPAD_RIGHT | \
  246. EN_REPRAPWORLD_KEYPAD_MIDDLE | \
  247. EN_REPRAPWORLD_KEYPAD_UP | \
  248. EN_REPRAPWORLD_KEYPAD_LEFT) \
  249. )
  250. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  251. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  252. encoderPosition = dir;
  253. switch (axis) {
  254. case X_AXIS: lcd_move_x(); break;
  255. case Y_AXIS: lcd_move_y(); break;
  256. case Z_AXIS: lcd_move_z();
  257. default: break;
  258. }
  259. }
  260. inline void reprapworld_keypad_move_z_up() { _reprapworld_keypad_move(Z_AXIS, 1); }
  261. inline void reprapworld_keypad_move_z_down() { _reprapworld_keypad_move(Z_AXIS, -1); }
  262. inline void reprapworld_keypad_move_x_left() { _reprapworld_keypad_move(X_AXIS, -1); }
  263. inline void reprapworld_keypad_move_x_right() { _reprapworld_keypad_move(X_AXIS, 1); }
  264. inline void reprapworld_keypad_move_y_up() { _reprapworld_keypad_move(Y_AXIS, -1); }
  265. inline void reprapworld_keypad_move_y_down() { _reprapworld_keypad_move(Y_AXIS, 1); }
  266. inline void reprapworld_keypad_move_home() { enqueue_and_echo_commands_P(PSTR("G28")); } // move all axes home and wait
  267. inline void reprapworld_keypad_move_menu() { lcd_goto_screen(menu_move); }
  268. inline void handle_reprapworld_keypad() {
  269. static uint8_t keypad_debounce = 0;
  270. if (!REPRAPWORLD_KEYPAD_PRESSED) {
  271. if (keypad_debounce > 0) keypad_debounce--;
  272. }
  273. else if (!keypad_debounce) {
  274. keypad_debounce = 2;
  275. if (REPRAPWORLD_KEYPAD_MOVE_MENU) reprapworld_keypad_move_menu();
  276. #if DISABLED(DELTA) && Z_HOME_DIR == -1
  277. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) reprapworld_keypad_move_z_up();
  278. #endif
  279. if (all_axes_homed()) {
  280. #if ENABLED(DELTA) || Z_HOME_DIR != -1
  281. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) reprapworld_keypad_move_z_up();
  282. #endif
  283. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) reprapworld_keypad_move_z_down();
  284. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) reprapworld_keypad_move_x_left();
  285. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) reprapworld_keypad_move_x_right();
  286. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) reprapworld_keypad_move_y_down();
  287. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) reprapworld_keypad_move_y_up();
  288. }
  289. else {
  290. if (REPRAPWORLD_KEYPAD_MOVE_HOME) reprapworld_keypad_move_home();
  291. }
  292. }
  293. }
  294. #endif // REPRAPWORLD_KEYPAD
  295. /**
  296. * Status Screen
  297. *
  298. * This is very display-dependent, so the lcd implementation draws this.
  299. */
  300. #if ENABLED(LCD_PROGRESS_BAR)
  301. millis_t progress_bar_ms = 0; // Start millis of the current progress bar cycle
  302. #if PROGRESS_MSG_EXPIRE > 0
  303. static millis_t expire_status_ms = 0;
  304. void dontExpireStatus() { expire_status_ms = 0; }
  305. #endif
  306. #endif
  307. #if LCD_INFO_SCREEN_STYLE == 0
  308. void lcd_impl_status_screen_0();
  309. #elif LCD_INFO_SCREEN_STYLE == 1
  310. void lcd_impl_status_screen_1();
  311. #endif
  312. void lcd_status_screen() {
  313. #if HAS_LCD_MENU
  314. ENCODER_DIRECTION_NORMAL();
  315. ENCODER_RATE_MULTIPLY(false);
  316. #endif
  317. #if ENABLED(LCD_SET_PROGRESS_MANUALLY) && ENABLED(SDSUPPORT) && (ENABLED(LCD_PROGRESS_BAR) || HAS_GRAPHICAL_LCD)
  318. // Progress bar % comes from SD when actively printing
  319. if (IS_SD_PRINTING())
  320. progress_bar_percent = card.percentDone();
  321. #endif
  322. #if ENABLED(LCD_PROGRESS_BAR)
  323. //
  324. // HD44780 implements the following message blinking and
  325. // message expiration because Status Line and Progress Bar
  326. // share the same line on the display.
  327. //
  328. millis_t ms = millis();
  329. // If the message will blink rather than expire...
  330. #if DISABLED(PROGRESS_MSG_ONCE)
  331. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  332. progress_bar_ms = ms;
  333. #endif
  334. #if PROGRESS_MSG_EXPIRE > 0
  335. // Handle message expire
  336. if (expire_status_ms > 0) {
  337. #if DISABLED(LCD_SET_PROGRESS_MANUALLY)
  338. const uint8_t progress_bar_percent = card.percentDone();
  339. #endif
  340. // Expire the message if a job is active and the bar has ticks
  341. if (progress_bar_percent > 2 && !print_job_timer.isPaused()) {
  342. if (ELAPSED(ms, expire_status_ms)) {
  343. lcd_status_message[0] = '\0';
  344. expire_status_ms = 0;
  345. }
  346. }
  347. else {
  348. // Defer message expiration before bar appears
  349. // and during any pause (not just SD)
  350. expire_status_ms += LCD_UPDATE_INTERVAL;
  351. }
  352. }
  353. #endif // PROGRESS_MSG_EXPIRE
  354. #endif // LCD_PROGRESS_BAR
  355. #if HAS_LCD_MENU
  356. if (use_click()) {
  357. #if ENABLED(FILAMENT_LCD_DISPLAY) && ENABLED(SDSUPPORT)
  358. previous_lcd_status_ms = millis(); // get status message to show up for a while
  359. #endif
  360. lcd_goto_screen(menu_main);
  361. lcd_implementation_init(); // May revive the LCD if static electricity killed it
  362. return;
  363. }
  364. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  365. const int16_t new_frm = feedrate_percentage + (int32_t)encoderPosition;
  366. // Dead zone at 100% feedrate
  367. if ((feedrate_percentage < 100 && new_frm > 100) || (feedrate_percentage > 100 && new_frm < 100)) {
  368. feedrate_percentage = 100;
  369. encoderPosition = 0;
  370. }
  371. else if (feedrate_percentage == 100) {
  372. if ((int32_t)encoderPosition > ENCODER_FEEDRATE_DEADZONE) {
  373. feedrate_percentage += (int32_t)encoderPosition - (ENCODER_FEEDRATE_DEADZONE);
  374. encoderPosition = 0;
  375. }
  376. else if ((int32_t)encoderPosition < -(ENCODER_FEEDRATE_DEADZONE)) {
  377. feedrate_percentage += (int32_t)encoderPosition + ENCODER_FEEDRATE_DEADZONE;
  378. encoderPosition = 0;
  379. }
  380. }
  381. else {
  382. feedrate_percentage = new_frm;
  383. encoderPosition = 0;
  384. }
  385. #endif // ULTIPANEL_FEEDMULTIPLY
  386. feedrate_percentage = constrain(feedrate_percentage, 10, 999);
  387. #endif // HAS_LCD_MENU
  388. #if LCD_INFO_SCREEN_STYLE == 0
  389. lcd_impl_status_screen_0();
  390. #elif LCD_INFO_SCREEN_STYLE == 1
  391. lcd_impl_status_screen_1();
  392. #endif
  393. }
  394. /**
  395. * Reset the status message
  396. */
  397. void lcd_reset_status() {
  398. static const char paused[] PROGMEM = MSG_PRINT_PAUSED;
  399. static const char printing[] PROGMEM = MSG_PRINTING;
  400. static const char welcome[] PROGMEM = WELCOME_MSG;
  401. PGM_P msg;
  402. if (print_job_timer.isPaused())
  403. msg = paused;
  404. #if ENABLED(SDSUPPORT)
  405. else if (card.sdprinting)
  406. return lcd_setstatus(card.longest_filename(), true);
  407. #endif
  408. else if (print_job_timer.isRunning())
  409. msg = printing;
  410. else
  411. msg = welcome;
  412. lcd_setstatusPGM(msg, -1);
  413. }
  414. void kill_screen(PGM_P lcd_msg) {
  415. lcd_init();
  416. lcd_setalertstatusPGM(lcd_msg);
  417. lcd_kill_screen();
  418. }
  419. #if HAS_BUZZER
  420. void lcd_buzz(const long duration, const uint16_t freq) {
  421. #if ENABLED(LCD_USE_I2C_BUZZER)
  422. lcd.buzz(duration, freq);
  423. #elif PIN_EXISTS(BEEPER)
  424. buzzer.tone(duration, freq);
  425. #endif
  426. }
  427. #endif
  428. void lcd_quick_feedback(const bool clear_buttons) {
  429. #if HAS_LCD_MENU
  430. lcd_refresh();
  431. if (clear_buttons) buttons = 0;
  432. next_button_update_ms = millis() + 500;
  433. #else
  434. UNUSED(clear_buttons);
  435. #endif
  436. // Buzz and wait. The delay is needed for buttons to settle!
  437. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  438. #if HAS_LCD_MENU
  439. #if ENABLED(LCD_USE_I2C_BUZZER)
  440. delay(10);
  441. #elif PIN_EXISTS(BEEPER)
  442. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  443. #endif
  444. #endif
  445. }
  446. #if HAS_LCD_MENU
  447. extern bool no_reentry; // Flag to prevent recursion into menu handlers
  448. int8_t manual_move_axis = (int8_t)NO_AXIS;
  449. millis_t manual_move_start_time = 0;
  450. #if IS_KINEMATIC
  451. bool processing_manual_move = false;
  452. float manual_move_offset = 0;
  453. #endif
  454. #if !IS_KINEMATIC || (IS_KINEMATIC && EXTRUDERS > 1)
  455. int8_t manual_move_e_index = 0;
  456. #else
  457. constexpr int8_t manual_move_e_index = 0;
  458. #endif
  459. /**
  460. * If the most recent manual move hasn't been fed to the planner yet,
  461. * and the planner can accept one, send a move immediately.
  462. */
  463. void manage_manual_move() {
  464. if (processing_manual_move) return;
  465. if (manual_move_axis != (int8_t)NO_AXIS && ELAPSED(millis(), manual_move_start_time) && !planner.is_full()) {
  466. #if IS_KINEMATIC
  467. const float old_feedrate = feedrate_mm_s;
  468. feedrate_mm_s = MMM_TO_MMS(manual_feedrate_mm_m[manual_move_axis]);
  469. #if EXTRUDERS > 1
  470. const int8_t old_extruder = active_extruder;
  471. if (manual_move_axis == E_AXIS) active_extruder = manual_move_e_index;
  472. #endif
  473. // Set movement on a single axis
  474. set_destination_from_current();
  475. destination[manual_move_axis] += manual_move_offset;
  476. // Reset for the next move
  477. manual_move_offset = 0;
  478. manual_move_axis = (int8_t)NO_AXIS;
  479. // DELTA and SCARA machines use segmented moves, which could fill the planner during the call to
  480. // move_to_destination. This will cause idle() to be called, which can then call this function while the
  481. // previous invocation is being blocked. Modifications to manual_move_offset shouldn't be made while
  482. // processing_manual_move is true or the planner will get out of sync.
  483. processing_manual_move = true;
  484. prepare_move_to_destination(); // will call set_current_from_destination()
  485. processing_manual_move = false;
  486. feedrate_mm_s = old_feedrate;
  487. #if EXTRUDERS > 1
  488. active_extruder = old_extruder;
  489. #endif
  490. #else
  491. planner.buffer_line(current_position, MMM_TO_MMS(manual_feedrate_mm_m[manual_move_axis]), manual_move_axis == E_AXIS ? manual_move_e_index : active_extruder);
  492. manual_move_axis = (int8_t)NO_AXIS;
  493. #endif
  494. }
  495. }
  496. #endif // HAS_LCD_MENU
  497. /**
  498. * Update the LCD, read encoder buttons, etc.
  499. * - Read button states
  500. * - Check the SD Card slot state
  501. * - Act on RepRap World keypad input
  502. * - Update the encoder position
  503. * - Apply acceleration to the encoder position
  504. * - Set lcdDrawUpdate = LCDVIEW_CALL_REDRAW_NOW on controller events
  505. * - Reset the Info Screen timeout if there's any input
  506. * - Update status indicators, if any
  507. *
  508. * Run the current LCD menu handler callback function:
  509. * - Call the handler only if lcdDrawUpdate != LCDVIEW_NONE
  510. * - Before calling the handler, LCDVIEW_CALL_NO_REDRAW => LCDVIEW_NONE
  511. * - Call the menu handler. Menu handlers should do the following:
  512. * - If a value changes, set lcdDrawUpdate to LCDVIEW_REDRAW_NOW and draw the value
  513. * (Encoder events automatically set lcdDrawUpdate for you.)
  514. * - if (lcdDrawUpdate) { redraw }
  515. * - Before exiting the handler set lcdDrawUpdate to:
  516. * - LCDVIEW_CLEAR_CALL_REDRAW to clear screen and set LCDVIEW_CALL_REDRAW_NEXT.
  517. * - LCDVIEW_REDRAW_NOW to draw now (including remaining stripes).
  518. * - LCDVIEW_CALL_REDRAW_NEXT to draw now and get LCDVIEW_REDRAW_NOW on the next loop.
  519. * - LCDVIEW_CALL_NO_REDRAW to draw now and get LCDVIEW_NONE on the next loop.
  520. * - NOTE: For graphical displays menu handlers may be called 2 or more times per loop,
  521. * so don't change lcdDrawUpdate without considering this.
  522. *
  523. * After the menu handler callback runs (or not):
  524. * - Clear the LCD if lcdDrawUpdate == LCDVIEW_CLEAR_CALL_REDRAW
  525. * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually)
  526. *
  527. * This function is only called from the main thread.
  528. */
  529. LCDViewAction lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  530. #if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(G26_MESH_VALIDATION)
  531. bool lcd_external_control; // = false
  532. #endif
  533. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  534. volatile uint8_t slow_buttons;
  535. #endif
  536. void lcd_update() {
  537. static uint16_t max_display_update_time = 0;
  538. static millis_t next_lcd_update_ms;
  539. #if HAS_LCD_MENU
  540. static millis_t return_to_status_ms = 0;
  541. // Handle any queued Move Axis motion
  542. manage_manual_move();
  543. // Update button states for LCD_CLICKED(), etc.
  544. // After state changes the next button update
  545. // may be delayed 300-500ms.
  546. lcd_buttons_update();
  547. #if ENABLED(AUTO_BED_LEVELING_UBL)
  548. // Don't run the debouncer if UBL owns the display
  549. #define UBL_CONDITION !lcd_external_control
  550. #else
  551. #define UBL_CONDITION true
  552. #endif
  553. // If the action button is pressed...
  554. if (UBL_CONDITION && LCD_CLICKED()) {
  555. if (!wait_for_unclick) { // If not waiting for a debounce release:
  556. wait_for_unclick = true; // Set debounce flag to ignore continous clicks
  557. lcd_clicked = !wait_for_user && !no_reentry; // Keep the click if not waiting for a user-click
  558. wait_for_user = false; // Any click clears wait for user
  559. lcd_quick_feedback(true); // Always make a click sound
  560. }
  561. }
  562. else wait_for_unclick = false;
  563. #if BUTTON_EXISTS(BACK)
  564. if (LCD_BACK_CLICKED) {
  565. lcd_quick_feedback(true);
  566. lcd_goto_previous_menu();
  567. }
  568. #endif
  569. #endif // HAS_LCD_MENU
  570. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  571. const uint8_t sd_status = (uint8_t)IS_SD_INSERTED();
  572. if (sd_status != lcd_sd_status && lcd_detected()) {
  573. uint8_t old_sd_status = lcd_sd_status; // prevent re-entry to this block!
  574. lcd_sd_status = sd_status;
  575. if (sd_status) {
  576. safe_delay(500); // Some boards need a delay to get settled
  577. card.initsd();
  578. if (old_sd_status == 2)
  579. card.beginautostart(); // Initial boot
  580. else
  581. LCD_MESSAGEPGM(MSG_SD_INSERTED);
  582. }
  583. else {
  584. card.release();
  585. if (old_sd_status != 2) LCD_MESSAGEPGM(MSG_SD_REMOVED);
  586. }
  587. lcd_refresh();
  588. lcd_implementation_init(); // May revive the LCD if static electricity killed it
  589. }
  590. #endif // SDSUPPORT && SD_DETECT_PIN
  591. #if ENABLED(POWER_LOSS_RECOVERY)
  592. if (job_recovery_commands_count && job_recovery_phase == JOB_RECOVERY_IDLE) {
  593. lcd_goto_screen(menu_job_recovery);
  594. job_recovery_phase = JOB_RECOVERY_MAYBE; // Waiting for a response
  595. }
  596. #endif
  597. const millis_t ms = millis();
  598. if (ELAPSED(ms, next_lcd_update_ms)
  599. #if HAS_GRAPHICAL_LCD
  600. || drawing_screen
  601. #endif
  602. ) {
  603. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL;
  604. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  605. lcd_implementation_update_indicators();
  606. #endif
  607. #if HAS_LCD_MENU
  608. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  609. slow_buttons = lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  610. #endif
  611. #if ENABLED(ADC_KEYPAD)
  612. if (handle_adc_keypad())
  613. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  614. #elif ENABLED(REPRAPWORLD_KEYPAD)
  615. handle_reprapworld_keypad();
  616. #endif
  617. const bool encoderPastThreshold = (ABS(encoderDiff) >= ENCODER_PULSES_PER_STEP);
  618. if (encoderPastThreshold || lcd_clicked) {
  619. if (encoderPastThreshold) {
  620. int32_t encoderMultiplier = 1;
  621. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  622. if (encoderRateMultiplierEnabled) {
  623. int32_t encoderMovementSteps = ABS(encoderDiff) / ENCODER_PULSES_PER_STEP;
  624. if (lastEncoderMovementMillis) {
  625. // Note that the rate is always calculated between two passes through the
  626. // loop and that the abs of the encoderDiff value is tracked.
  627. float encoderStepRate = float(encoderMovementSteps) / float(ms - lastEncoderMovementMillis) * 1000;
  628. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  629. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  630. #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
  631. SERIAL_ECHO_START();
  632. SERIAL_ECHOPAIR("Enc Step Rate: ", encoderStepRate);
  633. SERIAL_ECHOPAIR(" Multiplier: ", encoderMultiplier);
  634. SERIAL_ECHOPAIR(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
  635. SERIAL_ECHOPAIR(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
  636. SERIAL_EOL();
  637. #endif // ENCODER_RATE_MULTIPLIER_DEBUG
  638. }
  639. lastEncoderMovementMillis = ms;
  640. } // encoderRateMultiplierEnabled
  641. #endif // ENCODER_RATE_MULTIPLIER
  642. encoderPosition += (encoderDiff * encoderMultiplier) / ENCODER_PULSES_PER_STEP;
  643. encoderDiff = 0;
  644. }
  645. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  646. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  647. }
  648. #endif // HAS_LCD_MENU
  649. // This runs every ~100ms when idling often enough.
  650. // Instead of tracking changes just redraw the Status Screen once per second.
  651. if (
  652. #if HAS_LCD_MENU
  653. currentScreen == lcd_status_screen &&
  654. #endif
  655. !lcd_status_update_delay--
  656. ) {
  657. lcd_status_update_delay = 9
  658. #if HAS_GRAPHICAL_LCD
  659. + 3
  660. #endif
  661. ;
  662. max_display_update_time--;
  663. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  664. }
  665. #if HAS_LCD_MENU && ENABLED(SCROLL_LONG_FILENAMES)
  666. // If scrolling of long file names is enabled and we are in the sd card menu,
  667. // cause a refresh to occur until all the text has scrolled into view.
  668. if (currentScreen == menu_sdcard && filename_scroll_pos < filename_scroll_max && !lcd_status_update_delay--) {
  669. lcd_status_update_delay = 6;
  670. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  671. filename_scroll_pos++;
  672. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  673. }
  674. #endif
  675. // then we want to use 1/2 of the time only.
  676. uint16_t bbr2 = planner.block_buffer_runtime() >> 1;
  677. #if HAS_GRAPHICAL_LCD
  678. const bool &is_drawing = drawing_screen;
  679. #else
  680. constexpr bool is_drawing = false;
  681. #endif
  682. if ((lcdDrawUpdate || is_drawing) && (!bbr2 || bbr2 > max_display_update_time)) {
  683. // Change state of drawing flag between screen updates
  684. if (!is_drawing) switch (lcdDrawUpdate) {
  685. case LCDVIEW_CALL_NO_REDRAW:
  686. lcdDrawUpdate = LCDVIEW_NONE;
  687. break;
  688. case LCDVIEW_CLEAR_CALL_REDRAW:
  689. case LCDVIEW_CALL_REDRAW_NEXT:
  690. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  691. case LCDVIEW_REDRAW_NOW: // set above, or by a handler through LCDVIEW_CALL_REDRAW_NEXT
  692. case LCDVIEW_NONE:
  693. break;
  694. } // switch
  695. #if ENABLED(ADC_KEYPAD)
  696. buttons_reprapworld_keypad = 0;
  697. #endif
  698. #if HAS_LCD_MENU
  699. #define CURRENTSCREEN() (*currentScreen)()
  700. #else
  701. #define CURRENTSCREEN() lcd_status_screen()
  702. #endif
  703. #if HAS_GRAPHICAL_LCD
  704. #if ENABLED(LIGHTWEIGHT_UI)
  705. #if HAS_LCD_MENU
  706. const bool in_status = currentScreen == lcd_status_screen;
  707. #else
  708. constexpr bool in_status = true;
  709. #endif
  710. const bool do_u8g_loop = !in_status;
  711. lcd_in_status(in_status);
  712. if (in_status) lcd_status_screen();
  713. #else
  714. constexpr bool do_u8g_loop = true;
  715. #endif
  716. if (do_u8g_loop) {
  717. if (!drawing_screen) { // If not already drawing pages
  718. u8g.firstPage(); // Start the first page
  719. drawing_screen = first_page = true; // Flag as drawing pages
  720. }
  721. lcd_setFont(FONT_MENU); // Setup font for every page draw
  722. u8g.setColorIndex(1); // And reset the color
  723. CURRENTSCREEN(); // Draw and process the current screen
  724. first_page = false;
  725. // The screen handler can clear drawing_screen for an action that changes the screen.
  726. // If still drawing and there's another page, update max-time and return now.
  727. // The nextPage will already be set up on the next call.
  728. if (drawing_screen && (drawing_screen = u8g.nextPage())) {
  729. NOLESS(max_display_update_time, millis() - ms);
  730. return;
  731. }
  732. }
  733. #else
  734. CURRENTSCREEN();
  735. #endif
  736. #if HAS_LCD_MENU
  737. lcd_clicked = false;
  738. #endif
  739. // Keeping track of the longest time for an individual LCD update.
  740. // Used to do screen throttling when the planner starts to fill up.
  741. NOLESS(max_display_update_time, millis() - ms);
  742. }
  743. #if HAS_LCD_MENU
  744. // Return to Status Screen after a timeout
  745. if (currentScreen == lcd_status_screen || defer_return_to_status)
  746. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  747. else if (ELAPSED(ms, return_to_status_ms))
  748. lcd_return_to_status();
  749. #endif // HAS_LCD_MENU
  750. // Change state of drawing flag between screen updates
  751. if (!is_drawing) switch (lcdDrawUpdate) {
  752. case LCDVIEW_CLEAR_CALL_REDRAW:
  753. lcd_implementation_clear(); break;
  754. case LCDVIEW_REDRAW_NOW:
  755. lcdDrawUpdate = LCDVIEW_NONE;
  756. case LCDVIEW_NONE:
  757. case LCDVIEW_CALL_REDRAW_NEXT:
  758. case LCDVIEW_CALL_NO_REDRAW:
  759. default: break;
  760. } // switch
  761. } // ELAPSED(ms, next_lcd_update_ms)
  762. }
  763. void lcd_finishstatus(const bool persist=false) {
  764. #if !(ENABLED(LCD_PROGRESS_BAR) && (PROGRESS_MSG_EXPIRE > 0))
  765. UNUSED(persist);
  766. #endif
  767. #if ENABLED(LCD_PROGRESS_BAR)
  768. progress_bar_ms = millis();
  769. #if PROGRESS_MSG_EXPIRE > 0
  770. expire_status_ms = persist ? 0 : progress_bar_ms + PROGRESS_MSG_EXPIRE;
  771. #endif
  772. #endif
  773. lcd_refresh();
  774. #if ENABLED(FILAMENT_LCD_DISPLAY) && ENABLED(SDSUPPORT)
  775. previous_lcd_status_ms = millis(); //get status message to show up for a while
  776. #endif
  777. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  778. status_scroll_offset = 0;
  779. #endif
  780. }
  781. bool lcd_hasstatus() { return (lcd_status_message[0] != '\0'); }
  782. void lcd_setstatus(const char * const message, const bool persist) {
  783. if (lcd_status_message_level > 0) return;
  784. // Here we have a problem. The message is encoded in UTF8, so
  785. // arbitrarily cutting it will be a problem. We MUST be sure
  786. // that there is no cutting in the middle of a multibyte character!
  787. // Get a pointer to the null terminator
  788. const char* pend = message + strlen(message);
  789. // If length of supplied UTF8 string is greater than
  790. // our buffer size, start cutting whole UTF8 chars
  791. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  792. --pend;
  793. while (!START_OF_UTF8_CHAR(*pend)) --pend;
  794. };
  795. // At this point, we have the proper cut point. Use it
  796. uint8_t maxLen = pend - message;
  797. strncpy(lcd_status_message, message, maxLen);
  798. lcd_status_message[maxLen] = '\0';
  799. lcd_finishstatus(persist);
  800. }
  801. void lcd_setstatusPGM(PGM_P const message, int8_t level) {
  802. if (level < 0) level = lcd_status_message_level = 0;
  803. if (level < lcd_status_message_level) return;
  804. lcd_status_message_level = level;
  805. // Here we have a problem. The message is encoded in UTF8, so
  806. // arbitrarily cutting it will be a problem. We MUST be sure
  807. // that there is no cutting in the middle of a multibyte character!
  808. // Get a pointer to the null terminator
  809. PGM_P pend = message + strlen_P(message);
  810. // If length of supplied UTF8 string is greater than
  811. // our buffer size, start cutting whole UTF8 chars
  812. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  813. --pend;
  814. while (!START_OF_UTF8_CHAR(pgm_read_byte(pend))) --pend;
  815. };
  816. // At this point, we have the proper cut point. Use it
  817. uint8_t maxLen = pend - message;
  818. strncpy_P(lcd_status_message, message, maxLen);
  819. lcd_status_message[maxLen] = '\0';
  820. lcd_finishstatus(level > 0);
  821. }
  822. void lcd_status_printf_P(const uint8_t level, PGM_P const fmt, ...) {
  823. if (level < lcd_status_message_level) return;
  824. lcd_status_message_level = level;
  825. va_list args;
  826. va_start(args, fmt);
  827. vsnprintf_P(lcd_status_message, MAX_MESSAGE_LENGTH, fmt, args);
  828. va_end(args);
  829. lcd_finishstatus(level > 0);
  830. }
  831. void lcd_setalertstatusPGM(PGM_P const message) {
  832. lcd_setstatusPGM(message, 1);
  833. #if HAS_LCD_MENU
  834. lcd_return_to_status();
  835. #endif
  836. }
  837. void lcd_reset_alert_level() { lcd_status_message_level = 0; }
  838. #if ENABLED(ADC_KEYPAD)
  839. typedef struct {
  840. uint16_t ADCKeyValueMin, ADCKeyValueMax;
  841. uint8_t ADCKeyNo;
  842. } _stADCKeypadTable_;
  843. static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
  844. // VALUE_MIN, VALUE_MAX, KEY
  845. { 4000, 4096, BLEN_REPRAPWORLD_KEYPAD_F1 + 1 }, // F1
  846. { 4000, 4096, BLEN_REPRAPWORLD_KEYPAD_F2 + 1 }, // F2
  847. { 4000, 4096, BLEN_REPRAPWORLD_KEYPAD_F3 + 1 }, // F3
  848. { 300, 500, BLEN_REPRAPWORLD_KEYPAD_LEFT + 1 }, // LEFT
  849. { 1900, 2200, BLEN_REPRAPWORLD_KEYPAD_RIGHT + 1 }, // RIGHT
  850. { 570, 870, BLEN_REPRAPWORLD_KEYPAD_UP + 1 }, // UP
  851. { 2670, 2870, BLEN_REPRAPWORLD_KEYPAD_DOWN + 1 }, // DOWN
  852. { 1150, 1450, BLEN_REPRAPWORLD_KEYPAD_MIDDLE + 1 }, // ENTER
  853. };
  854. uint8_t get_ADC_keyValue(void) {
  855. if (thermalManager.ADCKey_count >= 16) {
  856. const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw >> 2;
  857. #if ENABLED(ADC_KEYPAD_DEBUG)
  858. SERIAL_PROTOCOLLN(currentkpADCValue);
  859. #endif
  860. thermalManager.current_ADCKey_raw = 0;
  861. thermalManager.ADCKey_count = 0;
  862. if (currentkpADCValue < 4000)
  863. for (uint8_t i = 0; i < ADC_KEY_NUM; i++) {
  864. const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
  865. hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
  866. if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
  867. }
  868. }
  869. return 0;
  870. }
  871. #endif
  872. #if HAS_LCD_MENU
  873. /**
  874. * Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
  875. * These values are independent of which pins are used for EN_A and EN_B indications
  876. * The rotary encoder part is also independent to the chipset used for the LCD
  877. */
  878. #if defined(EN_A) && defined(EN_B)
  879. #define encrot0 0
  880. #define encrot1 2
  881. #define encrot2 3
  882. #define encrot3 1
  883. #endif
  884. #define GET_SHIFT_BUTTON_STATES(DST) \
  885. uint8_t new_##DST = 0; \
  886. WRITE(SHIFT_LD, LOW); \
  887. WRITE(SHIFT_LD, HIGH); \
  888. for (int8_t i = 0; i < 8; i++) { \
  889. new_##DST >>= 1; \
  890. if (READ(SHIFT_OUT)) SBI(new_##DST, 7); \
  891. WRITE(SHIFT_CLK, HIGH); \
  892. WRITE(SHIFT_CLK, LOW); \
  893. } \
  894. DST = ~new_##DST; //invert it, because a pressed switch produces a logical 0
  895. #if (ENABLED(LCD_I2C_TYPE_MCP23017) || ENABLED(LCD_I2C_TYPE_MCP23008)) && ENABLED(DETECT_DEVICE)
  896. bool lcd_detected() { return lcd.LcdDetected() == 1; }
  897. #else
  898. bool lcd_detected() { return true; }
  899. #endif
  900. #if ENABLED(G26_MESH_VALIDATION)
  901. void lcd_chirp() {
  902. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  903. }
  904. #endif
  905. #if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(G26_MESH_VALIDATION)
  906. bool is_lcd_clicked() { return LCD_CLICKED(); }
  907. void wait_for_release() {
  908. while (is_lcd_clicked()) safe_delay(50);
  909. safe_delay(50);
  910. }
  911. #endif
  912. /**
  913. * Read encoder buttons from the hardware registers
  914. * Warning: This function is called from interrupt context!
  915. */
  916. void lcd_buttons_update() {
  917. static uint8_t lastEncoderBits;
  918. const millis_t now = millis();
  919. if (ELAPSED(now, next_button_update_ms)) {
  920. #if ENABLED(NEWPANEL)
  921. uint8_t newbutton = 0;
  922. #if BUTTON_EXISTS(EN1)
  923. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  924. #endif
  925. #if BUTTON_EXISTS(EN2)
  926. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  927. #endif
  928. #if BUTTON_EXISTS(ENC)
  929. if (BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  930. #endif
  931. #if BUTTON_EXISTS(BACK)
  932. if (BUTTON_PRESSED(BACK)) newbutton |= EN_D;
  933. #endif
  934. //
  935. // Directional buttons
  936. //
  937. #if LCD_HAS_DIRECTIONAL_BUTTONS
  938. #if ENABLED(REVERSE_MENU_DIRECTION)
  939. #define _ENCODER_UD_STEPS (ENCODER_STEPS_PER_MENU_ITEM * encoderDirection)
  940. #else
  941. #define _ENCODER_UD_STEPS ENCODER_STEPS_PER_MENU_ITEM
  942. #endif
  943. #if ENABLED(REVERSE_ENCODER_DIRECTION)
  944. #define ENCODER_UD_STEPS _ENCODER_UD_STEPS
  945. #define ENCODER_LR_PULSES ENCODER_PULSES_PER_STEP
  946. #else
  947. #define ENCODER_UD_STEPS -(_ENCODER_UD_STEPS)
  948. #define ENCODER_LR_PULSES -(ENCODER_PULSES_PER_STEP)
  949. #endif
  950. if (false) {
  951. // for the else-ifs below
  952. }
  953. #if BUTTON_EXISTS(UP)
  954. else if (BUTTON_PRESSED(UP)) {
  955. encoderDiff = -(ENCODER_UD_STEPS);
  956. next_button_update_ms = now + 300;
  957. }
  958. #endif
  959. #if BUTTON_EXISTS(DWN)
  960. else if (BUTTON_PRESSED(DWN)) {
  961. encoderDiff = ENCODER_UD_STEPS;
  962. next_button_update_ms = now + 300;
  963. }
  964. #endif
  965. #if BUTTON_EXISTS(LFT)
  966. else if (BUTTON_PRESSED(LFT)) {
  967. encoderDiff = -(ENCODER_LR_PULSES);
  968. next_button_update_ms = now + 300;
  969. }
  970. #endif
  971. #if BUTTON_EXISTS(RT)
  972. else if (BUTTON_PRESSED(RT)) {
  973. encoderDiff = ENCODER_LR_PULSES;
  974. next_button_update_ms = now + 300;
  975. }
  976. #endif
  977. #endif // LCD_HAS_DIRECTIONAL_BUTTONS
  978. buttons = newbutton;
  979. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  980. buttons |= slow_buttons;
  981. #endif
  982. #if ENABLED(ADC_KEYPAD)
  983. uint8_t newbutton_reprapworld_keypad = 0;
  984. buttons = 0;
  985. if (buttons_reprapworld_keypad == 0) {
  986. newbutton_reprapworld_keypad = get_ADC_keyValue();
  987. if (WITHIN(newbutton_reprapworld_keypad, 1, 8))
  988. buttons_reprapworld_keypad = _BV(newbutton_reprapworld_keypad - 1);
  989. }
  990. #elif ENABLED(REPRAPWORLD_KEYPAD)
  991. GET_SHIFT_BUTTON_STATES(buttons_reprapworld_keypad);
  992. #endif
  993. #else // !NEWPANEL
  994. GET_SHIFT_BUTTON_STATES(buttons);
  995. #endif
  996. } // next_button_update_ms
  997. // Manage encoder rotation
  998. #if ENABLED(REVERSE_MENU_DIRECTION) && ENABLED(REVERSE_ENCODER_DIRECTION)
  999. #define ENCODER_DIFF_CW (encoderDiff -= encoderDirection)
  1000. #define ENCODER_DIFF_CCW (encoderDiff += encoderDirection)
  1001. #elif ENABLED(REVERSE_MENU_DIRECTION)
  1002. #define ENCODER_DIFF_CW (encoderDiff += encoderDirection)
  1003. #define ENCODER_DIFF_CCW (encoderDiff -= encoderDirection)
  1004. #elif ENABLED(REVERSE_ENCODER_DIRECTION)
  1005. #define ENCODER_DIFF_CW (encoderDiff--)
  1006. #define ENCODER_DIFF_CCW (encoderDiff++)
  1007. #else
  1008. #define ENCODER_DIFF_CW (encoderDiff++)
  1009. #define ENCODER_DIFF_CCW (encoderDiff--)
  1010. #endif
  1011. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: ENCODER_DIFF_CW; break; case _E2: ENCODER_DIFF_CCW; }
  1012. uint8_t enc = 0;
  1013. if (buttons & EN_A) enc |= B01;
  1014. if (buttons & EN_B) enc |= B10;
  1015. if (enc != lastEncoderBits) {
  1016. switch (enc) {
  1017. case encrot0: ENCODER_SPIN(encrot3, encrot1); break;
  1018. case encrot1: ENCODER_SPIN(encrot0, encrot2); break;
  1019. case encrot2: ENCODER_SPIN(encrot1, encrot3); break;
  1020. case encrot3: ENCODER_SPIN(encrot2, encrot0); break;
  1021. }
  1022. #if ENABLED(AUTO_BED_LEVELING_UBL)
  1023. if (lcd_external_control) {
  1024. ubl.encoder_diff = encoderDiff; // Make encoder rotation available to UBL G29 mesh editing.
  1025. encoderDiff = 0; // Hide the encoder event from the current screen handler.
  1026. }
  1027. #endif
  1028. lastEncoderBits = enc;
  1029. }
  1030. }
  1031. #endif // HAS_LCD_MENU
  1032. #endif // ULTRA_LCD