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