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

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