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

ultralcd.cpp 38KB

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