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

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