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.

malyan_lcd.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * malyan_lcd.cpp
  24. *
  25. * LCD implementation for Malyan's LCD, a separate ESP8266 MCU running
  26. * on Serial1 for the M200 board. This module outputs a pseudo-gcode
  27. * wrapped in curly braces which the LCD implementation translates into
  28. * actual G-code commands.
  29. *
  30. * Added to Marlin for Mini/Malyan M200
  31. * Unknown commands as of Jan 2018: {H:}
  32. * Not currently implemented:
  33. * {E:} when sent by LCD. Meaning unknown.
  34. *
  35. * Notes for connecting to boards that are not Malyan:
  36. * The LCD is 3.3v, so if powering from a RAMPS 1.4 board or
  37. * other 5v/12v board, use a buck converter to power the LCD and
  38. * the 3.3v side of a logic level shifter. Aux1 on the RAMPS board
  39. * has Serial1 and 12v, making it perfect for this.
  40. * Copyright (c) 2017 Jason Nelson (xC0000005)
  41. */
  42. #include "../../inc/MarlinConfigPre.h"
  43. #if ENABLED(MALYAN_LCD)
  44. //#define DEBUG_MALYAN_LCD
  45. #include "ui_api.h"
  46. #include "../marlinui.h"
  47. #include "../../sd/cardreader.h"
  48. #include "../../module/temperature.h"
  49. #include "../../module/stepper.h"
  50. #include "../../module/motion.h"
  51. #include "../../libs/duration_t.h"
  52. #include "../../module/printcounter.h"
  53. #include "../../gcode/queue.h"
  54. #define DEBUG_OUT ENABLED(DEBUG_MALYAN_LCD)
  55. #include "../../core/debug_out.h"
  56. // This is based on longest sys command + a filename, plus some buffer
  57. // in case we encounter some data we don't recognize
  58. // There is no evidence a line will ever be this long, but better safe than sorry
  59. #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
  60. // Track incoming command bytes from the LCD
  61. uint16_t inbound_count;
  62. // For sending print completion messages
  63. bool last_printing_status = false;
  64. // Everything written needs the high bit set.
  65. void write_to_lcd_P(PGM_P const message) {
  66. char encoded_message[MAX_CURLY_COMMAND];
  67. uint8_t message_length = _MIN(strlen_P(message), sizeof(encoded_message));
  68. LOOP_L_N(i, message_length)
  69. encoded_message[i] = pgm_read_byte(&message[i]) | 0x80;
  70. LCD_SERIAL.Print::write(encoded_message, message_length);
  71. }
  72. void write_to_lcd(const char * const message) {
  73. char encoded_message[MAX_CURLY_COMMAND];
  74. const uint8_t message_length = _MIN(strlen(message), sizeof(encoded_message));
  75. LOOP_L_N(i, message_length)
  76. encoded_message[i] = message[i] | 0x80;
  77. LCD_SERIAL.Print::write(encoded_message, message_length);
  78. }
  79. // {E:<msg>} is for error states.
  80. void set_lcd_error_P(PGM_P const error, PGM_P const component=nullptr) {
  81. write_to_lcd_P(PSTR("{E:"));
  82. write_to_lcd_P(error);
  83. if (component) {
  84. write_to_lcd_P(PSTR(" "));
  85. write_to_lcd_P(component);
  86. }
  87. write_to_lcd_P(PSTR("}"));
  88. }
  89. /**
  90. * Process an LCD 'C' command.
  91. * These are currently all temperature commands
  92. * {C:T0190}
  93. * Set temp for hotend to 190
  94. * {C:P050}
  95. * Set temp for bed to 50
  96. *
  97. * {C:S09} set feedrate to 90 %.
  98. * {C:S12} set feedrate to 120 %.
  99. *
  100. * the command portion begins after the :
  101. */
  102. void process_lcd_c_command(const char* command) {
  103. const int target_val = command[1] ? atoi(command + 1) : -1;
  104. if (target_val < 0) {
  105. DEBUG_ECHOLNPAIR("UNKNOWN C COMMAND ", command);
  106. return;
  107. }
  108. switch (command[0]) {
  109. case 'C': // Cope with both V1 early rev and later LCDs.
  110. case 'S':
  111. feedrate_percentage = target_val * 10;
  112. LIMIT(feedrate_percentage, 10, 999);
  113. break;
  114. case 'T':
  115. // Sometimes the LCD will send commands to turn off both extruder and bed, though
  116. // this should not happen since the printing screen is up. Better safe than sorry.
  117. if (!print_job_timer.isRunning() || target_val > 0)
  118. ExtUI::setTargetTemp_celsius(target_val, ExtUI::extruder_t::E0);
  119. break;
  120. #if HAS_HEATED_BED
  121. case 'P': ExtUI::setTargetTemp_celsius(target_val, ExtUI::heater_t::BED); break;
  122. #endif
  123. default: DEBUG_ECHOLNPAIR("UNKNOWN C COMMAND ", command);
  124. }
  125. }
  126. /**
  127. * Process an LCD 'B' command.
  128. * {B:0} results in: {T0:008/195}{T1:000/000}{TP:000/000}{TQ:000C}{TT:000000}
  129. * T0/T1 are hot end temperatures, TP is bed, TQ is percent, and TT is probably
  130. * time remaining (HH:MM:SS). The UI can't handle displaying a second hotend,
  131. * but the stock firmware always sends it, and it's always zero.
  132. */
  133. void process_lcd_eb_command(const char* command) {
  134. char elapsed_buffer[10];
  135. static uint8_t iteration = 0;
  136. duration_t elapsed;
  137. switch (command[0]) {
  138. case '0': {
  139. elapsed = print_job_timer.duration();
  140. sprintf_P(elapsed_buffer, PSTR("%02u%02u%02u"), uint16_t(elapsed.hour()), uint16_t(elapsed.minute()) % 60, uint16_t(elapsed.second()) % 60);
  141. char message_buffer[MAX_CURLY_COMMAND];
  142. uint8_t done_pct = print_job_timer.isRunning() ? (iteration * 10) : 100;
  143. iteration = (iteration + 1) % 10; // Provide progress animation
  144. #if ENABLED(SDSUPPORT)
  145. if (ExtUI::isPrintingFromMedia() || ExtUI::isPrintingFromMediaPaused())
  146. done_pct = card.percentDone();
  147. #endif
  148. sprintf_P(message_buffer,
  149. PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}{TQ:%03i}{TT:%s}"),
  150. int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
  151. #if HAS_HEATED_BED
  152. int(thermalManager.degBed()), thermalManager.degTargetBed(),
  153. #else
  154. 0, 0,
  155. #endif
  156. #if ENABLED(SDSUPPORT)
  157. done_pct,
  158. #else
  159. 0,
  160. #endif
  161. elapsed_buffer
  162. );
  163. write_to_lcd(message_buffer);
  164. } break;
  165. default: DEBUG_ECHOLNPAIR("UNKNOWN E/B COMMAND ", command);
  166. }
  167. }
  168. /**
  169. * Process an LCD 'J' command.
  170. * These are currently all movement commands.
  171. * The command portion begins after the :
  172. * Move X Axis
  173. *
  174. * {J:E}{J:X-200}{J:E}
  175. * {J:E}{J:X+200}{J:E}
  176. * X, Y, Z, A (extruder)
  177. */
  178. template<typename T>
  179. void j_move_axis(const char* command, const T axis) {
  180. const float dist = atof(command + 1) / 10.0;
  181. ExtUI::setAxisPosition_mm(ExtUI::getAxisPosition_mm(axis) + dist, axis);
  182. };
  183. void process_lcd_j_command(const char* command) {
  184. switch (command[0]) {
  185. case 'E': break;
  186. case 'A': j_move_axis<ExtUI::extruder_t>(command, ExtUI::extruder_t::E0); break;
  187. case 'Y': j_move_axis<ExtUI::axis_t>(command, ExtUI::axis_t::Y); break;
  188. case 'Z': j_move_axis<ExtUI::axis_t>(command, ExtUI::axis_t::Z); break;
  189. case 'X': j_move_axis<ExtUI::axis_t>(command, ExtUI::axis_t::X); break;
  190. default: DEBUG_ECHOLNPAIR("UNKNOWN J COMMAND ", command);
  191. }
  192. }
  193. /**
  194. * Process an LCD 'P' command, related to homing and printing.
  195. * Cancel:
  196. * {P:X}
  197. *
  198. * Home all axes:
  199. * {P:H}
  200. *
  201. * Print a file:
  202. * {P:000}
  203. * The File number is specified as a three digit value.
  204. * Printer responds with:
  205. * {PRINTFILE:Mini_SNES_Bottom.gcode}
  206. * {SYS:BUILD}echo:Now fresh file: Mini_SNES_Bottom.gcode
  207. * File opened: Mini_SNES_Bottom.gcode Size: 5805813
  208. * File selected
  209. * {SYS:BUILD}
  210. * T:-2526.8 E:0
  211. * T:-2533.0 E:0
  212. * T:-2537.4 E:0
  213. * Note only the curly brace stuff matters.
  214. */
  215. void process_lcd_p_command(const char* command) {
  216. switch (command[0]) {
  217. case 'P':
  218. ExtUI::pausePrint();
  219. write_to_lcd_P(PSTR("{SYS:PAUSED}"));
  220. break;
  221. case 'R':
  222. ExtUI::resumePrint();
  223. write_to_lcd_P(PSTR("{SYS:RESUMED}"));
  224. break;
  225. case 'X':
  226. write_to_lcd_P(PSTR("{SYS:CANCELING}"));
  227. ExtUI::stopPrint();
  228. write_to_lcd_P(PSTR("{SYS:STARTED}"));
  229. break;
  230. case 'H': queue.enqueue_now_P(G28_STR); break; // Home all axes
  231. default: {
  232. #if ENABLED(SDSUPPORT)
  233. // Print file 000 - a three digit number indicating which
  234. // file to print in the SD card. If it's a directory,
  235. // then switch to the directory.
  236. // Find the name of the file to print.
  237. // It's needed to echo the PRINTFILE option.
  238. // The {S:L} command should've ensured the SD card was mounted.
  239. card.selectFileByIndex(atoi(command));
  240. // There may be a difference in how V1 and V2 LCDs handle subdirectory
  241. // prints. Investigate more. This matches the V1 motion controller actions
  242. // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
  243. if (card.flag.filenameIsDir) {
  244. card.cd(card.filename);
  245. write_to_lcd_P(PSTR("{SYS:DIR}"));
  246. }
  247. else {
  248. char message_buffer[MAX_CURLY_COMMAND];
  249. sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.longest_filename());
  250. write_to_lcd(message_buffer);
  251. write_to_lcd_P(PSTR("{SYS:BUILD}"));
  252. card.openAndPrintFile(card.filename);
  253. }
  254. #endif
  255. } break; // default
  256. } // switch
  257. }
  258. /**
  259. * Handle an lcd 'S' command
  260. * {S:I} - Temperature request
  261. * {T0:999/000}{T1:000/000}{TP:004/000}
  262. *
  263. * {S:L} - File Listing request
  264. * Printer Response:
  265. * {FILE:buttons.gcode}
  266. * {FILE:update.bin}
  267. * {FILE:nupdate.bin}
  268. * {FILE:fcupdate.flg}
  269. * {SYS:OK}
  270. */
  271. void process_lcd_s_command(const char* command) {
  272. switch (command[0]) {
  273. case 'I': {
  274. // temperature information
  275. char message_buffer[MAX_CURLY_COMMAND];
  276. sprintf_P(message_buffer, PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}"),
  277. int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
  278. #if HAS_HEATED_BED
  279. int(thermalManager.degBed()), thermalManager.degTargetBed()
  280. #else
  281. 0, 0
  282. #endif
  283. );
  284. write_to_lcd(message_buffer);
  285. } break;
  286. case 'L': {
  287. #if ENABLED(SDSUPPORT)
  288. if (!card.isMounted()) card.mount();
  289. // A more efficient way to do this would be to
  290. // implement a callback in the ls_SerialPrint code, but
  291. // that requires changes to the core cardreader class that
  292. // would not benefit the majority of users. Since one can't
  293. // select a file for printing during a print, there's
  294. // little reason not to do it this way.
  295. char message_buffer[MAX_CURLY_COMMAND];
  296. uint16_t file_count = card.get_num_Files();
  297. for (uint16_t i = 0; i < file_count; i++) {
  298. card.selectFileByIndex(i);
  299. sprintf_P(message_buffer, card.flag.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
  300. write_to_lcd(message_buffer);
  301. }
  302. write_to_lcd_P(PSTR("{SYS:OK}"));
  303. #endif
  304. } break;
  305. default: DEBUG_ECHOLNPAIR("UNKNOWN S COMMAND ", command);
  306. }
  307. }
  308. /**
  309. * Receive a curly brace command and translate to G-code.
  310. * Currently {E:0} is not handled. Its function is unknown,
  311. * but it occurs during the temp window after a sys build.
  312. */
  313. void process_lcd_command(const char* command) {
  314. const char *current = command;
  315. byte command_code = *current++;
  316. if (*current == ':') {
  317. current++; // skip the :
  318. switch (command_code) {
  319. case 'S': process_lcd_s_command(current); break;
  320. case 'J': process_lcd_j_command(current); break;
  321. case 'P': process_lcd_p_command(current); break;
  322. case 'C': process_lcd_c_command(current); break;
  323. case 'B':
  324. case 'E': process_lcd_eb_command(current); break;
  325. default: DEBUG_ECHOLNPAIR("UNKNOWN COMMAND ", command);
  326. }
  327. }
  328. else
  329. DEBUG_ECHOLNPAIR("UNKNOWN COMMAND FORMAT ", command);
  330. }
  331. //
  332. // Parse LCD commands mixed with G-Code
  333. //
  334. void parse_lcd_byte(const byte b) {
  335. static char inbound_buffer[MAX_CURLY_COMMAND];
  336. static uint8_t parsing = 0; // Parsing state
  337. static bool prevcr = false; // Was the last c a CR?
  338. const char c = b & 0x7F;
  339. if (parsing) {
  340. const bool is_lcd = parsing == 1; // 1 for LCD
  341. if ( ( is_lcd && c == '}') // Closing brace on LCD command
  342. || (!is_lcd && c == '\n') // LF on a G-code command
  343. ) {
  344. inbound_buffer[inbound_count] = '\0'; // Reset before processing
  345. inbound_count = 0; // Reset buffer index
  346. if (parsing == 1)
  347. process_lcd_command(inbound_buffer); // Handle the LCD command
  348. else
  349. queue.enqueue_one_now(inbound_buffer); // Handle the G-code command
  350. parsing = 0; // Unflag and...
  351. }
  352. else if (inbound_count < MAX_CURLY_COMMAND - 2)
  353. inbound_buffer[inbound_count++] = is_lcd ? c : b; // Buffer while space remains
  354. }
  355. else {
  356. if (c == '{') parsing = 1; // Brace opens an LCD command
  357. else if (prevcr && c == '\n') parsing = 2; // CRLF indicates G-code
  358. prevcr = (c == '\r'); // Remember if it was a CR
  359. }
  360. }
  361. /**
  362. * UC means connected.
  363. * UD means disconnected
  364. * The stock firmware considers USB initialized as "connected."
  365. */
  366. void update_usb_status(const bool forceUpdate) {
  367. static bool last_usb_connected_status = false;
  368. // This is mildly different than stock, which
  369. // appears to use the usb discovery status.
  370. // This is more logical.
  371. if (last_usb_connected_status != MYSERIAL1.connected() || forceUpdate) {
  372. last_usb_connected_status = MYSERIAL1.connected();
  373. write_to_lcd_P(last_usb_connected_status ? PSTR("{R:UC}\r\n") : PSTR("{R:UD}\r\n"));
  374. }
  375. }
  376. namespace ExtUI {
  377. void onStartup() {
  378. /**
  379. * The Malyan LCD actually runs as a separate MCU on Serial 1.
  380. * This code's job is to siphon the weird curly-brace commands from
  381. * it and translate into ExtUI operations where possible.
  382. */
  383. inbound_count = 0;
  384. #ifndef LCD_BAUDRATE
  385. #define LCD_BAUDRATE 500000
  386. #endif
  387. LCD_SERIAL.begin(LCD_BAUDRATE);
  388. // Signal init
  389. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  390. // send a version that says "unsupported"
  391. write_to_lcd_P(PSTR("{VER:99}\r\n"));
  392. // No idea why it does this twice.
  393. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  394. update_usb_status(true);
  395. }
  396. void onIdle() {
  397. /**
  398. * - from printer on startup:
  399. * {SYS:STARTED}{VER:29}{SYS:STARTED}{R:UD}
  400. */
  401. // First report USB status.
  402. update_usb_status(false);
  403. // now drain commands...
  404. while (LCD_SERIAL.available())
  405. parse_lcd_byte((byte)LCD_SERIAL.read());
  406. #if ENABLED(SDSUPPORT)
  407. // The way last printing status works is simple:
  408. // The UI needs to see at least one TQ which is not 100%
  409. // and then when the print is complete, one which is.
  410. static uint8_t last_percent_done = 100;
  411. // If there was a print in progress, we need to emit the final
  412. // print status as {TQ:100}. Reset last percent done so a new print will
  413. // issue a percent of 0.
  414. const uint8_t percent_done = (ExtUI::isPrinting() || ExtUI::isPrintingFromMediaPaused()) ? ExtUI::getProgress_percent() : last_printing_status ? 100 : 0;
  415. if (percent_done != last_percent_done) {
  416. char message_buffer[16];
  417. sprintf_P(message_buffer, PSTR("{TQ:%03i}"), percent_done);
  418. write_to_lcd(message_buffer);
  419. last_percent_done = percent_done;
  420. last_printing_status = ExtUI::isPrinting();
  421. }
  422. #endif
  423. }
  424. void onPrinterKilled(PGM_P const error, PGM_P const component) {
  425. set_lcd_error_P(error, component);
  426. }
  427. #if HAS_PID_HEATING
  428. void onPidTuning(const result_t rst) {
  429. // Called for temperature PID tuning result
  430. //SERIAL_ECHOLNPAIR("OnPidTuning:", rst);
  431. switch (rst) {
  432. case PID_BAD_EXTRUDER_NUM:
  433. set_lcd_error_P(GET_TEXT(MSG_PID_BAD_EXTRUDER_NUM));
  434. break;
  435. case PID_TEMP_TOO_HIGH:
  436. set_lcd_error_P(GET_TEXT(MSG_PID_TEMP_TOO_HIGH));
  437. break;
  438. case PID_TUNING_TIMEOUT:
  439. set_lcd_error_P(GET_TEXT(MSG_PID_TIMEOUT));
  440. break;
  441. case PID_DONE:
  442. set_lcd_error_P(GET_TEXT(MSG_PID_AUTOTUNE_DONE));
  443. break;
  444. }
  445. }
  446. #endif
  447. void onPrintTimerStarted() { write_to_lcd_P(PSTR("{SYS:BUILD}")); }
  448. void onPrintTimerPaused() {}
  449. void onPrintTimerStopped() { write_to_lcd_P(PSTR("{TQ:100}")); }
  450. // Not needed for Malyan LCD
  451. void onStatusChanged(const char * const) {}
  452. void onMediaInserted() {}
  453. void onMediaError() {}
  454. void onMediaRemoved() {}
  455. void onPlayTone(const uint16_t, const uint16_t) {}
  456. void onFilamentRunout(const extruder_t extruder) {}
  457. void onUserConfirmRequired(const char * const) {}
  458. void onHomingStart() {}
  459. void onHomingComplete() {}
  460. void onPrintFinished() {}
  461. void onFactoryReset() {}
  462. void onStoreSettings(char*) {}
  463. void onLoadSettings(const char*) {}
  464. void onConfigurationStoreWritten(bool) {}
  465. void onConfigurationStoreRead(bool) {}
  466. #if HAS_MESH
  467. void onMeshLevelingStart() {}
  468. void onMeshUpdate(const int8_t xpos, const int8_t ypos, const float zval) {}
  469. void onMeshUpdate(const int8_t xpos, const int8_t ypos, const ExtUI::probe_state_t state) {}
  470. #endif
  471. #if ENABLED(POWER_LOSS_RECOVERY)
  472. void onPowerLossResume() {}
  473. #endif
  474. void onSteppersDisabled() {}
  475. void onSteppersEnabled() {}
  476. }
  477. #endif // MALYAN_LCD