My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ultralcd.cpp 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. int8_t encoderDiff; /* encoderDiff is updated from interrupt context and added to encoderPosition every LCD update */
  11. /* Configuration settings */
  12. int plaPreheatHotendTemp;
  13. int plaPreheatHPBTemp;
  14. int plaPreheatFanSpeed;
  15. int absPreheatHotendTemp;
  16. int absPreheatHPBTemp;
  17. int absPreheatFanSpeed;
  18. /* !Configuration settings */
  19. //Function pointer to menu functions.
  20. typedef void (*menuFunc_t)();
  21. uint8_t lcd_status_message_level;
  22. char lcd_status_message[LCD_WIDTH+1] = WELCOME_MSG;
  23. #ifdef DOGLCD
  24. #include "dogm_lcd_implementation.h"
  25. #else
  26. #include "ultralcd_implementation_hitachi_HD44780.h"
  27. #endif
  28. /** forward declerations **/
  29. void copy_and_scalePID_i();
  30. void copy_and_scalePID_d();
  31. /* Different menus */
  32. static void lcd_status_screen();
  33. #ifdef ULTIPANEL
  34. extern bool powersupply;
  35. static void lcd_main_menu();
  36. static void lcd_tune_menu();
  37. static void lcd_prepare_menu();
  38. static void lcd_move_menu();
  39. static void lcd_control_menu();
  40. static void lcd_control_temperature_menu();
  41. static void lcd_control_temperature_preheat_pla_settings_menu();
  42. static void lcd_control_temperature_preheat_abs_settings_menu();
  43. static void lcd_control_motion_menu();
  44. #ifdef DOGLCD
  45. static void lcd_set_contrast();
  46. #endif
  47. static void lcd_control_retract_menu();
  48. static void lcd_sdcard_menu();
  49. static void lcd_quick_feedback();//Cause an LCD refresh, and give the user visual or audiable feedback that something has happend
  50. /* Different types of actions that can be used in menuitems. */
  51. static void menu_action_back(menuFunc_t data);
  52. static void menu_action_submenu(menuFunc_t data);
  53. static void menu_action_gcode(const char* pgcode);
  54. static void menu_action_function(menuFunc_t data);
  55. static void menu_action_sdfile(const char* filename, char* longFilename);
  56. static void menu_action_sddirectory(const char* filename, char* longFilename);
  57. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr);
  58. static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  59. static void menu_action_setting_edit_float3(const char* pstr, float* ptr, float minValue, float maxValue);
  60. static void menu_action_setting_edit_float32(const char* pstr, float* ptr, float minValue, float maxValue);
  61. static void menu_action_setting_edit_float5(const char* pstr, float* ptr, float minValue, float maxValue);
  62. static void menu_action_setting_edit_float51(const char* pstr, float* ptr, float minValue, float maxValue);
  63. static void menu_action_setting_edit_float52(const char* pstr, float* ptr, float minValue, float maxValue);
  64. static void menu_action_setting_edit_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue);
  65. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callbackFunc);
  66. static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, menuFunc_t callbackFunc);
  67. static void menu_action_setting_edit_callback_float3(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  68. static void menu_action_setting_edit_callback_float32(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  69. static void menu_action_setting_edit_callback_float5(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  70. static void menu_action_setting_edit_callback_float51(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  71. static void menu_action_setting_edit_callback_float52(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  72. static void menu_action_setting_edit_callback_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue, menuFunc_t callbackFunc);
  73. #define ENCODER_FEEDRATE_DEADZONE 10
  74. #if !defined(LCD_I2C_VIKI)
  75. #define ENCODER_STEPS_PER_MENU_ITEM 5
  76. #else
  77. #define ENCODER_STEPS_PER_MENU_ITEM 2 // VIKI LCD rotary encoder uses a different number of steps per rotation
  78. #endif
  79. /* Helper macros for menus */
  80. #define START_MENU() do { \
  81. if (encoderPosition > 0x8000) encoderPosition = 0; \
  82. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM < currentMenuViewOffset) currentMenuViewOffset = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM;\
  83. uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
  84. for(uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
  85. _menuItemNr = 0;
  86. #define MENU_ITEM(type, label, args...) do { \
  87. if (_menuItemNr == _lineNr) { \
  88. if (lcdDrawUpdate) { \
  89. const char* _label_pstr = PSTR(label); \
  90. if ((encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) { \
  91. lcd_implementation_drawmenu_ ## type ## _selected (_drawLineNr, _label_pstr , ## args ); \
  92. }else{\
  93. lcd_implementation_drawmenu_ ## type (_drawLineNr, _label_pstr , ## args ); \
  94. }\
  95. }\
  96. if (LCD_CLICKED && (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) {\
  97. lcd_quick_feedback(); \
  98. menu_action_ ## type ( args ); \
  99. return;\
  100. }\
  101. }\
  102. _menuItemNr++;\
  103. } while(0)
  104. #define MENU_ITEM_DUMMY() do { _menuItemNr++; } while(0)
  105. #define MENU_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, PSTR(label) , ## args )
  106. #define MENU_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, PSTR(label) , ## args )
  107. #define END_MENU() \
  108. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM >= _menuItemNr) encoderPosition = _menuItemNr * ENCODER_STEPS_PER_MENU_ITEM - 1; \
  109. 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; } \
  110. } } while(0)
  111. /** Used variables to keep track of the menu */
  112. #ifndef REPRAPWORLD_KEYPAD
  113. volatile uint8_t buttons;//Contains the bits of the currently pressed buttons.
  114. #else
  115. volatile uint8_t buttons_reprapworld_keypad; // to store the reprapworld_keypad shiftregister values
  116. #endif
  117. uint8_t currentMenuViewOffset; /* scroll offset in the current menu */
  118. uint32_t blocking_enc;
  119. uint8_t lastEncoderBits;
  120. uint32_t encoderPosition;
  121. #if (SDCARDDETECT > 0)
  122. bool lcd_oldcardstatus;
  123. #endif
  124. #endif//ULTIPANEL
  125. menuFunc_t currentMenu = lcd_status_screen; /* function pointer to the currently active menu */
  126. uint32_t lcd_next_update_millis;
  127. uint8_t lcd_status_update_delay;
  128. 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 atleast 1 full redraw (first redraw is partial) */
  129. //prevMenu and prevEncoderPosition are used to store the previous menu location when editing settings.
  130. menuFunc_t prevMenu = NULL;
  131. uint16_t prevEncoderPosition;
  132. //Variables used when editing values.
  133. const char* editLabel;
  134. void* editValue;
  135. int32_t minEditValue, maxEditValue;
  136. menuFunc_t callbackFunc;
  137. // placeholders for Ki and Kd edits
  138. float raw_Ki, raw_Kd;
  139. /* Main status screen. It's up to the implementation specific part to show what is needed. As this is very display dependend */
  140. static void lcd_status_screen()
  141. {
  142. if (lcd_status_update_delay)
  143. lcd_status_update_delay--;
  144. else
  145. lcdDrawUpdate = 1;
  146. if (lcdDrawUpdate)
  147. {
  148. lcd_implementation_status_screen();
  149. 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 */
  150. }
  151. #ifdef ULTIPANEL
  152. if (LCD_CLICKED)
  153. {
  154. currentMenu = lcd_main_menu;
  155. encoderPosition = 0;
  156. lcd_quick_feedback();
  157. }
  158. // Dead zone at 100% feedrate
  159. if ((feedmultiply < 100 && (feedmultiply + int(encoderPosition)) > 100) ||
  160. (feedmultiply > 100 && (feedmultiply + int(encoderPosition)) < 100))
  161. {
  162. encoderPosition = 0;
  163. feedmultiply = 100;
  164. }
  165. if (feedmultiply == 100 && int(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  166. {
  167. feedmultiply += int(encoderPosition) - ENCODER_FEEDRATE_DEADZONE;
  168. encoderPosition = 0;
  169. }
  170. else if (feedmultiply == 100 && int(encoderPosition) < -ENCODER_FEEDRATE_DEADZONE)
  171. {
  172. feedmultiply += int(encoderPosition) + ENCODER_FEEDRATE_DEADZONE;
  173. encoderPosition = 0;
  174. }
  175. else if (feedmultiply != 100)
  176. {
  177. feedmultiply += int(encoderPosition);
  178. encoderPosition = 0;
  179. }
  180. if (feedmultiply < 10)
  181. feedmultiply = 10;
  182. if (feedmultiply > 999)
  183. feedmultiply = 999;
  184. #endif//ULTIPANEL
  185. }
  186. #ifdef ULTIPANEL
  187. static void lcd_return_to_status()
  188. {
  189. encoderPosition = 0;
  190. currentMenu = lcd_status_screen;
  191. }
  192. static void lcd_sdcard_pause()
  193. {
  194. card.pauseSDPrint();
  195. }
  196. static void lcd_sdcard_resume()
  197. {
  198. card.startFileprint();
  199. }
  200. static void lcd_sdcard_stop()
  201. {
  202. card.sdprinting = false;
  203. card.closefile();
  204. quickStop();
  205. if(SD_FINISHED_STEPPERRELEASE)
  206. {
  207. enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  208. }
  209. autotempShutdown();
  210. }
  211. /* Menu implementation */
  212. static void lcd_main_menu()
  213. {
  214. START_MENU();
  215. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  216. if (movesplanned() || IS_SD_PRINTING)
  217. {
  218. MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
  219. }else{
  220. MENU_ITEM(submenu, MSG_PREPARE, lcd_prepare_menu);
  221. }
  222. MENU_ITEM(submenu, MSG_CONTROL, lcd_control_menu);
  223. #ifdef SDSUPPORT
  224. if (card.cardOK)
  225. {
  226. if (card.isFileOpen())
  227. {
  228. if (card.sdprinting)
  229. MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
  230. else
  231. MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_resume);
  232. MENU_ITEM(function, MSG_STOP_PRINT, lcd_sdcard_stop);
  233. }else{
  234. MENU_ITEM(submenu, MSG_CARD_MENU, lcd_sdcard_menu);
  235. #if SDCARDDETECT < 1
  236. MENU_ITEM(gcode, MSG_CNG_SDCARD, PSTR("M21")); // SD-card changed by user
  237. #endif
  238. }
  239. }else{
  240. MENU_ITEM(submenu, MSG_NO_CARD, lcd_sdcard_menu);
  241. #if SDCARDDETECT < 1
  242. MENU_ITEM(gcode, MSG_INIT_SDCARD, PSTR("M21")); // Manually initialize the SD-card via user interface
  243. #endif
  244. }
  245. #endif
  246. END_MENU();
  247. }
  248. #ifdef SDSUPPORT
  249. static void lcd_autostart_sd()
  250. {
  251. card.lastnr=0;
  252. card.setroot();
  253. card.checkautostart(true);
  254. }
  255. #endif
  256. void lcd_preheat_pla()
  257. {
  258. setTargetHotend0(plaPreheatHotendTemp);
  259. setTargetHotend1(plaPreheatHotendTemp);
  260. setTargetHotend2(plaPreheatHotendTemp);
  261. setTargetBed(plaPreheatHPBTemp);
  262. fanSpeed = plaPreheatFanSpeed;
  263. lcd_return_to_status();
  264. setWatch(); // heater sanity check timer
  265. }
  266. void lcd_preheat_abs()
  267. {
  268. setTargetHotend0(absPreheatHotendTemp);
  269. setTargetHotend1(absPreheatHotendTemp);
  270. setTargetHotend2(absPreheatHotendTemp);
  271. setTargetBed(absPreheatHPBTemp);
  272. fanSpeed = absPreheatFanSpeed;
  273. lcd_return_to_status();
  274. setWatch(); // heater sanity check timer
  275. }
  276. static void lcd_cooldown()
  277. {
  278. setTargetHotend0(0);
  279. setTargetHotend1(0);
  280. setTargetHotend2(0);
  281. setTargetBed(0);
  282. lcd_return_to_status();
  283. }
  284. static void lcd_tune_menu()
  285. {
  286. START_MENU();
  287. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  288. MENU_ITEM_EDIT(int3, MSG_SPEED, &feedmultiply, 10, 999);
  289. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 15);
  290. #if TEMP_SENSOR_1 != 0
  291. MENU_ITEM_EDIT(int3, MSG_NOZZLE1, &target_temperature[1], 0, HEATER_1_MAXTEMP - 15);
  292. #endif
  293. #if TEMP_SENSOR_2 != 0
  294. MENU_ITEM_EDIT(int3, MSG_NOZZLE2, &target_temperature[2], 0, HEATER_2_MAXTEMP - 15);
  295. #endif
  296. #if TEMP_SENSOR_BED != 0
  297. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 15);
  298. #endif
  299. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);
  300. MENU_ITEM_EDIT(int3, MSG_FLOW, &extrudemultiply, 10, 999);
  301. #ifdef FILAMENTCHANGEENABLE
  302. MENU_ITEM(gcode, MSG_FILAMENTCHANGE, PSTR("M600"));
  303. #endif
  304. END_MENU();
  305. }
  306. static void lcd_prepare_menu()
  307. {
  308. START_MENU();
  309. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  310. #ifdef SDSUPPORT
  311. //MENU_ITEM(function, MSG_AUTOSTART, lcd_autostart_sd);
  312. #endif
  313. MENU_ITEM(gcode, MSG_DISABLE_STEPPERS, PSTR("M84"));
  314. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  315. //MENU_ITEM(gcode, MSG_SET_ORIGIN, PSTR("G92 X0 Y0 Z0"));
  316. MENU_ITEM(function, MSG_PREHEAT_PLA, lcd_preheat_pla);
  317. MENU_ITEM(function, MSG_PREHEAT_ABS, lcd_preheat_abs);
  318. MENU_ITEM(function, MSG_COOLDOWN, lcd_cooldown);
  319. #if PS_ON_PIN > -1
  320. if (powersupply)
  321. {
  322. MENU_ITEM(gcode, MSG_SWITCH_PS_OFF, PSTR("M81"));
  323. }else{
  324. MENU_ITEM(gcode, MSG_SWITCH_PS_ON, PSTR("M80"));
  325. }
  326. #endif
  327. MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu);
  328. END_MENU();
  329. }
  330. float move_menu_scale;
  331. static void lcd_move_menu_axis();
  332. static void lcd_move_x()
  333. {
  334. if (encoderPosition != 0)
  335. {
  336. current_position[X_AXIS] += float((int)encoderPosition) * move_menu_scale;
  337. if (min_software_endstops && current_position[X_AXIS] < X_MIN_POS)
  338. current_position[X_AXIS] = X_MIN_POS;
  339. if (max_software_endstops && current_position[X_AXIS] > X_MAX_POS)
  340. current_position[X_AXIS] = X_MAX_POS;
  341. encoderPosition = 0;
  342. #ifdef DELTA
  343. calculate_delta(current_position);
  344. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], 600, active_extruder);
  345. #else
  346. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600, active_extruder);
  347. #endif
  348. lcdDrawUpdate = 1;
  349. }
  350. if (lcdDrawUpdate)
  351. {
  352. lcd_implementation_drawedit(PSTR("X"), ftostr31(current_position[X_AXIS]));
  353. }
  354. if (LCD_CLICKED)
  355. {
  356. lcd_quick_feedback();
  357. currentMenu = lcd_move_menu_axis;
  358. encoderPosition = 0;
  359. }
  360. }
  361. static void lcd_move_y()
  362. {
  363. if (encoderPosition != 0)
  364. {
  365. current_position[Y_AXIS] += float((int)encoderPosition) * move_menu_scale;
  366. if (min_software_endstops && current_position[Y_AXIS] < Y_MIN_POS)
  367. current_position[Y_AXIS] = Y_MIN_POS;
  368. if (max_software_endstops && current_position[Y_AXIS] > Y_MAX_POS)
  369. current_position[Y_AXIS] = Y_MAX_POS;
  370. encoderPosition = 0;
  371. #ifdef DELTA
  372. calculate_delta(current_position);
  373. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], 600, active_extruder);
  374. #else
  375. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600, active_extruder);
  376. #endif
  377. lcdDrawUpdate = 1;
  378. }
  379. if (lcdDrawUpdate)
  380. {
  381. lcd_implementation_drawedit(PSTR("Y"), ftostr31(current_position[Y_AXIS]));
  382. }
  383. if (LCD_CLICKED)
  384. {
  385. lcd_quick_feedback();
  386. currentMenu = lcd_move_menu_axis;
  387. encoderPosition = 0;
  388. }
  389. }
  390. static void lcd_move_z()
  391. {
  392. if (encoderPosition != 0)
  393. {
  394. current_position[Z_AXIS] += float((int)encoderPosition) * move_menu_scale;
  395. if (min_software_endstops && current_position[Z_AXIS] < Z_MIN_POS)
  396. current_position[Z_AXIS] = Z_MIN_POS;
  397. if (max_software_endstops && current_position[Z_AXIS] > Z_MAX_POS)
  398. current_position[Z_AXIS] = Z_MAX_POS;
  399. encoderPosition = 0;
  400. #ifdef DELTA
  401. calculate_delta(current_position);
  402. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  403. #else
  404. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder);
  405. #endif
  406. lcdDrawUpdate = 1;
  407. }
  408. if (lcdDrawUpdate)
  409. {
  410. lcd_implementation_drawedit(PSTR("Z"), ftostr31(current_position[Z_AXIS]));
  411. }
  412. if (LCD_CLICKED)
  413. {
  414. lcd_quick_feedback();
  415. currentMenu = lcd_move_menu_axis;
  416. encoderPosition = 0;
  417. }
  418. }
  419. static void lcd_move_e()
  420. {
  421. if (encoderPosition != 0)
  422. {
  423. current_position[E_AXIS] += float((int)encoderPosition) * move_menu_scale;
  424. encoderPosition = 0;
  425. #ifdef DELTA
  426. calculate_delta(current_position);
  427. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], 20, active_extruder);
  428. #else
  429. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 20, active_extruder);
  430. #endif
  431. lcdDrawUpdate = 1;
  432. }
  433. if (lcdDrawUpdate)
  434. {
  435. lcd_implementation_drawedit(PSTR("Extruder"), ftostr31(current_position[E_AXIS]));
  436. }
  437. if (LCD_CLICKED)
  438. {
  439. lcd_quick_feedback();
  440. currentMenu = lcd_move_menu_axis;
  441. encoderPosition = 0;
  442. }
  443. }
  444. static void lcd_move_menu_axis()
  445. {
  446. START_MENU();
  447. MENU_ITEM(back, MSG_MOVE_AXIS, lcd_move_menu);
  448. MENU_ITEM(submenu, "Move X", lcd_move_x);
  449. MENU_ITEM(submenu, "Move Y", lcd_move_y);
  450. if (move_menu_scale < 10.0)
  451. {
  452. MENU_ITEM(submenu, "Move Z", lcd_move_z);
  453. MENU_ITEM(submenu, "Extruder", lcd_move_e);
  454. }
  455. END_MENU();
  456. }
  457. static void lcd_move_menu_10mm()
  458. {
  459. move_menu_scale = 10.0;
  460. lcd_move_menu_axis();
  461. }
  462. static void lcd_move_menu_1mm()
  463. {
  464. move_menu_scale = 1.0;
  465. lcd_move_menu_axis();
  466. }
  467. static void lcd_move_menu_01mm()
  468. {
  469. move_menu_scale = 0.1;
  470. lcd_move_menu_axis();
  471. }
  472. static void lcd_move_menu()
  473. {
  474. START_MENU();
  475. MENU_ITEM(back, MSG_PREPARE, lcd_prepare_menu);
  476. MENU_ITEM(submenu, "Move 10mm", lcd_move_menu_10mm);
  477. MENU_ITEM(submenu, "Move 1mm", lcd_move_menu_1mm);
  478. MENU_ITEM(submenu, "Move 0.1mm", lcd_move_menu_01mm);
  479. //TODO:X,Y,Z,E
  480. END_MENU();
  481. }
  482. static void lcd_control_menu()
  483. {
  484. START_MENU();
  485. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  486. MENU_ITEM(submenu, MSG_TEMPERATURE, lcd_control_temperature_menu);
  487. MENU_ITEM(submenu, MSG_MOTION, lcd_control_motion_menu);
  488. #ifdef DOGLCD
  489. // MENU_ITEM_EDIT(int3, MSG_CONTRAST, &lcd_contrast, 0, 63);
  490. MENU_ITEM(submenu, MSG_CONTRAST, lcd_set_contrast);
  491. #endif
  492. #ifdef FWRETRACT
  493. MENU_ITEM(submenu, MSG_RETRACT, lcd_control_retract_menu);
  494. #endif
  495. #ifdef EEPROM_SETTINGS
  496. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  497. MENU_ITEM(function, MSG_LOAD_EPROM, Config_RetrieveSettings);
  498. #endif
  499. MENU_ITEM(function, MSG_RESTORE_FAILSAFE, Config_ResetDefault);
  500. END_MENU();
  501. }
  502. static void lcd_control_temperature_menu()
  503. {
  504. #ifdef PIDTEMP
  505. // set up temp variables - undo the default scaling
  506. raw_Ki = unscalePID_i(Ki);
  507. raw_Kd = unscalePID_d(Kd);
  508. #endif
  509. START_MENU();
  510. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  511. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 15);
  512. #if TEMP_SENSOR_1 != 0
  513. MENU_ITEM_EDIT(int3, MSG_NOZZLE1, &target_temperature[1], 0, HEATER_1_MAXTEMP - 15);
  514. #endif
  515. #if TEMP_SENSOR_2 != 0
  516. MENU_ITEM_EDIT(int3, MSG_NOZZLE2, &target_temperature[2], 0, HEATER_2_MAXTEMP - 15);
  517. #endif
  518. #if TEMP_SENSOR_BED != 0
  519. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 15);
  520. #endif
  521. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);
  522. #ifdef AUTOTEMP
  523. MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &autotemp_enabled);
  524. MENU_ITEM_EDIT(float3, MSG_MIN, &autotemp_min, 0, HEATER_0_MAXTEMP - 15);
  525. MENU_ITEM_EDIT(float3, MSG_MAX, &autotemp_max, 0, HEATER_0_MAXTEMP - 15);
  526. MENU_ITEM_EDIT(float32, MSG_FACTOR, &autotemp_factor, 0.0, 1.0);
  527. #endif
  528. #ifdef PIDTEMP
  529. MENU_ITEM_EDIT(float52, MSG_PID_P, &Kp, 1, 9990);
  530. // i is typically a small value so allows values below 1
  531. MENU_ITEM_EDIT_CALLBACK(float52, MSG_PID_I, &raw_Ki, 0.01, 9990, copy_and_scalePID_i);
  532. MENU_ITEM_EDIT_CALLBACK(float52, MSG_PID_D, &raw_Kd, 1, 9990, copy_and_scalePID_d);
  533. # ifdef PID_ADD_EXTRUSION_RATE
  534. MENU_ITEM_EDIT(float3, MSG_PID_C, &Kc, 1, 9990);
  535. # endif//PID_ADD_EXTRUSION_RATE
  536. #endif//PIDTEMP
  537. MENU_ITEM(submenu, MSG_PREHEAT_PLA_SETTINGS, lcd_control_temperature_preheat_pla_settings_menu);
  538. MENU_ITEM(submenu, MSG_PREHEAT_ABS_SETTINGS, lcd_control_temperature_preheat_abs_settings_menu);
  539. END_MENU();
  540. }
  541. static void lcd_control_temperature_preheat_pla_settings_menu()
  542. {
  543. START_MENU();
  544. MENU_ITEM(back, MSG_TEMPERATURE, lcd_control_temperature_menu);
  545. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &plaPreheatFanSpeed, 0, 255);
  546. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &plaPreheatHotendTemp, 0, HEATER_0_MAXTEMP - 15);
  547. #if TEMP_SENSOR_BED != 0
  548. MENU_ITEM_EDIT(int3, MSG_BED, &plaPreheatHPBTemp, 0, BED_MAXTEMP - 15);
  549. #endif
  550. #ifdef EEPROM_SETTINGS
  551. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  552. #endif
  553. END_MENU();
  554. }
  555. static void lcd_control_temperature_preheat_abs_settings_menu()
  556. {
  557. START_MENU();
  558. MENU_ITEM(back, MSG_TEMPERATURE, lcd_control_temperature_menu);
  559. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &absPreheatFanSpeed, 0, 255);
  560. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &absPreheatHotendTemp, 0, HEATER_0_MAXTEMP - 15);
  561. #if TEMP_SENSOR_BED != 0
  562. MENU_ITEM_EDIT(int3, MSG_BED, &absPreheatHPBTemp, 0, BED_MAXTEMP - 15);
  563. #endif
  564. #ifdef EEPROM_SETTINGS
  565. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  566. #endif
  567. END_MENU();
  568. }
  569. static void lcd_control_motion_menu()
  570. {
  571. START_MENU();
  572. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  573. MENU_ITEM_EDIT(float5, MSG_ACC, &acceleration, 500, 99000);
  574. MENU_ITEM_EDIT(float3, MSG_VXY_JERK, &max_xy_jerk, 1, 990);
  575. MENU_ITEM_EDIT(float52, MSG_VZ_JERK, &max_z_jerk, 0.1, 990);
  576. MENU_ITEM_EDIT(float3, MSG_VE_JERK, &max_e_jerk, 1, 990);
  577. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_X, &max_feedrate[X_AXIS], 1, 999);
  578. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Y, &max_feedrate[Y_AXIS], 1, 999);
  579. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Z, &max_feedrate[Z_AXIS], 1, 999);
  580. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_E, &max_feedrate[E_AXIS], 1, 999);
  581. MENU_ITEM_EDIT(float3, MSG_VMIN, &minimumfeedrate, 0, 999);
  582. MENU_ITEM_EDIT(float3, MSG_VTRAV_MIN, &mintravelfeedrate, 0, 999);
  583. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_X, &max_acceleration_units_per_sq_second[X_AXIS], 100, 99000, reset_acceleration_rates);
  584. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Y, &max_acceleration_units_per_sq_second[Y_AXIS], 100, 99000, reset_acceleration_rates);
  585. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Z, &max_acceleration_units_per_sq_second[Z_AXIS], 100, 99000, reset_acceleration_rates);
  586. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_E, &max_acceleration_units_per_sq_second[E_AXIS], 100, 99000, reset_acceleration_rates);
  587. MENU_ITEM_EDIT(float5, MSG_A_RETRACT, &retract_acceleration, 100, 99000);
  588. MENU_ITEM_EDIT(float52, MSG_XSTEPS, &axis_steps_per_unit[X_AXIS], 5, 9999);
  589. MENU_ITEM_EDIT(float52, MSG_YSTEPS, &axis_steps_per_unit[Y_AXIS], 5, 9999);
  590. MENU_ITEM_EDIT(float51, MSG_ZSTEPS, &axis_steps_per_unit[Z_AXIS], 5, 9999);
  591. MENU_ITEM_EDIT(float51, MSG_ESTEPS, &axis_steps_per_unit[E_AXIS], 5, 9999);
  592. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  593. MENU_ITEM_EDIT(bool, "Endstop abort", &abort_on_endstop_hit);
  594. #endif
  595. END_MENU();
  596. }
  597. #ifdef DOGLCD
  598. static void lcd_set_contrast()
  599. {
  600. if (encoderPosition != 0)
  601. {
  602. lcd_contrast -= encoderPosition;
  603. if (lcd_contrast < 0) lcd_contrast = 0;
  604. else if (lcd_contrast > 63) lcd_contrast = 63;
  605. encoderPosition = 0;
  606. lcdDrawUpdate = 1;
  607. u8g.setContrast(lcd_contrast);
  608. }
  609. if (lcdDrawUpdate)
  610. {
  611. lcd_implementation_drawedit(PSTR("Contrast"), itostr2(lcd_contrast));
  612. }
  613. if (LCD_CLICKED)
  614. {
  615. lcd_quick_feedback();
  616. currentMenu = lcd_control_menu;
  617. encoderPosition = 0;
  618. }
  619. }
  620. #endif
  621. #ifdef FWRETRACT
  622. static void lcd_control_retract_menu()
  623. {
  624. START_MENU();
  625. MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  626. MENU_ITEM_EDIT(bool, MSG_AUTORETRACT, &autoretract_enabled);
  627. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT, &retract_length, 0, 100);
  628. MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACTF, &retract_feedrate, 1, 999);
  629. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_ZLIFT, &retract_zlift, 0, 999);
  630. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_RECOVER, &retract_recover_length, 0, 100);
  631. MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACT_RECOVERF, &retract_recover_feedrate, 1, 999);
  632. END_MENU();
  633. }
  634. #endif
  635. #if SDCARDDETECT == -1
  636. static void lcd_sd_refresh()
  637. {
  638. card.initsd();
  639. currentMenuViewOffset = 0;
  640. }
  641. #endif
  642. static void lcd_sd_updir()
  643. {
  644. card.updir();
  645. currentMenuViewOffset = 0;
  646. }
  647. void lcd_sdcard_menu()
  648. {
  649. uint16_t fileCnt = card.getnrfilenames();
  650. START_MENU();
  651. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  652. card.getWorkDirName();
  653. if(card.filename[0]=='/')
  654. {
  655. #if SDCARDDETECT == -1
  656. MENU_ITEM(function, LCD_STR_REFRESH MSG_REFRESH, lcd_sd_refresh);
  657. #endif
  658. }else{
  659. MENU_ITEM(function, LCD_STR_FOLDER "..", lcd_sd_updir);
  660. }
  661. for(uint16_t i=0;i<fileCnt;i++)
  662. {
  663. if (_menuItemNr == _lineNr)
  664. {
  665. card.getfilename(i);
  666. if (card.filenameIsDir)
  667. {
  668. MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
  669. }else{
  670. MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
  671. }
  672. }else{
  673. MENU_ITEM_DUMMY();
  674. }
  675. }
  676. END_MENU();
  677. }
  678. #define menu_edit_type(_type, _name, _strFunc, scale) \
  679. void menu_edit_ ## _name () \
  680. { \
  681. if ((int32_t)encoderPosition < minEditValue) \
  682. encoderPosition = minEditValue; \
  683. if ((int32_t)encoderPosition > maxEditValue) \
  684. encoderPosition = maxEditValue; \
  685. if (lcdDrawUpdate) \
  686. lcd_implementation_drawedit(editLabel, _strFunc(((_type)encoderPosition) / scale)); \
  687. if (LCD_CLICKED) \
  688. { \
  689. *((_type*)editValue) = ((_type)encoderPosition) / scale; \
  690. lcd_quick_feedback(); \
  691. currentMenu = prevMenu; \
  692. encoderPosition = prevEncoderPosition; \
  693. } \
  694. } \
  695. void menu_edit_callback_ ## _name () \
  696. { \
  697. if ((int32_t)encoderPosition < minEditValue) \
  698. encoderPosition = minEditValue; \
  699. if ((int32_t)encoderPosition > maxEditValue) \
  700. encoderPosition = maxEditValue; \
  701. if (lcdDrawUpdate) \
  702. lcd_implementation_drawedit(editLabel, _strFunc(((_type)encoderPosition) / scale)); \
  703. if (LCD_CLICKED) \
  704. { \
  705. *((_type*)editValue) = ((_type)encoderPosition) / scale; \
  706. lcd_quick_feedback(); \
  707. currentMenu = prevMenu; \
  708. encoderPosition = prevEncoderPosition; \
  709. (*callbackFunc)();\
  710. } \
  711. } \
  712. static void menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) \
  713. { \
  714. prevMenu = currentMenu; \
  715. prevEncoderPosition = encoderPosition; \
  716. \
  717. lcdDrawUpdate = 2; \
  718. currentMenu = menu_edit_ ## _name; \
  719. \
  720. editLabel = pstr; \
  721. editValue = ptr; \
  722. minEditValue = minValue * scale; \
  723. maxEditValue = maxValue * scale; \
  724. encoderPosition = (*ptr) * scale; \
  725. }\
  726. static void menu_action_setting_edit_callback_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue, menuFunc_t callback) \
  727. { \
  728. prevMenu = currentMenu; \
  729. prevEncoderPosition = encoderPosition; \
  730. \
  731. lcdDrawUpdate = 2; \
  732. currentMenu = menu_edit_callback_ ## _name; \
  733. \
  734. editLabel = pstr; \
  735. editValue = ptr; \
  736. minEditValue = minValue * scale; \
  737. maxEditValue = maxValue * scale; \
  738. encoderPosition = (*ptr) * scale; \
  739. callbackFunc = callback;\
  740. }
  741. menu_edit_type(int, int3, itostr3, 1)
  742. menu_edit_type(float, float3, ftostr3, 1)
  743. menu_edit_type(float, float32, ftostr32, 100)
  744. menu_edit_type(float, float5, ftostr5, 0.01)
  745. menu_edit_type(float, float51, ftostr51, 10)
  746. menu_edit_type(float, float52, ftostr52, 100)
  747. menu_edit_type(unsigned long, long5, ftostr5, 0.01)
  748. #ifdef REPRAPWORLD_KEYPAD
  749. static void reprapworld_keypad_move_z_up() {
  750. encoderPosition = 1;
  751. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  752. lcd_move_z();
  753. }
  754. static void reprapworld_keypad_move_z_down() {
  755. encoderPosition = -1;
  756. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  757. lcd_move_z();
  758. }
  759. static void reprapworld_keypad_move_x_left() {
  760. encoderPosition = -1;
  761. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  762. lcd_move_x();
  763. }
  764. static void reprapworld_keypad_move_x_right() {
  765. encoderPosition = 1;
  766. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  767. lcd_move_x();
  768. }
  769. static void reprapworld_keypad_move_y_down() {
  770. encoderPosition = 1;
  771. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  772. lcd_move_y();
  773. }
  774. static void reprapworld_keypad_move_y_up() {
  775. encoderPosition = -1;
  776. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  777. lcd_move_y();
  778. }
  779. static void reprapworld_keypad_move_home() {
  780. enquecommand_P((PSTR("G28"))); // move all axis home
  781. }
  782. #endif
  783. /** End of menus **/
  784. static void lcd_quick_feedback()
  785. {
  786. lcdDrawUpdate = 2;
  787. blocking_enc = millis() + 500;
  788. lcd_implementation_quick_feedback();
  789. }
  790. /** Menu action functions **/
  791. static void menu_action_back(menuFunc_t data)
  792. {
  793. currentMenu = data;
  794. encoderPosition = 0;
  795. }
  796. static void menu_action_submenu(menuFunc_t data)
  797. {
  798. currentMenu = data;
  799. encoderPosition = 0;
  800. }
  801. static void menu_action_gcode(const char* pgcode)
  802. {
  803. enquecommand_P(pgcode);
  804. }
  805. static void menu_action_function(menuFunc_t data)
  806. {
  807. (*data)();
  808. }
  809. static void menu_action_sdfile(const char* filename, char* longFilename)
  810. {
  811. char cmd[30];
  812. char* c;
  813. sprintf_P(cmd, PSTR("M23 %s"), filename);
  814. for(c = &cmd[4]; *c; c++)
  815. *c = tolower(*c);
  816. enquecommand(cmd);
  817. enquecommand_P(PSTR("M24"));
  818. lcd_return_to_status();
  819. }
  820. static void menu_action_sddirectory(const char* filename, char* longFilename)
  821. {
  822. card.chdir(filename);
  823. encoderPosition = 0;
  824. }
  825. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr)
  826. {
  827. *ptr = !(*ptr);
  828. }
  829. #endif//ULTIPANEL
  830. /** LCD API **/
  831. void lcd_init()
  832. {
  833. lcd_implementation_init();
  834. #ifdef NEWPANEL
  835. pinMode(BTN_EN1,INPUT);
  836. pinMode(BTN_EN2,INPUT);
  837. pinMode(SDCARDDETECT,INPUT);
  838. WRITE(BTN_EN1,HIGH);
  839. WRITE(BTN_EN2,HIGH);
  840. #if BTN_ENC > 0
  841. pinMode(BTN_ENC,INPUT);
  842. WRITE(BTN_ENC,HIGH);
  843. #endif
  844. #ifdef REPRAPWORLD_KEYPAD
  845. pinMode(SHIFT_CLK,OUTPUT);
  846. pinMode(SHIFT_LD,OUTPUT);
  847. pinMode(SHIFT_OUT,INPUT);
  848. WRITE(SHIFT_OUT,HIGH);
  849. WRITE(SHIFT_LD,HIGH);
  850. #endif
  851. #else
  852. pinMode(SHIFT_CLK,OUTPUT);
  853. pinMode(SHIFT_LD,OUTPUT);
  854. pinMode(SHIFT_EN,OUTPUT);
  855. pinMode(SHIFT_OUT,INPUT);
  856. WRITE(SHIFT_OUT,HIGH);
  857. WRITE(SHIFT_LD,HIGH);
  858. WRITE(SHIFT_EN,LOW);
  859. #endif//!NEWPANEL
  860. #if (SDCARDDETECT > 0)
  861. WRITE(SDCARDDETECT, HIGH);
  862. lcd_oldcardstatus = IS_SD_INSERTED;
  863. #endif//(SDCARDDETECT > 0)
  864. lcd_buttons_update();
  865. #ifdef ULTIPANEL
  866. encoderDiff = 0;
  867. #endif
  868. }
  869. void lcd_update()
  870. {
  871. static unsigned long timeoutToStatus = 0;
  872. lcd_buttons_update();
  873. #ifdef LCD_HAS_SLOW_BUTTONS
  874. buttons |= lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  875. #endif
  876. #if (SDCARDDETECT > 0)
  877. if((IS_SD_INSERTED != lcd_oldcardstatus))
  878. {
  879. lcdDrawUpdate = 2;
  880. lcd_oldcardstatus = IS_SD_INSERTED;
  881. lcd_implementation_init(); // to maybe revive the lcd if static electricty killed it.
  882. if(lcd_oldcardstatus)
  883. {
  884. card.initsd();
  885. LCD_MESSAGEPGM(MSG_SD_INSERTED);
  886. }
  887. else
  888. {
  889. card.release();
  890. LCD_MESSAGEPGM(MSG_SD_REMOVED);
  891. }
  892. }
  893. #endif//CARDINSERTED
  894. if (lcd_next_update_millis < millis())
  895. {
  896. #ifdef ULTIPANEL
  897. #ifdef REPRAPWORLD_KEYPAD
  898. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) {
  899. reprapworld_keypad_move_z_up();
  900. }
  901. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) {
  902. reprapworld_keypad_move_z_down();
  903. }
  904. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) {
  905. reprapworld_keypad_move_x_left();
  906. }
  907. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) {
  908. reprapworld_keypad_move_x_right();
  909. }
  910. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) {
  911. reprapworld_keypad_move_y_down();
  912. }
  913. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) {
  914. reprapworld_keypad_move_y_up();
  915. }
  916. if (REPRAPWORLD_KEYPAD_MOVE_HOME) {
  917. reprapworld_keypad_move_home();
  918. }
  919. #endif
  920. if (encoderDiff)
  921. {
  922. lcdDrawUpdate = 1;
  923. encoderPosition += encoderDiff;
  924. encoderDiff = 0;
  925. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  926. }
  927. if (LCD_CLICKED)
  928. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  929. #endif//ULTIPANEL
  930. #ifdef DOGLCD // Changes due to different driver architecture of the DOGM display
  931. blink++; // Variable for fan animation and alive dot
  932. u8g.firstPage();
  933. do
  934. {
  935. u8g.setFont(u8g_font_6x10_marlin);
  936. u8g.setPrintPos(125,0);
  937. if (blink % 2) u8g.setColorIndex(1); else u8g.setColorIndex(0); // Set color for the alive dot
  938. u8g.drawPixel(127,63); // draw alive dot
  939. u8g.setColorIndex(1); // black on white
  940. (*currentMenu)();
  941. if (!lcdDrawUpdate) break; // Terminate display update, when nothing new to draw. This must be done before the last dogm.next()
  942. } while( u8g.nextPage() );
  943. #else
  944. (*currentMenu)();
  945. #endif
  946. #ifdef LCD_HAS_STATUS_INDICATORS
  947. lcd_implementation_update_indicators();
  948. #endif
  949. #ifdef ULTIPANEL
  950. if(timeoutToStatus < millis() && currentMenu != lcd_status_screen)
  951. {
  952. lcd_return_to_status();
  953. lcdDrawUpdate = 2;
  954. }
  955. #endif//ULTIPANEL
  956. if (lcdDrawUpdate == 2)
  957. lcd_implementation_clear();
  958. if (lcdDrawUpdate)
  959. lcdDrawUpdate--;
  960. lcd_next_update_millis = millis() + 100;
  961. }
  962. }
  963. void lcd_setstatus(const char* message)
  964. {
  965. if (lcd_status_message_level > 0)
  966. return;
  967. strncpy(lcd_status_message, message, LCD_WIDTH);
  968. lcdDrawUpdate = 2;
  969. }
  970. void lcd_setstatuspgm(const char* message)
  971. {
  972. if (lcd_status_message_level > 0)
  973. return;
  974. strncpy_P(lcd_status_message, message, LCD_WIDTH);
  975. lcdDrawUpdate = 2;
  976. }
  977. void lcd_setalertstatuspgm(const char* message)
  978. {
  979. lcd_setstatuspgm(message);
  980. lcd_status_message_level = 1;
  981. #ifdef ULTIPANEL
  982. lcd_return_to_status();
  983. #endif//ULTIPANEL
  984. }
  985. void lcd_reset_alert_level()
  986. {
  987. lcd_status_message_level = 0;
  988. }
  989. #ifdef DOGLCD
  990. void lcd_setcontrast(uint8_t value)
  991. {
  992. lcd_contrast = value & 63;
  993. u8g.setContrast(lcd_contrast);
  994. }
  995. #endif
  996. #ifdef ULTIPANEL
  997. /* Warning: This function is called from interrupt context */
  998. void lcd_buttons_update()
  999. {
  1000. #ifdef NEWPANEL
  1001. uint8_t newbutton=0;
  1002. if(READ(BTN_EN1)==0) newbutton|=EN_A;
  1003. if(READ(BTN_EN2)==0) newbutton|=EN_B;
  1004. #if BTN_ENC > 0
  1005. if((blocking_enc<millis()) && (READ(BTN_ENC)==0))
  1006. newbutton |= EN_C;
  1007. #endif
  1008. buttons = newbutton;
  1009. #ifdef REPRAPWORLD_KEYPAD
  1010. // for the reprapworld_keypad
  1011. uint8_t newbutton_reprapworld_keypad=0;
  1012. WRITE(SHIFT_LD,LOW);
  1013. WRITE(SHIFT_LD,HIGH);
  1014. for(int8_t i=0;i<8;i++) {
  1015. newbutton_reprapworld_keypad = newbutton_reprapworld_keypad>>1;
  1016. if(READ(SHIFT_OUT))
  1017. newbutton_reprapworld_keypad|=(1<<7);
  1018. WRITE(SHIFT_CLK,HIGH);
  1019. WRITE(SHIFT_CLK,LOW);
  1020. }
  1021. buttons_reprapworld_keypad=~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
  1022. #endif
  1023. #else //read it from the shift register
  1024. uint8_t newbutton=0;
  1025. WRITE(SHIFT_LD,LOW);
  1026. WRITE(SHIFT_LD,HIGH);
  1027. unsigned char tmp_buttons=0;
  1028. for(int8_t i=0;i<8;i++)
  1029. {
  1030. newbutton = newbutton>>1;
  1031. if(READ(SHIFT_OUT))
  1032. newbutton|=(1<<7);
  1033. WRITE(SHIFT_CLK,HIGH);
  1034. WRITE(SHIFT_CLK,LOW);
  1035. }
  1036. buttons=~newbutton; //invert it, because a pressed switch produces a logical 0
  1037. #endif//!NEWPANEL
  1038. //manage encoder rotation
  1039. uint8_t enc=0;
  1040. if(buttons&EN_A)
  1041. enc|=(1<<0);
  1042. if(buttons&EN_B)
  1043. enc|=(1<<1);
  1044. if(enc != lastEncoderBits)
  1045. {
  1046. switch(enc)
  1047. {
  1048. case encrot0:
  1049. if(lastEncoderBits==encrot3)
  1050. encoderDiff++;
  1051. else if(lastEncoderBits==encrot1)
  1052. encoderDiff--;
  1053. break;
  1054. case encrot1:
  1055. if(lastEncoderBits==encrot0)
  1056. encoderDiff++;
  1057. else if(lastEncoderBits==encrot2)
  1058. encoderDiff--;
  1059. break;
  1060. case encrot2:
  1061. if(lastEncoderBits==encrot1)
  1062. encoderDiff++;
  1063. else if(lastEncoderBits==encrot3)
  1064. encoderDiff--;
  1065. break;
  1066. case encrot3:
  1067. if(lastEncoderBits==encrot2)
  1068. encoderDiff++;
  1069. else if(lastEncoderBits==encrot0)
  1070. encoderDiff--;
  1071. break;
  1072. }
  1073. }
  1074. lastEncoderBits = enc;
  1075. }
  1076. void lcd_buzz(long duration, uint16_t freq)
  1077. {
  1078. #ifdef LCD_USE_I2C_BUZZER
  1079. lcd.buzz(duration,freq);
  1080. #endif
  1081. }
  1082. bool lcd_clicked()
  1083. {
  1084. return LCD_CLICKED;
  1085. }
  1086. #endif//ULTIPANEL
  1087. /********************************/
  1088. /** Float conversion utilities **/
  1089. /********************************/
  1090. // convert float to string with +123.4 format
  1091. char conv[8];
  1092. char *ftostr3(const float &x)
  1093. {
  1094. return itostr3((int)x);
  1095. }
  1096. char *itostr2(const uint8_t &x)
  1097. {
  1098. //sprintf(conv,"%5.1f",x);
  1099. int xx=x;
  1100. conv[0]=(xx/10)%10+'0';
  1101. conv[1]=(xx)%10+'0';
  1102. conv[2]=0;
  1103. return conv;
  1104. }
  1105. // convert float to string with +123.4 format
  1106. char *ftostr31(const float &x)
  1107. {
  1108. int xx=x*10;
  1109. conv[0]=(xx>=0)?'+':'-';
  1110. xx=abs(xx);
  1111. conv[1]=(xx/1000)%10+'0';
  1112. conv[2]=(xx/100)%10+'0';
  1113. conv[3]=(xx/10)%10+'0';
  1114. conv[4]='.';
  1115. conv[5]=(xx)%10+'0';
  1116. conv[6]=0;
  1117. return conv;
  1118. }
  1119. // convert float to string with 123.4 format
  1120. char *ftostr31ns(const float &x)
  1121. {
  1122. int xx=x*10;
  1123. //conv[0]=(xx>=0)?'+':'-';
  1124. xx=abs(xx);
  1125. conv[0]=(xx/1000)%10+'0';
  1126. conv[1]=(xx/100)%10+'0';
  1127. conv[2]=(xx/10)%10+'0';
  1128. conv[3]='.';
  1129. conv[4]=(xx)%10+'0';
  1130. conv[5]=0;
  1131. return conv;
  1132. }
  1133. char *ftostr32(const float &x)
  1134. {
  1135. long xx=x*100;
  1136. if (xx >= 0)
  1137. conv[0]=(xx/10000)%10+'0';
  1138. else
  1139. conv[0]='-';
  1140. xx=abs(xx);
  1141. conv[1]=(xx/1000)%10+'0';
  1142. conv[2]=(xx/100)%10+'0';
  1143. conv[3]='.';
  1144. conv[4]=(xx/10)%10+'0';
  1145. conv[5]=(xx)%10+'0';
  1146. conv[6]=0;
  1147. return conv;
  1148. }
  1149. char *itostr31(const int &xx)
  1150. {
  1151. conv[0]=(xx>=0)?'+':'-';
  1152. conv[1]=(xx/1000)%10+'0';
  1153. conv[2]=(xx/100)%10+'0';
  1154. conv[3]=(xx/10)%10+'0';
  1155. conv[4]='.';
  1156. conv[5]=(xx)%10+'0';
  1157. conv[6]=0;
  1158. return conv;
  1159. }
  1160. char *itostr3(const int &xx)
  1161. {
  1162. if (xx >= 100)
  1163. conv[0]=(xx/100)%10+'0';
  1164. else
  1165. conv[0]=' ';
  1166. if (xx >= 10)
  1167. conv[1]=(xx/10)%10+'0';
  1168. else
  1169. conv[1]=' ';
  1170. conv[2]=(xx)%10+'0';
  1171. conv[3]=0;
  1172. return conv;
  1173. }
  1174. char *itostr3left(const int &xx)
  1175. {
  1176. if (xx >= 100)
  1177. {
  1178. conv[0]=(xx/100)%10+'0';
  1179. conv[1]=(xx/10)%10+'0';
  1180. conv[2]=(xx)%10+'0';
  1181. conv[3]=0;
  1182. }
  1183. else if (xx >= 10)
  1184. {
  1185. conv[0]=(xx/10)%10+'0';
  1186. conv[1]=(xx)%10+'0';
  1187. conv[2]=0;
  1188. }
  1189. else
  1190. {
  1191. conv[0]=(xx)%10+'0';
  1192. conv[1]=0;
  1193. }
  1194. return conv;
  1195. }
  1196. char *itostr4(const int &xx)
  1197. {
  1198. if (xx >= 1000)
  1199. conv[0]=(xx/1000)%10+'0';
  1200. else
  1201. conv[0]=' ';
  1202. if (xx >= 100)
  1203. conv[1]=(xx/100)%10+'0';
  1204. else
  1205. conv[1]=' ';
  1206. if (xx >= 10)
  1207. conv[2]=(xx/10)%10+'0';
  1208. else
  1209. conv[2]=' ';
  1210. conv[3]=(xx)%10+'0';
  1211. conv[4]=0;
  1212. return conv;
  1213. }
  1214. // convert float to string with 12345 format
  1215. char *ftostr5(const float &x)
  1216. {
  1217. long xx=abs(x);
  1218. if (xx >= 10000)
  1219. conv[0]=(xx/10000)%10+'0';
  1220. else
  1221. conv[0]=' ';
  1222. if (xx >= 1000)
  1223. conv[1]=(xx/1000)%10+'0';
  1224. else
  1225. conv[1]=' ';
  1226. if (xx >= 100)
  1227. conv[2]=(xx/100)%10+'0';
  1228. else
  1229. conv[2]=' ';
  1230. if (xx >= 10)
  1231. conv[3]=(xx/10)%10+'0';
  1232. else
  1233. conv[3]=' ';
  1234. conv[4]=(xx)%10+'0';
  1235. conv[5]=0;
  1236. return conv;
  1237. }
  1238. // convert float to string with +1234.5 format
  1239. char *ftostr51(const float &x)
  1240. {
  1241. long xx=x*10;
  1242. conv[0]=(xx>=0)?'+':'-';
  1243. xx=abs(xx);
  1244. conv[1]=(xx/10000)%10+'0';
  1245. conv[2]=(xx/1000)%10+'0';
  1246. conv[3]=(xx/100)%10+'0';
  1247. conv[4]=(xx/10)%10+'0';
  1248. conv[5]='.';
  1249. conv[6]=(xx)%10+'0';
  1250. conv[7]=0;
  1251. return conv;
  1252. }
  1253. // convert float to string with +123.45 format
  1254. char *ftostr52(const float &x)
  1255. {
  1256. long xx=x*100;
  1257. conv[0]=(xx>=0)?'+':'-';
  1258. xx=abs(xx);
  1259. conv[1]=(xx/10000)%10+'0';
  1260. conv[2]=(xx/1000)%10+'0';
  1261. conv[3]=(xx/100)%10+'0';
  1262. conv[4]='.';
  1263. conv[5]=(xx/10)%10+'0';
  1264. conv[6]=(xx)%10+'0';
  1265. conv[7]=0;
  1266. return conv;
  1267. }
  1268. // Callback for after editing PID i value
  1269. // grab the pid i value out of the temp variable; scale it; then update the PID driver
  1270. void copy_and_scalePID_i()
  1271. {
  1272. #ifdef PIDTEMP
  1273. Ki = scalePID_i(raw_Ki);
  1274. updatePID();
  1275. #endif
  1276. }
  1277. // Callback for after editing PID d value
  1278. // grab the pid d value out of the temp variable; scale it; then update the PID driver
  1279. void copy_and_scalePID_d()
  1280. {
  1281. #ifdef PIDTEMP
  1282. Kd = scalePID_d(raw_Kd);
  1283. updatePID();
  1284. #endif
  1285. }
  1286. #endif //ULTRA_LCD