My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ultralcd.cpp 87KB

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