add to the cat command -d nnn where nnn is the seconds to wait before printing out...
[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);
7f613782 208
7e81f138
JM
209 // ignore comments and blank lines and if this is a G code then also ignore it
210 char first_char = new_message.message[0];
211 if(strchr(";( \n\rGMTN", first_char) != NULL) return;
7f613782 212
b6c86164 213 string possible_command = new_message.message;
0325af12 214
3add9a23 215 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
7e81f138 216 string cmd = shift_parameter(possible_command);
6187a020 217
9e403697 218 // find command and execute it
7e81f138 219 parse_command(cmd.c_str(), possible_command, new_message.stream);
0325af12
AW
220}
221
0325af12
AW
222// Act upon an ls command
223// Convert the first parameter into an absolute path, then list the files in that path
9e403697
JM
224void SimpleShell::ls_command( string parameters, StreamOutput *stream )
225{
3579deea
JM
226 string path, opts;
227 while(!parameters.empty()) {
6d877d9b 228 string s = shift_parameter( parameters );
3579deea
JM
229 if(s.front() == '-') {
230 opts.append(s);
231 } else {
6d877d9b
JM
232 path = s;
233 if(!parameters.empty()) {
3579deea
JM
234 path.append(" ");
235 path.append(parameters);
236 }
237 break;
238 }
239 }
b557a801 240
6d877d9b 241 path = absolute_from_relative(path);
3579deea 242
9e403697
JM
243 DIR *d;
244 struct dirent *p;
3579deea 245 d = opendir(path.c_str());
9e403697
JM
246 if (d != NULL) {
247 while ((p = readdir(d)) != NULL) {
3579deea 248 stream->printf("%s", lc(string(p->d_name)).c_str());
6d877d9b 249 if(p->d_isdir) {
3579deea 250 stream->printf("/");
6d877d9b 251 } else if(opts.find("-s", 0, 2) != string::npos) {
3579deea
JM
252 stream->printf(" %d", p->d_fsize);
253 }
254 stream->printf("\r\n");
9e403697 255 }
ed7c5844 256 closedir(d);
0325af12 257 } else {
3579deea 258 stream->printf("Could not open directory %s\r\n", path.c_str());
0325af12
AW
259 }
260}
261
3704585b 262extern SDFAT mounter;
263
264void SimpleShell::remount_command( string parameters, StreamOutput *stream )
265{
266 mounter.remount();
48afc62a 267 stream->printf("remounted\r\n");
12fb447a 268}
3704585b 269
9e403697
JM
270// Delete a file
271void SimpleShell::rm_command( string parameters, StreamOutput *stream )
272{
6d877d9b 273 const char *fn = absolute_from_relative(shift_parameter( parameters )).c_str();
9e403697
JM
274 int s = remove(fn);
275 if (s != 0) stream->printf("Could not delete %s \r\n", fn);
276}
277
6d877d9b
JM
278// Rename a file
279void SimpleShell::mv_command( string parameters, StreamOutput *stream )
280{
281 string from = absolute_from_relative(shift_parameter( parameters ));
a940483b 282 string to = absolute_from_relative(shift_parameter(parameters));
6d877d9b
JM
283 int s = rename(from.c_str(), to.c_str());
284 if (s != 0) stream->printf("Could not rename %s to %s\r\n", from.c_str(), to.c_str());
285 else stream->printf("renamed %s to %s\r\n", from.c_str(), to.c_str());
286}
287
0325af12 288// Change current absolute path to provided path
9e403697
JM
289void SimpleShell::cd_command( string parameters, StreamOutput *stream )
290{
75f4581c 291 string folder = absolute_from_relative( parameters );
6bcd4886 292
0325af12 293 DIR *d;
0325af12 294 d = opendir(folder.c_str());
9e403697 295 if (d == NULL) {
58baeec1 296 stream->printf("Could not open directory %s \r\n", folder.c_str() );
9e403697 297 } else {
75f4581c 298 THEKERNEL->current_path = folder;
ed7c5844 299 closedir(d);
0325af12
AW
300 }
301}
302
b7250484 303// Responds with the present working directory
9e403697
JM
304void SimpleShell::pwd_command( string parameters, StreamOutput *stream )
305{
75f4581c 306 stream->printf("%s\r\n", THEKERNEL->current_path.c_str());
b7250484
L
307}
308
0325af12 309// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
9e403697
JM
310void SimpleShell::cat_command( string parameters, StreamOutput *stream )
311{
58baeec1 312 // Get parameters ( filename and line limit )
75f4581c 313 string filename = absolute_from_relative(shift_parameter( parameters ));
1f61f177 314 string limit_parameter = shift_parameter( parameters );
0325af12 315 int limit = -1;
1f61f177
JM
316 int delay= 0;
317 if ( limit_parameter == "-d" ) {
318 string d= shift_parameter( parameters );
9e403697 319 char *e = NULL;
1f61f177
JM
320 delay = strtol(d.c_str(), &e, 10);
321 if (e <= limit_parameter.c_str())
322 delay = 0;
323
324 }else if ( limit_parameter != "" ) {
325 char *e = NULL;
326 limit = strtol(limit_parameter.c_str(), &e, 10);
327 if (e <= limit_parameter.c_str())
f7e6f459
MM
328 limit = -1;
329 }
58baeec1 330
1f61f177
JM
331 // we have been asked to delay before cat, probably to allow time to issue upload command
332 while(delay-- > 0) {
333 for (int i = 0; i < 10; ++i) {
334 wait_ms(100);
335 THEKERNEL->call_event(ON_IDLE);
336 }
337 }
338
58baeec1 339 // Open file
0325af12 340 FILE *lp = fopen(filename.c_str(), "r");
9e403697 341 if (lp == NULL) {
58baeec1
MM
342 stream->printf("File not found: %s\r\n", filename.c_str());
343 return;
9ed670c5 344 }
0325af12
AW
345 string buffer;
346 int c;
58baeec1 347 int newlines = 0;
6d877d9b 348 int linecnt = 0;
0325af12 349 // Print each line of the file
9e403697 350 while ((c = fgetc (lp)) != EOF) {
58baeec1 351 buffer.append((char *)&c, 1);
dfb15d68 352 if ( char(c) == '\n' || ++linecnt > 80) {
58baeec1 353 newlines++;
d728799b 354 stream->puts(buffer.c_str());
58baeec1 355 buffer.clear();
6d877d9b 356 if(linecnt > 80) linecnt = 0;
6757ce1a
JM
357 // we need to kick things or they die
358 THEKERNEL->call_event(ON_IDLE);
68b7afb4 359 }
9e403697
JM
360 if ( newlines == limit ) {
361 break;
362 }
58baeec1 363 };
0325af12 364 fclose(lp);
6d877d9b
JM
365}
366
367void SimpleShell::upload_command( string parameters, StreamOutput *stream )
368{
369 // this needs to be a hack. it needs to read direct from serial and not allow on_main_loop run until done
370 // NOTE this will block all operation until the upload is complete, so do not do while printing
371 if(!THEKERNEL->conveyor->is_queue_empty()) {
372 stream->printf("upload not allowed while printing or busy\n");
373 return;
374 }
375
376 // open file to upload to
377 string upload_filename = absolute_from_relative( parameters );
378 FILE *fd = fopen(upload_filename.c_str(), "w");
379 if(fd != NULL) {
380 stream->printf("uploading to file: %s, send control-D or control-Z to finish\r\n", upload_filename.c_str());
381 } else {
382 stream->printf("failed to open file: %s.\r\n", upload_filename.c_str());
383 return;
384 }
0325af12 385
6d877d9b
JM
386 int cnt = 0;
387 bool uploading = true;
388 while(uploading) {
389 if(!stream->ready()) {
390 // we need to kick things or they die
391 THEKERNEL->call_event(ON_IDLE);
392 continue;
393 }
394
395 char c = stream->_getc();
396 if( c == 4 || c == 26) { // ctrl-D or ctrl-Z
397 uploading = false;
398 // close file
399 fclose(fd);
400 stream->printf("uploaded %d bytes\n", cnt);
401 return;
402
403 } else {
404 // write character to file
405 cnt++;
406 if(fputc(c, fd) != c) {
407 // error writing to file
408 stream->printf("error writing to file. ignoring all characters until EOF\r\n");
409 fclose(fd);
410 fd = NULL;
411 uploading= false;
412
413 } else {
414 if ((cnt%400) == 0) {
415 // HACK ALERT to get around fwrite corruption close and re open for append
416 fclose(fd);
417 fd = fopen(upload_filename.c_str(), "a");
6757ce1a
JM
418 // we need to kick things or they die
419 THEKERNEL->call_event(ON_IDLE);
6d877d9b
JM
420 }
421 }
422 }
423 }
424 // we got an error so ignore everything until EOF
425 char c;
426 do {
427 if(stream->ready()) {
428 c= stream->_getc();
429 }else{
430 THEKERNEL->call_event(ON_IDLE);
431 c= 0;
432 }
433 } while(c != 4 && c != 26);
0325af12
AW
434}
435
618c9b0f
JM
436// loads the specified config-override file
437void SimpleShell::load_command( string parameters, StreamOutput *stream )
438{
439 // Get parameters ( filename )
75f4581c 440 string filename = absolute_from_relative(parameters);
618c9b0f
JM
441 if(filename == "/") {
442 filename = THEKERNEL->config_override_filename();
443 }
444
6d877d9b 445 FILE *fp = fopen(filename.c_str(), "r");
618c9b0f
JM
446 if(fp != NULL) {
447 char buf[132];
448 stream->printf("Loading config override file: %s...\n", filename.c_str());
449 while(fgets(buf, sizeof buf, fp) != NULL) {
450 stream->printf(" %s", buf);
451 if(buf[0] == ';') continue; // skip the comments
6d877d9b 452 struct SerialMessage message = {&(StreamOutput::NullStream), buf};
618c9b0f
JM
453 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
454 }
455 stream->printf("config override file executed\n");
456 fclose(fp);
457
6d877d9b 458 } else {
618c9b0f
JM
459 stream->printf("File not found: %s\n", filename.c_str());
460 }
461}
462
463// saves the specified config-override file
464void SimpleShell::save_command( string parameters, StreamOutput *stream )
465{
466 // Get parameters ( filename )
75f4581c 467 string filename = absolute_from_relative(parameters);
618c9b0f
JM
468 if(filename == "/") {
469 filename = THEKERNEL->config_override_filename();
470 }
471
23eb804b
JM
472 THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run
473
06afe68b
JM
474 //remove(filename.c_str()); // seems to cause a hang every now and then
475 {
476 FileStream fs(filename.c_str());
477 fs.printf("; DO NOT EDIT THIS FILE\n");
478 // this also will truncate the existing file instead of deleting it
479 }
480
23eb804b 481 // stream that appends to file
4fed9ba1
JM
482 AppendFileStream *gs = new AppendFileStream(filename.c_str());
483 // if(!gs->is_open()) {
484 // stream->printf("Unable to open File %s for write\n", filename.c_str());
485 // return;
486 // }
618c9b0f 487
7acfedab 488 __disable_irq();
618c9b0f
JM
489 // issue a M500 which will store values in the file stream
490 Gcode *gcode = new Gcode("M500", gs);
491 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
492 delete gs;
493 delete gcode;
7acfedab 494 __enable_irq();
618c9b0f
JM
495
496 stream->printf("Settings Stored to %s\r\n", filename.c_str());
497}
498
6187a020 499// show free memory
9e403697
JM
500void SimpleShell::mem_command( string parameters, StreamOutput *stream)
501{
502 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos ;
503 unsigned long heap = (unsigned long)_sbrk(0);
504 unsigned long m = g_maximumHeapAddress - heap;
ecc610a4
JM
505 stream->printf("Unused Heap: %lu bytes\r\n", m);
506
6d877d9b 507 uint32_t f = heapWalk(stream, verbose);
0c683b26 508 stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
a200fc31 509
0c683b26 510 stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
6d877d9b 511 if (verbose) {
1803076a
MM
512 AHB0.debug(stream);
513 AHB1.debug(stream);
514 }
6187a020
JM
515}
516
9e403697
JM
517static uint32_t getDeviceType()
518{
519#define IAP_LOCATION 0x1FFF1FF1
01f35bcc
JM
520 uint32_t command[1];
521 uint32_t result[5];
9e403697 522 typedef void (*IAP)(uint32_t *, uint32_t *);
01f35bcc
JM
523 IAP iap = (IAP) IAP_LOCATION;
524
525 __disable_irq();
526
527 command[0] = 54;
528 iap(command, result);
529
530 __enable_irq();
531
532 return result[1];
533}
534
d4ee6ee2
JM
535// get network config
536void SimpleShell::net_command( string parameters, StreamOutput *stream)
537{
538 void *returned_data;
6d877d9b 539 bool ok = PublicData::get_value( network_checksum, get_ipconfig_checksum, &returned_data );
d4ee6ee2 540 if(ok) {
6d877d9b 541 char *str = (char *)returned_data;
d4ee6ee2
JM
542 stream->printf("%s\r\n", str);
543 free(str);
544
6d877d9b 545 } else {
d4ee6ee2
JM
546 stream->printf("No network detected\n");
547 }
548}
549
582559c6 550// print out build version
9e403697
JM
551void SimpleShell::version_command( string parameters, StreamOutput *stream)
552{
582559c6 553 Version vers;
9e403697
JM
554 uint32_t dev = getDeviceType();
555 const char *mcu = (dev & 0x00100000) ? "LPC1769" : "LPC1768";
01f35bcc 556 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
557}
558
77983aa1 559// Reset the system
9e403697
JM
560void SimpleShell::reset_command( string parameters, StreamOutput *stream)
561{
ead17727 562 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
7e81f138 563 reset_delay_secs = 5; // reboot in 5 seconds
2742fca9
JM
564}
565
566// go into dfu boot mode
9e403697
JM
567void SimpleShell::dfu_command( string parameters, StreamOutput *stream)
568{
ed7c5844
JM
569 stream->printf("Entering boot mode...\r\n");
570 system_reset(true);
77983aa1
L
571}
572
0f0b1656 573// Break out into the MRI debugging system
9e403697
JM
574void SimpleShell::break_command( string parameters, StreamOutput *stream)
575{
0f0b1656
L
576 stream->printf("Entering MRI debug mode...\r\n");
577 __debugbreak();
578}
579
40843ebc
JM
580static int get_active_tool()
581{
335957f5 582 void *returned_data;
40843ebc
JM
583 bool ok = PublicData::get_value(tool_manager_checksum, get_active_tool_checksum, &returned_data);
584 if (ok) {
335957f5
JM
585 int active_tool= *static_cast<int *>(returned_data);
586 return active_tool;
40843ebc
JM
587 } else {
588 return 0;
589 }
590}
591
8293d443 592// used to test out the get public data events
9e403697
JM
593void SimpleShell::get_command( string parameters, StreamOutput *stream)
594{
7e81f138 595 string what = shift_parameter( parameters );
c4e56997 596
7e81f138 597 if (what == "temp") {
3bfb2639 598 struct pad_temperature temp;
9e403697 599 string type = shift_parameter( parameters );
56a6c8c1
JM
600 if(type.empty()) {
601 // scan all temperature controls
602 std::vector<struct pad_temperature> controllers;
603 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
604 if (ok) {
605 for (auto &c : controllers) {
606 stream->printf("%s (%d) temp: %f/%f @%d\r\n", c.designator.c_str(), c.id, c.current_temperature, c.target_temperature, c.pwm);
607 }
b55cfff1 608
56a6c8c1
JM
609 } else {
610 stream->printf("no heaters found\r\n");
611 }
612
613 }else{
614 bool ok = PublicData::get_value( temperature_control_checksum, current_temperature_checksum, get_checksum(type), &temp );
615
616 if (ok) {
617 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
618 } else {
619 stream->printf("%s is not a known temperature device\r\n", type.c_str());
620 }
b55cfff1 621 }
c4e56997 622
7e81f138 623 } else if (what == "pos") {
e03f2747
JM
624 // convenience to call all the various M114 variants
625 char buf[64];
626 THEKERNEL->robot->print_position(0, buf, sizeof buf); stream->printf("last %s\n", buf);
627 THEKERNEL->robot->print_position(1, buf, sizeof buf); stream->printf("realtime %s\n", buf);
628 THEKERNEL->robot->print_position(2, buf, sizeof buf); stream->printf("%s\n", buf);
629 THEKERNEL->robot->print_position(3, buf, sizeof buf); stream->printf("%s\n", buf);
630 THEKERNEL->robot->print_position(4, buf, sizeof buf); stream->printf("%s\n", buf);
631 THEKERNEL->robot->print_position(5, buf, sizeof buf); stream->printf("%s\n", buf);
34210908
JM
632
633 } else if (what == "wcs") {
634 // print the wcs state
635 std::vector<Robot::wcs_t> v= THEKERNEL->robot->get_wcs_state();
636 char current_wcs= std::get<0>(v[0]);
40fd5d98 637 stream->printf("current WCS: %s\n", wcs2gcode(current_wcs).c_str());
34210908
JM
638 int n= std::get<1>(v[0]);
639 for (int i = 1; i <= n; ++i) {
40fd5d98 640 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
641 }
642
643 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]));
644 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
645
646 } else if (what == "state") {
647 // [G0 G54 G17 G21 G90 G94 M0 M5 M9 T0 F0.]
648 stream->printf("[G%d %s G%d G%d G%d G94 T%d F%1.1f]\n",
649 THEKERNEL->gcode_dispatch->get_modal_command(),
650 wcs2gcode(THEKERNEL->robot->get_current_wcs()).c_str(),
651 THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Y_AXIS && THEKERNEL->robot->plane_axis_2 == Z_AXIS ? 17 :
652 THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == Y_AXIS ? 18 :
653 THEKERNEL->robot->plane_axis_0 == Y_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == X_AXIS ? 19 : 17,
654 THEKERNEL->robot->inch_mode ? 20 : 21,
655 THEKERNEL->robot->absolute_mode ? 90 : 91,
656 get_active_tool(),
657 THEKERNEL->robot->get_feed_rate());
b55cfff1 658 }
8293d443
JM
659}
660
77047e76 661// used to test out the get public data events
9e403697
JM
662void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
663{
664 string type = shift_parameter( parameters );
665 string temp = shift_parameter( parameters );
04211969 666 float t = temp.empty() ? 0.0 : strtof(temp.c_str(), NULL);
75e6428d 667 bool ok = PublicData::set_value( temperature_control_checksum, get_checksum(type), &t );
991d98cc 668
9e403697 669 if (ok) {
991d98cc 670 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
9e403697 671 } else {
991d98cc
JM
672 stream->printf("%s is not a known temperature device\r\n", type.c_str());
673 }
77047e76
JM
674}
675
4c8f5447
JM
676void SimpleShell::print_thermistors_command( string parameters, StreamOutput *stream)
677{
678 Thermistor::print_predefined_thermistors(stream);
679}
680
1f8dab1a
JM
681void SimpleShell::calc_thermistor_command( string parameters, StreamOutput *stream)
682{
bbb839c1
JM
683 string s = shift_parameter( parameters );
684 int saveto= -1;
685 // see if we have -sn as first argument
686 if(s.find("-s", 0, 2) != string::npos) {
687 // save the results to thermistor n
688 saveto= strtol(s.substr(2).c_str(), nullptr, 10);
689 }else{
690 parameters= s;
691 }
692
1f8dab1a
JM
693 std::vector<float> trl= parse_number_list(parameters.c_str());
694 if(trl.size() == 6) {
695 // calculate the coefficients
696 float c1, c2, c3;
697 std::tie(c1, c2, c3) = Thermistor::calculate_steinhart_hart_coefficients(trl[0], trl[1], trl[2], trl[3], trl[4], trl[5]);
698 stream->printf("Steinhart Hart coefficients: I%1.18f J%1.18f K%1.18f\n", c1, c2, c3);
bbb839c1
JM
699 if(saveto == -1) {
700 stream->printf(" Paste the above in the M305 S0 command, then save with M500\n");
701 }else{
702 char buf[80];
703 int n = snprintf(buf, sizeof(buf), "M305 S%d I%1.18f J%1.18f K%1.18f", saveto, c1, c2, c3);
704 string g(buf, n);
705 Gcode gcode(g, &(StreamOutput::NullStream));
706 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode );
707 stream->printf(" Setting Thermistor %d to those settings, save with M500\n", saveto);
708 }
1f8dab1a
JM
709
710 }else{
711 // give help
712 stream->printf("Usage: calc_thermistor T1,R1,T2,R2,T3,R3\n");
713 }
714}
715
ae91dea4
JM
716// used to test out the get public data events for switch
717void SimpleShell::switch_command( string parameters, StreamOutput *stream)
718{
719 string type = shift_parameter( parameters );
720 string value = shift_parameter( parameters );
6d877d9b 721 bool ok = false;
ae91dea4 722 if(value == "on" || value == "off") {
6d877d9b 723 bool b = value == "on";
ae91dea4 724 ok = PublicData::set_value( switch_checksum, get_checksum(type), state_checksum, &b );
6d877d9b 725 } else {
ae91dea4
JM
726 float v = strtof(value.c_str(), NULL);
727 ok = PublicData::set_value( switch_checksum, get_checksum(type), value_checksum, &v );
728 }
729 if (ok) {
730 stream->printf("switch %s set to: %s\r\n", type.c_str(), value.c_str());
731 } else {
732 stream->printf("%s is not a known switch device\r\n", type.c_str());
733 }
734}
735
d55d551b
JM
736void SimpleShell::md5sum_command( string parameters, StreamOutput *stream )
737{
738 string filename = absolute_from_relative(parameters);
739
740 // Open file
741 FILE *lp = fopen(filename.c_str(), "r");
742 if (lp == NULL) {
743 stream->printf("File not found: %s\r\n", filename.c_str());
744 return;
745 }
746 MD5 md5;
747 uint8_t buf[64];
748 do {
749 size_t n= fread(buf, 1, sizeof buf, lp);
750 if(n > 0) md5.update(buf, n);
751 } while(!feof(lp));
752
753 stream->printf("%s %s\n", md5.finalize().hexdigest().c_str(), filename.c_str());
754 fclose(lp);
755}
756
757
758
9e403697
JM
759void SimpleShell::help_command( string parameters, StreamOutput *stream )
760{
ed7c5844 761 stream->printf("Commands:\r\n");
582559c6 762 stream->printf("version\r\n");
ecc610a4 763 stream->printf("mem [-v]\r\n");
3579deea 764 stream->printf("ls [-s] [folder]\r\n");
ed7c5844 765 stream->printf("cd folder\r\n");
c4e56997 766 stream->printf("pwd\r\n");
ed7c5844 767 stream->printf("cat file [limit]\r\n");
9e403697 768 stream->printf("rm file\r\n");
6d877d9b 769 stream->printf("mv file newfile\r\n");
12fb447a 770 stream->printf("remount\r\n");
4eb0e279 771 stream->printf("play file [-v]\r\n");
ed7c5844
JM
772 stream->printf("progress - shows progress of current play\r\n");
773 stream->printf("abort - abort currently playing file\r\n");
c4e56997
JM
774 stream->printf("reset - reset smoothie\r\n");
775 stream->printf("dfu - enter dfu boot loader\r\n");
776 stream->printf("break - break into debugger\r\n");
ed7c5844
JM
777 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
778 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
5647f709 779 stream->printf("get temp [bed|hotend]\r\n");
991d98cc 780 stream->printf("set_temp bed|hotend 185\r\n");
b55cfff1 781 stream->printf("get pos\r\n");
d4ee6ee2 782 stream->printf("net\r\n");
618c9b0f
JM
783 stream->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
784 stream->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");
bbb839c1
JM
785 stream->printf("upload filename - saves a stream of text to the named file\r\n");
786 stream->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
4c8f5447 787 stream->printf("thermistors - print out the predefined thermistors\r\n");
d55d551b 788 stream->printf("md5sum file - prints md5 sum of the given file\r\n");
235a7435
JM
789}
790