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 41KB

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