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

ultralcd.cpp 84KB

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