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

ultralcd.cpp 86KB

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