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

ultralcd.cpp 83KB

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