My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ultralcd.cpp 86KB

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