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

ultralcd.cpp 100KB

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