My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ultralcd.cpp 82KB

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