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.

extui_malyan_lcd.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * extui_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 "extui/ui_api.h"
  46. #include "ultralcd.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. // On the Malyan M200, this will be Serial1. On a RAMPS board,
  57. // it might not be.
  58. #define LCD_SERIAL Serial1
  59. // This is based on longest sys command + a filename, plus some buffer
  60. // in case we encounter some data we don't recognize
  61. // There is no evidence a line will ever be this long, but better safe than sorry
  62. #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
  63. // Track incoming command bytes from the LCD
  64. uint16_t inbound_count;
  65. // For sending print completion messages
  66. bool last_printing_status = false;
  67. // Everything written needs the high bit set.
  68. void write_to_lcd_P(PGM_P const message) {
  69. char encoded_message[MAX_CURLY_COMMAND];
  70. uint8_t message_length = _MIN(strlen_P(message), sizeof(encoded_message));
  71. LOOP_L_N(i, message_length)
  72. encoded_message[i] = pgm_read_byte(&message[i]) | 0x80;
  73. LCD_SERIAL.Print::write(encoded_message, message_length);
  74. }
  75. void write_to_lcd(const char * const message) {
  76. char encoded_message[MAX_CURLY_COMMAND];
  77. const uint8_t message_length = _MIN(strlen(message), sizeof(encoded_message));
  78. LOOP_L_N(i, message_length)
  79. encoded_message[i] = message[i] | 0x80;
  80. LCD_SERIAL.Print::write(encoded_message, message_length);
  81. }
  82. // {E:<msg>} is for error states.
  83. void set_lcd_error_P(PGM_P const error, PGM_P const component=nullptr) {
  84. write_to_lcd_P(PSTR("{E:"));
  85. write_to_lcd_P(error);
  86. if (component) {
  87. write_to_lcd_P(PSTR(" "));
  88. write_to_lcd_P(component);
  89. }
  90. write_to_lcd_P(PSTR("}"));
  91. }
  92. /**
  93. * Process an LCD 'C' command.
  94. * These are currently all temperature commands
  95. * {C:T0190}
  96. * Set temp for hotend to 190
  97. * {C:P050}
  98. * Set temp for bed to 50
  99. *
  100. * {C:S09} set feedrate to 90 %.
  101. * {C:S12} set feedrate to 120 %.
  102. *
  103. * the command portion begins after the :
  104. */
  105. void process_lcd_c_command(const char* command) {
  106. const int target_val = command[1] ? atoi(command + 1) : -1;
  107. if (target_val < 0) {
  108. DEBUG_ECHOLNPAIR("UNKNOWN C COMMAND ", command);
  109. return;
  110. }
  111. switch (command[0]) {
  112. case 'C': // Cope with both V1 early rev and later LCDs.
  113. case 'S':
  114. feedrate_percentage = target_val * 10;
  115. LIMIT(feedrate_percentage, 10, 999);
  116. break;
  117. case 'T':
  118. // Sometimes the LCD will send commands to turn off both extruder and bed, though
  119. // this should not happen since the printing screen is up. Better safe than sorry.
  120. if (!print_job_timer.isRunning() || target_val > 0)
  121. ExtUI::setTargetTemp_celsius(target_val, ExtUI::extruder_t::E0);
  122. break;
  123. #if HAS_HEATED_BED
  124. case 'P': ExtUI::setTargetTemp_celsius(target_val, ExtUI::heater_t::BED); break;
  125. #endif
  126. default: DEBUG_ECHOLNPAIR("UNKNOWN C COMMAND ", command);
  127. }
  128. }
  129. /**
  130. * Process an LCD 'B' command.
  131. * {B:0} results in: {T0:008/195}{T1:000/000}{TP:000/000}{TQ:000C}{TT:000000}
  132. * T0/T1 are hot end temperatures, TP is bed, TQ is percent, and TT is probably
  133. * time remaining (HH:MM:SS). The UI can't handle displaying a second hotend,
  134. * but the stock firmware always sends it, and it's always zero.
  135. */
  136. void process_lcd_eb_command(const char* command) {
  137. char elapsed_buffer[10];
  138. static uint8_t iteration = 0;
  139. duration_t elapsed;
  140. switch (command[0]) {
  141. case '0': {
  142. elapsed = print_job_timer.duration();
  143. sprintf_P(elapsed_buffer, PSTR("%02u%02u%02u"), uint16_t(elapsed.hour()), uint16_t(elapsed.minute()) % 60, uint16_t(elapsed.second()) % 60);
  144. char message_buffer[MAX_CURLY_COMMAND];
  145. uint8_t done_pct = print_job_timer.isRunning() ? (iteration * 10) : 100;
  146. iteration = (iteration + 1) % 10; // Provide progress animation
  147. #if ENABLED(SDSUPPORT)
  148. if (ExtUI::isPrintingFromMedia() || ExtUI::isPrintingFromMediaPaused())
  149. done_pct = card.percentDone();
  150. #endif
  151. sprintf_P(message_buffer,
  152. PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}{TQ:%03i}{TT:%s}"),
  153. int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
  154. #if HAS_HEATED_BED
  155. int(thermalManager.degBed()), thermalManager.degTargetBed(),
  156. #else
  157. 0, 0,
  158. #endif
  159. #if ENABLED(SDSUPPORT)
  160. done_pct,
  161. #else
  162. 0,
  163. #endif
  164. elapsed_buffer
  165. );
  166. write_to_lcd(message_buffer);
  167. } break;
  168. default: DEBUG_ECHOLNPAIR("UNKNOWN E/B COMMAND ", command);
  169. }
  170. }
  171. /**
  172. * Process an LCD 'J' command.
  173. * These are currently all movement commands.
  174. * The command portion begins after the :
  175. * Move X Axis
  176. *
  177. * {J:E}{J:X-200}{J:E}
  178. * {J:E}{J:X+200}{J:E}
  179. * X, Y, Z, A (extruder)
  180. */
  181. template<typename T>
  182. void j_move_axis(const char* command, const T axis) {
  183. const float dist = atof(command + 1) / 10.0;
  184. ExtUI::setAxisPosition_mm(ExtUI::getAxisPosition_mm(axis) + dist, axis);
  185. };
  186. void process_lcd_j_command(const char* command) {
  187. switch (command[0]) {
  188. case 'E': break;
  189. case 'A': j_move_axis<ExtUI::extruder_t>(command, ExtUI::extruder_t::E0); break;
  190. case 'Y': j_move_axis<ExtUI::axis_t>(command, ExtUI::axis_t::Y); break;
  191. case 'Z': j_move_axis<ExtUI::axis_t>(command, ExtUI::axis_t::Z); break;
  192. case 'X': j_move_axis<ExtUI::axis_t>(command, ExtUI::axis_t::X); break;
  193. default: DEBUG_ECHOLNPAIR("UNKNOWN J COMMAND ", command);
  194. }
  195. }
  196. /**
  197. * Process an LCD 'P' command, related to homing and printing.
  198. * Cancel:
  199. * {P:X}
  200. *
  201. * Home all axes:
  202. * {P:H}
  203. *
  204. * Print a file:
  205. * {P:000}
  206. * The File number is specified as a three digit value.
  207. * Printer responds with:
  208. * {PRINTFILE:Mini_SNES_Bottom.gcode}
  209. * {SYS:BUILD}echo:Now fresh file: Mini_SNES_Bottom.gcode
  210. * File opened: Mini_SNES_Bottom.gcode Size: 5805813
  211. * File selected
  212. * {SYS:BUILD}
  213. * T:-2526.8 E:0
  214. * T:-2533.0 E:0
  215. * T:-2537.4 E:0
  216. * Note only the curly brace stuff matters.
  217. */
  218. void process_lcd_p_command(const char* command) {
  219. switch (command[0]) {
  220. case 'P':
  221. ExtUI::pausePrint();
  222. write_to_lcd_P(PSTR("{SYS:PAUSED}"));
  223. break;
  224. case 'R':
  225. ExtUI::resumePrint();
  226. write_to_lcd_P(PSTR("{SYS:RESUMED}"));
  227. break;
  228. case 'X':
  229. write_to_lcd_P(PSTR("{SYS:CANCELING}"));
  230. ExtUI::stopPrint();
  231. write_to_lcd_P(PSTR("{SYS:STARTED}"));
  232. break;
  233. case 'H': queue.enqueue_now_P(G28_STR); break; // Home all axes
  234. default: {
  235. #if ENABLED(SDSUPPORT)
  236. // Print file 000 - a three digit number indicating which
  237. // file to print in the SD card. If it's a directory,
  238. // then switch to the directory.
  239. // Find the name of the file to print.
  240. // It's needed to echo the PRINTFILE option.
  241. // The {S:L} command should've ensured the SD card was mounted.
  242. card.selectFileByIndex(atoi(command));
  243. // There may be a difference in how V1 and V2 LCDs handle subdirectory
  244. // prints. Investigate more. This matches the V1 motion controller actions
  245. // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
  246. if (card.flag.filenameIsDir) {
  247. card.cd(card.filename);
  248. write_to_lcd_P(PSTR("{SYS:DIR}"));
  249. }
  250. else {
  251. char message_buffer[MAX_CURLY_COMMAND];
  252. sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.longest_filename());
  253. write_to_lcd(message_buffer);
  254. write_to_lcd_P(PSTR("{SYS:BUILD}"));
  255. card.openAndPrintFile(card.filename);
  256. }
  257. #endif
  258. } break; // default
  259. } // switch
  260. }
  261. /**
  262. * Handle an lcd 'S' command
  263. * {S:I} - Temperature request
  264. * {T0:999/000}{T1:000/000}{TP:004/000}
  265. *
  266. * {S:L} - File Listing request
  267. * Printer Response:
  268. * {FILE:buttons.gcode}
  269. * {FILE:update.bin}
  270. * {FILE:nupdate.bin}
  271. * {FILE:fcupdate.flg}
  272. * {SYS:OK}
  273. */
  274. void process_lcd_s_command(const char* command) {
  275. switch (command[0]) {
  276. case 'I': {
  277. // temperature information
  278. char message_buffer[MAX_CURLY_COMMAND];
  279. sprintf_P(message_buffer, PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}"),
  280. int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
  281. #if HAS_HEATED_BED
  282. int(thermalManager.degBed()), thermalManager.degTargetBed()
  283. #else
  284. 0, 0
  285. #endif
  286. );
  287. write_to_lcd(message_buffer);
  288. } break;
  289. case 'L': {
  290. #if ENABLED(SDSUPPORT)
  291. if (!card.isMounted()) card.mount();
  292. // A more efficient way to do this would be to
  293. // implement a callback in the ls_SerialPrint code, but
  294. // that requires changes to the core cardreader class that
  295. // would not benefit the majority of users. Since one can't
  296. // select a file for printing during a print, there's
  297. // little reason not to do it this way.
  298. char message_buffer[MAX_CURLY_COMMAND];
  299. uint16_t file_count = card.get_num_Files();
  300. for (uint16_t i = 0; i < file_count; i++) {
  301. card.selectFileByIndex(i);
  302. sprintf_P(message_buffer, card.flag.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
  303. write_to_lcd(message_buffer);
  304. }
  305. write_to_lcd_P(PSTR("{SYS:OK}"));
  306. #endif
  307. } break;
  308. default: DEBUG_ECHOLNPAIR("UNKNOWN S COMMAND ", command);
  309. }
  310. }
  311. /**
  312. * Receive a curly brace command and translate to G-code.
  313. * Currently {E:0} is not handled. Its function is unknown,
  314. * but it occurs during the temp window after a sys build.
  315. */
  316. void process_lcd_command(const char* command) {
  317. const char *current = command;
  318. byte command_code = *current++;
  319. if (*current == ':') {
  320. current++; // skip the :
  321. switch (command_code) {
  322. case 'S': process_lcd_s_command(current); break;
  323. case 'J': process_lcd_j_command(current); break;
  324. case 'P': process_lcd_p_command(current); break;
  325. case 'C': process_lcd_c_command(current); break;
  326. case 'B':
  327. case 'E': process_lcd_eb_command(current); break;
  328. default: DEBUG_ECHOLNPAIR("UNKNOWN COMMAND ", command);
  329. }
  330. }
  331. else
  332. DEBUG_ECHOLNPAIR("UNKNOWN COMMAND FORMAT ", command);
  333. }
  334. //
  335. // Parse LCD commands mixed with G-Code
  336. //
  337. void parse_lcd_byte(const byte b) {
  338. static char inbound_buffer[MAX_CURLY_COMMAND];
  339. static uint8_t parsing = 0; // Parsing state
  340. static bool prevcr = false; // Was the last c a CR?
  341. const char c = b & 0x7F;
  342. if (parsing) {
  343. const bool is_lcd = parsing == 1; // 1 for LCD
  344. if ( ( is_lcd && c == '}') // Closing brace on LCD command
  345. || (!is_lcd && c == '\n') // LF on a G-code command
  346. ) {
  347. inbound_buffer[inbound_count] = '\0'; // Reset before processing
  348. inbound_count = 0; // Reset buffer index
  349. if (parsing == 1)
  350. process_lcd_command(inbound_buffer); // Handle the LCD command
  351. else
  352. queue.enqueue_one_now(inbound_buffer); // Handle the G-code command
  353. parsing = 0; // Unflag and...
  354. }
  355. else if (inbound_count < MAX_CURLY_COMMAND - 2)
  356. inbound_buffer[inbound_count++] = is_lcd ? c : b; // Buffer while space remains
  357. }
  358. else {
  359. if (c == '{') parsing = 1; // Brace opens an LCD command
  360. else if (prevcr && c == '\n') parsing = 2; // CRLF indicates G-code
  361. prevcr = (c == '\r'); // Remember if it was a CR
  362. }
  363. }
  364. /**
  365. * UC means connected.
  366. * UD means disconnected
  367. * The stock firmware considers USB initialized as "connected."
  368. */
  369. void update_usb_status(const bool forceUpdate) {
  370. static bool last_usb_connected_status = false;
  371. // This is mildly different than stock, which
  372. // appears to use the usb discovery status.
  373. // This is more logical.
  374. if (last_usb_connected_status != MYSERIAL0 || forceUpdate) {
  375. last_usb_connected_status = MYSERIAL0;
  376. write_to_lcd_P(last_usb_connected_status ? PSTR("{R:UC}\r\n") : PSTR("{R:UD}\r\n"));
  377. }
  378. }
  379. namespace ExtUI {
  380. void onStartup() {
  381. /**
  382. * The Malyan LCD actually runs as a separate MCU on Serial 1.
  383. * This code's job is to siphon the weird curly-brace commands from
  384. * it and translate into ExtUI operations where possible.
  385. */
  386. inbound_count = 0;
  387. LCD_SERIAL.begin(500000);
  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 onFactoryReset() {}
  459. void onStoreSettings(char*) {}
  460. void onLoadSettings(const char*) {}
  461. void onConfigurationStoreWritten(bool) {}
  462. void onConfigurationStoreRead(bool) {}
  463. #if HAS_MESH
  464. void onMeshUpdate(const int8_t xpos, const int8_t ypos, const float zval) {}
  465. void onMeshUpdate(const int8_t xpos, const int8_t ypos, const ExtUI::probe_state_t state) {}
  466. #endif
  467. #if ENABLED(POWER_LOSS_RECOVERY)
  468. void onPowerLossResume() {}
  469. #endif
  470. }
  471. #endif // MALYAN_LCD