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

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