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

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