add M2 and M30 end of program handling
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
CommitLineData
58baeec1
MM
1/*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
cd011f58
AW
6*/
7
8
0325af12 9#include "SimpleShell.h"
ba8da804 10#include "libs/Kernel.h"
0325af12
AW
11#include "libs/nuts_bolts.h"
12#include "libs/utils.h"
423df6df 13#include "libs/SerialMessage.h"
838b33b4 14#include "libs/StreamOutput.h"
3fceb8eb 15#include "modules/robot/Conveyor.h"
172d42d9 16#include "DirHandle.h"
0f0b1656 17#include "mri.h"
582559c6 18#include "version.h"
8293d443 19#include "PublicDataRequest.h"
4fed9ba1 20#include "AppendFileStream.h"
618c9b0f 21#include "FileStream.h"
61134a65
JM
22#include "checksumm.h"
23#include "PublicData.h"
24#include "Gcode.h"
564cf1f0 25#include "Robot.h"
40843ebc
JM
26#include "ToolManagerPublicAccess.h"
27#include "GcodeDispatch.h"
61134a65 28
40843ebc 29#include "TemperatureControlPublicAccess.h"
d4ee6ee2 30#include "NetworkPublicAccess.h"
a200fc31 31#include "platform_memory.h"
ae91dea4 32#include "SwitchPublicAccess.h"
3704585b 33#include "SDFAT.h"
1f8dab1a 34#include "Thermistor.h"
d55d551b 35#include "md5.h"
47339e4a 36
61134a65
JM
37#include "system_LPC17xx.h"
38#include "LPC17xx.h"
39
23eb804b
JM
40#include "mbed.h" // for wait_ms()
41
6187a020
JM
42extern unsigned int g_maximumHeapAddress;
43
ecc610a4
JM
44#include <malloc.h>
45#include <mri.h>
46#include <stdio.h>
47#include <stdint.h>
48
49extern "C" uint32_t __end__;
50extern "C" uint32_t __malloc_free_list;
51extern "C" uint32_t _sbrk(int size);
52
9e403697 53// command lookup table
7e81f138
JM
54const SimpleShell::ptentry_t SimpleShell::commands_table[] = {
55 {"ls", SimpleShell::ls_command},
56 {"cd", SimpleShell::cd_command},
57 {"pwd", SimpleShell::pwd_command},
58 {"cat", SimpleShell::cat_command},
59 {"rm", SimpleShell::rm_command},
6d877d9b
JM
60 {"mv", SimpleShell::mv_command},
61 {"upload", SimpleShell::upload_command},
7e81f138
JM
62 {"reset", SimpleShell::reset_command},
63 {"dfu", SimpleShell::dfu_command},
64 {"break", SimpleShell::break_command},
65 {"help", SimpleShell::help_command},
66 {"?", SimpleShell::help_command},
67 {"version", SimpleShell::version_command},
68 {"mem", SimpleShell::mem_command},
69 {"get", SimpleShell::get_command},
70 {"set_temp", SimpleShell::set_temp_command},
ae91dea4 71 {"switch", SimpleShell::switch_command},
7e81f138
JM
72 {"net", SimpleShell::net_command},
73 {"load", SimpleShell::load_command},
74 {"save", SimpleShell::save_command},
6d877d9b 75 {"remount", SimpleShell::remount_command},
1f8dab1a 76 {"calc_thermistor", SimpleShell::calc_thermistor_command},
4c8f5447 77 {"thermistors", SimpleShell::print_thermistors_command},
d55d551b 78 {"md5sum", SimpleShell::md5sum_command},
9e403697
JM
79
80 // unknown command
7e81f138 81 {NULL, NULL}
9e403697 82};
ecc610a4 83
6d877d9b 84int SimpleShell::reset_delay_secs = 0;
7e81f138 85
ecc610a4 86// Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
0c683b26 87static uint32_t heapWalk(StreamOutput *stream, bool verbose)
ecc610a4
JM
88{
89 uint32_t chunkNumber = 1;
90 // The __end__ linker symbol points to the beginning of the heap.
91 uint32_t chunkCurr = (uint32_t)&__end__;
92 // __malloc_free_list is the head pointer to newlib-nano's link list of free chunks.
93 uint32_t freeCurr = __malloc_free_list;
94 // Calling _sbrk() with 0 reserves no more memory but it returns the current top of heap.
95 uint32_t heapEnd = _sbrk(0);
96 // accumulate totals
9e403697
JM
97 uint32_t freeSize = 0;
98 uint32_t usedSize = 0;
ecc610a4
JM
99
100 stream->printf("Used Heap Size: %lu\n", heapEnd - chunkCurr);
101
102 // Walk through the chunks until we hit the end of the heap.
9e403697 103 while (chunkCurr < heapEnd) {
ecc610a4
JM
104 // Assume the chunk is in use. Will update later.
105 int isChunkFree = 0;
106 // The first 32-bit word in a chunk is the size of the allocation. newlib-nano over allocates by 8 bytes.
107 // 4 bytes for this 32-bit chunk size and another 4 bytes to allow for 8 byte-alignment of returned pointer.
9e403697 108 uint32_t chunkSize = *(uint32_t *)chunkCurr;
ecc610a4
JM
109 // The start of the next chunk is right after the end of this one.
110 uint32_t chunkNext = chunkCurr + chunkSize;
111
112 // The free list is sorted by address.
113 // Check to see if we have found the next free chunk in the heap.
9e403697 114 if (chunkCurr == freeCurr) {
ecc610a4
JM
115 // Chunk is free so flag it as such.
116 isChunkFree = 1;
117 // The second 32-bit word in a free chunk is a pointer to the next free chunk (again sorted by address).
9e403697 118 freeCurr = *(uint32_t *)(freeCurr + 4);
ecc610a4
JM
119 }
120
121 // Skip past the 32-bit size field in the chunk header.
122 chunkCurr += 4;
123 // 8-byte align the data pointer.
124 chunkCurr = (chunkCurr + 7) & ~7;
125 // newlib-nano over allocates by 8 bytes, 4 bytes for the 32-bit chunk size and another 4 bytes to allow for 8
126 // byte-alignment of the returned pointer.
127 chunkSize -= 8;
9e403697 128 if (verbose)
ecc610a4
JM
129 stream->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber, chunkCurr, chunkSize, isChunkFree ? "CHUNK FREE" : "");
130
9e403697 131 if (isChunkFree) freeSize += chunkSize;
ecc610a4
JM
132 else usedSize += chunkSize;
133
134 chunkCurr = chunkNext;
135 chunkNumber++;
136 }
137 stream->printf("Allocated: %lu, Free: %lu\r\n", usedSize, freeSize);
0c683b26 138 return freeSize;
ecc610a4
JM
139}
140
141
9e403697
JM
142void SimpleShell::on_module_loaded()
143{
0325af12 144 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
48afc62a 145 this->register_for_event(ON_GCODE_RECEIVED);
146 this->register_for_event(ON_SECOND_TICK);
c4e56997 147
7e81f138 148 reset_delay_secs = 0;
ead17727
JM
149}
150
9e403697
JM
151void SimpleShell::on_second_tick(void *)
152{
ead17727 153 // we are timing out for the reset
7e81f138
JM
154 if (reset_delay_secs > 0) {
155 if (--reset_delay_secs == 0) {
ead17727
JM
156 system_reset(false);
157 }
158 }
0325af12
AW
159}
160
9e403697
JM
161void SimpleShell::on_gcode_received(void *argument)
162{
163 Gcode *gcode = static_cast<Gcode *>(argument);
6d877d9b 164 string args = get_arguments(gcode->get_command());
c4e56997
JM
165
166 if (gcode->has_m) {
167 if (gcode->m == 20) { // list sd card
c4e56997
JM
168 gcode->stream->printf("Begin file list\r\n");
169 ls_command("/sd", gcode->stream);
170 gcode->stream->printf("End file list\r\n");
d4ee6ee2
JM
171
172 } else if (gcode->m == 30) { // remove file
3a238fdc 173 rm_command("/sd/" + args, gcode->stream);
618c9b0f 174
6d877d9b 175 } else if(gcode->m == 501) { // load config override
618c9b0f
JM
176 if(args.empty()) {
177 load_command("/sd/config-override", gcode->stream);
6d877d9b 178 } else {
618c9b0f
JM
179 load_command("/sd/config-override." + args, gcode->stream);
180 }
181
6d877d9b 182 } else if(gcode->m == 504) { // save to specific config override file
618c9b0f
JM
183 if(args.empty()) {
184 save_command("/sd/config-override", gcode->stream);
6d877d9b 185 } else {
618c9b0f
JM
186 save_command("/sd/config-override." + args, gcode->stream);
187 }
3a238fdc 188 }
c4e56997
JM
189 }
190}
191
7e81f138 192bool SimpleShell::parse_command(const char *cmd, string args, StreamOutput *stream)
9e403697 193{
7e81f138
JM
194 for (const ptentry_t *p = commands_table; p->command != NULL; ++p) {
195 if (strncasecmp(cmd, p->command, strlen(p->command)) == 0) {
196 p->func(args, stream);
9e403697
JM
197 return true;
198 }
199 }
200
201 return false;
202}
203
0325af12 204// When a new line is received, check if it is a command, and if it is, act upon it
9e403697
JM
205void SimpleShell::on_console_line_received( void *argument )
206{
207 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
6c0193b3 208 string possible_command = new_message.message;
7f613782 209
6c0193b3
JM
210 // ignore anything that is not lowercase or a $ as it is not a command
211 if(possible_command.size() == 0 || (!islower(possible_command[0]) && possible_command[0] != '$')) {
212 return;
213 }
7f613782 214
6c0193b3
JM
215 // it is a grbl compatible command
216 if(possible_command[0] == '$' && possible_command.size() >= 2) {
217 switch(possible_command[1]) {
218 case 'G':
219 // issue get state
220 get_command("state", new_message.stream);
221 break;
222
223 case 'X':
224 THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt
225 new_message.stream->printf("[Caution: Unlocked]\n");
226 break;
227
228 case '#':
229 grblDP_command("", new_message.stream);
230 break;
231
232 case 'H':
233 {
8ad60a4c 234 // issue G28.2 which is force homing cycle
53ece53b 235 Gcode gcode("G28.2", new_message.stream);
6c0193b3
JM
236 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode);
237 }
238 break;
0325af12 239
6c0193b3
JM
240 default:
241 new_message.stream->printf("error: Invalid statement\n");
242 break;
243 }
244
245 }else{
6187a020 246
6c0193b3
JM
247 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
248 string cmd = shift_parameter(possible_command);
249
250 // find command and execute it
251 if(!parse_command(cmd.c_str(), possible_command, new_message.stream)) {
252 new_message.stream->printf("error: Unsupported command\n");
253 }
254 }
0325af12
AW
255}
256
0325af12
AW
257// Act upon an ls command
258// Convert the first parameter into an absolute path, then list the files in that path
9e403697
JM
259void SimpleShell::ls_command( string parameters, StreamOutput *stream )
260{
3579deea
JM
261 string path, opts;
262 while(!parameters.empty()) {
6d877d9b 263 string s = shift_parameter( parameters );
3579deea
JM
264 if(s.front() == '-') {
265 opts.append(s);
266 } else {
6d877d9b
JM
267 path = s;
268 if(!parameters.empty()) {
3579deea
JM
269 path.append(" ");
270 path.append(parameters);
271 }
272 break;
273 }
274 }
b557a801 275
6d877d9b 276 path = absolute_from_relative(path);
3579deea 277
9e403697
JM
278 DIR *d;
279 struct dirent *p;
3579deea 280 d = opendir(path.c_str());
9e403697
JM
281 if (d != NULL) {
282 while ((p = readdir(d)) != NULL) {
3579deea 283 stream->printf("%s", lc(string(p->d_name)).c_str());
6d877d9b 284 if(p->d_isdir) {
3579deea 285 stream->printf("/");
6d877d9b 286 } else if(opts.find("-s", 0, 2) != string::npos) {
3579deea
JM
287 stream->printf(" %d", p->d_fsize);
288 }
289 stream->printf("\r\n");
9e403697 290 }
ed7c5844 291 closedir(d);
0325af12 292 } else {
3579deea 293 stream->printf("Could not open directory %s\r\n", path.c_str());
0325af12
AW
294 }
295}
296
3704585b 297extern SDFAT mounter;
298
299void SimpleShell::remount_command( string parameters, StreamOutput *stream )
300{
301 mounter.remount();
48afc62a 302 stream->printf("remounted\r\n");
12fb447a 303}
3704585b 304
9e403697
JM
305// Delete a file
306void SimpleShell::rm_command( string parameters, StreamOutput *stream )
307{
6d877d9b 308 const char *fn = absolute_from_relative(shift_parameter( parameters )).c_str();
9e403697
JM
309 int s = remove(fn);
310 if (s != 0) stream->printf("Could not delete %s \r\n", fn);
311}
312
6d877d9b
JM
313// Rename a file
314void SimpleShell::mv_command( string parameters, StreamOutput *stream )
315{
316 string from = absolute_from_relative(shift_parameter( parameters ));
a940483b 317 string to = absolute_from_relative(shift_parameter(parameters));
6d877d9b
JM
318 int s = rename(from.c_str(), to.c_str());
319 if (s != 0) stream->printf("Could not rename %s to %s\r\n", from.c_str(), to.c_str());
320 else stream->printf("renamed %s to %s\r\n", from.c_str(), to.c_str());
321}
322
0325af12 323// Change current absolute path to provided path
9e403697
JM
324void SimpleShell::cd_command( string parameters, StreamOutput *stream )
325{
75f4581c 326 string folder = absolute_from_relative( parameters );
6bcd4886 327
0325af12 328 DIR *d;
0325af12 329 d = opendir(folder.c_str());
9e403697 330 if (d == NULL) {
58baeec1 331 stream->printf("Could not open directory %s \r\n", folder.c_str() );
9e403697 332 } else {
75f4581c 333 THEKERNEL->current_path = folder;
ed7c5844 334 closedir(d);
0325af12
AW
335 }
336}
337
b7250484 338// Responds with the present working directory
9e403697
JM
339void SimpleShell::pwd_command( string parameters, StreamOutput *stream )
340{
75f4581c 341 stream->printf("%s\r\n", THEKERNEL->current_path.c_str());
b7250484
L
342}
343
0325af12 344// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
9e403697
JM
345void SimpleShell::cat_command( string parameters, StreamOutput *stream )
346{
58baeec1 347 // Get parameters ( filename and line limit )
75f4581c 348 string filename = absolute_from_relative(shift_parameter( parameters ));
1f61f177 349 string limit_parameter = shift_parameter( parameters );
0325af12 350 int limit = -1;
1f61f177 351 int delay= 0;
2ab3fca6 352 bool send_eof= false;
1f61f177
JM
353 if ( limit_parameter == "-d" ) {
354 string d= shift_parameter( parameters );
9e403697 355 char *e = NULL;
1f61f177 356 delay = strtol(d.c_str(), &e, 10);
01e97c58 357 if (e <= d.c_str()) {
1f61f177
JM
358 delay = 0;
359
01e97c58
JM
360 } else {
361 send_eof= true; // we need to terminate file send with an eof
362 }
363
1f61f177
JM
364 }else if ( limit_parameter != "" ) {
365 char *e = NULL;
366 limit = strtol(limit_parameter.c_str(), &e, 10);
367 if (e <= limit_parameter.c_str())
f7e6f459
MM
368 limit = -1;
369 }
58baeec1 370
1f61f177
JM
371 // we have been asked to delay before cat, probably to allow time to issue upload command
372 while(delay-- > 0) {
373 for (int i = 0; i < 10; ++i) {
374 wait_ms(100);
375 THEKERNEL->call_event(ON_IDLE);
376 }
377 }
378
58baeec1 379 // Open file
0325af12 380 FILE *lp = fopen(filename.c_str(), "r");
9e403697 381 if (lp == NULL) {
58baeec1
MM
382 stream->printf("File not found: %s\r\n", filename.c_str());
383 return;
9ed670c5 384 }
0325af12
AW
385 string buffer;
386 int c;
58baeec1 387 int newlines = 0;
6d877d9b 388 int linecnt = 0;
0325af12 389 // Print each line of the file
9e403697 390 while ((c = fgetc (lp)) != EOF) {
58baeec1 391 buffer.append((char *)&c, 1);
2ab3fca6
JM
392 if ( c == '\n' || ++linecnt > 80) {
393 if(c == '\n') newlines++;
d728799b 394 stream->puts(buffer.c_str());
58baeec1 395 buffer.clear();
6d877d9b 396 if(linecnt > 80) linecnt = 0;
6757ce1a
JM
397 // we need to kick things or they die
398 THEKERNEL->call_event(ON_IDLE);
68b7afb4 399 }
9e403697
JM
400 if ( newlines == limit ) {
401 break;
402 }
58baeec1 403 };
0325af12 404 fclose(lp);
2ab3fca6
JM
405
406 if(send_eof) {
407 stream->puts("\032"); // ^Z terminates the upload
408 }
6d877d9b
JM
409}
410
411void SimpleShell::upload_command( string parameters, StreamOutput *stream )
412{
413 // this needs to be a hack. it needs to read direct from serial and not allow on_main_loop run until done
414 // NOTE this will block all operation until the upload is complete, so do not do while printing
415 if(!THEKERNEL->conveyor->is_queue_empty()) {
416 stream->printf("upload not allowed while printing or busy\n");
417 return;
418 }
419
420 // open file to upload to
421 string upload_filename = absolute_from_relative( parameters );
422 FILE *fd = fopen(upload_filename.c_str(), "w");
423 if(fd != NULL) {
424 stream->printf("uploading to file: %s, send control-D or control-Z to finish\r\n", upload_filename.c_str());
425 } else {
426 stream->printf("failed to open file: %s.\r\n", upload_filename.c_str());
427 return;
428 }
0325af12 429
6d877d9b
JM
430 int cnt = 0;
431 bool uploading = true;
432 while(uploading) {
433 if(!stream->ready()) {
434 // we need to kick things or they die
435 THEKERNEL->call_event(ON_IDLE);
436 continue;
437 }
438
439 char c = stream->_getc();
440 if( c == 4 || c == 26) { // ctrl-D or ctrl-Z
441 uploading = false;
442 // close file
443 fclose(fd);
444 stream->printf("uploaded %d bytes\n", cnt);
445 return;
446
447 } else {
448 // write character to file
449 cnt++;
450 if(fputc(c, fd) != c) {
451 // error writing to file
452 stream->printf("error writing to file. ignoring all characters until EOF\r\n");
453 fclose(fd);
454 fd = NULL;
455 uploading= false;
456
457 } else {
458 if ((cnt%400) == 0) {
459 // HACK ALERT to get around fwrite corruption close and re open for append
460 fclose(fd);
461 fd = fopen(upload_filename.c_str(), "a");
6757ce1a
JM
462 // we need to kick things or they die
463 THEKERNEL->call_event(ON_IDLE);
6d877d9b
JM
464 }
465 }
466 }
467 }
468 // we got an error so ignore everything until EOF
469 char c;
470 do {
471 if(stream->ready()) {
472 c= stream->_getc();
473 }else{
474 THEKERNEL->call_event(ON_IDLE);
475 c= 0;
476 }
477 } while(c != 4 && c != 26);
0325af12
AW
478}
479
618c9b0f
JM
480// loads the specified config-override file
481void SimpleShell::load_command( string parameters, StreamOutput *stream )
482{
483 // Get parameters ( filename )
75f4581c 484 string filename = absolute_from_relative(parameters);
618c9b0f
JM
485 if(filename == "/") {
486 filename = THEKERNEL->config_override_filename();
487 }
488
6d877d9b 489 FILE *fp = fopen(filename.c_str(), "r");
618c9b0f
JM
490 if(fp != NULL) {
491 char buf[132];
492 stream->printf("Loading config override file: %s...\n", filename.c_str());
493 while(fgets(buf, sizeof buf, fp) != NULL) {
494 stream->printf(" %s", buf);
495 if(buf[0] == ';') continue; // skip the comments
6d877d9b 496 struct SerialMessage message = {&(StreamOutput::NullStream), buf};
618c9b0f
JM
497 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
498 }
499 stream->printf("config override file executed\n");
500 fclose(fp);
501
6d877d9b 502 } else {
618c9b0f
JM
503 stream->printf("File not found: %s\n", filename.c_str());
504 }
505}
506
507// saves the specified config-override file
508void SimpleShell::save_command( string parameters, StreamOutput *stream )
509{
510 // Get parameters ( filename )
75f4581c 511 string filename = absolute_from_relative(parameters);
618c9b0f
JM
512 if(filename == "/") {
513 filename = THEKERNEL->config_override_filename();
514 }
515
23eb804b
JM
516 THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run
517
06afe68b
JM
518 //remove(filename.c_str()); // seems to cause a hang every now and then
519 {
520 FileStream fs(filename.c_str());
521 fs.printf("; DO NOT EDIT THIS FILE\n");
522 // this also will truncate the existing file instead of deleting it
523 }
524
23eb804b 525 // stream that appends to file
4fed9ba1
JM
526 AppendFileStream *gs = new AppendFileStream(filename.c_str());
527 // if(!gs->is_open()) {
528 // stream->printf("Unable to open File %s for write\n", filename.c_str());
529 // return;
530 // }
618c9b0f 531
7acfedab 532 __disable_irq();
618c9b0f
JM
533 // issue a M500 which will store values in the file stream
534 Gcode *gcode = new Gcode("M500", gs);
535 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
536 delete gs;
537 delete gcode;
7acfedab 538 __enable_irq();
618c9b0f
JM
539
540 stream->printf("Settings Stored to %s\r\n", filename.c_str());
541}
542
6187a020 543// show free memory
9e403697
JM
544void SimpleShell::mem_command( string parameters, StreamOutput *stream)
545{
546 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos ;
547 unsigned long heap = (unsigned long)_sbrk(0);
548 unsigned long m = g_maximumHeapAddress - heap;
ecc610a4
JM
549 stream->printf("Unused Heap: %lu bytes\r\n", m);
550
6d877d9b 551 uint32_t f = heapWalk(stream, verbose);
0c683b26 552 stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
a200fc31 553
0c683b26 554 stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
6d877d9b 555 if (verbose) {
1803076a
MM
556 AHB0.debug(stream);
557 AHB1.debug(stream);
558 }
6187a020
JM
559}
560
9e403697
JM
561static uint32_t getDeviceType()
562{
563#define IAP_LOCATION 0x1FFF1FF1
01f35bcc
JM
564 uint32_t command[1];
565 uint32_t result[5];
9e403697 566 typedef void (*IAP)(uint32_t *, uint32_t *);
01f35bcc
JM
567 IAP iap = (IAP) IAP_LOCATION;
568
569 __disable_irq();
570
571 command[0] = 54;
572 iap(command, result);
573
574 __enable_irq();
575
576 return result[1];
577}
578
d4ee6ee2
JM
579// get network config
580void SimpleShell::net_command( string parameters, StreamOutput *stream)
581{
582 void *returned_data;
6d877d9b 583 bool ok = PublicData::get_value( network_checksum, get_ipconfig_checksum, &returned_data );
d4ee6ee2 584 if(ok) {
6d877d9b 585 char *str = (char *)returned_data;
d4ee6ee2
JM
586 stream->printf("%s\r\n", str);
587 free(str);
588
6d877d9b 589 } else {
d4ee6ee2
JM
590 stream->printf("No network detected\n");
591 }
592}
593
582559c6 594// print out build version
9e403697
JM
595void SimpleShell::version_command( string parameters, StreamOutput *stream)
596{
582559c6 597 Version vers;
9e403697
JM
598 uint32_t dev = getDeviceType();
599 const char *mcu = (dev & 0x00100000) ? "LPC1769" : "LPC1768";
01f35bcc 600 stream->printf("Build version: %s, Build date: %s, MCU: %s, System Clock: %ldMHz\r\n", vers.get_build(), vers.get_build_date(), mcu, SystemCoreClock / 1000000);
582559c6
JM
601}
602
77983aa1 603// Reset the system
9e403697
JM
604void SimpleShell::reset_command( string parameters, StreamOutput *stream)
605{
ead17727 606 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
7e81f138 607 reset_delay_secs = 5; // reboot in 5 seconds
2742fca9
JM
608}
609
610// go into dfu boot mode
9e403697
JM
611void SimpleShell::dfu_command( string parameters, StreamOutput *stream)
612{
ed7c5844
JM
613 stream->printf("Entering boot mode...\r\n");
614 system_reset(true);
77983aa1
L
615}
616
0f0b1656 617// Break out into the MRI debugging system
9e403697
JM
618void SimpleShell::break_command( string parameters, StreamOutput *stream)
619{
0f0b1656
L
620 stream->printf("Entering MRI debug mode...\r\n");
621 __debugbreak();
622}
623
40843ebc
JM
624static int get_active_tool()
625{
335957f5 626 void *returned_data;
40843ebc
JM
627 bool ok = PublicData::get_value(tool_manager_checksum, get_active_tool_checksum, &returned_data);
628 if (ok) {
335957f5
JM
629 int active_tool= *static_cast<int *>(returned_data);
630 return active_tool;
40843ebc
JM
631 } else {
632 return 0;
633 }
634}
635
6c0193b3
JM
636void SimpleShell::grblDP_command( string parameters, StreamOutput *stream)
637{
638 /*
639 [G54:95.000,40.000,-23.600]
640 [G55:0.000,0.000,0.000]
641 [G56:0.000,0.000,0.000]
642 [G57:0.000,0.000,0.000]
643 [G58:0.000,0.000,0.000]
644 [G59:0.000,0.000,0.000]
645 [G28:0.000,0.000,0.000]
646 [G30:0.000,0.000,0.000]
647 [G92:0.000,0.000,0.000]
648 [TLO:0.000]
649 [PRB:0.000,0.000,0.000:0]
650 */
651 std::vector<Robot::wcs_t> v= THEKERNEL->robot->get_wcs_state();
652 int n= std::get<1>(v[0]);
653 for (int i = 1; i <= n; ++i) {
654 stream->printf("[%s:%1.3f,%1.3f,%1.3f]\n", wcs2gcode(i-1).c_str(), std::get<0>(v[i]), std::get<1>(v[i]), std::get<2>(v[i]));
655 }
656
657 stream->printf("[G28:%1.3f,%1.3f,%1.3f]\n", 0.0F, 0.0F, 0.0F);
658 stream->printf("[G30:%1.3f,%1.3f,%1.3f]\n", 0.0F, 0.0F, 0.0F);
659 stream->printf("[G92:%1.3f,%1.3f,%1.3f]\n", std::get<0>(v[n+1]), std::get<1>(v[n+1]), std::get<2>(v[n+1]));
660 stream->printf("[TL0:%1.3f]\n", std::get<2>(v[n+2]));
661
662 // TODO this shoul dbe the last probe position, which will be this if probe was the last thing done
663 float current_machine_pos[3];
664 THEKERNEL->robot->get_axis_position(current_machine_pos);
665 stream->printf("[PRB:%1.3f,%1.3f,%1.3f:%d]\n", current_machine_pos[X_AXIS], current_machine_pos[Y_AXIS], current_machine_pos[Z_AXIS], 0);
666}
667
8293d443 668// used to test out the get public data events
9e403697
JM
669void SimpleShell::get_command( string parameters, StreamOutput *stream)
670{
7e81f138 671 string what = shift_parameter( parameters );
c4e56997 672
7e81f138 673 if (what == "temp") {
3bfb2639 674 struct pad_temperature temp;
9e403697 675 string type = shift_parameter( parameters );
56a6c8c1
JM
676 if(type.empty()) {
677 // scan all temperature controls
678 std::vector<struct pad_temperature> controllers;
679 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
680 if (ok) {
681 for (auto &c : controllers) {
682 stream->printf("%s (%d) temp: %f/%f @%d\r\n", c.designator.c_str(), c.id, c.current_temperature, c.target_temperature, c.pwm);
683 }
b55cfff1 684
56a6c8c1
JM
685 } else {
686 stream->printf("no heaters found\r\n");
687 }
688
689 }else{
690 bool ok = PublicData::get_value( temperature_control_checksum, current_temperature_checksum, get_checksum(type), &temp );
691
692 if (ok) {
693 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
694 } else {
695 stream->printf("%s is not a known temperature device\r\n", type.c_str());
696 }
b55cfff1 697 }
c4e56997 698
7e81f138 699 } else if (what == "pos") {
e03f2747
JM
700 // convenience to call all the various M114 variants
701 char buf[64];
702 THEKERNEL->robot->print_position(0, buf, sizeof buf); stream->printf("last %s\n", buf);
703 THEKERNEL->robot->print_position(1, buf, sizeof buf); stream->printf("realtime %s\n", buf);
704 THEKERNEL->robot->print_position(2, buf, sizeof buf); stream->printf("%s\n", buf);
705 THEKERNEL->robot->print_position(3, buf, sizeof buf); stream->printf("%s\n", buf);
706 THEKERNEL->robot->print_position(4, buf, sizeof buf); stream->printf("%s\n", buf);
707 THEKERNEL->robot->print_position(5, buf, sizeof buf); stream->printf("%s\n", buf);
34210908
JM
708
709 } else if (what == "wcs") {
710 // print the wcs state
711 std::vector<Robot::wcs_t> v= THEKERNEL->robot->get_wcs_state();
712 char current_wcs= std::get<0>(v[0]);
40fd5d98 713 stream->printf("current WCS: %s\n", wcs2gcode(current_wcs).c_str());
34210908
JM
714 int n= std::get<1>(v[0]);
715 for (int i = 1; i <= n; ++i) {
40fd5d98 716 stream->printf("%s: %1.4f, %1.4f, %1.4f\n", wcs2gcode(i-1).c_str(), std::get<0>(v[i]), std::get<1>(v[i]), std::get<2>(v[i]));
34210908
JM
717 }
718
719 stream->printf("G92: %1.4f, %1.4f, %1.4f\n", std::get<0>(v[n+1]), std::get<1>(v[n+1]), std::get<2>(v[n+1]));
720 stream->printf("ToolOffset: %1.4f, %1.4f, %1.4f\n", std::get<0>(v[n+2]), std::get<1>(v[n+2]), std::get<2>(v[n+2]));
40843ebc
JM
721
722 } else if (what == "state") {
6c0193b3 723 // also $G
40843ebc 724 // [G0 G54 G17 G21 G90 G94 M0 M5 M9 T0 F0.]
6c0193b3 725 stream->printf("[G%d %s G%d G%d G%d G94 M0 M5 M9 T%d F%1.1f]\n",
40843ebc
JM
726 THEKERNEL->gcode_dispatch->get_modal_command(),
727 wcs2gcode(THEKERNEL->robot->get_current_wcs()).c_str(),
728 THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Y_AXIS && THEKERNEL->robot->plane_axis_2 == Z_AXIS ? 17 :
729 THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == Y_AXIS ? 18 :
730 THEKERNEL->robot->plane_axis_0 == Y_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == X_AXIS ? 19 : 17,
731 THEKERNEL->robot->inch_mode ? 20 : 21,
732 THEKERNEL->robot->absolute_mode ? 90 : 91,
733 get_active_tool(),
734 THEKERNEL->robot->get_feed_rate());
6c0193b3
JM
735
736 } else {
737 stream->printf("error: unknown option %s\n", what.c_str());
b55cfff1 738 }
8293d443
JM
739}
740
77047e76 741// used to test out the get public data events
9e403697
JM
742void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
743{
744 string type = shift_parameter( parameters );
745 string temp = shift_parameter( parameters );
04211969 746 float t = temp.empty() ? 0.0 : strtof(temp.c_str(), NULL);
75e6428d 747 bool ok = PublicData::set_value( temperature_control_checksum, get_checksum(type), &t );
991d98cc 748
9e403697 749 if (ok) {
991d98cc 750 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
9e403697 751 } else {
991d98cc
JM
752 stream->printf("%s is not a known temperature device\r\n", type.c_str());
753 }
77047e76
JM
754}
755
4c8f5447
JM
756void SimpleShell::print_thermistors_command( string parameters, StreamOutput *stream)
757{
758 Thermistor::print_predefined_thermistors(stream);
759}
760
1f8dab1a
JM
761void SimpleShell::calc_thermistor_command( string parameters, StreamOutput *stream)
762{
bbb839c1
JM
763 string s = shift_parameter( parameters );
764 int saveto= -1;
765 // see if we have -sn as first argument
766 if(s.find("-s", 0, 2) != string::npos) {
767 // save the results to thermistor n
768 saveto= strtol(s.substr(2).c_str(), nullptr, 10);
769 }else{
770 parameters= s;
771 }
772
1f8dab1a
JM
773 std::vector<float> trl= parse_number_list(parameters.c_str());
774 if(trl.size() == 6) {
775 // calculate the coefficients
776 float c1, c2, c3;
777 std::tie(c1, c2, c3) = Thermistor::calculate_steinhart_hart_coefficients(trl[0], trl[1], trl[2], trl[3], trl[4], trl[5]);
778 stream->printf("Steinhart Hart coefficients: I%1.18f J%1.18f K%1.18f\n", c1, c2, c3);
bbb839c1
JM
779 if(saveto == -1) {
780 stream->printf(" Paste the above in the M305 S0 command, then save with M500\n");
781 }else{
782 char buf[80];
783 int n = snprintf(buf, sizeof(buf), "M305 S%d I%1.18f J%1.18f K%1.18f", saveto, c1, c2, c3);
784 string g(buf, n);
785 Gcode gcode(g, &(StreamOutput::NullStream));
786 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode );
787 stream->printf(" Setting Thermistor %d to those settings, save with M500\n", saveto);
788 }
1f8dab1a
JM
789
790 }else{
791 // give help
792 stream->printf("Usage: calc_thermistor T1,R1,T2,R2,T3,R3\n");
793 }
794}
795
ae91dea4
JM
796// used to test out the get public data events for switch
797void SimpleShell::switch_command( string parameters, StreamOutput *stream)
798{
799 string type = shift_parameter( parameters );
800 string value = shift_parameter( parameters );
6d877d9b 801 bool ok = false;
ae91dea4 802 if(value == "on" || value == "off") {
6d877d9b 803 bool b = value == "on";
ae91dea4 804 ok = PublicData::set_value( switch_checksum, get_checksum(type), state_checksum, &b );
6d877d9b 805 } else {
ae91dea4
JM
806 float v = strtof(value.c_str(), NULL);
807 ok = PublicData::set_value( switch_checksum, get_checksum(type), value_checksum, &v );
808 }
809 if (ok) {
810 stream->printf("switch %s set to: %s\r\n", type.c_str(), value.c_str());
811 } else {
812 stream->printf("%s is not a known switch device\r\n", type.c_str());
813 }
814}
815
d55d551b
JM
816void SimpleShell::md5sum_command( string parameters, StreamOutput *stream )
817{
818 string filename = absolute_from_relative(parameters);
819
820 // Open file
821 FILE *lp = fopen(filename.c_str(), "r");
822 if (lp == NULL) {
823 stream->printf("File not found: %s\r\n", filename.c_str());
824 return;
825 }
826 MD5 md5;
827 uint8_t buf[64];
828 do {
829 size_t n= fread(buf, 1, sizeof buf, lp);
830 if(n > 0) md5.update(buf, n);
831 } while(!feof(lp));
832
833 stream->printf("%s %s\n", md5.finalize().hexdigest().c_str(), filename.c_str());
834 fclose(lp);
835}
836
837
838
9e403697
JM
839void SimpleShell::help_command( string parameters, StreamOutput *stream )
840{
ed7c5844 841 stream->printf("Commands:\r\n");
582559c6 842 stream->printf("version\r\n");
ecc610a4 843 stream->printf("mem [-v]\r\n");
3579deea 844 stream->printf("ls [-s] [folder]\r\n");
ed7c5844 845 stream->printf("cd folder\r\n");
c4e56997 846 stream->printf("pwd\r\n");
6c0193b3 847 stream->printf("cat file [limit] [-d 10]\r\n");
9e403697 848 stream->printf("rm file\r\n");
6d877d9b 849 stream->printf("mv file newfile\r\n");
12fb447a 850 stream->printf("remount\r\n");
4eb0e279 851 stream->printf("play file [-v]\r\n");
ed7c5844
JM
852 stream->printf("progress - shows progress of current play\r\n");
853 stream->printf("abort - abort currently playing file\r\n");
c4e56997
JM
854 stream->printf("reset - reset smoothie\r\n");
855 stream->printf("dfu - enter dfu boot loader\r\n");
856 stream->printf("break - break into debugger\r\n");
ed7c5844
JM
857 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
858 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
5647f709 859 stream->printf("get temp [bed|hotend]\r\n");
991d98cc 860 stream->printf("set_temp bed|hotend 185\r\n");
b55cfff1 861 stream->printf("get pos\r\n");
6c0193b3
JM
862 stream->printf("get wcs\r\n");
863 stream->printf("get state\r\n");
d4ee6ee2 864 stream->printf("net\r\n");
618c9b0f
JM
865 stream->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
866 stream->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");
bbb839c1
JM
867 stream->printf("upload filename - saves a stream of text to the named file\r\n");
868 stream->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
4c8f5447 869 stream->printf("thermistors - print out the predefined thermistors\r\n");
d55d551b 870 stream->printf("md5sum file - prints md5 sum of the given file\r\n");
235a7435
JM
871}
872