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

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