My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ultralcd.cpp 78KB

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