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

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