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

ultralcd.cpp 82KB

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