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

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