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

ultralcd.cpp 83KB

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