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

ultralcd.cpp 78KB

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