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