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

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