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 84KB

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