ultralcd.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295
  1. #include "temperature.h"
  2. #include "ultralcd.h"
  3. #ifdef ULTRA_LCD
  4. #include "Marlin.h"
  5. #include "language.h"
  6. #include "cardreader.h"
  7. #include "temperature.h"
  8. #include "stepper.h"
  9. #include "ConfigurationStore.h"
  10. #include <string.h>
  11. //#include "Configuration.h"
  12. #define _STRINGIFY(s) #s
  13. int8_t encoderDiff; /* encoderDiff is updated from interrupt context and added to encoderPosition every LCD update */
  14. extern int lcd_change_fil_state;
  15. int babystepMem[3];
  16. float babystepMemMM[3];
  17. union Data
  18. {
  19. byte b[2];
  20. int value;
  21. };
  22. int8_t ReInitLCD = 0;
  23. int8_t SDscrool = 0;
  24. int8_t SilentModeMenu = 0;
  25. int lcd_commands_type=0;
  26. int lcd_commands_step=0;
  27. bool isPrintPaused = false;
  28. bool menuExiting = false;
  29. /* Configuration settings */
  30. int plaPreheatHotendTemp;
  31. int plaPreheatHPBTemp;
  32. int plaPreheatFanSpeed;
  33. int absPreheatHotendTemp;
  34. int absPreheatHPBTemp;
  35. int absPreheatFanSpeed;
  36. int ppPreheatHotendTemp = PP_PREHEAT_HOTEND_TEMP;
  37. int ppPreheatHPBTemp = PP_PREHEAT_HPB_TEMP;
  38. int ppPreheatFanSpeed = PP_PREHEAT_FAN_SPEED;
  39. int petPreheatHotendTemp = PET_PREHEAT_HOTEND_TEMP;
  40. int petPreheatHPBTemp = PET_PREHEAT_HPB_TEMP;
  41. int petPreheatFanSpeed = PET_PREHEAT_FAN_SPEED;
  42. int hipsPreheatHotendTemp = HIPS_PREHEAT_HOTEND_TEMP;
  43. int hipsPreheatHPBTemp = HIPS_PREHEAT_HPB_TEMP;
  44. int hipsPreheatFanSpeed = HIPS_PREHEAT_FAN_SPEED;
  45. int flexPreheatHotendTemp = FLEX_PREHEAT_HOTEND_TEMP;
  46. int flexPreheatHPBTemp = FLEX_PREHEAT_HPB_TEMP;
  47. int flexPreheatFanSpeed = FLEX_PREHEAT_FAN_SPEED;
  48. #ifdef FILAMENT_LCD_DISPLAY
  49. unsigned long message_millis = 0;
  50. #endif
  51. #ifdef ULTIPANEL
  52. static float manual_feedrate[] = MANUAL_FEEDRATE;
  53. #endif // ULTIPANEL
  54. /* !Configuration settings */
  55. //Function pointer to menu functions.
  56. typedef void (*menuFunc_t)();
  57. uint8_t lcd_status_message_level;
  58. char lcd_status_message[LCD_WIDTH + 1] = ""; //////WELCOME!
  59. unsigned char firstrun = 1;
  60. #ifdef DOGLCD
  61. #include "dogm_lcd_implementation.h"
  62. #else
  63. #include "ultralcd_implementation_hitachi_HD44780.h"
  64. #endif
  65. /** forward declarations **/
  66. void copy_and_scalePID_i();
  67. void copy_and_scalePID_d();
  68. /* Different menus */
  69. static void lcd_status_screen();
  70. #ifdef ULTIPANEL
  71. extern bool powersupply;
  72. static void lcd_main_menu();
  73. static void lcd_tune_menu();
  74. static void lcd_prepare_menu();
  75. static void lcd_move_menu();
  76. static void lcd_control_menu();
  77. static void lcd_settings_menu();
  78. static void lcd_language_menu();
  79. static void lcd_control_temperature_menu();
  80. static void lcd_control_temperature_preheat_pla_settings_menu();
  81. static void lcd_control_temperature_preheat_abs_settings_menu();
  82. static void lcd_control_motion_menu();
  83. static void lcd_control_volumetric_menu();
  84. #ifdef DOGLCD
  85. static void lcd_set_contrast();
  86. #endif
  87. static void lcd_control_retract_menu();
  88. static void lcd_sdcard_menu();
  89. #ifdef DELTA_CALIBRATION_MENU
  90. static void lcd_delta_calibrate_menu();
  91. #endif // DELTA_CALIBRATION_MENU
  92. static void lcd_quick_feedback();//Cause an LCD refresh, and give the user visual or audible feedback that something has happened
  93. /* Different types of actions that can be used in menu items. */
  94. static void menu_action_back(menuFunc_t data);
  95. static void menu_action_submenu(menuFunc_t data);
  96. static void menu_action_gcode(const char* pgcode);
  97. static void menu_action_function(menuFunc_t data);
  98. static void menu_action_setlang(unsigned char lang);
  99. static void menu_action_sdfile(const char* filename, char* longFilename);
  100. static void menu_action_sddirectory(const char* filename, char* longFilename);
  101. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr);
  102. static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  103. static void menu_action_setting_edit_float3(const char* pstr, float* ptr, float minValue, float maxValue);
  104. static void menu_action_setting_edit_float32(const char* pstr, float* ptr, float minValue, float maxValue);
  105. static void menu_action_setting_edit_float43(const char* pstr, float* ptr, float minValue, float maxValue);
  106. static void menu_action_setting_edit_float5(const char* pstr, float* ptr, float minValue, float maxValue);
  107. static void menu_action_setting_edit_float51(const char* pstr, float* ptr, float minValue, float maxValue);
  108. static void menu_action_setting_edit_float52(const char* pstr, float* ptr, float minValue, float maxValue);
  109. static void menu_action_setting_edit_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue);
  110. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callbackFunc);
  111. static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, menuFunc_t callbackFunc);
  112. static void menu_action_setting_edit_callback_float3(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  113. static void menu_action_setting_edit_callback_float32(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  114. static void menu_action_setting_edit_callback_float43(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  115. static void menu_action_setting_edit_callback_float5(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  116. static void menu_action_setting_edit_callback_float51(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  117. static void menu_action_setting_edit_callback_float52(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  118. static void menu_action_setting_edit_callback_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue, menuFunc_t callbackFunc);
  119. #define ENCODER_FEEDRATE_DEADZONE 10
  120. #if !defined(LCD_I2C_VIKI)
  121. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  122. #define ENCODER_STEPS_PER_MENU_ITEM 5
  123. #endif
  124. #ifndef ENCODER_PULSES_PER_STEP
  125. #define ENCODER_PULSES_PER_STEP 1
  126. #endif
  127. #else
  128. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  129. #define ENCODER_STEPS_PER_MENU_ITEM 2 // VIKI LCD rotary encoder uses a different number of steps per rotation
  130. #endif
  131. #ifndef ENCODER_PULSES_PER_STEP
  132. #define ENCODER_PULSES_PER_STEP 1
  133. #endif
  134. #endif
  135. /* Helper macros for menus */
  136. #define START_MENU() do { \
  137. if (encoderPosition > 0x8000) encoderPosition = 0; \
  138. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM < currentMenuViewOffset) currentMenuViewOffset = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM;\
  139. uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
  140. bool wasClicked = LCD_CLICKED;\
  141. for(uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
  142. _menuItemNr = 0;
  143. #define MENU_ITEM(type, label, args...) do { \
  144. if (_menuItemNr == _lineNr) { \
  145. if (lcdDrawUpdate) { \
  146. const char* _label_pstr = (label); \
  147. if ((encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) { \
  148. lcd_implementation_drawmenu_ ## type ## _selected (_drawLineNr, _label_pstr , ## args ); \
  149. }else{\
  150. lcd_implementation_drawmenu_ ## type (_drawLineNr, _label_pstr , ## args ); \
  151. }\
  152. }\
  153. if (wasClicked && (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) {\
  154. lcd_quick_feedback(); \
  155. menu_action_ ## type ( args ); \
  156. return;\
  157. }\
  158. }\
  159. _menuItemNr++;\
  160. } while(0)
  161. #define MENU_ITEM_DUMMY() do { _menuItemNr++; } while(0)
  162. #define MENU_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, (label) , ## args )
  163. #define MENU_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, (label) , ## args )
  164. #define END_MENU() \
  165. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM >= _menuItemNr) encoderPosition = _menuItemNr * ENCODER_STEPS_PER_MENU_ITEM - 1; \
  166. if ((uint8_t)(encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) >= currentMenuViewOffset + LCD_HEIGHT) { currentMenuViewOffset = (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) - LCD_HEIGHT + 1; lcdDrawUpdate = 1; _lineNr = currentMenuViewOffset - 1; _drawLineNr = -1; } \
  167. } } while(0)
  168. /** Used variables to keep track of the menu */
  169. #ifndef REPRAPWORLD_KEYPAD
  170. volatile uint8_t buttons;//Contains the bits of the currently pressed buttons.
  171. #else
  172. volatile uint8_t buttons_reprapworld_keypad; // to store the reprapworld_keypad shift register values
  173. #endif
  174. #ifdef LCD_HAS_SLOW_BUTTONS
  175. volatile uint8_t slow_buttons;//Contains the bits of the currently pressed buttons.
  176. #endif
  177. uint8_t currentMenuViewOffset; /* scroll offset in the current menu */
  178. uint32_t blocking_enc;
  179. uint8_t lastEncoderBits;
  180. uint32_t encoderPosition;
  181. #if (SDCARDDETECT > 0)
  182. bool lcd_oldcardstatus;
  183. #endif
  184. #endif //ULTIPANEL
  185. menuFunc_t currentMenu = lcd_status_screen; /* function pointer to the currently active menu */
  186. uint32_t lcd_next_update_millis;
  187. uint8_t lcd_status_update_delay;
  188. bool ignore_click = false;
  189. bool wait_for_unclick;
  190. uint8_t lcdDrawUpdate = 2; /* Set to none-zero when the LCD needs to draw, decreased after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial) */
  191. //prevMenu and prevEncoderPosition are used to store the previous menu location when editing settings.
  192. menuFunc_t prevMenu = NULL;
  193. uint16_t prevEncoderPosition;
  194. //Variables used when editing values.
  195. const char* editLabel;
  196. void* editValue;
  197. int32_t minEditValue, maxEditValue;
  198. menuFunc_t callbackFunc;
  199. // place-holders for Ki and Kd edits
  200. float raw_Ki, raw_Kd;
  201. static void lcd_goto_menu(menuFunc_t menu, const uint32_t encoder = 0, const bool feedback = true) {
  202. if (currentMenu != menu) {
  203. currentMenu = menu;
  204. encoderPosition = encoder;
  205. if (feedback) lcd_quick_feedback();
  206. // For LCD_PROGRESS_BAR re-initialize the custom characters
  207. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  208. lcd_set_custom_characters(menu == lcd_status_screen);
  209. #endif
  210. }
  211. }
  212. /* Main status screen. It's up to the implementation specific part to show what is needed. As this is very display dependent */
  213. /*
  214. extern char langbuffer[];
  215. void lcd_printPGM(const char *s1) {
  216. strncpy_P(langbuffer,s1,LCD_WIDTH);
  217. lcd.print(langbuffer);
  218. }
  219. */
  220. unsigned char langsel;
  221. void set_language_from_EEPROM() {
  222. unsigned char eep = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  223. if (eep < LANG_NUM)
  224. {
  225. lang_selected = eep;
  226. langsel = 0;
  227. }
  228. else
  229. {
  230. lang_selected = 1;
  231. langsel = 1;
  232. }
  233. }
  234. void lcd_mylang();
  235. static void lcd_status_screen()
  236. {
  237. if (firstrun == 1)
  238. {
  239. firstrun = 0;
  240. set_language_from_EEPROM();
  241. strncpy_P(lcd_status_message, WELCOME_MSG, LCD_WIDTH);
  242. if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255)
  243. {
  244. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  245. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  246. }
  247. if (langsel) {
  248. //strncpy_P(lcd_status_message, PSTR(">>>>>>>>>>>> PRESS v"), LCD_WIDTH);
  249. lcd_mylang();
  250. }
  251. }
  252. if (lcd_status_update_delay)
  253. lcd_status_update_delay--;
  254. else
  255. lcdDrawUpdate = 1;
  256. if (lcdDrawUpdate)
  257. {
  258. ReInitLCD++;
  259. if (ReInitLCD == 30) {
  260. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  261. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  262. currentMenu == lcd_status_screen
  263. #endif
  264. );
  265. ReInitLCD = 0 ;
  266. } else {
  267. if ((ReInitLCD % 10) == 0) {
  268. //lcd_implementation_nodisplay();
  269. lcd_implementation_init_noclear( // to maybe revive the LCD if static electricity killed it.
  270. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  271. currentMenu == lcd_status_screen
  272. #endif
  273. );
  274. }
  275. }
  276. //lcd_implementation_display();
  277. lcd_implementation_status_screen();
  278. //lcd_implementation_clear();
  279. 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 */
  280. if (lcd_commands_type != 0)
  281. {
  282. lcd_commands();
  283. }
  284. }
  285. #ifdef ULTIPANEL
  286. bool current_click = LCD_CLICKED;
  287. if (ignore_click) {
  288. if (wait_for_unclick) {
  289. if (!current_click) {
  290. ignore_click = wait_for_unclick = false;
  291. }
  292. else {
  293. current_click = false;
  294. }
  295. }
  296. else if (current_click) {
  297. lcd_quick_feedback();
  298. wait_for_unclick = true;
  299. current_click = false;
  300. }
  301. }
  302. //if (--langsel ==0) {langsel=1;current_click=true;}
  303. if (current_click)
  304. {
  305. lcd_goto_menu(lcd_main_menu);
  306. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  307. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  308. currentMenu == lcd_status_screen
  309. #endif
  310. );
  311. #ifdef FILAMENT_LCD_DISPLAY
  312. message_millis = millis(); // get status message to show up for a while
  313. #endif
  314. }
  315. #ifdef ULTIPANEL_FEEDMULTIPLY
  316. // Dead zone at 100% feedrate
  317. if ((feedmultiply < 100 && (feedmultiply + int(encoderPosition)) > 100) ||
  318. (feedmultiply > 100 && (feedmultiply + int(encoderPosition)) < 100))
  319. {
  320. encoderPosition = 0;
  321. feedmultiply = 100;
  322. }
  323. if (feedmultiply == 100 && int(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  324. {
  325. feedmultiply += int(encoderPosition) - ENCODER_FEEDRATE_DEADZONE;
  326. encoderPosition = 0;
  327. }
  328. else if (feedmultiply == 100 && int(encoderPosition) < -ENCODER_FEEDRATE_DEADZONE)
  329. {
  330. feedmultiply += int(encoderPosition) + ENCODER_FEEDRATE_DEADZONE;
  331. encoderPosition = 0;
  332. }
  333. else if (feedmultiply != 100)
  334. {
  335. feedmultiply += int(encoderPosition);
  336. encoderPosition = 0;
  337. }
  338. #endif //ULTIPANEL_FEEDMULTIPLY
  339. if (feedmultiply < 10)
  340. feedmultiply = 10;
  341. else if (feedmultiply > 999)
  342. feedmultiply = 999;
  343. #endif //ULTIPANEL
  344. }
  345. #ifdef ULTIPANEL
  346. void lcd_commands()
  347. {
  348. if (lcd_commands_type == 1) //// load filament sequence
  349. {
  350. if (lcd_commands_step == 0) { lcd_commands_step = 5; custom_message = true; }
  351. if (lcd_commands_step == 1 && !blocks_queued())
  352. {
  353. lcd_commands_step = 0;
  354. lcd_commands_type = 0;
  355. lcd_setstatuspgm(WELCOME_MSG);
  356. disable_z();
  357. custom_message = false;
  358. custom_message_type = 0;
  359. }
  360. if (lcd_commands_step == 2 && !blocks_queued())
  361. {
  362. lcd_setstatuspgm(MSG_LOADING_FILAMENT);
  363. enquecommand_P(PSTR(LOAD_FILAMENT_2));
  364. lcd_commands_step = 1;
  365. }
  366. if (lcd_commands_step == 3 && !blocks_queued())
  367. {
  368. enquecommand_P(PSTR(LOAD_FILAMENT_1));
  369. lcd_commands_step = 2;
  370. }
  371. if (lcd_commands_step == 4 && !blocks_queued())
  372. {
  373. lcd_setstatuspgm(MSG_INSERT_FILAMENT);
  374. enquecommand_P(PSTR(LOAD_FILAMENT_0));
  375. lcd_commands_step = 3;
  376. }
  377. if (lcd_commands_step == 5 && !blocks_queued())
  378. {
  379. lcd_setstatuspgm(MSG_PLEASE_WAIT);
  380. enable_z();
  381. custom_message = true;
  382. custom_message_type = 2;
  383. lcd_commands_step = 4;
  384. }
  385. }
  386. if (lcd_commands_type == 2) /// stop print
  387. {
  388. if (lcd_commands_step == 0) { lcd_commands_step = 6; custom_message = true; }
  389. if (lcd_commands_step == 1 && !blocks_queued())
  390. {
  391. lcd_commands_step = 0;
  392. lcd_commands_type = 0;
  393. lcd_setstatuspgm(WELCOME_MSG);
  394. custom_message = false;
  395. }
  396. if (lcd_commands_step == 2 && !blocks_queued())
  397. {
  398. setTargetBed(0);
  399. setTargetHotend(0, 0);
  400. setTargetHotend(0, 1);
  401. setTargetHotend(0, 2);
  402. manage_heater();
  403. lcd_setstatuspgm(WELCOME_MSG);
  404. cancel_heatup = false;
  405. lcd_commands_step = 1;
  406. }
  407. if (lcd_commands_step == 3 && !blocks_queued())
  408. {
  409. enquecommand_P(PSTR("M84"));
  410. autotempShutdown();
  411. lcd_commands_step = 2;
  412. }
  413. if (lcd_commands_step == 4 && !blocks_queued())
  414. {
  415. enquecommand_P(PSTR("G90"));
  416. #ifdef X_CANCEL_POS
  417. enquecommand_P(PSTR("G1 X" STRINGIFY(X_CANCEL_POS) " Y" STRINGIFY(Y_CANCEL_POS) " E0 F7000"));
  418. #else
  419. enquecommand_P(PSTR("G1 X50 Y" STRINGIFY(Y_MAX_POS) " E0 F7000"));
  420. #endif
  421. lcd_ignore_click(false);
  422. lcd_commands_step = 3;
  423. }
  424. if (lcd_commands_step == 5 && !blocks_queued())
  425. {
  426. lcd_setstatuspgm(MSG_PRINT_ABORTED);
  427. enquecommand_P(PSTR("G91"));
  428. enquecommand_P(PSTR("G1 Z15 F1500"));
  429. lcd_commands_step = 4;
  430. }
  431. if (lcd_commands_step == 6 && !blocks_queued())
  432. {
  433. lcd_setstatuspgm(MSG_PRINT_ABORTED);
  434. cancel_heatup = true;
  435. setTargetBed(0);
  436. setTargetHotend(0, 0);
  437. setTargetHotend(0, 1);
  438. setTargetHotend(0, 2);
  439. manage_heater();
  440. lcd_commands_step = 5;
  441. }
  442. }
  443. if (lcd_commands_type == 3)
  444. {
  445. lcd_commands_type = 0;
  446. }
  447. }
  448. static void lcd_return_to_status() {
  449. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  450. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  451. currentMenu == lcd_status_screen
  452. #endif
  453. );
  454. lcd_goto_menu(lcd_status_screen, 0, false);
  455. }
  456. static void lcd_sdcard_pause() {
  457. card.pauseSDPrint();
  458. isPrintPaused = true;
  459. lcdDrawUpdate = 3;
  460. }
  461. static void lcd_sdcard_resume() {
  462. card.startFileprint();
  463. isPrintPaused = false;
  464. lcdDrawUpdate = 3;
  465. }
  466. float move_menu_scale;
  467. static void lcd_move_menu_axis();
  468. /* Menu implementation */
  469. void lcd_preheat_pla()
  470. {
  471. setTargetHotend0(plaPreheatHotendTemp);
  472. setTargetBed(plaPreheatHPBTemp);
  473. fanSpeed = 0;
  474. lcd_return_to_status();
  475. setWatch(); // heater sanity check timer
  476. }
  477. void lcd_preheat_abs()
  478. {
  479. setTargetHotend0(absPreheatHotendTemp);
  480. setTargetBed(absPreheatHPBTemp);
  481. fanSpeed = 0;
  482. lcd_return_to_status();
  483. setWatch(); // heater sanity check timer
  484. }
  485. void lcd_preheat_pp()
  486. {
  487. setTargetHotend0(ppPreheatHotendTemp);
  488. setTargetBed(ppPreheatHPBTemp);
  489. fanSpeed = 0;
  490. lcd_return_to_status();
  491. setWatch(); // heater sanity check timer
  492. }
  493. void lcd_preheat_pet()
  494. {
  495. setTargetHotend0(petPreheatHotendTemp);
  496. setTargetBed(petPreheatHPBTemp);
  497. fanSpeed = 0;
  498. lcd_return_to_status();
  499. setWatch(); // heater sanity check timer
  500. }
  501. void lcd_preheat_hips()
  502. {
  503. setTargetHotend0(hipsPreheatHotendTemp);
  504. setTargetBed(hipsPreheatHPBTemp);
  505. fanSpeed = 0;
  506. lcd_return_to_status();
  507. setWatch(); // heater sanity check timer
  508. }
  509. void lcd_preheat_flex()
  510. {
  511. setTargetHotend0(flexPreheatHotendTemp);
  512. setTargetBed(flexPreheatHPBTemp);
  513. fanSpeed = 0;
  514. lcd_return_to_status();
  515. setWatch(); // heater sanity check timer
  516. }
  517. void lcd_cooldown()
  518. {
  519. setTargetHotend0(0);
  520. setTargetHotend1(0);
  521. setTargetHotend2(0);
  522. setTargetBed(0);
  523. fanSpeed = 0;
  524. lcd_return_to_status();
  525. }
  526. static void lcd_preheat_menu()
  527. {
  528. START_MENU();
  529. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  530. MENU_ITEM(function, PSTR("ABS - " STRINGIFY(ABS_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(ABS_PREHEAT_HPB_TEMP)), lcd_preheat_abs);
  531. MENU_ITEM(function, PSTR("PLA - " STRINGIFY(PLA_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PLA_PREHEAT_HPB_TEMP)), lcd_preheat_pla);
  532. MENU_ITEM(function, PSTR("PET - " STRINGIFY(PET_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PET_PREHEAT_HPB_TEMP)), lcd_preheat_pet);
  533. MENU_ITEM(function, PSTR("HIPS - " STRINGIFY(HIPS_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(HIPS_PREHEAT_HPB_TEMP)), lcd_preheat_hips);
  534. MENU_ITEM(function, PSTR("PP - " STRINGIFY(PP_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PP_PREHEAT_HPB_TEMP)), lcd_preheat_pp);
  535. MENU_ITEM(function, PSTR("FLEX - " STRINGIFY(FLEX_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(FLEX_PREHEAT_HPB_TEMP)), lcd_preheat_flex);
  536. MENU_ITEM(function, MSG_COOLDOWN, lcd_cooldown);
  537. END_MENU();
  538. }
  539. static void lcd_support_menu()
  540. {
  541. START_MENU();
  542. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  543. MENU_ITEM(back, PSTR(MSG_FW_VERSION " - " FW_version), lcd_main_menu);
  544. MENU_ITEM(back, MSG_PRUSA3D, lcd_main_menu);
  545. MENU_ITEM(back, MSG_PRUSA3D_FORUM, lcd_main_menu);
  546. MENU_ITEM(back, MSG_PRUSA3D_HOWTO, lcd_main_menu);
  547. MENU_ITEM(back, PSTR("------------"), lcd_main_menu);
  548. MENU_ITEM(back, PSTR(FILAMENT_SIZE), lcd_main_menu);
  549. MENU_ITEM(back, PSTR(ELECTRONICS),lcd_main_menu);
  550. MENU_ITEM(back, PSTR(NOZZLE_TYPE),lcd_main_menu);
  551. MENU_ITEM(back, PSTR("------------"), lcd_main_menu);
  552. MENU_ITEM(back, PSTR("Date: "), lcd_main_menu);
  553. MENU_ITEM(back, PSTR(__DATE__), lcd_main_menu);
  554. END_MENU();
  555. }
  556. void lcd_unLoadFilament()
  557. {
  558. if (degHotend0() > EXTRUDE_MINTEMP) {
  559. enquecommand_P(PSTR(UNLOAD_FILAMENT_0));
  560. enquecommand_P(PSTR(UNLOAD_FILAMENT_1));
  561. } else {
  562. lcd_implementation_clear();
  563. lcd.setCursor(0, 0);
  564. lcd_printPGM(MSG_ERROR);
  565. lcd.setCursor(0, 2);
  566. lcd_printPGM(MSG_PREHEAT_NOZZLE);
  567. delay(2000);
  568. lcd_implementation_clear();
  569. }
  570. lcd_return_to_status();
  571. }
  572. void lcd_change_filament() {
  573. lcd_implementation_clear();
  574. lcd.setCursor(0, 1);
  575. lcd_printPGM(MSG_CHANGING_FILAMENT);
  576. }
  577. void lcd_wait_interact() {
  578. lcd_implementation_clear();
  579. lcd.setCursor(0, 1);
  580. lcd_printPGM(MSG_INSERT_FILAMENT);
  581. lcd.setCursor(0, 2);
  582. lcd_printPGM(MSG_PRESS);
  583. }
  584. void lcd_change_success() {
  585. lcd_implementation_clear();
  586. lcd.setCursor(0, 2);
  587. lcd_printPGM(MSG_CHANGE_SUCCESS);
  588. }
  589. void lcd_loading_color() {
  590. lcd_implementation_clear();
  591. lcd.setCursor(0, 0);
  592. lcd_printPGM(MSG_LOADING_COLOR);
  593. lcd.setCursor(0, 2);
  594. lcd_printPGM(MSG_PLEASE_WAIT);
  595. for (int i = 0; i < 20; i++) {
  596. lcd.setCursor(i, 3);
  597. lcd.print(".");
  598. for (int j = 0; j < 10 ; j++) {
  599. manage_heater();
  600. manage_inactivity(true);
  601. delay(85);
  602. }
  603. }
  604. }
  605. void lcd_loading_filament() {
  606. lcd_implementation_clear();
  607. lcd.setCursor(0, 0);
  608. lcd_printPGM(MSG_LOADING_FILAMENT);
  609. lcd.setCursor(0, 2);
  610. lcd_printPGM(MSG_PLEASE_WAIT);
  611. for (int i = 0; i < 20; i++) {
  612. lcd.setCursor(i, 3);
  613. lcd.print(".");
  614. for (int j = 0; j < 10 ; j++) {
  615. manage_heater();
  616. manage_inactivity(true);
  617. delay(110);
  618. }
  619. }
  620. }
  621. void lcd_alright() {
  622. int enc_dif = 0;
  623. int cursor_pos = 1;
  624. lcd_implementation_clear();
  625. lcd.setCursor(0, 0);
  626. lcd_printPGM(MSG_CORRECTLY);
  627. lcd.setCursor(1, 1);
  628. lcd_printPGM(MSG_YES);
  629. lcd.setCursor(1, 2);
  630. lcd_printPGM(MSG_NOT_LOADED);
  631. lcd.setCursor(1, 3);
  632. lcd_printPGM(MSG_NOT_COLOR);
  633. lcd.setCursor(0, 1);
  634. lcd.print(">");
  635. enc_dif = encoderDiff;
  636. while (lcd_change_fil_state == 0) {
  637. manage_heater();
  638. manage_inactivity(true);
  639. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  640. if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  641. if (enc_dif > encoderDiff ) {
  642. cursor_pos --;
  643. }
  644. if (enc_dif < encoderDiff ) {
  645. cursor_pos ++;
  646. }
  647. if (cursor_pos > 3) {
  648. cursor_pos = 3;
  649. }
  650. if (cursor_pos < 1) {
  651. cursor_pos = 1;
  652. }
  653. lcd.setCursor(0, 1);
  654. lcd.print(" ");
  655. lcd.setCursor(0, 2);
  656. lcd.print(" ");
  657. lcd.setCursor(0, 3);
  658. lcd.print(" ");
  659. lcd.setCursor(0, cursor_pos);
  660. lcd.print(">");
  661. enc_dif = encoderDiff;
  662. delay(100);
  663. }
  664. }
  665. if (lcd_clicked()) {
  666. lcd_change_fil_state = cursor_pos;
  667. delay(500);
  668. }
  669. };
  670. lcd_implementation_clear();
  671. lcd_return_to_status();
  672. }
  673. void lcd_LoadFilament()
  674. {
  675. if (degHotend0() > EXTRUDE_MINTEMP)
  676. {
  677. custom_message = true;
  678. lcd_commands_type = 1;
  679. SERIAL_ECHOLN("Loading filament");
  680. // commands() will handle the rest
  681. }
  682. else
  683. {
  684. lcd_implementation_clear();
  685. lcd.setCursor(0, 0);
  686. lcd_printPGM(MSG_ERROR);
  687. lcd.setCursor(0, 2);
  688. lcd_printPGM(MSG_PREHEAT_NOZZLE);
  689. delay(2000);
  690. lcd_implementation_clear();
  691. }
  692. lcd_return_to_status();
  693. }
  694. static void lcd_menu_statistics()
  695. {
  696. if (IS_SD_PRINTING)
  697. {
  698. int _met = total_filament_used / 100000;
  699. int _cm = (total_filament_used - (_met * 100000))/10;
  700. int _t = (millis() - starttime) / 1000;
  701. int _h = _t / 3600;
  702. int _m = (_t - (_h * 60)) / 60;
  703. int _s = _t - ((_h * 3600) + (_m * 60));
  704. lcd.setCursor(0, 0);
  705. lcd_printPGM(MSG_STATS_FILAMENTUSED);
  706. lcd.setCursor(6, 1);
  707. lcd.print(itostr3(_met));
  708. lcd.print("m ");
  709. lcd.print(ftostr32ns(_cm));
  710. lcd.print("cm");
  711. lcd.setCursor(0, 2);
  712. lcd_printPGM(MSG_STATS_PRINTTIME);
  713. lcd.setCursor(8, 3);
  714. lcd.print(itostr2(_h));
  715. lcd.print("h ");
  716. lcd.print(itostr2(_m));
  717. lcd.print("m ");
  718. lcd.print(itostr2(_s));
  719. lcd.print("s");
  720. if (lcd_clicked())
  721. {
  722. lcd_quick_feedback();
  723. lcd_return_to_status();
  724. }
  725. }
  726. else
  727. {
  728. unsigned long _filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED);
  729. unsigned long _time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME);
  730. uint8_t _days, _hours, _minutes;
  731. float _filament_m = (float)_filament;
  732. int _filament_km = (_filament >= 100000) ? _filament / 100000 : 0;
  733. if (_filament_km > 0) _filament_m = _filament - (_filament_km * 100000);
  734. _days = _time / 1440;
  735. _hours = (_time - (_days * 1440)) / 60;
  736. _minutes = _time - ((_days * 1440) + (_hours * 60));
  737. lcd_implementation_clear();
  738. lcd.setCursor(0, 0);
  739. lcd_printPGM(MSG_STATS_TOTALFILAMENT);
  740. lcd.setCursor(17 - strlen(ftostr32ns(_filament_m)), 1);
  741. lcd.print(ftostr32ns(_filament_m));
  742. if (_filament_km > 0)
  743. {
  744. lcd.setCursor(17 - strlen(ftostr32ns(_filament_m)) - 3, 1);
  745. lcd.print("km");
  746. lcd.setCursor(17 - strlen(ftostr32ns(_filament_m)) - 8, 1);
  747. lcd.print(itostr4(_filament_km));
  748. }
  749. lcd.setCursor(18, 1);
  750. lcd.print("m");
  751. lcd.setCursor(0, 2);
  752. lcd_printPGM(MSG_STATS_TOTALPRINTTIME);;
  753. lcd.setCursor(18, 3);
  754. lcd.print("m");
  755. lcd.setCursor(14, 3);
  756. lcd.print(itostr3(_minutes));
  757. lcd.setCursor(14, 3);
  758. lcd.print(":");
  759. lcd.setCursor(12, 3);
  760. lcd.print("h");
  761. lcd.setCursor(9, 3);
  762. lcd.print(itostr3(_hours));
  763. lcd.setCursor(9, 3);
  764. lcd.print(":");
  765. lcd.setCursor(7, 3);
  766. lcd.print("d");
  767. lcd.setCursor(4, 3);
  768. lcd.print(itostr3(_days));
  769. while (!lcd_clicked())
  770. {
  771. manage_heater();
  772. manage_inactivity(true);
  773. delay(100);
  774. }
  775. lcd_quick_feedback();
  776. lcd_return_to_status();
  777. }
  778. }
  779. static void _lcd_move(const char *name, int axis, int min, int max) {
  780. if (encoderPosition != 0) {
  781. refresh_cmd_timeout();
  782. current_position[axis] += float((int)encoderPosition) * move_menu_scale;
  783. if (min_software_endstops && current_position[axis] < min) current_position[axis] = min;
  784. if (max_software_endstops && current_position[axis] > max) current_position[axis] = max;
  785. encoderPosition = 0;
  786. 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);
  787. lcdDrawUpdate = 1;
  788. }
  789. if (lcdDrawUpdate) lcd_implementation_drawedit(name, ftostr31(current_position[axis]));
  790. if (LCD_CLICKED) lcd_goto_menu(lcd_move_menu_axis);
  791. }
  792. static void lcd_move_e()
  793. {
  794. if (encoderPosition != 0)
  795. {
  796. current_position[E_AXIS] += float((int)encoderPosition) * move_menu_scale;
  797. encoderPosition = 0;
  798. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[E_AXIS] / 60, active_extruder);
  799. lcdDrawUpdate = 1;
  800. }
  801. if (lcdDrawUpdate)
  802. {
  803. lcd_implementation_drawedit(PSTR("Extruder"), ftostr31(current_position[E_AXIS]));
  804. }
  805. if (LCD_CLICKED) lcd_goto_menu(lcd_move_menu_axis);
  806. }
  807. // Save a single axis babystep value.
  808. void EEPROM_save_B(int pos, int* value)
  809. {
  810. union Data data;
  811. data.value = *value;
  812. eeprom_update_byte((unsigned char*)pos, data.b[0]);
  813. eeprom_update_byte((unsigned char*)pos + 1, data.b[1]);
  814. }
  815. // Read a single axis babystep value.
  816. void EEPROM_read_B(int pos, int* value)
  817. {
  818. union Data data;
  819. data.b[0] = eeprom_read_byte((unsigned char*)pos);
  820. data.b[1] = eeprom_read_byte((unsigned char*)pos + 1);
  821. *value = data.value;
  822. }
  823. static void lcd_move_x() {
  824. _lcd_move(PSTR("X"), X_AXIS, X_MIN_POS, X_MAX_POS);
  825. }
  826. static void lcd_move_y() {
  827. _lcd_move(PSTR("Y"), Y_AXIS, Y_MIN_POS, Y_MAX_POS);
  828. }
  829. static void lcd_move_z() {
  830. _lcd_move(PSTR("Z"), Z_AXIS, Z_MIN_POS, Z_MAX_POS);
  831. }
  832. static void _lcd_babystep(int axis, const char *msg) {
  833. if (encoderPosition != 0)
  834. {
  835. babystepsTodo[axis] += (int)encoderPosition;
  836. babystepMem[axis] += (int)encoderPosition;
  837. babystepMemMM[axis] = babystepMem[axis]/axis_steps_per_unit[Z_AXIS];
  838. delay(50);
  839. encoderPosition = 0;
  840. lcdDrawUpdate = 1;
  841. }
  842. if (lcdDrawUpdate) lcd_implementation_drawedit_2(msg, ftostr13ns(babystepMemMM[axis]));
  843. if (LCD_CLICKED || menuExiting) {
  844. // Only update the EEPROM when leaving the menu.
  845. EEPROM_save_B(
  846. (axis == 0) ? EEPROM_BABYSTEP_X : ((axis == 1) ? EEPROM_BABYSTEP_Y : EEPROM_BABYSTEP_Z),
  847. &babystepMem[axis]);
  848. }
  849. if (LCD_CLICKED) lcd_goto_menu(lcd_main_menu);
  850. }
  851. static void lcd_babystep_x() {
  852. _lcd_babystep(X_AXIS, (MSG_BABYSTEPPING_X));
  853. }
  854. static void lcd_babystep_y() {
  855. _lcd_babystep(Y_AXIS, (MSG_BABYSTEPPING_Y));
  856. }
  857. static void lcd_babystep_z() {
  858. _lcd_babystep(Z_AXIS, (MSG_BABYSTEPPING_Z));
  859. }
  860. void lcd_adjust_z() {
  861. int enc_dif = 0;
  862. int cursor_pos = 1;
  863. int fsm = 0;
  864. lcd_implementation_clear();
  865. lcd.setCursor(0, 0);
  866. lcd_printPGM(MSG_ADJUSTZ);
  867. lcd.setCursor(1, 1);
  868. lcd_printPGM(MSG_YES);
  869. lcd.setCursor(1, 2);
  870. lcd_printPGM(MSG_NO);
  871. lcd.setCursor(0, 1);
  872. lcd.print(">");
  873. enc_dif = encoderDiff;
  874. while (fsm == 0) {
  875. manage_heater();
  876. manage_inactivity(true);
  877. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  878. if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  879. if (enc_dif > encoderDiff ) {
  880. cursor_pos --;
  881. }
  882. if (enc_dif < encoderDiff ) {
  883. cursor_pos ++;
  884. }
  885. if (cursor_pos > 2) {
  886. cursor_pos = 2;
  887. }
  888. if (cursor_pos < 1) {
  889. cursor_pos = 1;
  890. }
  891. lcd.setCursor(0, 1);
  892. lcd.print(" ");
  893. lcd.setCursor(0, 2);
  894. lcd.print(" ");
  895. lcd.setCursor(0, cursor_pos);
  896. lcd.print(">");
  897. enc_dif = encoderDiff;
  898. delay(100);
  899. }
  900. }
  901. if (lcd_clicked()) {
  902. fsm = cursor_pos;
  903. if (fsm == 1) {
  904. EEPROM_read_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  905. EEPROM_read_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  906. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  907. babystepsTodo[Z_AXIS] = babystepMem[2];
  908. } else {
  909. babystepMem[0] = 0;
  910. babystepMem[1] = 0;
  911. babystepMem[2] = 0;
  912. EEPROM_save_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  913. EEPROM_save_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  914. EEPROM_save_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  915. }
  916. delay(500);
  917. }
  918. };
  919. lcd_implementation_clear();
  920. lcd_return_to_status();
  921. }
  922. // Lets the user move the Z carriage up to the end stoppers.
  923. // When done, it sets the current Z to Z_MAX_POS and returns true.
  924. // Otherwise the Z calibration is not changed and false is returned.
  925. bool lcd_calibrate_z_end_stop_manual()
  926. {
  927. const unsigned long max_inactive_time = 60 * 1000; // 60 seconds
  928. unsigned long previous_millis_cmd = millis();
  929. int8_t cursor_pos;
  930. int8_t enc_dif = 0;
  931. // Don't know where we are. Let's claim we are Z=0, so the soft end stops will not be triggered when moving up.
  932. current_position[Z_AXIS] = 0;
  933. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  934. // Until confirmed by the confirmation dialog.
  935. for (;;) {
  936. previous_millis_cmd = millis();
  937. lcd_implementation_clear();
  938. lcd.setCursor(0, 0);
  939. lcd_printPGM(MSG_MOVE_CARRIAGE_TO_THE_TOP_LINE1);
  940. lcd.setCursor(0, 1);
  941. lcd_printPGM(MSG_MOVE_CARRIAGE_TO_THE_TOP_LINE2);
  942. lcd.setCursor(0, 2);
  943. lcd_printPGM(MSG_MOVE_CARRIAGE_TO_THE_TOP_LINE3);
  944. lcd.setCursor(0, 3);
  945. lcd_printPGM(MSG_MOVE_CARRIAGE_TO_THE_TOP_LINE4);
  946. // Until the user finishes the z up movement.
  947. encoderDiff = 0;
  948. encoderPosition = 0;
  949. for (;;) {
  950. if (millis() - previous_millis_cmd > max_inactive_time)
  951. goto canceled;
  952. manage_heater();
  953. manage_inactivity(true);
  954. if (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP) {
  955. delay(50);
  956. previous_millis_cmd = millis();
  957. encoderPosition += abs(encoderDiff / ENCODER_PULSES_PER_STEP);
  958. encoderDiff = 0;
  959. // Only move up, whatever the user does.
  960. current_position[Z_AXIS] += fabs(encoderPosition);
  961. encoderPosition = 0;
  962. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[Z_AXIS] / 60, active_extruder);
  963. // Wait for the motors to stop.
  964. st_synchronize();
  965. // Claim we are at Z=0, so the soft end stop will not trigger.
  966. current_position[Z_AXIS] = 0;
  967. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  968. }
  969. if (lcd_clicked()) {
  970. // Wait until the Z up movement is finished.
  971. st_synchronize();
  972. while (lcd_clicked()) ;
  973. delay(10);
  974. while (lcd_clicked()) ;
  975. break;
  976. }
  977. }
  978. // Let the user confirm, that the Z carriage is at the top end stoppers.
  979. lcd_implementation_clear();
  980. lcd.setCursor(0, 0);
  981. lcd_printPGM(MSG_CONFIRM_CARRIAGE_AT_THE_TOP_LINE1);
  982. lcd.setCursor(0, 1);
  983. lcd_printPGM(MSG_CONFIRM_CARRIAGE_AT_THE_TOP_LINE2);
  984. lcd.setCursor(1, 2);
  985. lcd_printPGM(MSG_YES);
  986. lcd.setCursor(1, 3);
  987. lcd_printPGM(MSG_NO);
  988. cursor_pos = 3;
  989. lcd.setCursor(0, cursor_pos);
  990. lcd_printPGM(PSTR(">"));
  991. previous_millis_cmd = millis();
  992. enc_dif = encoderDiff;
  993. for (;;) {
  994. if (millis() - previous_millis_cmd > max_inactive_time)
  995. goto canceled;
  996. manage_heater();
  997. manage_inactivity(true);
  998. if (abs((enc_dif - encoderDiff)) > 4) {
  999. if (abs(enc_dif - encoderDiff) > 1) {
  1000. lcd.setCursor(0, 2);
  1001. if (enc_dif > encoderDiff && cursor_pos == 4) {
  1002. lcd_printPGM((PSTR(" ")));
  1003. lcd.setCursor(0, 3);
  1004. lcd_printPGM((PSTR(">")));
  1005. -- cursor_pos;
  1006. } else if (enc_dif < encoderDiff && cursor_pos == 3) {
  1007. ++ cursor_pos;
  1008. lcd_printPGM((PSTR(">")));
  1009. lcd.setCursor(0, 3);
  1010. lcd_printPGM((PSTR(" ")));
  1011. }
  1012. enc_dif = encoderDiff;
  1013. }
  1014. }
  1015. if (lcd_clicked()) {
  1016. while (lcd_clicked()) ;
  1017. delay(10);
  1018. while (lcd_clicked()) ;
  1019. if (cursor_pos == 3) {
  1020. // Perform another round of the Z up dialog.
  1021. break;
  1022. }
  1023. goto calibrated;
  1024. }
  1025. }
  1026. }
  1027. calibrated:
  1028. current_position[Z_AXIS] = Z_MAX_POS;
  1029. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1030. return true;
  1031. canceled:
  1032. return false;
  1033. }
  1034. static inline bool pgm_is_whitespace(const char *c)
  1035. {
  1036. return pgm_read_byte(c) == ' ' || pgm_read_byte(c) == '\t' || pgm_read_byte(c) == '\r' || pgm_read_byte(c) == '\n';
  1037. }
  1038. void lcd_display_message_fullscreen_P(const char *msg)
  1039. {
  1040. // Disable update of the screen by the usual lcd_update() routine.
  1041. lcd_update_enable(false);
  1042. lcd_implementation_clear();
  1043. lcd.setCursor(0, 0);
  1044. for (int8_t row = 0; row < 4; ++ row) {
  1045. while (pgm_is_whitespace(msg))
  1046. ++ msg;
  1047. if (pgm_read_byte(msg) == 0)
  1048. // End of the message.
  1049. break;
  1050. lcd.setCursor(0, row);
  1051. const char *msgend2 = msg + min(strlen_P(msg), 20);
  1052. const char *msgend = msgend2;
  1053. if (pgm_read_byte(msgend) != 0 && ! pgm_is_whitespace(msgend)) {
  1054. // Splitting a word. Find the start of the current word.
  1055. while (msgend > msg && ! pgm_is_whitespace(msgend - 1))
  1056. -- msgend;
  1057. if (msgend == msg)
  1058. // Found a single long word, which cannot be split. Just cut it.
  1059. msgend = msgend2;
  1060. }
  1061. for (; msg < msgend; ++ msg) {
  1062. char c = char(pgm_read_byte(msg));
  1063. if (c == '~')
  1064. c = ' ';
  1065. lcd.print(c);
  1066. }
  1067. }
  1068. }
  1069. void lcd_bed_calibration_show_result(BedSkewOffsetDetectionResultType result)
  1070. {
  1071. const char *msg = NULL;
  1072. switch (result) {
  1073. case BED_SKEW_OFFSET_DETECTION_FAILED:
  1074. default:
  1075. msg = MSG_BED_SKEW_OFFSET_DETECTION_FAILED;
  1076. break;
  1077. case BED_SKEW_OFFSET_DETECTION_PERFECT:
  1078. msg = MSG_BED_SKEW_OFFSET_DETECTION_PERFECT;
  1079. break;
  1080. case BED_SKEW_OFFSET_DETECTION_SKEW_MILD:
  1081. msg = MSG_BED_SKEW_OFFSET_DETECTION_SKEW_MILD;
  1082. break;
  1083. case BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME:
  1084. msg = MSG_BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME;
  1085. break;
  1086. case BED_SKEW_OFFSET_DETECTION_FRONT_LEFT_FAR:
  1087. msg = MSG_BED_SKEW_OFFSET_DETECTION_FRONT_LEFT_FAR;
  1088. break;
  1089. case BED_SKEW_OFFSET_DETECTION_FRONT_RIGHT_FAR:
  1090. msg = MSG_BED_SKEW_OFFSET_DETECTION_FRONT_RIGHT_FAR;
  1091. break;
  1092. }
  1093. lcd_display_message_fullscreen_P(msg);
  1094. // Until confirmed by a button click.
  1095. for (;;) {
  1096. delay_keep_alive(50);
  1097. if (lcd_clicked()) {
  1098. while (lcd_clicked()) ;
  1099. delay(10);
  1100. while (lcd_clicked()) ;
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. static void lcd_show_end_stops() {
  1106. lcd.setCursor(0, 0);
  1107. lcd_printPGM((PSTR("End stops diag")));
  1108. lcd.setCursor(0, 1);
  1109. lcd_printPGM((READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? (PSTR("X1")) : (PSTR("X0")));
  1110. lcd.setCursor(0, 2);
  1111. lcd_printPGM((READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? (PSTR("Y1")) : (PSTR("Y0")));
  1112. lcd.setCursor(0, 3);
  1113. lcd_printPGM((READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? (PSTR("Z1")) : (PSTR("Z0")));
  1114. }
  1115. static void menu_show_end_stops() {
  1116. lcd_show_end_stops();
  1117. if (LCD_CLICKED) lcd_goto_menu(lcd_settings_menu);
  1118. }
  1119. // Lets the user move the Z carriage up to the end stoppers.
  1120. // When done, it sets the current Z to Z_MAX_POS and returns true.
  1121. // Otherwise the Z calibration is not changed and false is returned.
  1122. void lcd_diag_show_end_stops()
  1123. {
  1124. int enc_dif = encoderDiff;
  1125. lcd_implementation_clear();
  1126. for (;;) {
  1127. manage_heater();
  1128. manage_inactivity(true);
  1129. lcd_show_end_stops();
  1130. if (lcd_clicked()) {
  1131. while (lcd_clicked()) ;
  1132. delay(10);
  1133. while (lcd_clicked()) ;
  1134. break;
  1135. }
  1136. }
  1137. lcd_implementation_clear();
  1138. lcd_return_to_status();
  1139. }
  1140. void lcd_pick_babystep(){
  1141. int enc_dif = 0;
  1142. int cursor_pos = 1;
  1143. int fsm = 0;
  1144. lcd_implementation_clear();
  1145. lcd.setCursor(0, 0);
  1146. lcd_printPGM(MSG_PICK_Z);
  1147. lcd.setCursor(3, 2);
  1148. lcd.print("1");
  1149. lcd.setCursor(3, 3);
  1150. lcd.print("2");
  1151. lcd.setCursor(12, 2);
  1152. lcd.print("3");
  1153. lcd.setCursor(12, 3);
  1154. lcd.print("4");
  1155. lcd.setCursor(1, 2);
  1156. lcd.print(">");
  1157. enc_dif = encoderDiff;
  1158. while (fsm == 0) {
  1159. manage_heater();
  1160. manage_inactivity(true);
  1161. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  1162. if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  1163. if (enc_dif > encoderDiff ) {
  1164. cursor_pos --;
  1165. }
  1166. if (enc_dif < encoderDiff ) {
  1167. cursor_pos ++;
  1168. }
  1169. if (cursor_pos > 4) {
  1170. cursor_pos = 4;
  1171. }
  1172. if (cursor_pos < 1) {
  1173. cursor_pos = 1;
  1174. }
  1175. lcd.setCursor(1, 2);
  1176. lcd.print(" ");
  1177. lcd.setCursor(1, 3);
  1178. lcd.print(" ");
  1179. lcd.setCursor(10, 2);
  1180. lcd.print(" ");
  1181. lcd.setCursor(10, 3);
  1182. lcd.print(" ");
  1183. if (cursor_pos < 3) {
  1184. lcd.setCursor(1, cursor_pos+1);
  1185. lcd.print(">");
  1186. }else{
  1187. lcd.setCursor(10, cursor_pos-1);
  1188. lcd.print(">");
  1189. }
  1190. enc_dif = encoderDiff;
  1191. delay(100);
  1192. }
  1193. }
  1194. if (lcd_clicked()) {
  1195. fsm = cursor_pos;
  1196. EEPROM_read_B(EEPROM_BABYSTEP_Z0+((fsm-1)*2),&babystepMem[2]);
  1197. EEPROM_save_B(EEPROM_BABYSTEP_Z,&babystepMem[2]);
  1198. eeprom_write_byte((unsigned char*)EEPROM_BABYSTEP_Z_SET, 0x01);
  1199. delay(500);
  1200. }
  1201. };
  1202. lcd_implementation_clear();
  1203. lcd_return_to_status();
  1204. }
  1205. void lcd_move_menu_axis()
  1206. {
  1207. START_MENU();
  1208. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  1209. MENU_ITEM(submenu, MSG_MOVE_X, lcd_move_x);
  1210. MENU_ITEM(submenu, MSG_MOVE_Y, lcd_move_y);
  1211. if (move_menu_scale < 10.0)
  1212. {
  1213. if (!isPrintPaused)
  1214. {
  1215. MENU_ITEM(submenu, MSG_MOVE_Z, lcd_move_z);
  1216. }
  1217. MENU_ITEM(submenu, MSG_MOVE_E, lcd_move_e);
  1218. }
  1219. END_MENU();
  1220. }
  1221. static void lcd_move_menu_1mm()
  1222. {
  1223. move_menu_scale = 1.0;
  1224. lcd_move_menu_axis();
  1225. }
  1226. void EEPROM_save(int pos, uint8_t* value, uint8_t size)
  1227. {
  1228. do
  1229. {
  1230. eeprom_write_byte((unsigned char*)pos, *value);
  1231. pos++;
  1232. value++;
  1233. } while (--size);
  1234. }
  1235. void EEPROM_read(int pos, uint8_t* value, uint8_t size)
  1236. {
  1237. do
  1238. {
  1239. *value = eeprom_read_byte((unsigned char*)pos);
  1240. pos++;
  1241. value++;
  1242. } while (--size);
  1243. }
  1244. static void lcd_silent_mode_set() {
  1245. SilentModeMenu = !SilentModeMenu;
  1246. EEPROM_save(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1247. digipot_init();
  1248. lcd_goto_menu(lcd_settings_menu, 7);
  1249. }
  1250. static void lcd_set_lang(unsigned char lang) {
  1251. lang_selected = lang;
  1252. firstrun = 1;
  1253. eeprom_write_byte((unsigned char *)EEPROM_LANG, lang);/*langsel=0;*/if (langsel == 1)langsel = 2;
  1254. }
  1255. void lcd_force_language_selection() {
  1256. eeprom_write_byte((unsigned char *)EEPROM_LANG, 255);
  1257. }
  1258. static void lcd_language_menu()
  1259. {
  1260. START_MENU();
  1261. if (!langsel) {
  1262. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  1263. }
  1264. if (langsel == 2) {
  1265. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  1266. }
  1267. for (int i=0;i<LANG_NUM;i++){
  1268. MENU_ITEM(setlang, MSG_LANGUAGE_NAME_EXPLICIT(i), i);
  1269. }
  1270. //MENU_ITEM(setlang, MSG_LANGUAGE_NAME_EXPLICIT(1), 1);
  1271. END_MENU();
  1272. }
  1273. void lcd_mesh_bedleveling()
  1274. {
  1275. enquecommand_P(PSTR("G80"));
  1276. lcd_return_to_status();
  1277. }
  1278. void lcd_mesh_calibration()
  1279. {
  1280. enquecommand_P(PSTR("M45"));
  1281. lcd_return_to_status();
  1282. }
  1283. static void lcd_settings_menu()
  1284. {
  1285. EEPROM_read(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1286. START_MENU();
  1287. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  1288. MENU_ITEM(submenu, MSG_TEMPERATURE, lcd_control_temperature_menu);
  1289. MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu_1mm);
  1290. if (!isPrintPaused)
  1291. {
  1292. #ifndef MESH_BED_LEVELING
  1293. MENU_ITEM(gcode, MSG_HOMEYZ, PSTR("G28 Z"));
  1294. #else
  1295. MENU_ITEM(submenu, MSG_HOMEYZ, lcd_mesh_bedleveling);
  1296. #endif
  1297. }
  1298. if (!isPrintPaused)
  1299. {
  1300. MENU_ITEM(gcode, MSG_DISABLE_STEPPERS, PSTR("M84"));
  1301. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  1302. }
  1303. if (SilentModeMenu == 0) {
  1304. MENU_ITEM(function, MSG_SILENT_MODE_OFF, lcd_silent_mode_set);
  1305. } else {
  1306. MENU_ITEM(function, MSG_SILENT_MODE_ON, lcd_silent_mode_set);
  1307. }
  1308. EEPROM_read_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  1309. EEPROM_read_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  1310. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  1311. babystepMemMM[2] = babystepMem[2]/axis_steps_per_unit[Z_AXIS];
  1312. if (!isPrintPaused)
  1313. {
  1314. MENU_ITEM(submenu, MSG_BABYSTEP_Z, lcd_babystep_z);//8
  1315. }
  1316. MENU_ITEM(submenu, MSG_LANGUAGE_SELECT, lcd_language_menu);
  1317. if (!isPrintPaused)
  1318. {
  1319. MENU_ITEM(submenu, MSG_SELFTEST, lcd_selftest);
  1320. MENU_ITEM(submenu, MSG_SHOW_END_STOPS, menu_show_end_stops);
  1321. MENU_ITEM(submenu, MSG_CALIBRATE_BED, lcd_mesh_calibration);
  1322. MENU_ITEM(gcode, MSG_CALIBRATE_BED_RESET, PSTR("M44"));
  1323. }
  1324. END_MENU();
  1325. }
  1326. /*
  1327. void lcd_mylang_top(int hlaska) {
  1328. lcd.setCursor(0,0);
  1329. lcd.print(" ");
  1330. lcd.setCursor(0,0);
  1331. lcd_printPGM(MSG_ALL[hlaska-1][LANGUAGE_SELECT]);
  1332. }
  1333. void lcd_mylang_drawmenu(int cursor) {
  1334. int first = 0;
  1335. if (cursor>2) first = cursor-2;
  1336. if (cursor==LANG_NUM) first = LANG_NUM-3;
  1337. lcd.setCursor(0, 1);
  1338. lcd.print(" ");
  1339. lcd.setCursor(1, 1);
  1340. lcd_printPGM(MSG_ALL[first][LANGUAGE_NAME]);
  1341. lcd.setCursor(0, 2);
  1342. lcd.print(" ");
  1343. lcd.setCursor(1, 2);
  1344. lcd_printPGM(MSG_ALL[first+1][LANGUAGE_NAME]);
  1345. lcd.setCursor(0, 3);
  1346. lcd.print(" ");
  1347. lcd.setCursor(1, 3);
  1348. lcd_printPGM(MSG_ALL[first+2][LANGUAGE_NAME]);
  1349. if (cursor==1) lcd.setCursor(0, 1);
  1350. if (cursor>1 && cursor<LANG_NUM) lcd.setCursor(0, 2);
  1351. if (cursor==LANG_NUM) lcd.setCursor(0, 3);
  1352. lcd.print(">");
  1353. if (cursor<LANG_NUM-1) {
  1354. lcd.setCursor(19,3);
  1355. lcd.print("\x01");
  1356. }
  1357. if (cursor>2) {
  1358. lcd.setCursor(19,1);
  1359. lcd.print("^");
  1360. }
  1361. }
  1362. */
  1363. void lcd_mylang_drawmenu(int cursor) {
  1364. int first = 0;
  1365. if (cursor>3) first = cursor-3;
  1366. if (cursor==LANG_NUM && LANG_NUM>4) first = LANG_NUM-4;
  1367. if (cursor==LANG_NUM && LANG_NUM==4) first = LANG_NUM-4;
  1368. lcd.setCursor(0, 0);
  1369. lcd.print(" ");
  1370. lcd.setCursor(1, 0);
  1371. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+0));
  1372. lcd.setCursor(0, 1);
  1373. lcd.print(" ");
  1374. lcd.setCursor(1, 1);
  1375. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+1));
  1376. lcd.setCursor(0, 2);
  1377. lcd.print(" ");
  1378. if (LANG_NUM > 2){
  1379. lcd.setCursor(1, 2);
  1380. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+2));
  1381. }
  1382. lcd.setCursor(0, 3);
  1383. lcd.print(" ");
  1384. if (LANG_NUM>3) {
  1385. lcd.setCursor(1, 3);
  1386. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+3));
  1387. }
  1388. if (cursor==1) lcd.setCursor(0, 0);
  1389. if (cursor==2) lcd.setCursor(0, 1);
  1390. if (cursor>2) lcd.setCursor(0, 2);
  1391. if (cursor==LANG_NUM && LANG_NUM>3) lcd.setCursor(0, 3);
  1392. lcd.print(">");
  1393. if (cursor<LANG_NUM-1 && LANG_NUM>4) {
  1394. lcd.setCursor(19,3);
  1395. lcd.print("\x01");
  1396. }
  1397. if (cursor>3 && LANG_NUM>4) {
  1398. lcd.setCursor(19,0);
  1399. lcd.print("^");
  1400. }
  1401. }
  1402. void lcd_set_custom_characters_arrows();
  1403. void lcd_set_custom_characters_degree();
  1404. void lcd_mylang_drawcursor(int cursor) {
  1405. if (cursor==1) lcd.setCursor(0, 1);
  1406. if (cursor>1 && cursor<LANG_NUM) lcd.setCursor(0, 2);
  1407. if (cursor==LANG_NUM) lcd.setCursor(0, 3);
  1408. lcd.print(">");
  1409. }
  1410. void lcd_mylang() {
  1411. int enc_dif = 0;
  1412. int cursor_pos = 1;
  1413. lang_selected=255;
  1414. int hlaska=1;
  1415. int counter=0;
  1416. lcd_set_custom_characters_arrows();
  1417. lcd_implementation_clear();
  1418. //lcd_mylang_top(hlaska);
  1419. lcd_mylang_drawmenu(cursor_pos);
  1420. enc_dif = encoderDiff;
  1421. while ( (lang_selected == 255) && (MYSERIAL.available() < 2) ) {
  1422. manage_heater();
  1423. manage_inactivity(true);
  1424. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  1425. //if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  1426. if (enc_dif > encoderDiff ) {
  1427. cursor_pos --;
  1428. }
  1429. if (enc_dif < encoderDiff ) {
  1430. cursor_pos ++;
  1431. }
  1432. if (cursor_pos > LANG_NUM) {
  1433. cursor_pos = LANG_NUM;
  1434. }
  1435. if (cursor_pos < 1) {
  1436. cursor_pos = 1;
  1437. }
  1438. lcd_mylang_drawmenu(cursor_pos);
  1439. enc_dif = encoderDiff;
  1440. delay(100);
  1441. //}
  1442. } else delay(20);
  1443. if (lcd_clicked()) {
  1444. lcd_set_lang(cursor_pos-1);
  1445. delay(500);
  1446. }
  1447. /*
  1448. if (++counter == 80) {
  1449. hlaska++;
  1450. if(hlaska>LANG_NUM) hlaska=1;
  1451. lcd_mylang_top(hlaska);
  1452. lcd_mylang_drawcursor(cursor_pos);
  1453. counter=0;
  1454. }
  1455. */
  1456. };
  1457. if(MYSERIAL.available() > 1){
  1458. lang_selected = 0;
  1459. firstrun = 0;
  1460. }
  1461. lcd_set_custom_characters_degree();
  1462. lcd_implementation_clear();
  1463. lcd_return_to_status();
  1464. }
  1465. static void lcd_main_menu()
  1466. {
  1467. SDscrool = 0;
  1468. /*
  1469. if (langsel == 1)
  1470. {
  1471. lcd_goto_menu(lcd_language_menu);
  1472. }
  1473. */
  1474. START_MENU();
  1475. // Majkl superawesome menu
  1476. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  1477. if ( ( IS_SD_PRINTING || is_usb_printing ) && (current_position[Z_AXIS] < 0.5) )
  1478. {
  1479. EEPROM_read_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  1480. EEPROM_read_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  1481. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  1482. MENU_ITEM(submenu, MSG_BABYSTEP_Z, lcd_babystep_z);//8
  1483. }
  1484. if ( movesplanned() || IS_SD_PRINTING || is_usb_printing )
  1485. {
  1486. MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
  1487. } else
  1488. {
  1489. MENU_ITEM(submenu, MSG_PREHEAT, lcd_preheat_menu);
  1490. }
  1491. #ifdef SDSUPPORT
  1492. if (card.cardOK)
  1493. {
  1494. if (card.isFileOpen())
  1495. {
  1496. if (card.sdprinting)
  1497. {
  1498. MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
  1499. }
  1500. else
  1501. {
  1502. MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_resume);
  1503. }
  1504. MENU_ITEM(submenu, MSG_STOP_PRINT, lcd_sdcard_stop);
  1505. }
  1506. else
  1507. {
  1508. if (!is_usb_printing)
  1509. {
  1510. MENU_ITEM(submenu, MSG_CARD_MENU, lcd_sdcard_menu);
  1511. }
  1512. #if SDCARDDETECT < 1
  1513. MENU_ITEM(gcode, MSG_CNG_SDCARD, PSTR("M21")); // SD-card changed by user
  1514. #endif
  1515. }
  1516. } else
  1517. {
  1518. MENU_ITEM(submenu, MSG_NO_CARD, lcd_sdcard_menu);
  1519. #if SDCARDDETECT < 1
  1520. MENU_ITEM(gcode, MSG_INIT_SDCARD, PSTR("M21")); // Manually initialize the SD-card via user interface
  1521. #endif
  1522. }
  1523. #endif
  1524. if (IS_SD_PRINTING || is_usb_printing)
  1525. {
  1526. }
  1527. else
  1528. {
  1529. MENU_ITEM(function, MSG_LOAD_FILAMENT, lcd_LoadFilament);
  1530. MENU_ITEM(function, MSG_UNLOAD_FILAMENT, lcd_unLoadFilament);
  1531. MENU_ITEM(submenu, MSG_SETTINGS, lcd_settings_menu);
  1532. }
  1533. if (!is_usb_printing)
  1534. {
  1535. MENU_ITEM(submenu, MSG_STATISTICS, lcd_menu_statistics);
  1536. }
  1537. MENU_ITEM(submenu, MSG_SUPPORT, lcd_support_menu);
  1538. END_MENU();
  1539. }
  1540. #ifdef SDSUPPORT
  1541. static void lcd_autostart_sd()
  1542. {
  1543. card.lastnr = 0;
  1544. card.setroot();
  1545. card.checkautostart(true);
  1546. }
  1547. #endif
  1548. static void lcd_silent_mode_set_tune() {
  1549. SilentModeMenu = !SilentModeMenu;
  1550. EEPROM_save(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1551. digipot_init();
  1552. lcd_goto_menu(lcd_tune_menu, 9);
  1553. }
  1554. static void lcd_tune_menu()
  1555. {
  1556. EEPROM_read(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1557. START_MENU();
  1558. MENU_ITEM(back, MSG_MAIN, lcd_main_menu); //1
  1559. MENU_ITEM_EDIT(int3, MSG_SPEED, &feedmultiply, 10, 999);//2
  1560. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 10);//3
  1561. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 10);//4
  1562. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);//5
  1563. MENU_ITEM_EDIT(int3, MSG_FLOW, &extrudemultiply, 10, 999);//6
  1564. #ifdef FILAMENTCHANGEENABLE
  1565. MENU_ITEM(gcode, MSG_FILAMENTCHANGE, PSTR("M600"));//7
  1566. #endif
  1567. if (SilentModeMenu == 0) {
  1568. MENU_ITEM(function, MSG_SILENT_MODE_OFF, lcd_silent_mode_set_tune);
  1569. } else {
  1570. MENU_ITEM(function, MSG_SILENT_MODE_ON, lcd_silent_mode_set_tune);
  1571. }
  1572. END_MENU();
  1573. }
  1574. static void lcd_move_menu_01mm()
  1575. {
  1576. move_menu_scale = 0.1;
  1577. lcd_move_menu_axis();
  1578. }
  1579. static void lcd_control_temperature_menu()
  1580. {
  1581. #ifdef PIDTEMP
  1582. // set up temp variables - undo the default scaling
  1583. raw_Ki = unscalePID_i(Ki);
  1584. raw_Kd = unscalePID_d(Kd);
  1585. #endif
  1586. START_MENU();
  1587. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  1588. //MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  1589. #if TEMP_SENSOR_0 != 0
  1590. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 10);
  1591. #endif
  1592. #if TEMP_SENSOR_1 != 0
  1593. MENU_ITEM_EDIT(int3, MSG_NOZZLE1, &target_temperature[1], 0, HEATER_1_MAXTEMP - 10);
  1594. #endif
  1595. #if TEMP_SENSOR_2 != 0
  1596. MENU_ITEM_EDIT(int3, MSG_NOZZLE2, &target_temperature[2], 0, HEATER_2_MAXTEMP - 10);
  1597. #endif
  1598. #if TEMP_SENSOR_BED != 0
  1599. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 3);
  1600. #endif
  1601. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);
  1602. #if defined AUTOTEMP && (TEMP_SENSOR_0 != 0)
  1603. MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &autotemp_enabled);
  1604. MENU_ITEM_EDIT(float3, MSG_MIN, &autotemp_min, 0, HEATER_0_MAXTEMP - 10);
  1605. MENU_ITEM_EDIT(float3, MSG_MAX, &autotemp_max, 0, HEATER_0_MAXTEMP - 10);
  1606. MENU_ITEM_EDIT(float32, MSG_FACTOR, &autotemp_factor, 0.0, 1.0);
  1607. #endif
  1608. END_MENU();
  1609. }
  1610. #if SDCARDDETECT == -1
  1611. static void lcd_sd_refresh()
  1612. {
  1613. card.initsd();
  1614. currentMenuViewOffset = 0;
  1615. }
  1616. #endif
  1617. static void lcd_sd_updir()
  1618. {
  1619. SDscrool = 0;
  1620. card.updir();
  1621. currentMenuViewOffset = 0;
  1622. }
  1623. void lcd_sdcard_stop()
  1624. {
  1625. lcd.setCursor(0, 0);
  1626. lcd_printPGM(MSG_STOP_PRINT);
  1627. lcd.setCursor(2, 2);
  1628. lcd_printPGM(MSG_NO);
  1629. lcd.setCursor(2, 3);
  1630. lcd_printPGM(MSG_YES);
  1631. lcd.setCursor(0, 2); lcd.print(" ");
  1632. lcd.setCursor(0, 3); lcd.print(" ");
  1633. if ((int32_t)encoderPosition > 2) { encoderPosition = 2; }
  1634. if ((int32_t)encoderPosition < 1) { encoderPosition = 1; }
  1635. lcd.setCursor(0, 1 + encoderPosition);
  1636. lcd.print(">");
  1637. if (lcd_clicked())
  1638. {
  1639. if ((int32_t)encoderPosition == 1)
  1640. {
  1641. lcd_return_to_status();
  1642. }
  1643. if ((int32_t)encoderPosition == 2)
  1644. {
  1645. cancel_heatup = true;
  1646. quickStop();
  1647. lcd_setstatuspgm(MSG_PRINT_ABORTED);
  1648. card.sdprinting = false;
  1649. card.closefile();
  1650. stoptime = millis();
  1651. unsigned long t = (stoptime - starttime) / 1000;
  1652. save_statistics(total_filament_used, t);
  1653. lcd_return_to_status();
  1654. lcd_ignore_click(true);
  1655. lcd_commands_type = 2;
  1656. }
  1657. }
  1658. }
  1659. void lcd_sdcard_menu()
  1660. {
  1661. int tempScrool = 0;
  1662. if (lcdDrawUpdate == 0 && LCD_CLICKED == 0)
  1663. //delay(100);
  1664. return; // nothing to do (so don't thrash the SD card)
  1665. uint16_t fileCnt = card.getnrfilenames();
  1666. START_MENU();
  1667. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  1668. card.getWorkDirName();
  1669. if (card.filename[0] == '/')
  1670. {
  1671. #if SDCARDDETECT == -1
  1672. MENU_ITEM(function, MSG_REFRESH, lcd_sd_refresh);
  1673. #endif
  1674. } else {
  1675. MENU_ITEM(function, PSTR(LCD_STR_FOLDER ".."), lcd_sd_updir);
  1676. }
  1677. for (uint16_t i = 0; i < fileCnt; i++)
  1678. {
  1679. if (_menuItemNr == _lineNr)
  1680. {
  1681. #ifndef SDCARD_RATHERRECENTFIRST
  1682. card.getfilename(i);
  1683. #else
  1684. card.getfilename(fileCnt - 1 - i);
  1685. #endif
  1686. if (card.filenameIsDir)
  1687. {
  1688. MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
  1689. } else {
  1690. MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
  1691. }
  1692. } else {
  1693. MENU_ITEM_DUMMY();
  1694. }
  1695. }
  1696. END_MENU();
  1697. }
  1698. #define menu_edit_type(_type, _name, _strFunc, scale) \
  1699. void menu_edit_ ## _name () \
  1700. { \
  1701. if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
  1702. if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
  1703. if (lcdDrawUpdate) \
  1704. lcd_implementation_drawedit(editLabel, _strFunc(((_type)((int32_t)encoderPosition + minEditValue)) / scale)); \
  1705. if (LCD_CLICKED) \
  1706. { \
  1707. *((_type*)editValue) = ((_type)((int32_t)encoderPosition + minEditValue)) / scale; \
  1708. lcd_goto_menu(prevMenu, prevEncoderPosition); \
  1709. } \
  1710. } \
  1711. void menu_edit_callback_ ## _name () { \
  1712. menu_edit_ ## _name (); \
  1713. if (LCD_CLICKED) (*callbackFunc)(); \
  1714. } \
  1715. static void menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) \
  1716. { \
  1717. prevMenu = currentMenu; \
  1718. prevEncoderPosition = encoderPosition; \
  1719. \
  1720. lcdDrawUpdate = 2; \
  1721. currentMenu = menu_edit_ ## _name; \
  1722. \
  1723. editLabel = pstr; \
  1724. editValue = ptr; \
  1725. minEditValue = minValue * scale; \
  1726. maxEditValue = maxValue * scale - minEditValue; \
  1727. encoderPosition = (*ptr) * scale - minEditValue; \
  1728. }\
  1729. static void menu_action_setting_edit_callback_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue, menuFunc_t callback) \
  1730. { \
  1731. prevMenu = currentMenu; \
  1732. prevEncoderPosition = encoderPosition; \
  1733. \
  1734. lcdDrawUpdate = 2; \
  1735. currentMenu = menu_edit_callback_ ## _name; \
  1736. \
  1737. editLabel = pstr; \
  1738. editValue = ptr; \
  1739. minEditValue = minValue * scale; \
  1740. maxEditValue = maxValue * scale - minEditValue; \
  1741. encoderPosition = (*ptr) * scale - minEditValue; \
  1742. callbackFunc = callback;\
  1743. }
  1744. menu_edit_type(int, int3, itostr3, 1)
  1745. menu_edit_type(float, float3, ftostr3, 1)
  1746. menu_edit_type(float, float32, ftostr32, 100)
  1747. menu_edit_type(float, float43, ftostr43, 1000)
  1748. menu_edit_type(float, float5, ftostr5, 0.01)
  1749. menu_edit_type(float, float51, ftostr51, 10)
  1750. menu_edit_type(float, float52, ftostr52, 100)
  1751. menu_edit_type(unsigned long, long5, ftostr5, 0.01)
  1752. static void lcd_selftest()
  1753. {
  1754. int _progress = 0;
  1755. bool _result = false;
  1756. _progress = lcd_selftest_screen(-1, _progress, 4, true, 2000);
  1757. _progress = lcd_selftest_screen(0, _progress, 3, true, 2000);
  1758. _result = lcd_selfcheck_endstops();
  1759. if (_result)
  1760. {
  1761. _progress = lcd_selftest_screen(1, _progress, 3, true, 1000);
  1762. _result = lcd_selfcheck_check_heater(false);
  1763. }
  1764. if (_result)
  1765. {
  1766. _progress = lcd_selftest_screen(2, _progress, 3, true, 2000);
  1767. _result = lcd_selfcheck_axis(0, X_MAX_POS);
  1768. }
  1769. if (_result)
  1770. {
  1771. _progress = lcd_selftest_screen(3, _progress, 3, true, 1500);
  1772. _result = lcd_selfcheck_axis(1, Y_MAX_POS);
  1773. }
  1774. if (_result)
  1775. {
  1776. current_position[X_AXIS] = current_position[X_AXIS] - 3;
  1777. current_position[Y_AXIS] = current_position[Y_AXIS] - 14;
  1778. _progress = lcd_selftest_screen(4, _progress, 3, true, 1500);
  1779. _result = lcd_selfcheck_axis(2, Z_MAX_POS);
  1780. }
  1781. if (_result)
  1782. {
  1783. _progress = lcd_selftest_screen(5, _progress, 3, true, 2000);
  1784. _result = lcd_selfcheck_check_heater(true);
  1785. }
  1786. if (_result)
  1787. {
  1788. _progress = lcd_selftest_screen(6, _progress, 3, true, 5000);
  1789. }
  1790. else
  1791. {
  1792. _progress = lcd_selftest_screen(7, _progress, 3, true, 5000);
  1793. }
  1794. lcd_implementation_clear();
  1795. lcd_next_update_millis = millis() + LCD_UPDATE_INTERVAL;
  1796. if (_result)
  1797. {
  1798. LCD_ALERTMESSAGERPGM(MSG_SELFTEST_OK);
  1799. }
  1800. else
  1801. {
  1802. LCD_ALERTMESSAGERPGM(MSG_SELFTEST_FAILED);
  1803. }
  1804. }
  1805. static bool lcd_selfcheck_endstops()
  1806. {
  1807. bool _result = true;
  1808. if (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1 || READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1 || READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1)
  1809. {
  1810. current_position[0] = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? current_position[0] = current_position[0] + 10 : current_position[0];
  1811. current_position[1] = (READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? current_position[1] = current_position[1] + 10 : current_position[1];
  1812. current_position[2] = (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? current_position[2] = current_position[2] + 10 : current_position[2];
  1813. }
  1814. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[0] / 60, active_extruder);
  1815. delay(500);
  1816. if (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1 || READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1 || READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1)
  1817. {
  1818. _result = false;
  1819. String _error = String((READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? "X" : "") +
  1820. String((READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? "Y" : "") +
  1821. String((READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? "Z" : "");
  1822. lcd_selftest_error(3, _error.c_str(), "");
  1823. }
  1824. manage_heater();
  1825. manage_inactivity();
  1826. return _result;
  1827. }
  1828. static bool lcd_selfcheck_axis(int _axis, int _travel)
  1829. {
  1830. bool _stepdone = false;
  1831. bool _stepresult = false;
  1832. int _progress = 0;
  1833. int _travel_done = 0;
  1834. int _err_endstop = 0;
  1835. int _lcd_refresh = 0;
  1836. _travel = _travel + (_travel / 10);
  1837. do {
  1838. if (_axis == 2)
  1839. {
  1840. current_position[_axis] = current_position[_axis] - 1;
  1841. }
  1842. else
  1843. {
  1844. current_position[_axis] = current_position[_axis] - 3;
  1845. }
  1846. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[3], manual_feedrate[0] / 60, active_extruder);
  1847. st_synchronize();
  1848. if (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1 || READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1 || READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1)
  1849. {
  1850. if (_axis == 0)
  1851. {
  1852. _stepresult = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? true : false;
  1853. _err_endstop = (READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? 1 : 2;
  1854. disable_x();
  1855. }
  1856. if (_axis == 1)
  1857. {
  1858. _stepresult = (READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? true : false;
  1859. _err_endstop = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? 0 : 2;
  1860. disable_y();
  1861. }
  1862. if (_axis == 2)
  1863. {
  1864. _stepresult = (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? true : false;
  1865. _err_endstop = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? 0 : 1;
  1866. disable_z();
  1867. }
  1868. _stepdone = true;
  1869. }
  1870. if (_lcd_refresh < 6)
  1871. {
  1872. _lcd_refresh++;
  1873. }
  1874. else
  1875. {
  1876. _progress = lcd_selftest_screen(2 + _axis, _progress, 3, false, 0);
  1877. _lcd_refresh = 0;
  1878. }
  1879. manage_heater();
  1880. manage_inactivity();
  1881. delay(100);
  1882. (_travel_done <= _travel) ? _travel_done++ : _stepdone = true;
  1883. } while (!_stepdone);
  1884. current_position[_axis] = current_position[_axis] + 15;
  1885. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[3], manual_feedrate[0] / 60, active_extruder);
  1886. if (!_stepresult)
  1887. {
  1888. const char *_error_1;
  1889. const char *_error_2;
  1890. if (_axis == X_AXIS) _error_1 = "X";
  1891. if (_axis == Y_AXIS) _error_1 = "Y";
  1892. if (_axis == Z_AXIS) _error_1 = "Z";
  1893. if (_err_endstop == 0) _error_2 = "X";
  1894. if (_err_endstop == 1) _error_2 = "Y";
  1895. if (_err_endstop == 2) _error_2 = "Z";
  1896. if (_travel_done >= _travel)
  1897. {
  1898. lcd_selftest_error(5, _error_1, _error_2);
  1899. }
  1900. else
  1901. {
  1902. lcd_selftest_error(4, _error_1, _error_2);
  1903. }
  1904. }
  1905. return _stepresult;
  1906. }
  1907. static bool lcd_selfcheck_check_heater(bool _isbed)
  1908. {
  1909. int _counter = 0;
  1910. int _progress = 0;
  1911. bool _stepresult = false;
  1912. bool _docycle = true;
  1913. int _checked_snapshot = (_isbed) ? degBed() : degHotend(0);
  1914. int _opposite_snapshot = (_isbed) ? degHotend(0) : degBed();
  1915. int _cycles = (_isbed) ? 120 : 30;
  1916. target_temperature[0] = (_isbed) ? 0 : 100;
  1917. target_temperature_bed = (_isbed) ? 100 : 0;
  1918. manage_heater();
  1919. manage_inactivity();
  1920. do {
  1921. _counter++;
  1922. (_counter < _cycles) ? _docycle = true : _docycle = false;
  1923. manage_heater();
  1924. manage_inactivity();
  1925. _progress = (_isbed) ? lcd_selftest_screen(5, _progress, 2, false, 400) : lcd_selftest_screen(1, _progress, 2, false, 400);
  1926. } while (_docycle);
  1927. target_temperature[0] = 0;
  1928. target_temperature_bed = 0;
  1929. manage_heater();
  1930. int _checked_result = (_isbed) ? degBed() - _checked_snapshot : degHotend(0) - _checked_snapshot;
  1931. int _opposite_result = (_isbed) ? degHotend(0) - _opposite_snapshot : degBed() - _opposite_snapshot;
  1932. if (_opposite_result < (_isbed) ? 10 : 3)
  1933. {
  1934. if (_checked_result >= (_isbed) ? 3 : 10)
  1935. {
  1936. _stepresult = true;
  1937. }
  1938. else
  1939. {
  1940. lcd_selftest_error(1, "", "");
  1941. }
  1942. }
  1943. else
  1944. {
  1945. lcd_selftest_error(2, "", "");
  1946. }
  1947. manage_heater();
  1948. manage_inactivity();
  1949. return _stepresult;
  1950. }
  1951. static void lcd_selftest_error(int _error_no, const char *_error_1, const char *_error_2)
  1952. {
  1953. lcd_implementation_quick_feedback();
  1954. target_temperature[0] = 0;
  1955. target_temperature_bed = 0;
  1956. manage_heater();
  1957. manage_inactivity();
  1958. lcd_implementation_clear();
  1959. lcd.setCursor(0, 0);
  1960. lcd_printPGM(MSG_SELFTEST_ERROR);
  1961. lcd.setCursor(0, 1);
  1962. lcd_printPGM(MSG_SELFTEST_PLEASECHECK);
  1963. switch (_error_no)
  1964. {
  1965. case 1:
  1966. lcd.setCursor(0, 2);
  1967. lcd_printPGM(MSG_SELFTEST_HEATERTHERMISTOR);
  1968. lcd.setCursor(0, 3);
  1969. lcd_printPGM(MSG_SELFTEST_NOTCONNECTED);
  1970. break;
  1971. case 2:
  1972. lcd.setCursor(0, 2);
  1973. lcd_printPGM(MSG_SELFTEST_BEDHEATER);
  1974. lcd.setCursor(0, 3);
  1975. lcd_printPGM(MSG_SELFTEST_WIRINGERROR);
  1976. break;
  1977. case 3:
  1978. lcd.setCursor(0, 2);
  1979. lcd_printPGM(MSG_SELFTEST_ENDSTOPS);
  1980. lcd.setCursor(0, 3);
  1981. lcd_printPGM(MSG_SELFTEST_WIRINGERROR);
  1982. lcd.setCursor(17, 3);
  1983. lcd.print(_error_1);
  1984. break;
  1985. case 4:
  1986. lcd.setCursor(0, 2);
  1987. lcd_printPGM(MSG_SELFTEST_MOTOR);
  1988. lcd.setCursor(18, 2);
  1989. lcd.print(_error_1);
  1990. lcd.setCursor(0, 3);
  1991. lcd_printPGM(MSG_SELFTEST_ENDSTOP);
  1992. lcd.setCursor(18, 3);
  1993. lcd.print(_error_2);
  1994. break;
  1995. case 5:
  1996. lcd.setCursor(0, 2);
  1997. lcd_printPGM(MSG_SELFTEST_ENDSTOP_NOTHIT);
  1998. lcd.setCursor(0, 3);
  1999. lcd_printPGM(MSG_SELFTEST_MOTOR);
  2000. lcd.setCursor(18, 3);
  2001. lcd.print(_error_1);
  2002. break;
  2003. }
  2004. delay(1000);
  2005. lcd_implementation_quick_feedback();
  2006. do {
  2007. delay(100);
  2008. manage_heater();
  2009. manage_inactivity();
  2010. } while (!lcd_clicked());
  2011. LCD_ALERTMESSAGERPGM(MSG_SELFTEST_FAILED);
  2012. lcd_return_to_status();
  2013. }
  2014. static int lcd_selftest_screen(int _step, int _progress, int _progress_scale, bool _clear, int _delay)
  2015. {
  2016. lcd_next_update_millis = millis() + (LCD_UPDATE_INTERVAL * 10000);
  2017. int _step_block = 0;
  2018. const char *_indicator = (_progress > _progress_scale) ? "-" : "|";
  2019. if (_clear) lcd_implementation_clear();
  2020. lcd.setCursor(0, 0);
  2021. if (_step == -1) lcd_printPGM(MSG_SELFTEST_START);
  2022. if (_step == 0) lcd_printPGM(MSG_SELFTEST_CHECK_ENDSTOPS);
  2023. if (_step == 1) lcd_printPGM(MSG_SELFTEST_CHECK_HOTEND);
  2024. if (_step == 2) lcd_printPGM(MSG_SELFTEST_CHECK_X);
  2025. if (_step == 3) lcd_printPGM(MSG_SELFTEST_CHECK_Y);
  2026. if (_step == 4) lcd_printPGM(MSG_SELFTEST_CHECK_Z);
  2027. if (_step == 5) lcd_printPGM(MSG_SELFTEST_CHECK_BED);
  2028. if (_step == 6) lcd_printPGM(MSG_SELFTEST_CHECK_ALLCORRECT);
  2029. if (_step == 7) lcd_printPGM(MSG_SELFTEST_FAILED);
  2030. lcd.setCursor(0, 1);
  2031. lcd.print("--------------------");
  2032. _step_block = 1;
  2033. lcd_selftest_screen_step(3, 9, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Hotend", _indicator);
  2034. _step_block = 2;
  2035. lcd_selftest_screen_step(2, 2, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "X", _indicator);
  2036. _step_block = 3;
  2037. lcd_selftest_screen_step(2, 8, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Y", _indicator);
  2038. _step_block = 4;
  2039. lcd_selftest_screen_step(2, 14, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Z", _indicator);
  2040. _step_block = 5;
  2041. lcd_selftest_screen_step(3, 0, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Bed", _indicator);
  2042. if (_delay > 0) delay(_delay);
  2043. _progress++;
  2044. return (_progress > _progress_scale * 2) ? 0 : _progress;
  2045. }
  2046. static void lcd_selftest_screen_step(int _row, int _col, int _state, const char *_name, const char *_indicator)
  2047. {
  2048. lcd.setCursor(_col, _row);
  2049. switch (_state)
  2050. {
  2051. case 1:
  2052. lcd.print(_name);
  2053. lcd.setCursor(_col + strlen(_name), _row);
  2054. lcd.print(":");
  2055. lcd.setCursor(_col + strlen(_name) + 1, _row);
  2056. lcd.print(_indicator);
  2057. break;
  2058. case 2:
  2059. lcd.print(_name);
  2060. lcd.setCursor(_col + strlen(_name), _row);
  2061. lcd.print(":");
  2062. lcd.setCursor(_col + strlen(_name) + 1, _row);
  2063. lcd.print("OK");
  2064. break;
  2065. default:
  2066. lcd.print(_name);
  2067. }
  2068. }
  2069. /** End of menus **/
  2070. static void lcd_quick_feedback()
  2071. {
  2072. lcdDrawUpdate = 2;
  2073. blocking_enc = millis() + 500;
  2074. lcd_implementation_quick_feedback();
  2075. }
  2076. /** Menu action functions **/
  2077. static void menu_action_back(menuFunc_t data) {
  2078. lcd_goto_menu(data);
  2079. }
  2080. static void menu_action_submenu(menuFunc_t data) {
  2081. lcd_goto_menu(data);
  2082. }
  2083. static void menu_action_gcode(const char* pgcode) {
  2084. enquecommand_P(pgcode);
  2085. }
  2086. static void menu_action_setlang(unsigned char lang) {
  2087. lcd_set_lang(lang);
  2088. }
  2089. static void menu_action_function(menuFunc_t data) {
  2090. (*data)();
  2091. }
  2092. static void menu_action_sdfile(const char* filename, char* longFilename)
  2093. {
  2094. char cmd[30];
  2095. char* c;
  2096. sprintf_P(cmd, PSTR("M23 %s"), filename);
  2097. for (c = &cmd[4]; *c; c++)
  2098. *c = tolower(*c);
  2099. enquecommand(cmd);
  2100. enquecommand_P(PSTR("M24"));
  2101. lcd_return_to_status();
  2102. }
  2103. static void menu_action_sddirectory(const char* filename, char* longFilename)
  2104. {
  2105. card.chdir(filename);
  2106. encoderPosition = 0;
  2107. }
  2108. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr)
  2109. {
  2110. *ptr = !(*ptr);
  2111. }
  2112. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callback)
  2113. {
  2114. menu_action_setting_edit_bool(pstr, ptr);
  2115. (*callback)();
  2116. }
  2117. #endif//ULTIPANEL
  2118. /** LCD API **/
  2119. void lcd_init()
  2120. {
  2121. lcd_implementation_init();
  2122. #ifdef NEWPANEL
  2123. SET_INPUT(BTN_EN1);
  2124. SET_INPUT(BTN_EN2);
  2125. WRITE(BTN_EN1, HIGH);
  2126. WRITE(BTN_EN2, HIGH);
  2127. #if BTN_ENC > 0
  2128. SET_INPUT(BTN_ENC);
  2129. WRITE(BTN_ENC, HIGH);
  2130. #endif
  2131. #ifdef REPRAPWORLD_KEYPAD
  2132. pinMode(SHIFT_CLK, OUTPUT);
  2133. pinMode(SHIFT_LD, OUTPUT);
  2134. pinMode(SHIFT_OUT, INPUT);
  2135. WRITE(SHIFT_OUT, HIGH);
  2136. WRITE(SHIFT_LD, HIGH);
  2137. #endif
  2138. #else // Not NEWPANEL
  2139. #ifdef SR_LCD_2W_NL // Non latching 2 wire shift register
  2140. pinMode (SR_DATA_PIN, OUTPUT);
  2141. pinMode (SR_CLK_PIN, OUTPUT);
  2142. #elif defined(SHIFT_CLK)
  2143. pinMode(SHIFT_CLK, OUTPUT);
  2144. pinMode(SHIFT_LD, OUTPUT);
  2145. pinMode(SHIFT_EN, OUTPUT);
  2146. pinMode(SHIFT_OUT, INPUT);
  2147. WRITE(SHIFT_OUT, HIGH);
  2148. WRITE(SHIFT_LD, HIGH);
  2149. WRITE(SHIFT_EN, LOW);
  2150. #else
  2151. #ifdef ULTIPANEL
  2152. #error ULTIPANEL requires an encoder
  2153. #endif
  2154. #endif // SR_LCD_2W_NL
  2155. #endif//!NEWPANEL
  2156. #if defined (SDSUPPORT) && defined(SDCARDDETECT) && (SDCARDDETECT > 0)
  2157. pinMode(SDCARDDETECT, INPUT);
  2158. WRITE(SDCARDDETECT, HIGH);
  2159. lcd_oldcardstatus = IS_SD_INSERTED;
  2160. #endif//(SDCARDDETECT > 0)
  2161. #ifdef LCD_HAS_SLOW_BUTTONS
  2162. slow_buttons = 0;
  2163. #endif
  2164. lcd_buttons_update();
  2165. #ifdef ULTIPANEL
  2166. encoderDiff = 0;
  2167. #endif
  2168. }
  2169. //#include <avr/pgmspace.h>
  2170. static volatile bool lcd_update_enabled = true;
  2171. void lcd_update_enable(bool enabled)
  2172. {
  2173. lcd_update_enabled = enabled;
  2174. }
  2175. void lcd_update()
  2176. {
  2177. static unsigned long timeoutToStatus = 0;
  2178. if (! lcd_update_enabled)
  2179. return;
  2180. #ifdef LCD_HAS_SLOW_BUTTONS
  2181. slow_buttons = lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  2182. #endif
  2183. lcd_buttons_update();
  2184. #if (SDCARDDETECT > 0)
  2185. if ((IS_SD_INSERTED != lcd_oldcardstatus && lcd_detected()))
  2186. {
  2187. lcdDrawUpdate = 2;
  2188. lcd_oldcardstatus = IS_SD_INSERTED;
  2189. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  2190. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  2191. currentMenu == lcd_status_screen
  2192. #endif
  2193. );
  2194. if (lcd_oldcardstatus)
  2195. {
  2196. card.initsd();
  2197. LCD_MESSAGERPGM(MSG_SD_INSERTED);
  2198. }
  2199. else
  2200. {
  2201. card.release();
  2202. LCD_MESSAGERPGM(MSG_SD_REMOVED);
  2203. }
  2204. }
  2205. #endif//CARDINSERTED
  2206. if (lcd_next_update_millis < millis())
  2207. {
  2208. #ifdef ULTIPANEL
  2209. #ifdef REPRAPWORLD_KEYPAD
  2210. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) {
  2211. reprapworld_keypad_move_z_up();
  2212. }
  2213. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) {
  2214. reprapworld_keypad_move_z_down();
  2215. }
  2216. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) {
  2217. reprapworld_keypad_move_x_left();
  2218. }
  2219. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) {
  2220. reprapworld_keypad_move_x_right();
  2221. }
  2222. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) {
  2223. reprapworld_keypad_move_y_down();
  2224. }
  2225. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) {
  2226. reprapworld_keypad_move_y_up();
  2227. }
  2228. if (REPRAPWORLD_KEYPAD_MOVE_HOME) {
  2229. reprapworld_keypad_move_home();
  2230. }
  2231. #endif
  2232. if (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP)
  2233. {
  2234. lcdDrawUpdate = 1;
  2235. encoderPosition += encoderDiff / ENCODER_PULSES_PER_STEP;
  2236. encoderDiff = 0;
  2237. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  2238. }
  2239. if (LCD_CLICKED)
  2240. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  2241. #endif//ULTIPANEL
  2242. #ifdef DOGLCD // Changes due to different driver architecture of the DOGM display
  2243. blink++; // Variable for fan animation and alive dot
  2244. u8g.firstPage();
  2245. do
  2246. {
  2247. u8g.setFont(u8g_font_6x10_marlin);
  2248. u8g.setPrintPos(125, 0);
  2249. if (blink % 2) u8g.setColorIndex(1); else u8g.setColorIndex(0); // Set color for the alive dot
  2250. u8g.drawPixel(127, 63); // draw alive dot
  2251. u8g.setColorIndex(1); // black on white
  2252. (*currentMenu)();
  2253. if (!lcdDrawUpdate) break; // Terminate display update, when nothing new to draw. This must be done before the last dogm.next()
  2254. } while (u8g.nextPage());
  2255. #else
  2256. (*currentMenu)();
  2257. #endif
  2258. #ifdef LCD_HAS_STATUS_INDICATORS
  2259. lcd_implementation_update_indicators();
  2260. #endif
  2261. #ifdef ULTIPANEL
  2262. if (timeoutToStatus < millis() && currentMenu != lcd_status_screen)
  2263. {
  2264. // Exiting a menu. Let's call the menu function the last time with menuExiting flag set to true
  2265. // to give it a chance to save its state.
  2266. // This is useful for example, when the babystep value has to be written into EEPROM.
  2267. if (currentMenu != NULL) {
  2268. menuExiting = true;
  2269. (*currentMenu)();
  2270. menuExiting = false;
  2271. }
  2272. lcd_return_to_status();
  2273. lcdDrawUpdate = 2;
  2274. }
  2275. #endif//ULTIPANEL
  2276. if (lcdDrawUpdate == 2) lcd_implementation_clear();
  2277. if (lcdDrawUpdate) lcdDrawUpdate--;
  2278. lcd_next_update_millis = millis() + LCD_UPDATE_INTERVAL;
  2279. }
  2280. }
  2281. void lcd_ignore_click(bool b)
  2282. {
  2283. ignore_click = b;
  2284. wait_for_unclick = false;
  2285. }
  2286. void lcd_finishstatus() {
  2287. int len = strlen(lcd_status_message);
  2288. if (len > 0) {
  2289. while (len < LCD_WIDTH) {
  2290. lcd_status_message[len++] = ' ';
  2291. }
  2292. }
  2293. lcd_status_message[LCD_WIDTH] = '\0';
  2294. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  2295. #if PROGRESS_MSG_EXPIRE > 0
  2296. messageTick =
  2297. #endif
  2298. progressBarTick = millis();
  2299. #endif
  2300. lcdDrawUpdate = 2;
  2301. #ifdef FILAMENT_LCD_DISPLAY
  2302. message_millis = millis(); //get status message to show up for a while
  2303. #endif
  2304. }
  2305. void lcd_setstatus(const char* message)
  2306. {
  2307. if (lcd_status_message_level > 0)
  2308. return;
  2309. strncpy(lcd_status_message, message, LCD_WIDTH);
  2310. lcd_finishstatus();
  2311. }
  2312. void lcd_setstatuspgm(const char* message)
  2313. {
  2314. if (lcd_status_message_level > 0)
  2315. return;
  2316. strncpy_P(lcd_status_message, message, LCD_WIDTH);
  2317. lcd_finishstatus();
  2318. }
  2319. void lcd_setalertstatuspgm(const char* message)
  2320. {
  2321. lcd_setstatuspgm(message);
  2322. lcd_status_message_level = 1;
  2323. #ifdef ULTIPANEL
  2324. lcd_return_to_status();
  2325. #endif//ULTIPANEL
  2326. }
  2327. void lcd_reset_alert_level()
  2328. {
  2329. lcd_status_message_level = 0;
  2330. }
  2331. #ifdef DOGLCD
  2332. void lcd_setcontrast(uint8_t value)
  2333. {
  2334. lcd_contrast = value & 63;
  2335. u8g.setContrast(lcd_contrast);
  2336. }
  2337. #endif
  2338. #ifdef ULTIPANEL
  2339. /* Warning: This function is called from interrupt context */
  2340. void lcd_buttons_update()
  2341. {
  2342. #ifdef NEWPANEL
  2343. uint8_t newbutton = 0;
  2344. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  2345. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  2346. #if BTN_ENC > 0
  2347. if ((blocking_enc < millis()) && (READ(BTN_ENC) == 0))
  2348. newbutton |= EN_C;
  2349. #endif
  2350. buttons = newbutton;
  2351. #ifdef LCD_HAS_SLOW_BUTTONS
  2352. buttons |= slow_buttons;
  2353. #endif
  2354. #ifdef REPRAPWORLD_KEYPAD
  2355. // for the reprapworld_keypad
  2356. uint8_t newbutton_reprapworld_keypad = 0;
  2357. WRITE(SHIFT_LD, LOW);
  2358. WRITE(SHIFT_LD, HIGH);
  2359. for (int8_t i = 0; i < 8; i++) {
  2360. newbutton_reprapworld_keypad = newbutton_reprapworld_keypad >> 1;
  2361. if (READ(SHIFT_OUT))
  2362. newbutton_reprapworld_keypad |= (1 << 7);
  2363. WRITE(SHIFT_CLK, HIGH);
  2364. WRITE(SHIFT_CLK, LOW);
  2365. }
  2366. buttons_reprapworld_keypad = ~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
  2367. #endif
  2368. #else //read it from the shift register
  2369. uint8_t newbutton = 0;
  2370. WRITE(SHIFT_LD, LOW);
  2371. WRITE(SHIFT_LD, HIGH);
  2372. unsigned char tmp_buttons = 0;
  2373. for (int8_t i = 0; i < 8; i++)
  2374. {
  2375. newbutton = newbutton >> 1;
  2376. if (READ(SHIFT_OUT))
  2377. newbutton |= (1 << 7);
  2378. WRITE(SHIFT_CLK, HIGH);
  2379. WRITE(SHIFT_CLK, LOW);
  2380. }
  2381. buttons = ~newbutton; //invert it, because a pressed switch produces a logical 0
  2382. #endif//!NEWPANEL
  2383. //manage encoder rotation
  2384. uint8_t enc = 0;
  2385. if (buttons & EN_A) enc |= B01;
  2386. if (buttons & EN_B) enc |= B10;
  2387. if (enc != lastEncoderBits)
  2388. {
  2389. switch (enc)
  2390. {
  2391. case encrot0:
  2392. if (lastEncoderBits == encrot3)
  2393. encoderDiff++;
  2394. else if (lastEncoderBits == encrot1)
  2395. encoderDiff--;
  2396. break;
  2397. case encrot1:
  2398. if (lastEncoderBits == encrot0)
  2399. encoderDiff++;
  2400. else if (lastEncoderBits == encrot2)
  2401. encoderDiff--;
  2402. break;
  2403. case encrot2:
  2404. if (lastEncoderBits == encrot1)
  2405. encoderDiff++;
  2406. else if (lastEncoderBits == encrot3)
  2407. encoderDiff--;
  2408. break;
  2409. case encrot3:
  2410. if (lastEncoderBits == encrot2)
  2411. encoderDiff++;
  2412. else if (lastEncoderBits == encrot0)
  2413. encoderDiff--;
  2414. break;
  2415. }
  2416. }
  2417. lastEncoderBits = enc;
  2418. }
  2419. bool lcd_detected(void)
  2420. {
  2421. #if (defined(LCD_I2C_TYPE_MCP23017) || defined(LCD_I2C_TYPE_MCP23008)) && defined(DETECT_DEVICE)
  2422. return lcd.LcdDetected() == 1;
  2423. #else
  2424. return true;
  2425. #endif
  2426. }
  2427. void lcd_buzz(long duration, uint16_t freq)
  2428. {
  2429. #ifdef LCD_USE_I2C_BUZZER
  2430. lcd.buzz(duration, freq);
  2431. #endif
  2432. }
  2433. bool lcd_clicked()
  2434. {
  2435. return LCD_CLICKED;
  2436. }
  2437. #endif//ULTIPANEL
  2438. /********************************/
  2439. /** Float conversion utilities **/
  2440. /********************************/
  2441. // convert float to string with +123.4 format
  2442. char conv[8];
  2443. char *ftostr3(const float &x)
  2444. {
  2445. return itostr3((int)x);
  2446. }
  2447. char *itostr2(const uint8_t &x)
  2448. {
  2449. //sprintf(conv,"%5.1f",x);
  2450. int xx = x;
  2451. conv[0] = (xx / 10) % 10 + '0';
  2452. conv[1] = (xx) % 10 + '0';
  2453. conv[2] = 0;
  2454. return conv;
  2455. }
  2456. // Convert float to string with 123.4 format, dropping sign
  2457. char *ftostr31(const float &x)
  2458. {
  2459. int xx = x * 10;
  2460. conv[0] = (xx >= 0) ? '+' : '-';
  2461. xx = abs(xx);
  2462. conv[1] = (xx / 1000) % 10 + '0';
  2463. conv[2] = (xx / 100) % 10 + '0';
  2464. conv[3] = (xx / 10) % 10 + '0';
  2465. conv[4] = '.';
  2466. conv[5] = (xx) % 10 + '0';
  2467. conv[6] = 0;
  2468. return conv;
  2469. }
  2470. // Convert float to string with 123.4 format
  2471. char *ftostr31ns(const float &x)
  2472. {
  2473. int xx = x * 10;
  2474. //conv[0]=(xx>=0)?'+':'-';
  2475. xx = abs(xx);
  2476. conv[0] = (xx / 1000) % 10 + '0';
  2477. conv[1] = (xx / 100) % 10 + '0';
  2478. conv[2] = (xx / 10) % 10 + '0';
  2479. conv[3] = '.';
  2480. conv[4] = (xx) % 10 + '0';
  2481. conv[5] = 0;
  2482. return conv;
  2483. }
  2484. char *ftostr32(const float &x)
  2485. {
  2486. long xx = x * 100;
  2487. if (xx >= 0)
  2488. conv[0] = (xx / 10000) % 10 + '0';
  2489. else
  2490. conv[0] = '-';
  2491. xx = abs(xx);
  2492. conv[1] = (xx / 1000) % 10 + '0';
  2493. conv[2] = (xx / 100) % 10 + '0';
  2494. conv[3] = '.';
  2495. conv[4] = (xx / 10) % 10 + '0';
  2496. conv[5] = (xx) % 10 + '0';
  2497. conv[6] = 0;
  2498. return conv;
  2499. }
  2500. //// Convert float to rj string with 123.45 format
  2501. char *ftostr32ns(const float &x) {
  2502. long xx = abs(x);
  2503. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  2504. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2505. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : '0';
  2506. conv[3] = '.';
  2507. conv[4] = (xx / 10) % 10 + '0';
  2508. conv[5] = xx % 10 + '0';
  2509. return conv;
  2510. }
  2511. // Convert float to string with 1.234 format
  2512. char *ftostr43(const float &x)
  2513. {
  2514. long xx = x * 1000;
  2515. if (xx >= 0)
  2516. conv[0] = (xx / 1000) % 10 + '0';
  2517. else
  2518. conv[0] = '-';
  2519. xx = abs(xx);
  2520. conv[1] = '.';
  2521. conv[2] = (xx / 100) % 10 + '0';
  2522. conv[3] = (xx / 10) % 10 + '0';
  2523. conv[4] = (xx) % 10 + '0';
  2524. conv[5] = 0;
  2525. return conv;
  2526. }
  2527. //Float to string with 1.23 format
  2528. char *ftostr12ns(const float &x)
  2529. {
  2530. long xx = x * 100;
  2531. xx = abs(xx);
  2532. conv[0] = (xx / 100) % 10 + '0';
  2533. conv[1] = '.';
  2534. conv[2] = (xx / 10) % 10 + '0';
  2535. conv[3] = (xx) % 10 + '0';
  2536. conv[4] = 0;
  2537. return conv;
  2538. }
  2539. //Float to string with 1.234 format
  2540. char *ftostr13ns(const float &x)
  2541. {
  2542. long xx = x * 1000;
  2543. if (xx >= 0)
  2544. conv[0] = ' ';
  2545. else
  2546. conv[0] = '-';
  2547. xx = abs(xx);
  2548. conv[1] = (xx / 1000) % 10 + '0';
  2549. conv[2] = '.';
  2550. conv[3] = (xx / 100) % 10 + '0';
  2551. conv[4] = (xx / 10) % 10 + '0';
  2552. conv[5] = (xx) % 10 + '0';
  2553. conv[6] = 0;
  2554. return conv;
  2555. }
  2556. // convert float to space-padded string with -_23.4_ format
  2557. char *ftostr32sp(const float &x) {
  2558. long xx = abs(x * 100);
  2559. uint8_t dig;
  2560. if (x < 0) { // negative val = -_0
  2561. conv[0] = '-';
  2562. dig = (xx / 1000) % 10;
  2563. conv[1] = dig ? '0' + dig : ' ';
  2564. }
  2565. else { // positive val = __0
  2566. dig = (xx / 10000) % 10;
  2567. if (dig) {
  2568. conv[0] = '0' + dig;
  2569. conv[1] = '0' + (xx / 1000) % 10;
  2570. }
  2571. else {
  2572. conv[0] = ' ';
  2573. dig = (xx / 1000) % 10;
  2574. conv[1] = dig ? '0' + dig : ' ';
  2575. }
  2576. }
  2577. conv[2] = '0' + (xx / 100) % 10; // lsd always
  2578. dig = xx % 10;
  2579. if (dig) { // 2 decimal places
  2580. conv[5] = '0' + dig;
  2581. conv[4] = '0' + (xx / 10) % 10;
  2582. conv[3] = '.';
  2583. }
  2584. else { // 1 or 0 decimal place
  2585. dig = (xx / 10) % 10;
  2586. if (dig) {
  2587. conv[4] = '0' + dig;
  2588. conv[3] = '.';
  2589. }
  2590. else {
  2591. conv[3] = conv[4] = ' ';
  2592. }
  2593. conv[5] = ' ';
  2594. }
  2595. conv[6] = '\0';
  2596. return conv;
  2597. }
  2598. char *itostr31(const int &xx)
  2599. {
  2600. conv[0] = (xx >= 0) ? '+' : '-';
  2601. conv[1] = (xx / 1000) % 10 + '0';
  2602. conv[2] = (xx / 100) % 10 + '0';
  2603. conv[3] = (xx / 10) % 10 + '0';
  2604. conv[4] = '.';
  2605. conv[5] = (xx) % 10 + '0';
  2606. conv[6] = 0;
  2607. return conv;
  2608. }
  2609. // Convert int to rj string with 123 or -12 format
  2610. char *itostr3(const int &x)
  2611. {
  2612. int xx = x;
  2613. if (xx < 0) {
  2614. conv[0] = '-';
  2615. xx = -xx;
  2616. } else if (xx >= 100)
  2617. conv[0] = (xx / 100) % 10 + '0';
  2618. else
  2619. conv[0] = ' ';
  2620. if (xx >= 10)
  2621. conv[1] = (xx / 10) % 10 + '0';
  2622. else
  2623. conv[1] = ' ';
  2624. conv[2] = (xx) % 10 + '0';
  2625. conv[3] = 0;
  2626. return conv;
  2627. }
  2628. // Convert int to lj string with 123 format
  2629. char *itostr3left(const int &xx)
  2630. {
  2631. if (xx >= 100)
  2632. {
  2633. conv[0] = (xx / 100) % 10 + '0';
  2634. conv[1] = (xx / 10) % 10 + '0';
  2635. conv[2] = (xx) % 10 + '0';
  2636. conv[3] = 0;
  2637. }
  2638. else if (xx >= 10)
  2639. {
  2640. conv[0] = (xx / 10) % 10 + '0';
  2641. conv[1] = (xx) % 10 + '0';
  2642. conv[2] = 0;
  2643. }
  2644. else
  2645. {
  2646. conv[0] = (xx) % 10 + '0';
  2647. conv[1] = 0;
  2648. }
  2649. return conv;
  2650. }
  2651. // Convert int to rj string with 1234 format
  2652. char *itostr4(const int &xx) {
  2653. conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2654. conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2655. conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2656. conv[3] = xx % 10 + '0';
  2657. conv[4] = 0;
  2658. return conv;
  2659. }
  2660. // Convert float to rj string with 12345 format
  2661. char *ftostr5(const float &x) {
  2662. long xx = abs(x);
  2663. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  2664. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2665. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2666. conv[3] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2667. conv[4] = xx % 10 + '0';
  2668. conv[5] = 0;
  2669. return conv;
  2670. }
  2671. // Convert float to string with +1234.5 format
  2672. char *ftostr51(const float &x)
  2673. {
  2674. long xx = x * 10;
  2675. conv[0] = (xx >= 0) ? '+' : '-';
  2676. xx = abs(xx);
  2677. conv[1] = (xx / 10000) % 10 + '0';
  2678. conv[2] = (xx / 1000) % 10 + '0';
  2679. conv[3] = (xx / 100) % 10 + '0';
  2680. conv[4] = (xx / 10) % 10 + '0';
  2681. conv[5] = '.';
  2682. conv[6] = (xx) % 10 + '0';
  2683. conv[7] = 0;
  2684. return conv;
  2685. }
  2686. // Convert float to string with +123.45 format
  2687. char *ftostr52(const float &x)
  2688. {
  2689. long xx = x * 100;
  2690. conv[0] = (xx >= 0) ? '+' : '-';
  2691. xx = abs(xx);
  2692. conv[1] = (xx / 10000) % 10 + '0';
  2693. conv[2] = (xx / 1000) % 10 + '0';
  2694. conv[3] = (xx / 100) % 10 + '0';
  2695. conv[4] = '.';
  2696. conv[5] = (xx / 10) % 10 + '0';
  2697. conv[6] = (xx) % 10 + '0';
  2698. conv[7] = 0;
  2699. return conv;
  2700. }
  2701. // Callback for after editing PID i value
  2702. // grab the PID i value out of the temp variable; scale it; then update the PID driver
  2703. void copy_and_scalePID_i()
  2704. {
  2705. #ifdef PIDTEMP
  2706. Ki = scalePID_i(raw_Ki);
  2707. updatePID();
  2708. #endif
  2709. }
  2710. // Callback for after editing PID d value
  2711. // grab the PID d value out of the temp variable; scale it; then update the PID driver
  2712. void copy_and_scalePID_d()
  2713. {
  2714. #ifdef PIDTEMP
  2715. Kd = scalePID_d(raw_Kd);
  2716. updatePID();
  2717. #endif
  2718. }
  2719. #endif //ULTRA_LCD