My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ultralcd.cpp 78KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506
  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 "ultralcd.h"
  23. #if ENABLED(ULTRA_LCD)
  24. #include "Marlin.h"
  25. #include "language.h"
  26. #include "cardreader.h"
  27. #include "temperature.h"
  28. #include "stepper.h"
  29. #include "configuration_store.h"
  30. /**
  31. * REVERSE_MENU_DIRECTION
  32. *
  33. * To reverse the menu direction we need a general way to reverse
  34. * the direction of the encoder everywhere. So encoderDirection is
  35. * added to allow the encoder to go the other way.
  36. *
  37. * This behavior is limited to scrolling Menus and SD card listings,
  38. * and is disabled in other contexts.
  39. */
  40. #if ENABLED(REVERSE_MENU_DIRECTION)
  41. int8_t encoderDirection = 1;
  42. #define ENCODER_DIRECTION_NORMAL() (encoderDirection = 1)
  43. #define ENCODER_DIRECTION_MENUS() (encoderDirection = -1)
  44. #else
  45. #define ENCODER_DIRECTION_NORMAL() ;
  46. #define ENCODER_DIRECTION_MENUS() ;
  47. #endif
  48. int8_t encoderDiff; // updated from interrupt context and added to encoderPosition every LCD update
  49. bool encoderRateMultiplierEnabled;
  50. int32_t lastEncoderMovementMillis;
  51. int plaPreheatHotendTemp;
  52. int plaPreheatHPBTemp;
  53. int plaPreheatFanSpeed;
  54. int absPreheatHotendTemp;
  55. int absPreheatHPBTemp;
  56. int absPreheatFanSpeed;
  57. #if ENABLED(FILAMENT_LCD_DISPLAY)
  58. millis_t previous_lcd_status_ms = 0;
  59. #endif
  60. // Function pointer to menu functions.
  61. typedef void (*menuFunc_t)();
  62. uint8_t lcd_status_message_level;
  63. char lcd_status_message[3 * (LCD_WIDTH) + 1] = WELCOME_MSG; // worst case is kana with up to 3*LCD_WIDTH+1
  64. #if ENABLED(DOGLCD)
  65. #include "dogm_lcd_implementation.h"
  66. #else
  67. #include "ultralcd_implementation_hitachi_HD44780.h"
  68. #endif
  69. // The main status screen
  70. static void lcd_status_screen();
  71. #if ENABLED(ULTIPANEL)
  72. #if HAS_POWER_SWITCH
  73. extern bool powersupply;
  74. #endif
  75. const float manual_feedrate[] = MANUAL_FEEDRATE;
  76. static void lcd_main_menu();
  77. static void lcd_tune_menu();
  78. static void lcd_prepare_menu();
  79. static void lcd_move_menu();
  80. static void lcd_control_menu();
  81. static void lcd_control_temperature_menu();
  82. static void lcd_control_temperature_preheat_pla_settings_menu();
  83. static void lcd_control_temperature_preheat_abs_settings_menu();
  84. static void lcd_control_motion_menu();
  85. static void lcd_control_volumetric_menu();
  86. #if ENABLED(HAS_LCD_CONTRAST)
  87. static void lcd_set_contrast();
  88. #endif
  89. #if ENABLED(FWRETRACT)
  90. static void lcd_control_retract_menu();
  91. #endif
  92. #if ENABLED(DELTA_CALIBRATION_MENU)
  93. static void lcd_delta_calibrate_menu();
  94. #endif
  95. #if ENABLED(MANUAL_BED_LEVELING)
  96. #include "mesh_bed_leveling.h"
  97. static void _lcd_level_bed();
  98. static void _lcd_level_bed_homing();
  99. static void lcd_level_bed();
  100. #endif
  101. /* Different types of actions that can be used in menu items. */
  102. static void menu_action_back(menuFunc_t data);
  103. static void menu_action_submenu(menuFunc_t data);
  104. static void menu_action_gcode(const char* pgcode);
  105. static void menu_action_function(menuFunc_t data);
  106. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr);
  107. static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  108. static void menu_action_setting_edit_float3(const char* pstr, float* ptr, float minValue, float maxValue);
  109. static void menu_action_setting_edit_float32(const char* pstr, float* ptr, float minValue, float maxValue);
  110. static void menu_action_setting_edit_float43(const char* pstr, float* ptr, float minValue, float maxValue);
  111. static void menu_action_setting_edit_float5(const char* pstr, float* ptr, float minValue, float maxValue);
  112. static void menu_action_setting_edit_float51(const char* pstr, float* ptr, float minValue, float maxValue);
  113. static void menu_action_setting_edit_float52(const char* pstr, float* ptr, float minValue, float maxValue);
  114. static void menu_action_setting_edit_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue);
  115. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callbackFunc);
  116. static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, menuFunc_t callbackFunc);
  117. static void menu_action_setting_edit_callback_float3(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  118. static void menu_action_setting_edit_callback_float32(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  119. static void menu_action_setting_edit_callback_float43(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  120. static void menu_action_setting_edit_callback_float5(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  121. static void menu_action_setting_edit_callback_float51(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  122. static void menu_action_setting_edit_callback_float52(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  123. static void menu_action_setting_edit_callback_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue, menuFunc_t callbackFunc);
  124. #if ENABLED(SDSUPPORT)
  125. static void lcd_sdcard_menu();
  126. static void menu_action_sdfile(const char* filename, char* longFilename);
  127. static void menu_action_sddirectory(const char* filename, char* longFilename);
  128. #endif
  129. #define ENCODER_FEEDRATE_DEADZONE 10
  130. #if DISABLED(LCD_I2C_VIKI)
  131. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  132. #define ENCODER_STEPS_PER_MENU_ITEM 5
  133. #endif
  134. #ifndef ENCODER_PULSES_PER_STEP
  135. #define ENCODER_PULSES_PER_STEP 1
  136. #endif
  137. #else
  138. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  139. #define ENCODER_STEPS_PER_MENU_ITEM 2 // VIKI LCD rotary encoder uses a different number of steps per rotation
  140. #endif
  141. #ifndef ENCODER_PULSES_PER_STEP
  142. #define ENCODER_PULSES_PER_STEP 1
  143. #endif
  144. #endif
  145. /* Helper macros for menus */
  146. /**
  147. * START_MENU generates the init code for a menu function
  148. */
  149. #define START_MENU() do { \
  150. ENCODER_DIRECTION_MENUS(); \
  151. encoderRateMultiplierEnabled = false; \
  152. if (encoderPosition > 0x8000) encoderPosition = 0; \
  153. uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
  154. NOMORE(currentMenuViewOffset, encoderLine); \
  155. uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
  156. bool wasClicked = LCD_CLICKED, itemSelected; \
  157. for (uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
  158. _menuItemNr = 0;
  159. /**
  160. * MENU_ITEM generates draw & handler code for a menu item, potentially calling:
  161. *
  162. * lcd_implementation_drawmenu_[type](sel, row, label, arg3...)
  163. * menu_action_[type](arg3...)
  164. *
  165. * Examples:
  166. * MENU_ITEM(back, MSG_WATCH, lcd_status_screen)
  167. * lcd_implementation_drawmenu_back(sel, row, PSTR(MSG_WATCH), lcd_status_screen)
  168. * menu_action_back(lcd_status_screen)
  169. *
  170. * MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause)
  171. * lcd_implementation_drawmenu_function(sel, row, PSTR(MSG_PAUSE_PRINT), lcd_sdcard_pause)
  172. * menu_action_function(lcd_sdcard_pause)
  173. *
  174. * MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_multiplier, 10, 999)
  175. * MENU_ITEM(setting_edit_int3, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  176. * lcd_implementation_drawmenu_setting_edit_int3(sel, row, PSTR(MSG_SPEED), PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  177. * menu_action_setting_edit_int3(PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  178. *
  179. */
  180. #define MENU_ITEM(type, label, args...) do { \
  181. if (_menuItemNr == _lineNr) { \
  182. itemSelected = encoderLine == _menuItemNr; \
  183. if (lcdDrawUpdate) \
  184. lcd_implementation_drawmenu_ ## type(itemSelected, _drawLineNr, PSTR(label), ## args); \
  185. if (wasClicked && itemSelected) { \
  186. lcd_quick_feedback(); \
  187. menu_action_ ## type(args); \
  188. return; \
  189. } \
  190. } \
  191. _menuItemNr++; \
  192. } while(0)
  193. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  194. //#define ENCODER_RATE_MULTIPLIER_DEBUG // If defined, output the encoder steps per second value
  195. /**
  196. * MENU_MULTIPLIER_ITEM generates drawing and handling code for a multiplier menu item
  197. */
  198. #define MENU_MULTIPLIER_ITEM(type, label, args...) do { \
  199. if (_menuItemNr == _lineNr) { \
  200. itemSelected = encoderLine == _menuItemNr; \
  201. if (lcdDrawUpdate) \
  202. lcd_implementation_drawmenu_ ## type(itemSelected, _drawLineNr, PSTR(label), ## args); \
  203. if (wasClicked && itemSelected) { \
  204. lcd_quick_feedback(); \
  205. encoderRateMultiplierEnabled = true; \
  206. lastEncoderMovementMillis = 0; \
  207. menu_action_ ## type(args); \
  208. return; \
  209. } \
  210. } \
  211. _menuItemNr++; \
  212. } while(0)
  213. #endif //ENCODER_RATE_MULTIPLIER
  214. #define MENU_ITEM_DUMMY() do { _menuItemNr++; } while(0)
  215. #define MENU_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, PSTR(label), ## args)
  216. #define MENU_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, PSTR(label), ## args)
  217. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  218. #define MENU_MULTIPLIER_ITEM_EDIT(type, label, args...) MENU_MULTIPLIER_ITEM(setting_edit_ ## type, label, PSTR(label), ## args)
  219. #define MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(type, label, args...) MENU_MULTIPLIER_ITEM(setting_edit_callback_ ## type, label, PSTR(label), ## args)
  220. #else //!ENCODER_RATE_MULTIPLIER
  221. #define MENU_MULTIPLIER_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, PSTR(label), ## args)
  222. #define MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, PSTR(label), ## args)
  223. #endif //!ENCODER_RATE_MULTIPLIER
  224. #define END_MENU() \
  225. if (encoderLine >= _menuItemNr) { encoderPosition = _menuItemNr * (ENCODER_STEPS_PER_MENU_ITEM) - 1; encoderLine = _menuItemNr - 1; }\
  226. if (encoderLine >= currentMenuViewOffset + LCD_HEIGHT) { currentMenuViewOffset = encoderLine - (LCD_HEIGHT) + 1; lcdDrawUpdate = 1; _lineNr = currentMenuViewOffset - 1; _drawLineNr = -1; } \
  227. } } while(0)
  228. /** Used variables to keep track of the menu */
  229. volatile uint8_t buttons; //the last checked buttons in a bit array.
  230. #if ENABLED(REPRAPWORLD_KEYPAD)
  231. volatile uint8_t buttons_reprapworld_keypad; // to store the keypad shift register values
  232. #endif
  233. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  234. volatile uint8_t slow_buttons; // Bits of the pressed buttons.
  235. #endif
  236. uint8_t currentMenuViewOffset; /* scroll offset in the current menu */
  237. millis_t next_button_update_ms;
  238. uint8_t lastEncoderBits;
  239. uint32_t encoderPosition;
  240. #if PIN_EXISTS(SD_DETECT)
  241. uint8_t lcd_sd_status;
  242. #endif
  243. #endif // ULTIPANEL
  244. menuFunc_t currentMenu = lcd_status_screen; /* function pointer to the currently active menu */
  245. millis_t next_lcd_update_ms;
  246. uint8_t lcd_status_update_delay;
  247. bool ignore_click = false;
  248. bool wait_for_unclick;
  249. uint8_t lcdDrawUpdate = 2; /* Set to none-zero when the LCD needs to draw, decreased after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial) */
  250. //prevMenu and prevEncoderPosition are used to store the previous menu location when editing settings.
  251. menuFunc_t prevMenu = NULL;
  252. uint16_t prevEncoderPosition;
  253. //Variables used when editing values.
  254. const char* editLabel;
  255. void* editValue;
  256. int32_t minEditValue, maxEditValue;
  257. menuFunc_t callbackFunc;
  258. // place-holders for Ki and Kd edits
  259. float raw_Ki, raw_Kd;
  260. /**
  261. * General function to go directly to a menu
  262. */
  263. static void lcd_goto_menu(menuFunc_t menu, const bool feedback = false, const uint32_t encoder = 0) {
  264. if (currentMenu != menu) {
  265. currentMenu = menu;
  266. #if ENABLED(NEWPANEL)
  267. encoderPosition = encoder;
  268. if (feedback) lcd_quick_feedback();
  269. #endif
  270. // For LCD_PROGRESS_BAR re-initialize the custom characters
  271. #if ENABLED(LCD_PROGRESS_BAR)
  272. lcd_set_custom_characters(menu == lcd_status_screen);
  273. #endif
  274. }
  275. }
  276. inline void lcd_save_previous_menu() { prevMenu = currentMenu; prevEncoderPosition = encoderPosition; }
  277. static void lcd_goto_previous_menu() { lcd_goto_menu(prevMenu, true, prevEncoderPosition); }
  278. /**
  279. *
  280. * "Info Screen"
  281. *
  282. * This is very display-dependent, so the lcd implementation draws this.
  283. */
  284. static void lcd_status_screen() {
  285. ENCODER_DIRECTION_NORMAL();
  286. encoderRateMultiplierEnabled = false;
  287. #if ENABLED(LCD_PROGRESS_BAR)
  288. millis_t ms = millis();
  289. #if DISABLED(PROGRESS_MSG_ONCE)
  290. if (ms > progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME) {
  291. progress_bar_ms = ms;
  292. }
  293. #endif
  294. #if PROGRESS_MSG_EXPIRE > 0
  295. // Handle message expire
  296. if (expire_status_ms > 0) {
  297. #if ENABLED(SDSUPPORT)
  298. if (card.isFileOpen()) {
  299. // Expire the message when printing is active
  300. if (IS_SD_PRINTING) {
  301. if (ms >= expire_status_ms) {
  302. lcd_status_message[0] = '\0';
  303. expire_status_ms = 0;
  304. }
  305. }
  306. else {
  307. expire_status_ms += LCD_UPDATE_INTERVAL;
  308. }
  309. }
  310. else {
  311. expire_status_ms = 0;
  312. }
  313. #else
  314. expire_status_ms = 0;
  315. #endif //SDSUPPORT
  316. }
  317. #endif
  318. #endif //LCD_PROGRESS_BAR
  319. lcd_implementation_status_screen();
  320. #if ENABLED(ULTIPANEL)
  321. bool current_click = LCD_CLICKED;
  322. if (ignore_click) {
  323. if (wait_for_unclick) {
  324. if (!current_click)
  325. ignore_click = wait_for_unclick = false;
  326. else
  327. current_click = false;
  328. }
  329. else if (current_click) {
  330. lcd_quick_feedback();
  331. wait_for_unclick = true;
  332. current_click = false;
  333. }
  334. }
  335. if (current_click) {
  336. lcd_goto_menu(lcd_main_menu, true);
  337. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  338. #if ENABLED(LCD_PROGRESS_BAR)
  339. currentMenu == lcd_status_screen
  340. #endif
  341. );
  342. #if ENABLED(FILAMENT_LCD_DISPLAY)
  343. previous_lcd_status_ms = millis(); // get status message to show up for a while
  344. #endif
  345. }
  346. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  347. // Dead zone at 100% feedrate
  348. if ((feedrate_multiplier < 100 && (feedrate_multiplier + int(encoderPosition)) > 100) ||
  349. (feedrate_multiplier > 100 && (feedrate_multiplier + int(encoderPosition)) < 100)) {
  350. encoderPosition = 0;
  351. feedrate_multiplier = 100;
  352. }
  353. if (feedrate_multiplier == 100) {
  354. if (int(encoderPosition) > ENCODER_FEEDRATE_DEADZONE) {
  355. feedrate_multiplier += int(encoderPosition) - (ENCODER_FEEDRATE_DEADZONE);
  356. encoderPosition = 0;
  357. }
  358. else if (int(encoderPosition) < -ENCODER_FEEDRATE_DEADZONE) {
  359. feedrate_multiplier += int(encoderPosition) + ENCODER_FEEDRATE_DEADZONE;
  360. encoderPosition = 0;
  361. }
  362. }
  363. else {
  364. feedrate_multiplier += int(encoderPosition);
  365. encoderPosition = 0;
  366. }
  367. #endif // ULTIPANEL_FEEDMULTIPLY
  368. feedrate_multiplier = constrain(feedrate_multiplier, 10, 999);
  369. #endif //ULTIPANEL
  370. }
  371. #if ENABLED(ULTIPANEL)
  372. static void lcd_return_to_status() { lcd_goto_menu(lcd_status_screen); }
  373. #if ENABLED(SDSUPPORT)
  374. static void lcd_sdcard_pause() { card.pauseSDPrint(); }
  375. static void lcd_sdcard_resume() { card.startFileprint(); }
  376. static void lcd_sdcard_stop() {
  377. quickStop();
  378. card.sdprinting = false;
  379. card.closefile();
  380. autotempShutdown();
  381. cancel_heatup = true;
  382. lcd_setstatus(MSG_PRINT_ABORTED, true);
  383. }
  384. #endif //SDSUPPORT
  385. /**
  386. *
  387. * "Main" menu
  388. *
  389. */
  390. static void lcd_main_menu() {
  391. START_MENU();
  392. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  393. if (movesplanned() || IS_SD_PRINTING) {
  394. MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
  395. }
  396. else {
  397. MENU_ITEM(submenu, MSG_PREPARE, lcd_prepare_menu);
  398. #if ENABLED(DELTA_CALIBRATION_MENU)
  399. MENU_ITEM(submenu, MSG_DELTA_CALIBRATE, lcd_delta_calibrate_menu);
  400. #endif
  401. }
  402. MENU_ITEM(submenu, MSG_CONTROL, lcd_control_menu);
  403. #if ENABLED(SDSUPPORT)
  404. if (card.cardOK) {
  405. if (card.isFileOpen()) {
  406. if (card.sdprinting)
  407. MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
  408. else
  409. MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_resume);
  410. MENU_ITEM(function, MSG_STOP_PRINT, lcd_sdcard_stop);
  411. }
  412. else {
  413. MENU_ITEM(submenu, MSG_CARD_MENU, lcd_sdcard_menu);
  414. #if !PIN_EXISTS(SD_DETECT)
  415. MENU_ITEM(gcode, MSG_CNG_SDCARD, PSTR("M21")); // SD-card changed by user
  416. #endif
  417. }
  418. }
  419. else {
  420. MENU_ITEM(submenu, MSG_NO_CARD, lcd_sdcard_menu);
  421. #if !PIN_EXISTS(SD_DETECT)
  422. MENU_ITEM(gcode, MSG_INIT_SDCARD, PSTR("M21")); // Manually initialize the SD-card via user interface
  423. #endif
  424. }
  425. #endif //SDSUPPORT
  426. END_MENU();
  427. }
  428. #if ENABLED(SDSUPPORT) && ENABLED(MENU_ADDAUTOSTART)
  429. static void lcd_autostart_sd() {
  430. card.autostart_index = 0;
  431. card.setroot();
  432. card.checkautostart(true);
  433. }
  434. #endif
  435. /**
  436. * Set the home offset based on the current_position
  437. */
  438. void lcd_set_home_offsets() {
  439. // M428 Command
  440. enqueue_and_echo_commands_P(PSTR("M428"));
  441. lcd_return_to_status();
  442. }
  443. #if ENABLED(BABYSTEPPING)
  444. static void _lcd_babystep(const int axis, const char* msg) {
  445. ENCODER_DIRECTION_NORMAL();
  446. if (encoderPosition != 0) {
  447. encoderPosition = 0;
  448. lcdDrawUpdate = 1;
  449. int distance = (int)encoderPosition * BABYSTEP_MULTIPLICATOR;
  450. #if ENABLED(COREXY) || ENABLED(COREXZ)
  451. #if ENABLED(BABYSTEP_XY)
  452. switch(axis) {
  453. case X_AXIS: // X on CoreXY and CoreXZ
  454. babystepsTodo[A_AXIS] += distance * 2;
  455. babystepsTodo[CORE_AXIS_2] += distance * 2;
  456. break;
  457. case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ
  458. babystepsTodo[A_AXIS] += distance * 2;
  459. babystepsTodo[CORE_AXIS_2] -= distance * 2;
  460. break;
  461. case CORE_AXIS_3: // Z on CoreXY, Y on CoreXZ
  462. babystepsTodo[CORE_AXIS_3] += distance;
  463. break;
  464. }
  465. #elif ENABLED(COREXZ)
  466. babystepsTodo[A_AXIS] += distance * 2;
  467. babystepsTodo[C_AXIS] -= distance * 2;
  468. #else
  469. babystepsTodo[Z_AXIS] += distance;
  470. #endif
  471. #else
  472. babystepsTodo[axis] += distance;
  473. #endif
  474. }
  475. if (lcdDrawUpdate) lcd_implementation_drawedit(msg, "");
  476. if (LCD_CLICKED) lcd_goto_previous_menu();
  477. }
  478. #if ENABLED(BABYSTEP_XY)
  479. static void lcd_babystep_x() { _lcd_babystep(X_AXIS, PSTR(MSG_BABYSTEPPING_X)); }
  480. static void lcd_babystep_y() { _lcd_babystep(Y_AXIS, PSTR(MSG_BABYSTEPPING_Y)); }
  481. #endif
  482. static void lcd_babystep_z() { _lcd_babystep(Z_AXIS, PSTR(MSG_BABYSTEPPING_Z)); }
  483. #endif //BABYSTEPPING
  484. /**
  485. * Watch temperature callbacks
  486. */
  487. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  488. #if TEMP_SENSOR_0 != 0
  489. void watch_temp_callback_E0() { start_watching_heater(0); }
  490. #endif
  491. #if EXTRUDERS > 1 && TEMP_SENSOR_1 != 0
  492. void watch_temp_callback_E1() { start_watching_heater(1); }
  493. #endif // EXTRUDERS > 1
  494. #if EXTRUDERS > 2 && TEMP_SENSOR_2 != 0
  495. void watch_temp_callback_E2() { start_watching_heater(2); }
  496. #endif // EXTRUDERS > 2
  497. #if EXTRUDERS > 3 && TEMP_SENSOR_3 != 0
  498. void watch_temp_callback_E3() { start_watching_heater(3); }
  499. #endif // EXTRUDERS > 3
  500. #else
  501. #if TEMP_SENSOR_0 != 0
  502. void watch_temp_callback_E0() {}
  503. #endif
  504. #if EXTRUDERS > 1 && TEMP_SENSOR_1 != 0
  505. void watch_temp_callback_E1() {}
  506. #endif // EXTRUDERS > 1
  507. #if EXTRUDERS > 2 && TEMP_SENSOR_2 != 0
  508. void watch_temp_callback_E2() {}
  509. #endif // EXTRUDERS > 2
  510. #if EXTRUDERS > 3 && TEMP_SENSOR_3 != 0
  511. void watch_temp_callback_E3() {}
  512. #endif // EXTRUDERS > 3
  513. #endif
  514. /**
  515. *
  516. * "Tune" submenu
  517. *
  518. */
  519. static void lcd_tune_menu() {
  520. START_MENU();
  521. //
  522. // ^ Main
  523. //
  524. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  525. //
  526. // Speed:
  527. //
  528. MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_multiplier, 10, 999);
  529. // Manual bed leveling, Bed Z:
  530. #if ENABLED(MANUAL_BED_LEVELING)
  531. MENU_ITEM_EDIT(float43, MSG_BED_Z, &mbl.z_offset, -1, 1);
  532. #endif
  533. //
  534. // Nozzle:
  535. // Nozzle [1-4]:
  536. //
  537. #if EXTRUDERS == 1
  538. #if TEMP_SENSOR_0 != 0
  539. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  540. #endif
  541. #else //EXTRUDERS > 1
  542. #if TEMP_SENSOR_0 != 0
  543. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N1, &target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  544. #endif
  545. #if TEMP_SENSOR_1 != 0
  546. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N2, &target_temperature[1], 0, HEATER_1_MAXTEMP - 15, watch_temp_callback_E1);
  547. #endif
  548. #if EXTRUDERS > 2
  549. #if TEMP_SENSOR_2 != 0
  550. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N3, &target_temperature[2], 0, HEATER_2_MAXTEMP - 15, watch_temp_callback_E2);
  551. #endif
  552. #if EXTRUDERS > 3
  553. #if TEMP_SENSOR_3 != 0
  554. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N4, &target_temperature[3], 0, HEATER_3_MAXTEMP - 15, watch_temp_callback_E3);
  555. #endif
  556. #endif // EXTRUDERS > 3
  557. #endif // EXTRUDERS > 2
  558. #endif // EXTRUDERS > 1
  559. //
  560. // Bed:
  561. //
  562. #if TEMP_SENSOR_BED != 0
  563. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 15);
  564. #endif
  565. //
  566. // Fan Speed:
  567. //
  568. #if FAN_COUNT > 0
  569. #if HAS_FAN0
  570. #if FAN_COUNT > 1
  571. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED " 1"
  572. #else
  573. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED
  574. #endif
  575. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_1ST_FAN_SPEED, &fanSpeeds[0], 0, 255);
  576. #endif
  577. #if HAS_FAN1
  578. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 2", &fanSpeeds[1], 0, 255);
  579. #endif
  580. #if HAS_FAN2
  581. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 3", &fanSpeeds[2], 0, 255);
  582. #endif
  583. #endif // FAN_COUNT > 0
  584. //
  585. // Flow:
  586. // Flow 1:
  587. // Flow 2:
  588. // Flow 3:
  589. // Flow 4:
  590. //
  591. #if EXTRUDERS == 1
  592. MENU_ITEM_EDIT(int3, MSG_FLOW, &extruder_multiplier[0], 10, 999);
  593. #else // EXTRUDERS > 1
  594. MENU_ITEM_EDIT(int3, MSG_FLOW, &extruder_multiplier[active_extruder], 10, 999);
  595. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N1, &extruder_multiplier[0], 10, 999);
  596. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N2, &extruder_multiplier[1], 10, 999);
  597. #if EXTRUDERS > 2
  598. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N3, &extruder_multiplier[2], 10, 999);
  599. #if EXTRUDERS > 3
  600. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N4, &extruder_multiplier[3], 10, 999);
  601. #endif //EXTRUDERS > 3
  602. #endif //EXTRUDERS > 2
  603. #endif //EXTRUDERS > 1
  604. //
  605. // Babystep X:
  606. // Babystep Y:
  607. // Babystep Z:
  608. //
  609. #if ENABLED(BABYSTEPPING)
  610. #if ENABLED(BABYSTEP_XY)
  611. MENU_ITEM(submenu, MSG_BABYSTEP_X, lcd_babystep_x);
  612. MENU_ITEM(submenu, MSG_BABYSTEP_Y, lcd_babystep_y);
  613. #endif //BABYSTEP_XY
  614. MENU_ITEM(submenu, MSG_BABYSTEP_Z, lcd_babystep_z);
  615. #endif
  616. //
  617. // Change filament
  618. //
  619. #if ENABLED(FILAMENTCHANGEENABLE)
  620. MENU_ITEM(gcode, MSG_FILAMENTCHANGE, PSTR("M600"));
  621. #endif
  622. END_MENU();
  623. }
  624. void _lcd_preheat(int endnum, const float temph, const float tempb, const int fan) {
  625. if (temph > 0) setTargetHotend(temph, endnum);
  626. #if TEMP_SENSOR_BED != 0
  627. setTargetBed(tempb);
  628. #endif
  629. #if FAN_COUNT > 0
  630. #if FAN_COUNT > 1
  631. fanSpeeds[active_extruder < FAN_COUNT ? active_extruder : 0] = fan;
  632. #else
  633. fanSpeeds[0] = fan;
  634. #endif
  635. #endif
  636. lcd_return_to_status();
  637. }
  638. #if TEMP_SENSOR_0 != 0
  639. void lcd_preheat_pla0() { _lcd_preheat(0, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  640. void lcd_preheat_abs0() { _lcd_preheat(0, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  641. #endif
  642. #if EXTRUDERS > 1
  643. void lcd_preheat_pla1() { _lcd_preheat(1, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  644. void lcd_preheat_abs1() { _lcd_preheat(1, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  645. #if EXTRUDERS > 2
  646. void lcd_preheat_pla2() { _lcd_preheat(2, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  647. void lcd_preheat_abs2() { _lcd_preheat(2, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  648. #if EXTRUDERS > 3
  649. void lcd_preheat_pla3() { _lcd_preheat(3, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  650. void lcd_preheat_abs3() { _lcd_preheat(3, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  651. #endif
  652. #endif
  653. void lcd_preheat_pla0123() {
  654. setTargetHotend0(plaPreheatHotendTemp);
  655. setTargetHotend1(plaPreheatHotendTemp);
  656. setTargetHotend2(plaPreheatHotendTemp);
  657. _lcd_preheat(3, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed);
  658. }
  659. void lcd_preheat_abs0123() {
  660. setTargetHotend0(absPreheatHotendTemp);
  661. setTargetHotend1(absPreheatHotendTemp);
  662. setTargetHotend2(absPreheatHotendTemp);
  663. _lcd_preheat(3, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed);
  664. }
  665. #endif // EXTRUDERS > 1
  666. #if TEMP_SENSOR_BED != 0
  667. void lcd_preheat_pla_bedonly() { _lcd_preheat(0, 0, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  668. void lcd_preheat_abs_bedonly() { _lcd_preheat(0, 0, absPreheatHPBTemp, absPreheatFanSpeed); }
  669. #endif
  670. #if TEMP_SENSOR_0 != 0 && (TEMP_SENSOR_1 != 0 || TEMP_SENSOR_2 != 0 || TEMP_SENSOR_3 != 0 || TEMP_SENSOR_BED != 0)
  671. static void lcd_preheat_pla_menu() {
  672. START_MENU();
  673. MENU_ITEM(back, MSG_PREPARE, lcd_prepare_menu);
  674. #if EXTRUDERS == 1
  675. MENU_ITEM(function, MSG_PREHEAT_PLA, lcd_preheat_pla0);
  676. #else
  677. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H1, lcd_preheat_pla0);
  678. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H2, lcd_preheat_pla1);
  679. #if EXTRUDERS > 2
  680. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H3, lcd_preheat_pla2);
  681. #if EXTRUDERS > 3
  682. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H4, lcd_preheat_pla3);
  683. #endif
  684. #endif
  685. MENU_ITEM(function, MSG_PREHEAT_PLA_ALL, lcd_preheat_pla0123);
  686. #endif
  687. #if TEMP_SENSOR_BED != 0
  688. MENU_ITEM(function, MSG_PREHEAT_PLA_BEDONLY, lcd_preheat_pla_bedonly);
  689. #endif
  690. END_MENU();
  691. }
  692. static void lcd_preheat_abs_menu() {
  693. START_MENU();
  694. MENU_ITEM(back, MSG_PREPARE, lcd_prepare_menu);
  695. #if EXTRUDERS == 1
  696. MENU_ITEM(function, MSG_PREHEAT_ABS, lcd_preheat_abs0);
  697. #else
  698. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H1, lcd_preheat_abs0);
  699. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H2, lcd_preheat_abs1);
  700. #if EXTRUDERS > 2
  701. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H3, lcd_preheat_abs2);
  702. #if EXTRUDERS > 3
  703. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H4, lcd_preheat_abs3);
  704. #endif
  705. #endif
  706. MENU_ITEM(function, MSG_PREHEAT_ABS_ALL, lcd_preheat_abs0123);
  707. #endif
  708. #if TEMP_SENSOR_BED != 0
  709. MENU_ITEM(function, MSG_PREHEAT_ABS_BEDONLY, lcd_preheat_abs_bedonly);
  710. #endif
  711. END_MENU();
  712. }
  713. #endif // TEMP_SENSOR_0 && (TEMP_SENSOR_1 || TEMP_SENSOR_2 || TEMP_SENSOR_3 || TEMP_SENSOR_BED)
  714. void lcd_cooldown() {
  715. #if FAN_COUNT > 0
  716. for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
  717. #endif
  718. disable_all_heaters();
  719. lcd_return_to_status();
  720. }
  721. /**
  722. *
  723. * "Prepare" submenu
  724. *
  725. */
  726. static void lcd_prepare_menu() {
  727. START_MENU();
  728. //
  729. // ^ Main
  730. //
  731. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  732. //
  733. // Auto Home
  734. //
  735. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  736. //
  737. // Set Home Offsets
  738. //
  739. MENU_ITEM(function, MSG_SET_HOME_OFFSETS, lcd_set_home_offsets);
  740. //MENU_ITEM(gcode, MSG_SET_ORIGIN, PSTR("G92 X0 Y0 Z0"));
  741. //
  742. // Level Bed
  743. //
  744. #if ENABLED(AUTO_BED_LEVELING_FEATURE)
  745. if (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS])
  746. MENU_ITEM(gcode, MSG_LEVEL_BED, PSTR("G29"));
  747. #elif ENABLED(MANUAL_BED_LEVELING)
  748. MENU_ITEM(submenu, MSG_LEVEL_BED, lcd_level_bed);
  749. #endif
  750. //
  751. // Move Axis
  752. //
  753. MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu);
  754. //
  755. // Disable Steppers
  756. //
  757. MENU_ITEM(gcode, MSG_DISABLE_STEPPERS, PSTR("M84"));
  758. //
  759. // Preheat PLA
  760. // Preheat ABS
  761. //
  762. #if TEMP_SENSOR_0 != 0
  763. #if TEMP_SENSOR_1 != 0 || TEMP_SENSOR_2 != 0 || TEMP_SENSOR_3 != 0 || TEMP_SENSOR_BED != 0
  764. MENU_ITEM(submenu, MSG_PREHEAT_PLA, lcd_preheat_pla_menu);
  765. MENU_ITEM(submenu, MSG_PREHEAT_ABS, lcd_preheat_abs_menu);
  766. #else
  767. MENU_ITEM(function, MSG_PREHEAT_PLA, lcd_preheat_pla0);
  768. MENU_ITEM(function, MSG_PREHEAT_ABS, lcd_preheat_abs0);
  769. #endif
  770. #endif
  771. //
  772. // Cooldown
  773. //
  774. MENU_ITEM(function, MSG_COOLDOWN, lcd_cooldown);
  775. //
  776. // Switch power on/off
  777. //
  778. #if HAS_POWER_SWITCH
  779. if (powersupply)
  780. MENU_ITEM(gcode, MSG_SWITCH_PS_OFF, PSTR("M81"));
  781. else
  782. MENU_ITEM(gcode, MSG_SWITCH_PS_ON, PSTR("M80"));
  783. #endif
  784. //
  785. // Autostart
  786. //
  787. #if ENABLED(SDSUPPORT) && ENABLED(MENU_ADDAUTOSTART)
  788. MENU_ITEM(function, MSG_AUTOSTART, lcd_autostart_sd);
  789. #endif
  790. END_MENU();
  791. }
  792. #if ENABLED(DELTA_CALIBRATION_MENU)
  793. static void lcd_delta_calibrate_menu() {
  794. START_MENU();
  795. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  796. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  797. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_X, PSTR("G0 F8000 X-77.94 Y-45 Z0"));
  798. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_Y, PSTR("G0 F8000 X77.94 Y-45 Z0"));
  799. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_Z, PSTR("G0 F8000 X0 Y90 Z0"));
  800. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_CENTER, PSTR("G0 F8000 X0 Y0 Z0"));
  801. END_MENU();
  802. }
  803. #endif // DELTA_CALIBRATION_MENU
  804. inline void line_to_current(AxisEnum axis) {
  805. #if ENABLED(DELTA)
  806. calculate_delta(current_position);
  807. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
  808. #else
  809. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
  810. #endif
  811. }
  812. /**
  813. *
  814. * "Prepare" > "Move Axis" submenu
  815. *
  816. */
  817. float move_menu_scale;
  818. static void lcd_move_menu_axis();
  819. static void _lcd_move(const char* name, AxisEnum axis, int min, int max) {
  820. ENCODER_DIRECTION_NORMAL();
  821. if ((encoderPosition != 0) && (movesplanned() <= 3)) {
  822. refresh_cmd_timeout();
  823. current_position[axis] += float((int)encoderPosition) * move_menu_scale;
  824. if (min_software_endstops) NOLESS(current_position[axis], min);
  825. if (max_software_endstops) NOMORE(current_position[axis], max);
  826. encoderPosition = 0;
  827. line_to_current(axis);
  828. lcdDrawUpdate = 1;
  829. }
  830. if (lcdDrawUpdate) lcd_implementation_drawedit(name, ftostr31(current_position[axis]));
  831. if (LCD_CLICKED) lcd_goto_previous_menu();
  832. }
  833. #if ENABLED(DELTA)
  834. static float delta_clip_radius_2 = (DELTA_PRINTABLE_RADIUS) * (DELTA_PRINTABLE_RADIUS);
  835. static int delta_clip( float a ) { return sqrt(delta_clip_radius_2 - a*a); }
  836. static void lcd_move_x() { int clip = delta_clip(current_position[Y_AXIS]); _lcd_move(PSTR(MSG_MOVE_X), X_AXIS, max(X_MIN_POS, -clip), min(X_MAX_POS, clip)); }
  837. static void lcd_move_y() { int clip = delta_clip(current_position[X_AXIS]); _lcd_move(PSTR(MSG_MOVE_X), X_AXIS, max(X_MIN_POS, -clip), min(X_MAX_POS, clip)); }
  838. #else
  839. static void lcd_move_x() { _lcd_move(PSTR(MSG_MOVE_X), X_AXIS, X_MIN_POS, X_MAX_POS); }
  840. static void lcd_move_y() { _lcd_move(PSTR(MSG_MOVE_Y), Y_AXIS, Y_MIN_POS, Y_MAX_POS); }
  841. #endif
  842. static void lcd_move_z() { _lcd_move(PSTR(MSG_MOVE_Z), Z_AXIS, Z_MIN_POS, Z_MAX_POS); }
  843. static void lcd_move_e(
  844. #if EXTRUDERS > 1
  845. uint8_t e
  846. #endif
  847. ) {
  848. ENCODER_DIRECTION_NORMAL();
  849. #if EXTRUDERS > 1
  850. unsigned short original_active_extruder = active_extruder;
  851. active_extruder = e;
  852. #endif
  853. if ((encoderPosition != 0) && (movesplanned() <= 3)) {
  854. current_position[E_AXIS] += float((int)encoderPosition) * move_menu_scale;
  855. encoderPosition = 0;
  856. line_to_current(E_AXIS);
  857. lcdDrawUpdate = 1;
  858. }
  859. if (lcdDrawUpdate) {
  860. PGM_P pos_label;
  861. #if EXTRUDERS == 1
  862. pos_label = PSTR(MSG_MOVE_E);
  863. #else
  864. switch (e) {
  865. case 0: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E1); break;
  866. case 1: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E2); break;
  867. #if EXTRUDERS > 2
  868. case 2: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E3); break;
  869. #if EXTRUDERS > 3
  870. case 3: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E4); break;
  871. #endif //EXTRUDERS > 3
  872. #endif //EXTRUDERS > 2
  873. }
  874. #endif //EXTRUDERS > 1
  875. lcd_implementation_drawedit(pos_label, ftostr31(current_position[E_AXIS]));
  876. }
  877. if (LCD_CLICKED) lcd_goto_previous_menu();
  878. #if EXTRUDERS > 1
  879. active_extruder = original_active_extruder;
  880. #endif
  881. }
  882. #if EXTRUDERS > 1
  883. static void lcd_move_e0() { lcd_move_e(0); }
  884. static void lcd_move_e1() { lcd_move_e(1); }
  885. #if EXTRUDERS > 2
  886. static void lcd_move_e2() { lcd_move_e(2); }
  887. #if EXTRUDERS > 3
  888. static void lcd_move_e3() { lcd_move_e(3); }
  889. #endif
  890. #endif
  891. #endif // EXTRUDERS > 1
  892. /**
  893. *
  894. * "Prepare" > "Move Xmm" > "Move XYZ" submenu
  895. *
  896. */
  897. static void lcd_move_menu_axis() {
  898. START_MENU();
  899. MENU_ITEM(back, MSG_MOVE_AXIS, lcd_move_menu);
  900. MENU_ITEM(submenu, MSG_MOVE_X, lcd_move_x);
  901. MENU_ITEM(submenu, MSG_MOVE_Y, lcd_move_y);
  902. if (move_menu_scale < 10.0) {
  903. MENU_ITEM(submenu, MSG_MOVE_Z, lcd_move_z);
  904. #if EXTRUDERS == 1
  905. MENU_ITEM(submenu, MSG_MOVE_E, lcd_move_e);
  906. #else
  907. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E1, lcd_move_e0);
  908. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E2, lcd_move_e1);
  909. #if EXTRUDERS > 2
  910. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E3, lcd_move_e2);
  911. #if EXTRUDERS > 3
  912. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E4, lcd_move_e3);
  913. #endif
  914. #endif
  915. #endif // EXTRUDERS > 1
  916. }
  917. END_MENU();
  918. }
  919. static void lcd_move_menu_10mm() {
  920. move_menu_scale = 10.0;
  921. lcd_move_menu_axis();
  922. }
  923. static void lcd_move_menu_1mm() {
  924. move_menu_scale = 1.0;
  925. lcd_move_menu_axis();
  926. }
  927. static void lcd_move_menu_01mm() {
  928. move_menu_scale = 0.1;
  929. lcd_move_menu_axis();
  930. }
  931. /**
  932. *
  933. * "Prepare" > "Move Axis" submenu
  934. *
  935. */
  936. static void lcd_move_menu() {
  937. START_MENU();
  938. MENU_ITEM(back, MSG_PREPARE, lcd_prepare_menu);
  939. MENU_ITEM(submenu, MSG_MOVE_10MM, lcd_move_menu_10mm);
  940. MENU_ITEM(submenu, MSG_MOVE_1MM, lcd_move_menu_1mm);
  941. MENU_ITEM(submenu, MSG_MOVE_01MM, lcd_move_menu_01mm);
  942. //TODO:X,Y,Z,E
  943. END_MENU();
  944. }
  945. /**
  946. *
  947. * "Control" submenu
  948. *
  949. */
  950. static void lcd_control_menu() {
  951. START_MENU();
  952. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  953. MENU_ITEM(submenu, MSG_TEMPERATURE, lcd_control_temperature_menu);
  954. MENU_ITEM(submenu, MSG_MOTION, lcd_control_motion_menu);
  955. MENU_ITEM(submenu, MSG_VOLUMETRIC, lcd_control_volumetric_menu);
  956. #if ENABLED(HAS_LCD_CONTRAST)
  957. //MENU_ITEM_EDIT(int3, MSG_CONTRAST, &lcd_contrast, 0, 63);
  958. MENU_ITEM(submenu, MSG_CONTRAST, lcd_set_contrast);
  959. #endif
  960. #if ENABLED(FWRETRACT)
  961. MENU_ITEM(submenu, MSG_RETRACT, lcd_control_retract_menu);
  962. #endif
  963. #if ENABLED(EEPROM_SETTINGS)
  964. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  965. MENU_ITEM(function, MSG_LOAD_EPROM, Config_RetrieveSettings);
  966. #endif
  967. MENU_ITEM(function, MSG_RESTORE_FAILSAFE, Config_ResetDefault);
  968. END_MENU();
  969. }
  970. /**
  971. *
  972. * "Temperature" submenu
  973. *
  974. */
  975. #if ENABLED(PID_AUTOTUNE_MENU)
  976. #if ENABLED(PIDTEMP)
  977. int autotune_temp[EXTRUDERS] = { 150 };
  978. const int heater_maxtemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP);
  979. #endif
  980. #if ENABLED(PIDTEMPBED)
  981. int autotune_temp_bed = 70;
  982. #endif
  983. static void _lcd_autotune(int e) {
  984. char cmd[30];
  985. sprintf_P(cmd, PSTR("M303 U1 E%d S%d"), e,
  986. #if ENABLED(PIDTEMP) && ENABLED(PIDTEMPBED)
  987. e < 0 ? autotune_temp_bed : autotune_temp[e]
  988. #elif ENABLED(PIDTEMPBED)
  989. autotune_temp_bed
  990. #else
  991. autotune_temp[e]
  992. #endif
  993. );
  994. enqueue_and_echo_command(cmd);
  995. }
  996. #endif //PID_AUTOTUNE_MENU
  997. #if ENABLED(PIDTEMP)
  998. // Helpers for editing PID Ki & Kd values
  999. // grab the PID value out of the temp variable; scale it; then update the PID driver
  1000. void copy_and_scalePID_i(int e) {
  1001. PID_PARAM(Ki, e) = scalePID_i(raw_Ki);
  1002. updatePID();
  1003. }
  1004. void copy_and_scalePID_d(int e) {
  1005. PID_PARAM(Kd, e) = scalePID_d(raw_Kd);
  1006. updatePID();
  1007. }
  1008. #define _PIDTEMP_BASE_FUNCTIONS(eindex) \
  1009. void copy_and_scalePID_i_E ## eindex() { copy_and_scalePID_i(eindex); } \
  1010. void copy_and_scalePID_d_E ## eindex() { copy_and_scalePID_d(eindex); }
  1011. #if ENABLED(PID_AUTOTUNE_MENU)
  1012. #define _PIDTEMP_FUNCTIONS(eindex) \
  1013. _PIDTEMP_BASE_FUNCTIONS(eindex); \
  1014. void lcd_autotune_callback_E ## eindex() { _lcd_autotune(eindex); }
  1015. #else
  1016. #define _PIDTEMP_FUNCTIONS(eindex) _PIDTEMP_BASE_FUNCTIONS(eindex);
  1017. #endif
  1018. _PIDTEMP_FUNCTIONS(0);
  1019. #if ENABLED(PID_PARAMS_PER_EXTRUDER)
  1020. #if EXTRUDERS > 1
  1021. _PIDTEMP_FUNCTIONS(1);
  1022. #if EXTRUDERS > 2
  1023. _PIDTEMP_FUNCTIONS(2);
  1024. #if EXTRUDERS > 3
  1025. _PIDTEMP_FUNCTIONS(3);
  1026. #endif //EXTRUDERS > 3
  1027. #endif //EXTRUDERS > 2
  1028. #endif //EXTRUDERS > 1
  1029. #endif //PID_PARAMS_PER_EXTRUDER
  1030. #endif //PIDTEMP
  1031. /**
  1032. *
  1033. * "Control" > "Temperature" submenu
  1034. *
  1035. */
  1036. static void lcd_control_temperature_menu() {
  1037. START_MENU();
  1038. //
  1039. // ^ Control
  1040. //
  1041. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  1042. //
  1043. // Nozzle:
  1044. // Nozzle [1-4]:
  1045. //
  1046. #if EXTRUDERS == 1
  1047. #if TEMP_SENSOR_0 != 0
  1048. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  1049. #endif
  1050. #else //EXTRUDERS > 1
  1051. #if TEMP_SENSOR_0 != 0
  1052. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N1, &target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  1053. #endif
  1054. #if TEMP_SENSOR_1 != 0
  1055. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N2, &target_temperature[1], 0, HEATER_1_MAXTEMP - 15, watch_temp_callback_E1);
  1056. #endif
  1057. #if EXTRUDERS > 2
  1058. #if TEMP_SENSOR_2 != 0
  1059. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N3, &target_temperature[2], 0, HEATER_2_MAXTEMP - 15, watch_temp_callback_E2);
  1060. #endif
  1061. #if EXTRUDERS > 3
  1062. #if TEMP_SENSOR_3 != 0
  1063. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N4, &target_temperature[3], 0, HEATER_3_MAXTEMP - 15, watch_temp_callback_E3);
  1064. #endif
  1065. #endif // EXTRUDERS > 3
  1066. #endif // EXTRUDERS > 2
  1067. #endif // EXTRUDERS > 1
  1068. //
  1069. // Bed:
  1070. //
  1071. #if TEMP_SENSOR_BED != 0
  1072. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 15);
  1073. #endif
  1074. //
  1075. // Fan Speed:
  1076. //
  1077. #if FAN_COUNT > 0
  1078. #if HAS_FAN0
  1079. #if FAN_COUNT > 1
  1080. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED " 1"
  1081. #else
  1082. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED
  1083. #endif
  1084. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_1ST_FAN_SPEED, &fanSpeeds[0], 0, 255);
  1085. #endif
  1086. #if HAS_FAN1
  1087. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 2", &fanSpeeds[1], 0, 255);
  1088. #endif
  1089. #if HAS_FAN2
  1090. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 3", &fanSpeeds[2], 0, 255);
  1091. #endif
  1092. #endif // FAN_COUNT > 0
  1093. //
  1094. // Autotemp, Min, Max, Fact
  1095. //
  1096. #if ENABLED(AUTOTEMP) && (TEMP_SENSOR_0 != 0)
  1097. MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &autotemp_enabled);
  1098. MENU_ITEM_EDIT(float3, MSG_MIN, &autotemp_min, 0, HEATER_0_MAXTEMP - 15);
  1099. MENU_ITEM_EDIT(float3, MSG_MAX, &autotemp_max, 0, HEATER_0_MAXTEMP - 15);
  1100. MENU_ITEM_EDIT(float32, MSG_FACTOR, &autotemp_factor, 0.0, 1.0);
  1101. #endif
  1102. //
  1103. // PID-P, PID-I, PID-D, PID-C, PID Autotune
  1104. // PID-P E1, PID-I E1, PID-D E1, PID-C E1, PID Autotune E1
  1105. // PID-P E2, PID-I E2, PID-D E2, PID-C E2, PID Autotune E2
  1106. // PID-P E3, PID-I E3, PID-D E3, PID-C E3, PID Autotune E3
  1107. // PID-P E4, PID-I E4, PID-D E4, PID-C E4, PID Autotune E4
  1108. //
  1109. #if ENABLED(PIDTEMP)
  1110. #define _PID_BASE_MENU_ITEMS(ELABEL, eindex) \
  1111. raw_Ki = unscalePID_i(PID_PARAM(Ki, eindex)); \
  1112. raw_Kd = unscalePID_d(PID_PARAM(Kd, eindex)); \
  1113. MENU_ITEM_EDIT(float52, MSG_PID_P ELABEL, &PID_PARAM(Kp, eindex), 1, 9990); \
  1114. MENU_ITEM_EDIT_CALLBACK(float52, MSG_PID_I ELABEL, &raw_Ki, 0.01, 9990, copy_and_scalePID_i_E ## eindex); \
  1115. MENU_ITEM_EDIT_CALLBACK(float52, MSG_PID_D ELABEL, &raw_Kd, 1, 9990, copy_and_scalePID_d_E ## eindex)
  1116. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  1117. #define _PID_MENU_ITEMS(ELABEL, eindex) \
  1118. _PID_BASE_MENU_ITEMS(ELABEL, eindex); \
  1119. MENU_ITEM_EDIT(float3, MSG_PID_C ELABEL, &PID_PARAM(Kc, eindex), 1, 9990)
  1120. #else
  1121. #define _PID_MENU_ITEMS(ELABEL, eindex) _PID_BASE_MENU_ITEMS(ELABEL, eindex)
  1122. #endif
  1123. #if ENABLED(PID_AUTOTUNE_MENU)
  1124. #define PID_MENU_ITEMS(ELABEL, eindex) \
  1125. _PID_MENU_ITEMS(ELABEL, eindex); \
  1126. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_PID_AUTOTUNE ELABEL, &autotune_temp[eindex], 150, heater_maxtemp[eindex] - 15, lcd_autotune_callback_E ## eindex)
  1127. #else
  1128. #define PID_MENU_ITEMS(ELABEL, eindex) _PID_MENU_ITEMS(ELABEL, eindex)
  1129. #endif
  1130. #if ENABLED(PID_PARAMS_PER_EXTRUDER) && EXTRUDERS > 1
  1131. PID_MENU_ITEMS(MSG_E1, 0);
  1132. PID_MENU_ITEMS(MSG_E2, 1);
  1133. #if EXTRUDERS > 2
  1134. PID_MENU_ITEMS(MSG_E3, 2);
  1135. #if EXTRUDERS > 3
  1136. PID_MENU_ITEMS(MSG_E4, 3);
  1137. #endif //EXTRUDERS > 3
  1138. #endif //EXTRUDERS > 2
  1139. #else //!PID_PARAMS_PER_EXTRUDER || EXTRUDERS == 1
  1140. PID_MENU_ITEMS("", 0);
  1141. #endif //!PID_PARAMS_PER_EXTRUDER || EXTRUDERS == 1
  1142. #endif //PIDTEMP
  1143. //
  1144. // Preheat PLA conf
  1145. //
  1146. MENU_ITEM(submenu, MSG_PREHEAT_PLA_SETTINGS, lcd_control_temperature_preheat_pla_settings_menu);
  1147. //
  1148. // Preheat ABS conf
  1149. //
  1150. MENU_ITEM(submenu, MSG_PREHEAT_ABS_SETTINGS, lcd_control_temperature_preheat_abs_settings_menu);
  1151. END_MENU();
  1152. }
  1153. /**
  1154. *
  1155. * "Temperature" > "Preheat PLA conf" submenu
  1156. *
  1157. */
  1158. static void lcd_control_temperature_preheat_pla_settings_menu() {
  1159. START_MENU();
  1160. MENU_ITEM(back, MSG_TEMPERATURE, lcd_control_temperature_menu);
  1161. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &plaPreheatFanSpeed, 0, 255);
  1162. #if TEMP_SENSOR_0 != 0
  1163. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &plaPreheatHotendTemp, HEATER_0_MINTEMP, HEATER_0_MAXTEMP - 15);
  1164. #endif
  1165. #if TEMP_SENSOR_BED != 0
  1166. MENU_ITEM_EDIT(int3, MSG_BED, &plaPreheatHPBTemp, BED_MINTEMP, BED_MAXTEMP - 15);
  1167. #endif
  1168. #if ENABLED(EEPROM_SETTINGS)
  1169. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  1170. #endif
  1171. END_MENU();
  1172. }
  1173. /**
  1174. *
  1175. * "Temperature" > "Preheat ABS conf" submenu
  1176. *
  1177. */
  1178. static void lcd_control_temperature_preheat_abs_settings_menu() {
  1179. START_MENU();
  1180. MENU_ITEM(back, MSG_TEMPERATURE, lcd_control_temperature_menu);
  1181. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &absPreheatFanSpeed, 0, 255);
  1182. #if TEMP_SENSOR_0 != 0
  1183. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &absPreheatHotendTemp, HEATER_0_MINTEMP, HEATER_0_MAXTEMP - 15);
  1184. #endif
  1185. #if TEMP_SENSOR_BED != 0
  1186. MENU_ITEM_EDIT(int3, MSG_BED, &absPreheatHPBTemp, BED_MINTEMP, BED_MAXTEMP - 15);
  1187. #endif
  1188. #if ENABLED(EEPROM_SETTINGS)
  1189. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  1190. #endif
  1191. END_MENU();
  1192. }
  1193. /**
  1194. *
  1195. * "Control" > "Motion" submenu
  1196. *
  1197. */
  1198. static void lcd_control_motion_menu() {
  1199. START_MENU();
  1200. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  1201. #if ENABLED(AUTO_BED_LEVELING_FEATURE)
  1202. MENU_ITEM_EDIT(float32, MSG_ZPROBE_ZOFFSET, &zprobe_zoffset, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX);
  1203. #endif
  1204. // Manual bed leveling, Bed Z:
  1205. #if ENABLED(MANUAL_BED_LEVELING)
  1206. MENU_ITEM_EDIT(float43, MSG_BED_Z, &mbl.z_offset, -1, 1);
  1207. #endif
  1208. MENU_ITEM_EDIT(float5, MSG_ACC, &acceleration, 10, 99000);
  1209. MENU_ITEM_EDIT(float3, MSG_VXY_JERK, &max_xy_jerk, 1, 990);
  1210. #if ENABLED(DELTA)
  1211. MENU_ITEM_EDIT(float3, MSG_VZ_JERK, &max_z_jerk, 1, 990);
  1212. #else
  1213. MENU_ITEM_EDIT(float52, MSG_VZ_JERK, &max_z_jerk, 0.1, 990);
  1214. #endif
  1215. MENU_ITEM_EDIT(float3, MSG_VE_JERK, &max_e_jerk, 1, 990);
  1216. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_X, &max_feedrate[X_AXIS], 1, 999);
  1217. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Y, &max_feedrate[Y_AXIS], 1, 999);
  1218. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Z, &max_feedrate[Z_AXIS], 1, 999);
  1219. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_E, &max_feedrate[E_AXIS], 1, 999);
  1220. MENU_ITEM_EDIT(float3, MSG_VMIN, &minimumfeedrate, 0, 999);
  1221. MENU_ITEM_EDIT(float3, MSG_VTRAV_MIN, &mintravelfeedrate, 0, 999);
  1222. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_X, &max_acceleration_units_per_sq_second[X_AXIS], 100, 99000, reset_acceleration_rates);
  1223. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Y, &max_acceleration_units_per_sq_second[Y_AXIS], 100, 99000, reset_acceleration_rates);
  1224. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Z, &max_acceleration_units_per_sq_second[Z_AXIS], 10, 99000, reset_acceleration_rates);
  1225. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_E, &max_acceleration_units_per_sq_second[E_AXIS], 100, 99000, reset_acceleration_rates);
  1226. MENU_ITEM_EDIT(float5, MSG_A_RETRACT, &retract_acceleration, 100, 99000);
  1227. MENU_ITEM_EDIT(float5, MSG_A_TRAVEL, &travel_acceleration, 100, 99000);
  1228. MENU_ITEM_EDIT(float52, MSG_XSTEPS, &axis_steps_per_unit[X_AXIS], 5, 9999);
  1229. MENU_ITEM_EDIT(float52, MSG_YSTEPS, &axis_steps_per_unit[Y_AXIS], 5, 9999);
  1230. #if ENABLED(DELTA)
  1231. MENU_ITEM_EDIT(float52, MSG_ZSTEPS, &axis_steps_per_unit[Z_AXIS], 5, 9999);
  1232. #else
  1233. MENU_ITEM_EDIT(float51, MSG_ZSTEPS, &axis_steps_per_unit[Z_AXIS], 5, 9999);
  1234. #endif
  1235. MENU_ITEM_EDIT(float51, MSG_ESTEPS, &axis_steps_per_unit[E_AXIS], 5, 9999);
  1236. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  1237. MENU_ITEM_EDIT(bool, MSG_ENDSTOP_ABORT, &abort_on_endstop_hit);
  1238. #endif
  1239. #if ENABLED(SCARA)
  1240. MENU_ITEM_EDIT(float74, MSG_XSCALE, &axis_scaling[X_AXIS], 0.5, 2);
  1241. MENU_ITEM_EDIT(float74, MSG_YSCALE, &axis_scaling[Y_AXIS], 0.5, 2);
  1242. #endif
  1243. END_MENU();
  1244. }
  1245. /**
  1246. *
  1247. * "Control" > "Filament" submenu
  1248. *
  1249. */
  1250. static void lcd_control_volumetric_menu() {
  1251. START_MENU();
  1252. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  1253. MENU_ITEM_EDIT_CALLBACK(bool, MSG_VOLUMETRIC_ENABLED, &volumetric_enabled, calculate_volumetric_multipliers);
  1254. if (volumetric_enabled) {
  1255. #if EXTRUDERS == 1
  1256. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM, &filament_size[0], 1.5, 3.25, calculate_volumetric_multipliers);
  1257. #else //EXTRUDERS > 1
  1258. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E1, &filament_size[0], 1.5, 3.25, calculate_volumetric_multipliers);
  1259. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E2, &filament_size[1], 1.5, 3.25, calculate_volumetric_multipliers);
  1260. #if EXTRUDERS > 2
  1261. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E3, &filament_size[2], 1.5, 3.25, calculate_volumetric_multipliers);
  1262. #if EXTRUDERS > 3
  1263. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E4, &filament_size[3], 1.5, 3.25, calculate_volumetric_multipliers);
  1264. #endif //EXTRUDERS > 3
  1265. #endif //EXTRUDERS > 2
  1266. #endif //EXTRUDERS > 1
  1267. }
  1268. END_MENU();
  1269. }
  1270. /**
  1271. *
  1272. * "Control" > "Contrast" submenu
  1273. *
  1274. */
  1275. #if ENABLED(HAS_LCD_CONTRAST)
  1276. static void lcd_set_contrast() {
  1277. ENCODER_DIRECTION_NORMAL();
  1278. if (encoderPosition != 0) {
  1279. #if ENABLED(U8GLIB_LM6059_AF)
  1280. lcd_contrast += encoderPosition;
  1281. lcd_contrast &= 0xFF;
  1282. #else
  1283. lcd_contrast -= encoderPosition;
  1284. lcd_contrast &= 0x3F;
  1285. #endif
  1286. encoderPosition = 0;
  1287. lcdDrawUpdate = 1;
  1288. u8g.setContrast(lcd_contrast);
  1289. }
  1290. if (lcdDrawUpdate) {
  1291. #if ENABLED(U8GLIB_LM6059_AF)
  1292. lcd_implementation_drawedit(PSTR(MSG_CONTRAST), itostr3(lcd_contrast));
  1293. #else
  1294. lcd_implementation_drawedit(PSTR(MSG_CONTRAST), itostr2(lcd_contrast));
  1295. #endif
  1296. }
  1297. if (LCD_CLICKED) lcd_goto_previous_menu();
  1298. }
  1299. #endif // HAS_LCD_CONTRAST
  1300. /**
  1301. *
  1302. * "Control" > "Retract" submenu
  1303. *
  1304. */
  1305. #if ENABLED(FWRETRACT)
  1306. static void lcd_control_retract_menu() {
  1307. START_MENU();
  1308. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  1309. MENU_ITEM_EDIT(bool, MSG_AUTORETRACT, &autoretract_enabled);
  1310. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT, &retract_length, 0, 100);
  1311. #if EXTRUDERS > 1
  1312. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_SWAP, &retract_length_swap, 0, 100);
  1313. #endif
  1314. MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACTF, &retract_feedrate, 1, 999);
  1315. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_ZLIFT, &retract_zlift, 0, 999);
  1316. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_RECOVER, &retract_recover_length, 0, 100);
  1317. #if EXTRUDERS > 1
  1318. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_RECOVER_SWAP, &retract_recover_length_swap, 0, 100);
  1319. #endif
  1320. MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACT_RECOVERF, &retract_recover_feedrate, 1, 999);
  1321. END_MENU();
  1322. }
  1323. #endif // FWRETRACT
  1324. #if ENABLED(SDSUPPORT)
  1325. #if !PIN_EXISTS(SD_DETECT)
  1326. static void lcd_sd_refresh() {
  1327. card.initsd();
  1328. currentMenuViewOffset = 0;
  1329. }
  1330. #endif
  1331. static void lcd_sd_updir() {
  1332. card.updir();
  1333. currentMenuViewOffset = 0;
  1334. }
  1335. /**
  1336. *
  1337. * "Print from SD" submenu
  1338. *
  1339. */
  1340. void lcd_sdcard_menu() {
  1341. ENCODER_DIRECTION_MENUS();
  1342. if (lcdDrawUpdate == 0 && LCD_CLICKED == 0) return; // nothing to do (so don't thrash the SD card)
  1343. uint16_t fileCnt = card.getnrfilenames();
  1344. START_MENU();
  1345. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  1346. card.getWorkDirName();
  1347. if (card.filename[0] == '/') {
  1348. #if !PIN_EXISTS(SD_DETECT)
  1349. MENU_ITEM(function, LCD_STR_REFRESH MSG_REFRESH, lcd_sd_refresh);
  1350. #endif
  1351. }
  1352. else {
  1353. MENU_ITEM(function, LCD_STR_FOLDER "..", lcd_sd_updir);
  1354. }
  1355. for (uint16_t i = 0; i < fileCnt; i++) {
  1356. if (_menuItemNr == _lineNr) {
  1357. card.getfilename(
  1358. #if ENABLED(SDCARD_RATHERRECENTFIRST)
  1359. fileCnt-1 -
  1360. #endif
  1361. i
  1362. );
  1363. if (card.filenameIsDir)
  1364. MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
  1365. else
  1366. MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
  1367. }
  1368. else {
  1369. MENU_ITEM_DUMMY();
  1370. }
  1371. }
  1372. END_MENU();
  1373. }
  1374. #endif //SDSUPPORT
  1375. /**
  1376. *
  1377. * Functions for editing single values
  1378. *
  1379. * The "menu_edit_type" macro generates the functions needed to edit a numerical value.
  1380. *
  1381. * For example, menu_edit_type(int, int3, itostr3, 1) expands into these functions:
  1382. *
  1383. * bool _menu_edit_int3();
  1384. * void menu_edit_int3(); // edit int (interactively)
  1385. * void menu_edit_callback_int3(); // edit int (interactively) with callback on completion
  1386. * static void _menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  1387. * static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  1388. * static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, menuFunc_t callback); // edit int with callback
  1389. *
  1390. * You can then use one of the menu macros to present the edit interface:
  1391. * MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_multiplier, 10, 999)
  1392. *
  1393. * This expands into a more primitive menu item:
  1394. * MENU_ITEM(setting_edit_int3, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  1395. *
  1396. *
  1397. * Also: MENU_MULTIPLIER_ITEM_EDIT, MENU_ITEM_EDIT_CALLBACK, and MENU_MULTIPLIER_ITEM_EDIT_CALLBACK
  1398. *
  1399. * menu_action_setting_edit_int3(PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  1400. */
  1401. #define menu_edit_type(_type, _name, _strFunc, scale) \
  1402. bool _menu_edit_ ## _name () { \
  1403. ENCODER_DIRECTION_NORMAL(); \
  1404. bool isClicked = LCD_CLICKED; \
  1405. if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
  1406. if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
  1407. if (lcdDrawUpdate) \
  1408. lcd_implementation_drawedit(editLabel, _strFunc(((_type)((int32_t)encoderPosition + minEditValue)) / scale)); \
  1409. if (isClicked) { \
  1410. *((_type*)editValue) = ((_type)((int32_t)encoderPosition + minEditValue)) / scale; \
  1411. lcd_goto_previous_menu(); \
  1412. } \
  1413. return isClicked; \
  1414. } \
  1415. void menu_edit_ ## _name () { _menu_edit_ ## _name(); } \
  1416. void menu_edit_callback_ ## _name () { if (_menu_edit_ ## _name ()) (*callbackFunc)(); } \
  1417. static void _menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) { \
  1418. lcd_save_previous_menu(); \
  1419. \
  1420. lcdDrawUpdate = 2; \
  1421. currentMenu = menu_edit_ ## _name; \
  1422. \
  1423. editLabel = pstr; \
  1424. editValue = ptr; \
  1425. minEditValue = minValue * scale; \
  1426. maxEditValue = maxValue * scale - minEditValue; \
  1427. encoderPosition = (*ptr) * scale - minEditValue; \
  1428. } \
  1429. static void menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) { \
  1430. _menu_action_setting_edit_ ## _name(pstr, ptr, minValue, maxValue); \
  1431. currentMenu = menu_edit_ ## _name; \
  1432. }\
  1433. static void menu_action_setting_edit_callback_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue, menuFunc_t callback) { \
  1434. _menu_action_setting_edit_ ## _name(pstr, ptr, minValue, maxValue); \
  1435. currentMenu = menu_edit_callback_ ## _name; \
  1436. callbackFunc = callback; \
  1437. }
  1438. menu_edit_type(int, int3, itostr3, 1)
  1439. menu_edit_type(float, float3, ftostr3, 1)
  1440. menu_edit_type(float, float32, ftostr32, 100)
  1441. menu_edit_type(float, float43, ftostr43, 1000)
  1442. menu_edit_type(float, float5, ftostr5, 0.01)
  1443. menu_edit_type(float, float51, ftostr51, 10)
  1444. menu_edit_type(float, float52, ftostr52, 100)
  1445. menu_edit_type(unsigned long, long5, ftostr5, 0.01)
  1446. /**
  1447. *
  1448. * Handlers for RepRap World Keypad input
  1449. *
  1450. */
  1451. #if ENABLED(REPRAPWORLD_KEYPAD)
  1452. static void reprapworld_keypad_move_z_up() {
  1453. encoderPosition = 1;
  1454. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1455. lcd_move_z();
  1456. }
  1457. static void reprapworld_keypad_move_z_down() {
  1458. encoderPosition = -1;
  1459. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1460. lcd_move_z();
  1461. }
  1462. static void reprapworld_keypad_move_x_left() {
  1463. encoderPosition = -1;
  1464. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1465. lcd_move_x();
  1466. }
  1467. static void reprapworld_keypad_move_x_right() {
  1468. encoderPosition = 1;
  1469. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1470. lcd_move_x();
  1471. }
  1472. static void reprapworld_keypad_move_y_down() {
  1473. encoderPosition = 1;
  1474. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1475. lcd_move_y();
  1476. }
  1477. static void reprapworld_keypad_move_y_up() {
  1478. encoderPosition = -1;
  1479. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1480. lcd_move_y();
  1481. }
  1482. static void reprapworld_keypad_move_home() {
  1483. enqueue_and_echo_commands_P((PSTR("G28"))); // move all axis home
  1484. }
  1485. #endif // REPRAPWORLD_KEYPAD
  1486. /**
  1487. *
  1488. * Audio feedback for controller clicks
  1489. *
  1490. */
  1491. #if ENABLED(LCD_USE_I2C_BUZZER)
  1492. void lcd_buzz(long duration, uint16_t freq) { // called from buzz() in Marlin_main.cpp where lcd is unknown
  1493. lcd.buzz(duration, freq);
  1494. }
  1495. #endif
  1496. void lcd_quick_feedback() {
  1497. lcdDrawUpdate = 2;
  1498. next_button_update_ms = millis() + 500;
  1499. #if ENABLED(LCD_USE_I2C_BUZZER)
  1500. #ifndef LCD_FEEDBACK_FREQUENCY_HZ
  1501. #define LCD_FEEDBACK_FREQUENCY_HZ 100
  1502. #endif
  1503. #ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS
  1504. #define LCD_FEEDBACK_FREQUENCY_DURATION_MS (1000/6)
  1505. #endif
  1506. lcd.buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  1507. #elif PIN_EXISTS(BEEPER)
  1508. #ifndef LCD_FEEDBACK_FREQUENCY_HZ
  1509. #define LCD_FEEDBACK_FREQUENCY_HZ 5000
  1510. #endif
  1511. #ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS
  1512. #define LCD_FEEDBACK_FREQUENCY_DURATION_MS 2
  1513. #endif
  1514. buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  1515. #else
  1516. #ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS
  1517. #define LCD_FEEDBACK_FREQUENCY_DURATION_MS 2
  1518. #endif
  1519. delay(LCD_FEEDBACK_FREQUENCY_DURATION_MS);
  1520. #endif
  1521. }
  1522. /**
  1523. *
  1524. * Menu actions
  1525. *
  1526. */
  1527. static void menu_action_back(menuFunc_t func) { lcd_goto_menu(func); }
  1528. static void menu_action_submenu(menuFunc_t func) { lcd_save_previous_menu(); lcd_goto_menu(func); }
  1529. static void menu_action_gcode(const char* pgcode) { enqueue_and_echo_commands_P(pgcode); }
  1530. static void menu_action_function(menuFunc_t func) { (*func)(); }
  1531. #if ENABLED(SDSUPPORT)
  1532. static void menu_action_sdfile(const char* filename, char* longFilename) {
  1533. card.openAndPrintFile(filename);
  1534. lcd_return_to_status();
  1535. }
  1536. static void menu_action_sddirectory(const char* filename, char* longFilename) {
  1537. card.chdir(filename);
  1538. encoderPosition = 0;
  1539. }
  1540. #endif //SDSUPPORT
  1541. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr) { *ptr = !(*ptr); }
  1542. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callback) {
  1543. menu_action_setting_edit_bool(pstr, ptr);
  1544. (*callback)();
  1545. }
  1546. #endif //ULTIPANEL
  1547. /** LCD API **/
  1548. void lcd_init() {
  1549. lcd_implementation_init();
  1550. #if ENABLED(NEWPANEL)
  1551. #if BTN_EN1 > 0
  1552. SET_INPUT(BTN_EN1);
  1553. WRITE(BTN_EN1, HIGH);
  1554. #endif
  1555. #if BTN_EN2 > 0
  1556. SET_INPUT(BTN_EN2);
  1557. WRITE(BTN_EN2, HIGH);
  1558. #endif
  1559. #if BTN_ENC > 0
  1560. SET_INPUT(BTN_ENC);
  1561. WRITE(BTN_ENC, HIGH);
  1562. #endif
  1563. #if ENABLED(REPRAPWORLD_KEYPAD)
  1564. pinMode(SHIFT_CLK, OUTPUT);
  1565. pinMode(SHIFT_LD, OUTPUT);
  1566. pinMode(SHIFT_OUT, INPUT);
  1567. WRITE(SHIFT_OUT, HIGH);
  1568. WRITE(SHIFT_LD, HIGH);
  1569. #endif
  1570. #ifdef RIGIDBOT_PANEL
  1571. SET_INPUT(BTN_UP);
  1572. SET_INPUT(BTN_DWN);
  1573. SET_INPUT(BTN_LFT);
  1574. SET_INPUT(BTN_RT);
  1575. #endif
  1576. #else // Not NEWPANEL
  1577. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  1578. pinMode(SR_DATA_PIN, OUTPUT);
  1579. pinMode(SR_CLK_PIN, OUTPUT);
  1580. #elif defined(SHIFT_CLK)
  1581. pinMode(SHIFT_CLK, OUTPUT);
  1582. pinMode(SHIFT_LD, OUTPUT);
  1583. pinMode(SHIFT_EN, OUTPUT);
  1584. pinMode(SHIFT_OUT, INPUT);
  1585. WRITE(SHIFT_OUT, HIGH);
  1586. WRITE(SHIFT_LD, HIGH);
  1587. WRITE(SHIFT_EN, LOW);
  1588. #endif // SR_LCD_2W_NL
  1589. #endif//!NEWPANEL
  1590. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  1591. SET_INPUT(SD_DETECT_PIN);
  1592. WRITE(SD_DETECT_PIN, HIGH);
  1593. lcd_sd_status = 2; // UNKNOWN
  1594. #endif
  1595. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  1596. slow_buttons = 0;
  1597. #endif
  1598. lcd_buttons_update();
  1599. #if ENABLED(ULTIPANEL)
  1600. encoderDiff = 0;
  1601. #endif
  1602. }
  1603. int lcd_strlen(const char* s) {
  1604. int i = 0, j = 0;
  1605. while (s[i]) {
  1606. if ((s[i] & 0xc0) != 0x80) j++;
  1607. i++;
  1608. }
  1609. return j;
  1610. }
  1611. int lcd_strlen_P(const char* s) {
  1612. int j = 0;
  1613. while (pgm_read_byte(s)) {
  1614. if ((pgm_read_byte(s) & 0xc0) != 0x80) j++;
  1615. s++;
  1616. }
  1617. return j;
  1618. }
  1619. bool lcd_blink() {
  1620. static uint8_t blink = 0;
  1621. static millis_t next_blink_ms = 0;
  1622. if (millis() >= next_blink_ms) {
  1623. blink ^= 0xFF;
  1624. next_blink_ms = millis() + LCD_UPDATE_INTERVAL - 50;
  1625. }
  1626. return blink != 0;
  1627. }
  1628. /**
  1629. * Update the LCD, read encoder buttons, etc.
  1630. * - Read button states
  1631. * - Check the SD Card slot state
  1632. * - Act on RepRap World keypad input
  1633. * - Update the encoder position
  1634. * - Apply acceleration to the encoder position
  1635. * - Reset the Info Screen timeout if there's any input
  1636. * - Update status indicators, if any
  1637. * - Clear the LCD if lcdDrawUpdate == 2
  1638. *
  1639. * Warning: This function is called from interrupt context!
  1640. */
  1641. void lcd_update() {
  1642. #if ENABLED(ULTIPANEL)
  1643. static millis_t return_to_status_ms = 0;
  1644. #endif
  1645. lcd_buttons_update();
  1646. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  1647. bool sd_status = IS_SD_INSERTED;
  1648. if (sd_status != lcd_sd_status && lcd_detected()) {
  1649. lcdDrawUpdate = 2;
  1650. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  1651. #if ENABLED(LCD_PROGRESS_BAR)
  1652. currentMenu == lcd_status_screen
  1653. #endif
  1654. );
  1655. if (sd_status) {
  1656. card.initsd();
  1657. if (lcd_sd_status != 2) LCD_MESSAGEPGM(MSG_SD_INSERTED);
  1658. }
  1659. else {
  1660. card.release();
  1661. if (lcd_sd_status != 2) LCD_MESSAGEPGM(MSG_SD_REMOVED);
  1662. }
  1663. lcd_sd_status = sd_status;
  1664. }
  1665. #endif //SDSUPPORT && SD_DETECT_PIN
  1666. millis_t ms = millis();
  1667. if (ms > next_lcd_update_ms) {
  1668. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  1669. slow_buttons = lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  1670. #endif
  1671. #if ENABLED(ULTIPANEL)
  1672. #if ENABLED(REPRAPWORLD_KEYPAD)
  1673. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) reprapworld_keypad_move_z_up();
  1674. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) reprapworld_keypad_move_z_down();
  1675. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) reprapworld_keypad_move_x_left();
  1676. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) reprapworld_keypad_move_x_right();
  1677. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) reprapworld_keypad_move_y_down();
  1678. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) reprapworld_keypad_move_y_up();
  1679. if (REPRAPWORLD_KEYPAD_MOVE_HOME) reprapworld_keypad_move_home();
  1680. #endif
  1681. bool encoderPastThreshold = (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP);
  1682. if (encoderPastThreshold || LCD_CLICKED) {
  1683. if (encoderPastThreshold) {
  1684. int32_t encoderMultiplier = 1;
  1685. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  1686. if (encoderRateMultiplierEnabled) {
  1687. int32_t encoderMovementSteps = abs(encoderDiff) / ENCODER_PULSES_PER_STEP;
  1688. if (lastEncoderMovementMillis != 0) {
  1689. // Note that the rate is always calculated between to passes through the
  1690. // loop and that the abs of the encoderDiff value is tracked.
  1691. float encoderStepRate = (float)(encoderMovementSteps) / ((float)(ms - lastEncoderMovementMillis)) * 1000.0;
  1692. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  1693. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  1694. #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
  1695. SERIAL_ECHO_START;
  1696. SERIAL_ECHO("Enc Step Rate: ");
  1697. SERIAL_ECHO(encoderStepRate);
  1698. SERIAL_ECHO(" Multiplier: ");
  1699. SERIAL_ECHO(encoderMultiplier);
  1700. SERIAL_ECHO(" ENCODER_10X_STEPS_PER_SEC: ");
  1701. SERIAL_ECHO(ENCODER_10X_STEPS_PER_SEC);
  1702. SERIAL_ECHO(" ENCODER_100X_STEPS_PER_SEC: ");
  1703. SERIAL_ECHOLN(ENCODER_100X_STEPS_PER_SEC);
  1704. #endif //ENCODER_RATE_MULTIPLIER_DEBUG
  1705. }
  1706. lastEncoderMovementMillis = ms;
  1707. } // encoderRateMultiplierEnabled
  1708. #endif //ENCODER_RATE_MULTIPLIER
  1709. encoderPosition += (encoderDiff * encoderMultiplier) / ENCODER_PULSES_PER_STEP;
  1710. encoderDiff = 0;
  1711. }
  1712. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  1713. lcdDrawUpdate = 1;
  1714. }
  1715. #endif //ULTIPANEL
  1716. if (currentMenu == lcd_status_screen) {
  1717. if (!lcd_status_update_delay) {
  1718. lcdDrawUpdate = 1;
  1719. lcd_status_update_delay = 10; /* redraw the main screen every second. This is easier then trying keep track of all things that change on the screen */
  1720. }
  1721. else {
  1722. lcd_status_update_delay--;
  1723. }
  1724. }
  1725. if (lcdDrawUpdate) {
  1726. #if ENABLED(DOGLCD) // Changes due to different driver architecture of the DOGM display
  1727. bool blink = lcd_blink();
  1728. u8g.firstPage();
  1729. do {
  1730. lcd_setFont(FONT_MENU);
  1731. u8g.setPrintPos(125, 0);
  1732. u8g.setColorIndex(blink ? 1 : 0); // Set color for the alive dot
  1733. u8g.drawPixel(127, 63); // draw alive dot
  1734. u8g.setColorIndex(1); // black on white
  1735. (*currentMenu)();
  1736. } while (u8g.nextPage());
  1737. #else
  1738. (*currentMenu)();
  1739. #endif
  1740. }
  1741. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  1742. lcd_implementation_update_indicators();
  1743. #endif
  1744. #if ENABLED(ULTIPANEL)
  1745. // Return to Status Screen after a timeout
  1746. if (currentMenu != lcd_status_screen &&
  1747. #if ENABLED(MANUAL_BED_LEVELING)
  1748. currentMenu != _lcd_level_bed &&
  1749. currentMenu != _lcd_level_bed_homing &&
  1750. #endif
  1751. millis() > return_to_status_ms
  1752. ) {
  1753. lcd_return_to_status();
  1754. lcdDrawUpdate = 2;
  1755. }
  1756. #endif // ULTIPANEL
  1757. if (lcdDrawUpdate && --lcdDrawUpdate) lcd_implementation_clear();
  1758. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL;
  1759. }
  1760. }
  1761. void lcd_ignore_click(bool b) {
  1762. ignore_click = b;
  1763. wait_for_unclick = false;
  1764. }
  1765. void lcd_finishstatus(bool persist=false) {
  1766. #if ENABLED(LCD_PROGRESS_BAR)
  1767. progress_bar_ms = millis();
  1768. #if PROGRESS_MSG_EXPIRE > 0
  1769. expire_status_ms = persist ? 0 : progress_bar_ms + PROGRESS_MSG_EXPIRE;
  1770. #endif
  1771. #endif
  1772. lcdDrawUpdate = 2;
  1773. #if ENABLED(FILAMENT_LCD_DISPLAY)
  1774. previous_lcd_status_ms = millis(); //get status message to show up for a while
  1775. #endif
  1776. }
  1777. #if ENABLED(LCD_PROGRESS_BAR) && PROGRESS_MSG_EXPIRE > 0
  1778. void dontExpireStatus() { expire_status_ms = 0; }
  1779. #endif
  1780. void set_utf_strlen(char* s, uint8_t n) {
  1781. uint8_t i = 0, j = 0;
  1782. while (s[i] && (j < n)) {
  1783. if ((s[i] & 0xc0u) != 0x80u) j++;
  1784. i++;
  1785. }
  1786. while (j++ < n) s[i++] = ' ';
  1787. s[i] = 0;
  1788. }
  1789. bool lcd_hasstatus() { return (lcd_status_message[0] != '\0'); }
  1790. void lcd_setstatus(const char* message, bool persist) {
  1791. if (lcd_status_message_level > 0) return;
  1792. strncpy(lcd_status_message, message, 3 * (LCD_WIDTH));
  1793. set_utf_strlen(lcd_status_message, LCD_WIDTH);
  1794. lcd_finishstatus(persist);
  1795. }
  1796. void lcd_setstatuspgm(const char* message, uint8_t level) {
  1797. if (level >= lcd_status_message_level) {
  1798. strncpy_P(lcd_status_message, message, 3 * (LCD_WIDTH));
  1799. set_utf_strlen(lcd_status_message, LCD_WIDTH);
  1800. lcd_status_message_level = level;
  1801. lcd_finishstatus(level > 0);
  1802. }
  1803. }
  1804. void lcd_setalertstatuspgm(const char* message) {
  1805. lcd_setstatuspgm(message, 1);
  1806. #if ENABLED(ULTIPANEL)
  1807. lcd_return_to_status();
  1808. #endif
  1809. }
  1810. void lcd_reset_alert_level() { lcd_status_message_level = 0; }
  1811. #if ENABLED(HAS_LCD_CONTRAST)
  1812. void lcd_setcontrast(uint8_t value) {
  1813. lcd_contrast = value & 0x3F;
  1814. u8g.setContrast(lcd_contrast);
  1815. }
  1816. #endif
  1817. #if ENABLED(ULTIPANEL)
  1818. /**
  1819. * Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
  1820. * These values are independent of which pins are used for EN_A and EN_B indications
  1821. * The rotary encoder part is also independent to the chipset used for the LCD
  1822. */
  1823. #if defined(EN_A) && defined(EN_B)
  1824. #define encrot0 0
  1825. #define encrot1 2
  1826. #define encrot2 3
  1827. #define encrot3 1
  1828. #endif
  1829. /**
  1830. * Read encoder buttons from the hardware registers
  1831. * Warning: This function is called from interrupt context!
  1832. */
  1833. void lcd_buttons_update() {
  1834. #if ENABLED(NEWPANEL)
  1835. uint8_t newbutton = 0;
  1836. #if BTN_EN1 > 0
  1837. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  1838. #endif
  1839. #if BTN_EN2 > 0
  1840. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  1841. #endif
  1842. #if ENABLED(RIGIDBOT_PANEL) || BTN_ENC > 0
  1843. millis_t now = millis();
  1844. #endif
  1845. #if ENABLED(RIGIDBOT_PANEL)
  1846. if (now > next_button_update_ms) {
  1847. if (READ(BTN_UP) == 0) {
  1848. encoderDiff = -1 * (ENCODER_STEPS_PER_MENU_ITEM);
  1849. next_button_update_ms = now + 300;
  1850. }
  1851. else if (READ(BTN_DWN) == 0) {
  1852. encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
  1853. next_button_update_ms = now + 300;
  1854. }
  1855. else if (READ(BTN_LFT) == 0) {
  1856. encoderDiff = -1 * (ENCODER_PULSES_PER_STEP);
  1857. next_button_update_ms = now + 300;
  1858. }
  1859. else if (READ(BTN_RT) == 0) {
  1860. encoderDiff = ENCODER_PULSES_PER_STEP;
  1861. next_button_update_ms = now + 300;
  1862. }
  1863. }
  1864. #endif
  1865. #if BTN_ENC > 0
  1866. if (now > next_button_update_ms && READ(BTN_ENC) == 0) newbutton |= EN_C;
  1867. #endif
  1868. buttons = newbutton;
  1869. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  1870. buttons |= slow_buttons;
  1871. #endif
  1872. #if ENABLED(REPRAPWORLD_KEYPAD)
  1873. // for the reprapworld_keypad
  1874. uint8_t newbutton_reprapworld_keypad = 0;
  1875. WRITE(SHIFT_LD, LOW);
  1876. WRITE(SHIFT_LD, HIGH);
  1877. for (int8_t i = 0; i < 8; i++) {
  1878. newbutton_reprapworld_keypad >>= 1;
  1879. if (READ(SHIFT_OUT)) SBI(newbutton_reprapworld_keypad, 7);
  1880. WRITE(SHIFT_CLK, HIGH);
  1881. WRITE(SHIFT_CLK, LOW);
  1882. }
  1883. buttons_reprapworld_keypad = ~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
  1884. #endif
  1885. #else //read it from the shift register
  1886. uint8_t newbutton = 0;
  1887. WRITE(SHIFT_LD, LOW);
  1888. WRITE(SHIFT_LD, HIGH);
  1889. unsigned char tmp_buttons = 0;
  1890. for (int8_t i = 0; i < 8; i++) {
  1891. newbutton >>= 1;
  1892. if (READ(SHIFT_OUT)) SBI(newbutton, 7);
  1893. WRITE(SHIFT_CLK, HIGH);
  1894. WRITE(SHIFT_CLK, LOW);
  1895. }
  1896. buttons = ~newbutton; //invert it, because a pressed switch produces a logical 0
  1897. #endif //!NEWPANEL
  1898. #if ENABLED(REVERSE_MENU_DIRECTION)
  1899. #define ENCODER_DIFF_CW (encoderDiff += encoderDirection)
  1900. #define ENCODER_DIFF_CCW (encoderDiff -= encoderDirection)
  1901. #else
  1902. #define ENCODER_DIFF_CW (encoderDiff++)
  1903. #define ENCODER_DIFF_CCW (encoderDiff--)
  1904. #endif
  1905. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: ENCODER_DIFF_CW; break; case _E2: ENCODER_DIFF_CCW; }
  1906. //manage encoder rotation
  1907. uint8_t enc = 0;
  1908. if (buttons & EN_A) enc |= B01;
  1909. if (buttons & EN_B) enc |= B10;
  1910. if (enc != lastEncoderBits) {
  1911. switch (enc) {
  1912. case encrot0: ENCODER_SPIN(encrot3, encrot1); break;
  1913. case encrot1: ENCODER_SPIN(encrot0, encrot2); break;
  1914. case encrot2: ENCODER_SPIN(encrot1, encrot3); break;
  1915. case encrot3: ENCODER_SPIN(encrot2, encrot0); break;
  1916. }
  1917. }
  1918. lastEncoderBits = enc;
  1919. }
  1920. bool lcd_detected(void) {
  1921. #if (ENABLED(LCD_I2C_TYPE_MCP23017) || ENABLED(LCD_I2C_TYPE_MCP23008)) && ENABLED(DETECT_DEVICE)
  1922. return lcd.LcdDetected() == 1;
  1923. #else
  1924. return true;
  1925. #endif
  1926. }
  1927. bool lcd_clicked() { return LCD_CLICKED; }
  1928. #endif // ULTIPANEL
  1929. /*********************************/
  1930. /** Number to string conversion **/
  1931. /*********************************/
  1932. char conv[8];
  1933. // Convert float to rj string with 123 or -12 format
  1934. char *ftostr3(const float& x) { return itostr3((int)x); }
  1935. // Convert float to rj string with _123, -123, _-12, or __-1 format
  1936. char *ftostr4sign(const float& x) { return itostr4sign((int)x); }
  1937. // Convert int to string with 12 format
  1938. char* itostr2(const uint8_t& x) {
  1939. //sprintf(conv,"%5.1f",x);
  1940. int xx = x;
  1941. conv[0] = (xx / 10) % 10 + '0';
  1942. conv[1] = xx % 10 + '0';
  1943. conv[2] = 0;
  1944. return conv;
  1945. }
  1946. // Convert float to string with +123.4 format
  1947. char* ftostr31(const float& x) {
  1948. int xx = abs(x * 10);
  1949. conv[0] = (x >= 0) ? '+' : '-';
  1950. conv[1] = (xx / 1000) % 10 + '0';
  1951. conv[2] = (xx / 100) % 10 + '0';
  1952. conv[3] = (xx / 10) % 10 + '0';
  1953. conv[4] = '.';
  1954. conv[5] = xx % 10 + '0';
  1955. conv[6] = 0;
  1956. return conv;
  1957. }
  1958. // Convert float to string with 123.4 format, dropping sign
  1959. char* ftostr31ns(const float& x) {
  1960. int xx = abs(x * 10);
  1961. conv[0] = (xx / 1000) % 10 + '0';
  1962. conv[1] = (xx / 100) % 10 + '0';
  1963. conv[2] = (xx / 10) % 10 + '0';
  1964. conv[3] = '.';
  1965. conv[4] = xx % 10 + '0';
  1966. conv[5] = 0;
  1967. return conv;
  1968. }
  1969. // Convert float to string with 123.45 format
  1970. char *ftostr32(const float& x) {
  1971. long xx = abs(x * 100);
  1972. conv[0] = x >= 0 ? (xx / 10000) % 10 + '0' : '-';
  1973. conv[1] = (xx / 1000) % 10 + '0';
  1974. conv[2] = (xx / 100) % 10 + '0';
  1975. conv[3] = '.';
  1976. conv[4] = (xx / 10) % 10 + '0';
  1977. conv[5] = xx % 10 + '0';
  1978. conv[6] = 0;
  1979. return conv;
  1980. }
  1981. // Convert float to string with 1.234 format
  1982. char* ftostr43(const float& x) {
  1983. long xx = x * 1000;
  1984. if (xx >= 0)
  1985. conv[0] = (xx / 1000) % 10 + '0';
  1986. else
  1987. conv[0] = '-';
  1988. xx = abs(xx);
  1989. conv[1] = '.';
  1990. conv[2] = (xx / 100) % 10 + '0';
  1991. conv[3] = (xx / 10) % 10 + '0';
  1992. conv[4] = (xx) % 10 + '0';
  1993. conv[5] = 0;
  1994. return conv;
  1995. }
  1996. // Convert float to string with 1.23 format
  1997. char* ftostr12ns(const float& x) {
  1998. long xx = x * 100;
  1999. xx = abs(xx);
  2000. conv[0] = (xx / 100) % 10 + '0';
  2001. conv[1] = '.';
  2002. conv[2] = (xx / 10) % 10 + '0';
  2003. conv[3] = (xx) % 10 + '0';
  2004. conv[4] = 0;
  2005. return conv;
  2006. }
  2007. // Convert float to space-padded string with -_23.4_ format
  2008. char* ftostr32sp(const float& x) {
  2009. long xx = abs(x * 100);
  2010. uint8_t dig;
  2011. if (x < 0) { // negative val = -_0
  2012. conv[0] = '-';
  2013. dig = (xx / 1000) % 10;
  2014. conv[1] = dig ? '0' + dig : ' ';
  2015. }
  2016. else { // positive val = __0
  2017. dig = (xx / 10000) % 10;
  2018. if (dig) {
  2019. conv[0] = '0' + dig;
  2020. conv[1] = '0' + (xx / 1000) % 10;
  2021. }
  2022. else {
  2023. conv[0] = ' ';
  2024. dig = (xx / 1000) % 10;
  2025. conv[1] = dig ? '0' + dig : ' ';
  2026. }
  2027. }
  2028. conv[2] = '0' + (xx / 100) % 10; // lsd always
  2029. dig = xx % 10;
  2030. if (dig) { // 2 decimal places
  2031. conv[5] = '0' + dig;
  2032. conv[4] = '0' + (xx / 10) % 10;
  2033. conv[3] = '.';
  2034. }
  2035. else { // 1 or 0 decimal place
  2036. dig = (xx / 10) % 10;
  2037. if (dig) {
  2038. conv[4] = '0' + dig;
  2039. conv[3] = '.';
  2040. }
  2041. else {
  2042. conv[3] = conv[4] = ' ';
  2043. }
  2044. conv[5] = ' ';
  2045. }
  2046. conv[6] = '\0';
  2047. return conv;
  2048. }
  2049. // Convert int to lj string with +123.0 format
  2050. char* itostr31(const int& x) {
  2051. conv[0] = x >= 0 ? '+' : '-';
  2052. int xx = abs(x);
  2053. conv[1] = (xx / 100) % 10 + '0';
  2054. conv[2] = (xx / 10) % 10 + '0';
  2055. conv[3] = xx % 10 + '0';
  2056. conv[4] = '.';
  2057. conv[5] = '0';
  2058. conv[6] = 0;
  2059. return conv;
  2060. }
  2061. // Convert int to rj string with 123 or -12 format
  2062. char* itostr3(const int& x) {
  2063. int xx = x;
  2064. if (xx < 0) {
  2065. conv[0] = '-';
  2066. xx = -xx;
  2067. }
  2068. else
  2069. conv[0] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2070. conv[1] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2071. conv[2] = xx % 10 + '0';
  2072. conv[3] = 0;
  2073. return conv;
  2074. }
  2075. // Convert int to lj string with 123 format
  2076. char* itostr3left(const int& xx) {
  2077. if (xx >= 100) {
  2078. conv[0] = (xx / 100) % 10 + '0';
  2079. conv[1] = (xx / 10) % 10 + '0';
  2080. conv[2] = xx % 10 + '0';
  2081. conv[3] = 0;
  2082. }
  2083. else if (xx >= 10) {
  2084. conv[0] = (xx / 10) % 10 + '0';
  2085. conv[1] = xx % 10 + '0';
  2086. conv[2] = 0;
  2087. }
  2088. else {
  2089. conv[0] = xx % 10 + '0';
  2090. conv[1] = 0;
  2091. }
  2092. return conv;
  2093. }
  2094. // Convert int to rj string with 1234 format
  2095. char* itostr4(const int& xx) {
  2096. conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2097. conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2098. conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2099. conv[3] = xx % 10 + '0';
  2100. conv[4] = 0;
  2101. return conv;
  2102. }
  2103. // Convert int to rj string with _123, -123, _-12, or __-1 format
  2104. char *itostr4sign(const int& x) {
  2105. int xx = abs(x);
  2106. int sign = 0;
  2107. if (xx >= 100) {
  2108. conv[1] = (xx / 100) % 10 + '0';
  2109. conv[2] = (xx / 10) % 10 + '0';
  2110. }
  2111. else if (xx >= 10) {
  2112. conv[0] = ' ';
  2113. sign = 1;
  2114. conv[2] = (xx / 10) % 10 + '0';
  2115. }
  2116. else {
  2117. conv[0] = ' ';
  2118. conv[1] = ' ';
  2119. sign = 2;
  2120. }
  2121. conv[sign] = x < 0 ? '-' : ' ';
  2122. conv[3] = xx % 10 + '0';
  2123. conv[4] = 0;
  2124. return conv;
  2125. }
  2126. // Convert float to rj string with 12345 format
  2127. char* ftostr5(const float& x) {
  2128. long xx = abs(x);
  2129. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  2130. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2131. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2132. conv[3] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2133. conv[4] = xx % 10 + '0';
  2134. conv[5] = 0;
  2135. return conv;
  2136. }
  2137. // Convert float to string with +1234.5 format
  2138. char* ftostr51(const float& x) {
  2139. long xx = abs(x * 10);
  2140. conv[0] = (x >= 0) ? '+' : '-';
  2141. conv[1] = (xx / 10000) % 10 + '0';
  2142. conv[2] = (xx / 1000) % 10 + '0';
  2143. conv[3] = (xx / 100) % 10 + '0';
  2144. conv[4] = (xx / 10) % 10 + '0';
  2145. conv[5] = '.';
  2146. conv[6] = xx % 10 + '0';
  2147. conv[7] = 0;
  2148. return conv;
  2149. }
  2150. // Convert float to string with +123.45 format
  2151. char* ftostr52(const float& x) {
  2152. conv[0] = (x >= 0) ? '+' : '-';
  2153. long xx = abs(x * 100);
  2154. conv[1] = (xx / 10000) % 10 + '0';
  2155. conv[2] = (xx / 1000) % 10 + '0';
  2156. conv[3] = (xx / 100) % 10 + '0';
  2157. conv[4] = '.';
  2158. conv[5] = (xx / 10) % 10 + '0';
  2159. conv[6] = xx % 10 + '0';
  2160. conv[7] = 0;
  2161. return conv;
  2162. }
  2163. #if ENABLED(MANUAL_BED_LEVELING)
  2164. static int _lcd_level_bed_position;
  2165. /**
  2166. * MBL Wait for controller movement and clicks:
  2167. * - Movement adjusts the Z axis
  2168. * - Click saves the Z and goes to the next mesh point
  2169. */
  2170. static void _lcd_level_bed() {
  2171. ENCODER_DIRECTION_NORMAL();
  2172. if ((encoderPosition != 0) && (movesplanned() <= 3)) {
  2173. refresh_cmd_timeout();
  2174. current_position[Z_AXIS] += float((int)encoderPosition) * (MBL_Z_STEP);
  2175. if (min_software_endstops) NOLESS(current_position[Z_AXIS], Z_MIN_POS);
  2176. if (max_software_endstops) NOMORE(current_position[Z_AXIS], Z_MAX_POS);
  2177. encoderPosition = 0;
  2178. line_to_current(Z_AXIS);
  2179. lcdDrawUpdate = 2;
  2180. }
  2181. if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR("Z"), ftostr43(current_position[Z_AXIS]));
  2182. static bool debounce_click = false;
  2183. if (LCD_CLICKED) {
  2184. if (!debounce_click) {
  2185. debounce_click = true;
  2186. int ix = _lcd_level_bed_position % (MESH_NUM_X_POINTS),
  2187. iy = _lcd_level_bed_position / (MESH_NUM_X_POINTS);
  2188. if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // Zig zag
  2189. mbl.set_z(ix, iy, current_position[Z_AXIS]);
  2190. _lcd_level_bed_position++;
  2191. if (_lcd_level_bed_position == (MESH_NUM_X_POINTS) * (MESH_NUM_Y_POINTS)) {
  2192. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  2193. line_to_current(Z_AXIS);
  2194. st_synchronize();
  2195. mbl.active = 1;
  2196. enqueue_and_echo_commands_P(PSTR("G28"));
  2197. lcd_return_to_status();
  2198. }
  2199. else {
  2200. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  2201. line_to_current(Z_AXIS);
  2202. ix = _lcd_level_bed_position % (MESH_NUM_X_POINTS);
  2203. iy = _lcd_level_bed_position / (MESH_NUM_X_POINTS);
  2204. if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // Zig zag
  2205. current_position[X_AXIS] = mbl.get_x(ix);
  2206. current_position[Y_AXIS] = mbl.get_y(iy);
  2207. line_to_current(manual_feedrate[X_AXIS] <= manual_feedrate[Y_AXIS] ? X_AXIS : Y_AXIS);
  2208. lcdDrawUpdate = 2;
  2209. }
  2210. }
  2211. }
  2212. else {
  2213. debounce_click = false;
  2214. }
  2215. }
  2216. /**
  2217. * MBL Move to mesh starting point
  2218. */
  2219. static void _lcd_level_bed_homing() {
  2220. if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR("XYZ"), MSG_LEVEL_BED_HOMING);
  2221. if (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS]) {
  2222. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  2223. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2224. current_position[X_AXIS] = MESH_MIN_X;
  2225. current_position[Y_AXIS] = MESH_MIN_Y;
  2226. line_to_current(manual_feedrate[X_AXIS] <= manual_feedrate[Y_AXIS] ? X_AXIS : Y_AXIS);
  2227. _lcd_level_bed_position = 0;
  2228. lcd_goto_menu(_lcd_level_bed);
  2229. }
  2230. lcdDrawUpdate = 2;
  2231. }
  2232. /**
  2233. * MBL entry-point
  2234. */
  2235. static void lcd_level_bed() {
  2236. axis_known_position[X_AXIS] = axis_known_position[Y_AXIS] = axis_known_position[Z_AXIS] = false;
  2237. mbl.reset();
  2238. enqueue_and_echo_commands_P(PSTR("G28"));
  2239. lcdDrawUpdate = 2;
  2240. lcd_goto_menu(_lcd_level_bed_homing);
  2241. }
  2242. #endif // MANUAL_BED_LEVELING
  2243. #endif // ULTRA_LCD