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.

malyanlcd.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. * malyanlcd.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/MarlinConfig.h"
  43. #if ENABLED(MALYAN_LCD)
  44. #include "ultralcd.h"
  45. #include "../module/temperature.h"
  46. #include "../module/planner.h"
  47. #include "../module/stepper.h"
  48. #include "../module/motion.h"
  49. #include "../module/probe.h"
  50. #include "../libs/duration_t.h"
  51. #include "../module/printcounter.h"
  52. #include "../gcode/gcode.h"
  53. #include "../gcode/queue.h"
  54. #include "../module/configuration_store.h"
  55. #include "../Marlin.h"
  56. #if ENABLED(SDSUPPORT)
  57. #include "../sd/cardreader.h"
  58. #include "../sd/SdFatConfig.h"
  59. #else
  60. #define LONG_FILENAME_LENGTH 0
  61. #endif
  62. // On the Malyan M200, this will be Serial1. On a RAMPS board,
  63. // it might not be.
  64. #define LCD_SERIAL Serial1
  65. // This is based on longest sys command + a filename, plus some buffer
  66. // in case we encounter some data we don't recognize
  67. // There is no evidence a line will ever be this long, but better safe than sorry
  68. #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
  69. // Track incoming command bytes from the LCD
  70. int inbound_count;
  71. // For sending print completion messages
  72. bool last_printing_status = false;
  73. // Everything written needs the high bit set.
  74. void write_to_lcd_P(PGM_P const message) {
  75. char encoded_message[MAX_CURLY_COMMAND];
  76. uint8_t message_length = MIN(strlen_P(message), sizeof(encoded_message));
  77. for (uint8_t i = 0; i < message_length; i++)
  78. encoded_message[i] = pgm_read_byte(&message[i]) | 0x80;
  79. LCD_SERIAL.Print::write(encoded_message, message_length);
  80. }
  81. void write_to_lcd(const char * const message) {
  82. char encoded_message[MAX_CURLY_COMMAND];
  83. const uint8_t message_length = MIN(strlen(message), sizeof(encoded_message));
  84. for (uint8_t i = 0; i < message_length; i++)
  85. encoded_message[i] = message[i] | 0x80;
  86. LCD_SERIAL.Print::write(encoded_message, message_length);
  87. }
  88. /**
  89. * Process an LCD 'C' command.
  90. * These are currently all temperature commands
  91. * {C:T0190}
  92. * Set temp for hotend to 190
  93. * {C:P050}
  94. * Set temp for bed to 50
  95. *
  96. * {C:S09} set feedrate to 90 %.
  97. * {C:S12} set feedrate to 120 %.
  98. *
  99. * the command portion begins after the :
  100. */
  101. void process_lcd_c_command(const char* command) {
  102. switch (command[0]) {
  103. case 'C': {
  104. int raw_feedrate = atoi(command + 1);
  105. feedrate_percentage = raw_feedrate * 10;
  106. feedrate_percentage = constrain(feedrate_percentage, 10, 999);
  107. } break;
  108. case 'T': {
  109. thermalManager.setTargetHotend(atoi(command + 1), 0);
  110. } break;
  111. case 'P': {
  112. thermalManager.setTargetBed(atoi(command + 1));
  113. } break;
  114. default:
  115. SERIAL_ECHOLNPAIR("UNKNOWN C COMMAND", command);
  116. return;
  117. }
  118. }
  119. /**
  120. * Process an LCD 'B' command.
  121. * {B:0} results in: {T0:008/195}{T1:000/000}{TP:000/000}{TQ:000C}{TT:000000}
  122. * T0/T1 are hot end temperatures, TP is bed, TQ is percent, and TT is probably
  123. * time remaining (HH:MM:SS). The UI can't handle displaying a second hotend,
  124. * but the stock firmware always sends it, and it's always zero.
  125. */
  126. void process_lcd_eb_command(const char* command) {
  127. char elapsed_buffer[10];
  128. duration_t elapsed;
  129. switch (command[0]) {
  130. case '0': {
  131. elapsed = print_job_timer.duration();
  132. sprintf_P(elapsed_buffer, PSTR("%02u%02u%02u"), uint16_t(elapsed.hour()), uint16_t(elapsed.minute()) % 60UL, elapsed.second());
  133. char message_buffer[MAX_CURLY_COMMAND];
  134. sprintf_P(message_buffer,
  135. PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}{TQ:%03i}{TT:%s}"),
  136. thermalManager.degHotend(0),
  137. thermalManager.degTargetHotend(0),
  138. #if HAS_HEATED_BED
  139. thermalManager.degBed(),
  140. thermalManager.degTargetBed(),
  141. #else
  142. 0, 0,
  143. #endif
  144. #if ENABLED(SDSUPPORT)
  145. card.percentDone(),
  146. #else
  147. 0,
  148. #endif
  149. elapsed_buffer);
  150. write_to_lcd(message_buffer);
  151. } break;
  152. default:
  153. SERIAL_ECHOLNPAIR("UNKNOWN E/B COMMAND", command);
  154. return;
  155. }
  156. }
  157. /**
  158. * Process an LCD 'J' command.
  159. * These are currently all movement commands.
  160. * The command portion begins after the :
  161. * Move X Axis
  162. *
  163. * {J:E}{J:X-200}{J:E}
  164. * {J:E}{J:X+200}{J:E}
  165. * X, Y, Z, A (extruder)
  166. */
  167. void process_lcd_j_command(const char* command) {
  168. static bool steppers_enabled = false;
  169. char axis = command[0];
  170. switch (axis) {
  171. case 'E':
  172. // enable or disable steppers
  173. // switch to relative
  174. enqueue_and_echo_commands_now_P(PSTR("G91"));
  175. enqueue_and_echo_commands_now_P(steppers_enabled ? PSTR("M18") : PSTR("M17"));
  176. steppers_enabled = !steppers_enabled;
  177. break;
  178. case 'A':
  179. axis = 'E';
  180. // fallthru
  181. case 'Y':
  182. case 'Z':
  183. case 'X': {
  184. // G0 <AXIS><distance>
  185. // The M200 class UI seems to send movement in .1mm values.
  186. char cmd[20];
  187. sprintf_P(cmd, PSTR("G1 %c%03.1f"), axis, atof(command + 1) / 10.0);
  188. enqueue_and_echo_command_now(cmd);
  189. } break;
  190. default:
  191. SERIAL_ECHOLNPAIR("UNKNOWN J COMMAND", command);
  192. return;
  193. }
  194. }
  195. /**
  196. * Process an LCD 'P' command, related to homing and printing.
  197. * Cancel:
  198. * {P:X}
  199. *
  200. * Home all axes:
  201. * {P:H}
  202. *
  203. * Print a file:
  204. * {P:000}
  205. * The File number is specified as a three digit value.
  206. * Printer responds with:
  207. * {PRINTFILE:Mini_SNES_Bottom.gcode}
  208. * {SYS:BUILD}echo:Now fresh file: Mini_SNES_Bottom.gcode
  209. * File opened: Mini_SNES_Bottom.gcode Size: 5805813
  210. * File selected
  211. * {SYS:BUILD}
  212. * T:-2526.8 E:0
  213. * T:-2533.0 E:0
  214. * T:-2537.4 E:0
  215. * Note only the curly brace stuff matters.
  216. */
  217. void process_lcd_p_command(const char* command) {
  218. switch (command[0]) {
  219. case 'X':
  220. #if ENABLED(SDSUPPORT)
  221. // cancel print
  222. write_to_lcd_P(PSTR("{SYS:CANCELING}"));
  223. last_printing_status = false;
  224. card.stopSDPrint(
  225. #if SD_RESORT
  226. true
  227. #endif
  228. );
  229. clear_command_queue();
  230. quickstop_stepper();
  231. print_job_timer.stop();
  232. thermalManager.disable_all_heaters();
  233. zero_fan_speeds();
  234. wait_for_heatup = false;
  235. write_to_lcd_P(PSTR("{SYS:STARTED}"));
  236. #endif
  237. break;
  238. case 'H':
  239. // Home all axis
  240. enqueue_and_echo_commands_now_P(PSTR("G28"));
  241. break;
  242. default: {
  243. #if ENABLED(SDSUPPORT)
  244. // Print file 000 - a three digit number indicating which
  245. // file to print in the SD card. If it's a directory,
  246. // then switch to the directory.
  247. // Find the name of the file to print.
  248. // It's needed to echo the PRINTFILE option.
  249. // The {S:L} command should've ensured the SD card was mounted.
  250. card.getfilename(atoi(command));
  251. // There may be a difference in how V1 and V2 LCDs handle subdirectory
  252. // prints. Investigate more. This matches the V1 motion controller actions
  253. // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
  254. if (card.flag.filenameIsDir) {
  255. card.chdir(card.filename);
  256. write_to_lcd_P(PSTR("{SYS:DIR}"));
  257. }
  258. else {
  259. char message_buffer[MAX_CURLY_COMMAND];
  260. sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.longest_filename());
  261. write_to_lcd(message_buffer);
  262. write_to_lcd_P(PSTR("{SYS:BUILD}"));
  263. card.openAndPrintFile(card.filename);
  264. }
  265. #endif
  266. } break; // default
  267. } // switch
  268. }
  269. /**
  270. * Handle an lcd 'S' command
  271. * {S:I} - Temperature request
  272. * {T0:999/000}{T1:000/000}{TP:004/000}
  273. *
  274. * {S:L} - File Listing request
  275. * Printer Response:
  276. * {FILE:buttons.gcode}
  277. * {FILE:update.bin}
  278. * {FILE:nupdate.bin}
  279. * {FILE:fcupdate.flg}
  280. * {SYS:OK}
  281. */
  282. void process_lcd_s_command(const char* command) {
  283. switch (command[0]) {
  284. case 'I': {
  285. // temperature information
  286. char message_buffer[MAX_CURLY_COMMAND];
  287. sprintf_P(message_buffer, PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}"),
  288. thermalManager.degHotend(0), thermalManager.degTargetHotend(0),
  289. #if HAS_HEATED_BED
  290. thermalManager.degBed(), thermalManager.degTargetBed()
  291. #else
  292. 0, 0
  293. #endif
  294. );
  295. write_to_lcd(message_buffer);
  296. } break;
  297. case 'H':
  298. // Home all axis
  299. enqueue_and_echo_command("G28");
  300. break;
  301. case 'L': {
  302. #if ENABLED(SDSUPPORT)
  303. if (!card.flag.cardOK) card.initsd();
  304. // A more efficient way to do this would be to
  305. // implement a callback in the ls_SerialPrint code, but
  306. // that requires changes to the core cardreader class that
  307. // would not benefit the majority of users. Since one can't
  308. // select a file for printing during a print, there's
  309. // little reason not to do it this way.
  310. char message_buffer[MAX_CURLY_COMMAND];
  311. uint16_t file_count = card.get_num_Files();
  312. for (uint16_t i = 0; i < file_count; i++) {
  313. card.getfilename(i);
  314. sprintf_P(message_buffer, card.flag.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
  315. write_to_lcd(message_buffer);
  316. }
  317. write_to_lcd_P(PSTR("{SYS:OK}"));
  318. #endif
  319. } break;
  320. default:
  321. SERIAL_ECHOLNPAIR("UNKNOWN S COMMAND", command);
  322. return;
  323. }
  324. }
  325. /**
  326. * Receive a curly brace command and translate to G-code.
  327. * Currently {E:0} is not handled. Its function is unknown,
  328. * but it occurs during the temp window after a sys build.
  329. */
  330. void process_lcd_command(const char* command) {
  331. const char *current = command;
  332. current++; // skip the leading {. The trailing one is already gone.
  333. byte command_code = *current++;
  334. if (*current != ':') {
  335. SERIAL_ECHOLNPAIR("UNKNOWN COMMAND FORMAT", command);
  336. return;
  337. }
  338. current++; // skip the :
  339. switch (command_code) {
  340. case 'S':
  341. process_lcd_s_command(current);
  342. break;
  343. case 'J':
  344. process_lcd_j_command(current);
  345. break;
  346. case 'P':
  347. process_lcd_p_command(current);
  348. break;
  349. case 'C':
  350. process_lcd_c_command(current);
  351. break;
  352. case 'B':
  353. case 'E':
  354. process_lcd_eb_command(current);
  355. break;
  356. default:
  357. SERIAL_ECHOLNPAIR("UNKNOWN COMMAND", command);
  358. return;
  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 != Serial || forceUpdate) {
  372. last_usb_connected_status = Serial;
  373. write_to_lcd_P(last_usb_connected_status ? PSTR("{R:UC}\r\n") : PSTR("{R:UD}\r\n"));
  374. }
  375. }
  376. /**
  377. * - from printer on startup:
  378. * {SYS:STARTED}{VER:29}{SYS:STARTED}{R:UD}
  379. * The optimize attribute fixes a register Compile
  380. * error for amtel.
  381. */
  382. void MarlinUI::update() {
  383. static char inbound_buffer[MAX_CURLY_COMMAND];
  384. // First report USB status.
  385. update_usb_status(false);
  386. // now drain commands...
  387. while (LCD_SERIAL.available()) {
  388. const byte b = (byte)LCD_SERIAL.read() & 0x7F;
  389. inbound_buffer[inbound_count++] = b;
  390. if (b == '}' || inbound_count == sizeof(inbound_buffer) - 1) {
  391. inbound_buffer[inbound_count - 1] = '\0';
  392. process_lcd_command(inbound_buffer);
  393. inbound_count = 0;
  394. inbound_buffer[0] = 0;
  395. }
  396. }
  397. #if ENABLED(SDSUPPORT)
  398. // The way last printing status works is simple:
  399. // The UI needs to see at least one TQ which is not 100%
  400. // and then when the print is complete, one which is.
  401. static uint8_t last_percent_done = 100;
  402. // If there was a print in progress, we need to emit the final
  403. // print status as {TQ:100}. Reset last percent done so a new print will
  404. // issue a percent of 0.
  405. const uint8_t percent_done = IS_SD_PRINTING() ? card.percentDone() : last_printing_status ? 100 : 0;
  406. if (percent_done != last_percent_done) {
  407. char message_buffer[10];
  408. sprintf_P(message_buffer, PSTR("{TQ:%03i}"), percent_done);
  409. write_to_lcd(message_buffer);
  410. last_percent_done = percent_done;
  411. last_printing_status = IS_SD_PRINTING();
  412. }
  413. #endif
  414. }
  415. /**
  416. * The Malyan LCD actually runs as a separate MCU on Serial 1.
  417. * This code's job is to siphon the weird curly-brace commands from
  418. * it and translate into gcode, which then gets injected into
  419. * the command queue where possible.
  420. */
  421. void MarlinUI::init() {
  422. inbound_count = 0;
  423. LCD_SERIAL.begin(500000);
  424. // Signal init
  425. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  426. // send a version that says "unsupported"
  427. write_to_lcd_P(PSTR("{VER:99}\r\n"));
  428. // No idea why it does this twice.
  429. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  430. update_usb_status(true);
  431. }
  432. /**
  433. * Set an alert.
  434. */
  435. void MarlinUI::set_alert_status_P(PGM_P const message) {
  436. write_to_lcd_P(PSTR("{E:"));
  437. write_to_lcd_P(message);
  438. write_to_lcd_P("}");
  439. }
  440. #endif // MALYAN_LCD