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

ultralcd.cpp 86KB

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