Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

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