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.

ultralcd.cpp 100KB

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