My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ultralcd.cpp 78KB

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