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.

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