update firmware-latest.bin
[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"
2577a122 28#include "BaseSolution.h"
586cc733 29#include "StepperMotor.h"
3e54c9fc 30#include "Configurator.h"
8a9f9313 31#include "Block.h"
61134a65 32
40843ebc 33#include "TemperatureControlPublicAccess.h"
9339253b 34#include "EndstopsPublicAccess.h"
d4ee6ee2 35#include "NetworkPublicAccess.h"
a200fc31 36#include "platform_memory.h"
ae91dea4 37#include "SwitchPublicAccess.h"
3704585b 38#include "SDFAT.h"
1f8dab1a 39#include "Thermistor.h"
d55d551b 40#include "md5.h"
1aa3d42f 41#include "utils.h"
47339e4a 42
61134a65
JM
43#include "system_LPC17xx.h"
44#include "LPC17xx.h"
45
23eb804b
JM
46#include "mbed.h" // for wait_ms()
47
6187a020
JM
48extern unsigned int g_maximumHeapAddress;
49
ecc610a4
JM
50#include <malloc.h>
51#include <mri.h>
52#include <stdio.h>
53#include <stdint.h>
be7f67cd 54#include <functional>
ecc610a4
JM
55
56extern "C" uint32_t __end__;
57extern "C" uint32_t __malloc_free_list;
58extern "C" uint32_t _sbrk(int size);
59
04d608c4 60
9e403697 61// command lookup table
7e81f138
JM
62const SimpleShell::ptentry_t SimpleShell::commands_table[] = {
63 {"ls", SimpleShell::ls_command},
64 {"cd", SimpleShell::cd_command},
65 {"pwd", SimpleShell::pwd_command},
66 {"cat", SimpleShell::cat_command},
67 {"rm", SimpleShell::rm_command},
6d877d9b 68 {"mv", SimpleShell::mv_command},
04d608c4 69 {"mkdir", SimpleShell::mkdir_command},
6d877d9b 70 {"upload", SimpleShell::upload_command},
7e81f138
JM
71 {"reset", SimpleShell::reset_command},
72 {"dfu", SimpleShell::dfu_command},
73 {"break", SimpleShell::break_command},
74 {"help", SimpleShell::help_command},
75 {"?", SimpleShell::help_command},
76 {"version", SimpleShell::version_command},
77 {"mem", SimpleShell::mem_command},
78 {"get", SimpleShell::get_command},
79 {"set_temp", SimpleShell::set_temp_command},
ae91dea4 80 {"switch", SimpleShell::switch_command},
7e81f138
JM
81 {"net", SimpleShell::net_command},
82 {"load", SimpleShell::load_command},
83 {"save", SimpleShell::save_command},
6d877d9b 84 {"remount", SimpleShell::remount_command},
1f8dab1a 85 {"calc_thermistor", SimpleShell::calc_thermistor_command},
4c8f5447 86 {"thermistors", SimpleShell::print_thermistors_command},
d55d551b 87 {"md5sum", SimpleShell::md5sum_command},
518b5c4c 88 {"test", SimpleShell::test_command},
be7f67cd 89
9e403697 90 // unknown command
7e81f138 91 {NULL, NULL}
9e403697 92};
ecc610a4 93
6d877d9b 94int SimpleShell::reset_delay_secs = 0;
7e81f138 95
ecc610a4 96// Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
0c683b26 97static uint32_t heapWalk(StreamOutput *stream, bool verbose)
ecc610a4
JM
98{
99 uint32_t chunkNumber = 1;
100 // The __end__ linker symbol points to the beginning of the heap.
101 uint32_t chunkCurr = (uint32_t)&__end__;
102 // __malloc_free_list is the head pointer to newlib-nano's link list of free chunks.
103 uint32_t freeCurr = __malloc_free_list;
104 // Calling _sbrk() with 0 reserves no more memory but it returns the current top of heap.
105 uint32_t heapEnd = _sbrk(0);
106 // accumulate totals
9e403697
JM
107 uint32_t freeSize = 0;
108 uint32_t usedSize = 0;
ecc610a4
JM
109
110 stream->printf("Used Heap Size: %lu\n", heapEnd - chunkCurr);
111
112 // Walk through the chunks until we hit the end of the heap.
9e403697 113 while (chunkCurr < heapEnd) {
ecc610a4
JM
114 // Assume the chunk is in use. Will update later.
115 int isChunkFree = 0;
116 // The first 32-bit word in a chunk is the size of the allocation. newlib-nano over allocates by 8 bytes.
117 // 4 bytes for this 32-bit chunk size and another 4 bytes to allow for 8 byte-alignment of returned pointer.
9e403697 118 uint32_t chunkSize = *(uint32_t *)chunkCurr;
ecc610a4
JM
119 // The start of the next chunk is right after the end of this one.
120 uint32_t chunkNext = chunkCurr + chunkSize;
121
122 // The free list is sorted by address.
123 // Check to see if we have found the next free chunk in the heap.
9e403697 124 if (chunkCurr == freeCurr) {
ecc610a4
JM
125 // Chunk is free so flag it as such.
126 isChunkFree = 1;
127 // The second 32-bit word in a free chunk is a pointer to the next free chunk (again sorted by address).
9e403697 128 freeCurr = *(uint32_t *)(freeCurr + 4);
ecc610a4
JM
129 }
130
131 // Skip past the 32-bit size field in the chunk header.
132 chunkCurr += 4;
133 // 8-byte align the data pointer.
134 chunkCurr = (chunkCurr + 7) & ~7;
135 // newlib-nano over allocates by 8 bytes, 4 bytes for the 32-bit chunk size and another 4 bytes to allow for 8
136 // byte-alignment of the returned pointer.
137 chunkSize -= 8;
9e403697 138 if (verbose)
ecc610a4
JM
139 stream->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber, chunkCurr, chunkSize, isChunkFree ? "CHUNK FREE" : "");
140
9e403697 141 if (isChunkFree) freeSize += chunkSize;
ecc610a4
JM
142 else usedSize += chunkSize;
143
144 chunkCurr = chunkNext;
145 chunkNumber++;
146 }
147 stream->printf("Allocated: %lu, Free: %lu\r\n", usedSize, freeSize);
0c683b26 148 return freeSize;
ecc610a4
JM
149}
150
151
9e403697
JM
152void SimpleShell::on_module_loaded()
153{
0325af12 154 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
48afc62a 155 this->register_for_event(ON_GCODE_RECEIVED);
156 this->register_for_event(ON_SECOND_TICK);
c4e56997 157
7e81f138 158 reset_delay_secs = 0;
ead17727
JM
159}
160
9e403697
JM
161void SimpleShell::on_second_tick(void *)
162{
ead17727 163 // we are timing out for the reset
7e81f138
JM
164 if (reset_delay_secs > 0) {
165 if (--reset_delay_secs == 0) {
ead17727
JM
166 system_reset(false);
167 }
168 }
0325af12
AW
169}
170
9e403697
JM
171void SimpleShell::on_gcode_received(void *argument)
172{
173 Gcode *gcode = static_cast<Gcode *>(argument);
6d877d9b 174 string args = get_arguments(gcode->get_command());
c4e56997
JM
175
176 if (gcode->has_m) {
177 if (gcode->m == 20) { // list sd card
c4e56997
JM
178 gcode->stream->printf("Begin file list\r\n");
179 ls_command("/sd", gcode->stream);
180 gcode->stream->printf("End file list\r\n");
d4ee6ee2
JM
181
182 } else if (gcode->m == 30) { // remove file
01a8d21a
JM
183 if(!args.empty() && !THEKERNEL->is_grbl_mode())
184 rm_command("/sd/" + args, gcode->stream);
3a238fdc 185 }
c4e56997
JM
186 }
187}
188
7e81f138 189bool SimpleShell::parse_command(const char *cmd, string args, StreamOutput *stream)
9e403697 190{
7e81f138
JM
191 for (const ptentry_t *p = commands_table; p->command != NULL; ++p) {
192 if (strncasecmp(cmd, p->command, strlen(p->command)) == 0) {
193 p->func(args, stream);
9e403697
JM
194 return true;
195 }
196 }
197
198 return false;
199}
200
0325af12 201// When a new line is received, check if it is a command, and if it is, act upon it
9e403697
JM
202void SimpleShell::on_console_line_received( void *argument )
203{
204 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
6c0193b3 205 string possible_command = new_message.message;
7f613782 206
6c0193b3
JM
207 // ignore anything that is not lowercase or a $ as it is not a command
208 if(possible_command.size() == 0 || (!islower(possible_command[0]) && possible_command[0] != '$')) {
209 return;
210 }
7f613782 211
6c0193b3
JM
212 // it is a grbl compatible command
213 if(possible_command[0] == '$' && possible_command.size() >= 2) {
214 switch(possible_command[1]) {
215 case 'G':
216 // issue get state
217 get_command("state", new_message.stream);
543c4b6d 218 new_message.stream->printf("ok\n");
6c0193b3
JM
219 break;
220
221 case 'X':
b9ad75de
JM
222 if(THEKERNEL->is_halted()) {
223 THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt
224 new_message.stream->printf("[Caution: Unlocked]\nok\n");
225 }
6c0193b3
JM
226 break;
227
228 case '#':
229 grblDP_command("", new_message.stream);
543c4b6d 230 new_message.stream->printf("ok\n");
6c0193b3
JM
231 break;
232
233 case 'H':
6ab94cbd 234 if(THEKERNEL->is_halted()) THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt
07186543 235 if(THEKERNEL->is_grbl_mode()) {
8ad60a4c 236 // issue G28.2 which is force homing cycle
53ece53b 237 Gcode gcode("G28.2", new_message.stream);
6c0193b3 238 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode);
07186543 239 }else{
9869200b
JM
240 Gcode gcode("G28", new_message.stream);
241 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode);
6c0193b3 242 }
7f44e9e1 243 new_message.stream->printf("ok\n");
6c0193b3 244 break;
0325af12 245
6c0193b3 246 default:
07186543 247 new_message.stream->printf("error:Invalid statement\n");
6c0193b3
JM
248 break;
249 }
250
251 }else{
6187a020 252
6c0193b3
JM
253 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
254 string cmd = shift_parameter(possible_command);
255
3e54c9fc
JM
256 // Configurator commands
257 if (cmd == "config-get"){
258 THEKERNEL->configurator->config_get_command( possible_command, new_message.stream );
259
260 } else if (cmd == "config-set"){
261 THEKERNEL->configurator->config_set_command( possible_command, new_message.stream );
262
263 } else if (cmd == "config-load"){
264 THEKERNEL->configurator->config_load_command( possible_command, new_message.stream );
265
266 } else if (cmd == "play" || cmd == "progress" || cmd == "abort" || cmd == "suspend" || cmd == "resume") {
01a8d21a 267 // these are handled by Player module
93f20a8c 268
7ed2d732
JM
269 } else if (cmd == "fire") {
270 // these are handled by Laser module
271
93f20a8c
JM
272 } else if (cmd == "ok") {
273 // probably an echo so reply ok
274 new_message.stream->printf("ok\n");
3e54c9fc
JM
275
276 }else if(!parse_command(cmd.c_str(), possible_command, new_message.stream)) {
42bbc035 277 new_message.stream->printf("error:Unsupported command - %s\n", cmd.c_str());
6c0193b3
JM
278 }
279 }
0325af12
AW
280}
281
0325af12
AW
282// Act upon an ls command
283// Convert the first parameter into an absolute path, then list the files in that path
9e403697
JM
284void SimpleShell::ls_command( string parameters, StreamOutput *stream )
285{
3579deea
JM
286 string path, opts;
287 while(!parameters.empty()) {
6d877d9b 288 string s = shift_parameter( parameters );
3579deea
JM
289 if(s.front() == '-') {
290 opts.append(s);
291 } else {
6d877d9b
JM
292 path = s;
293 if(!parameters.empty()) {
3579deea
JM
294 path.append(" ");
295 path.append(parameters);
296 }
297 break;
298 }
299 }
b557a801 300
6d877d9b 301 path = absolute_from_relative(path);
3579deea 302
9e403697
JM
303 DIR *d;
304 struct dirent *p;
3579deea 305 d = opendir(path.c_str());
9e403697
JM
306 if (d != NULL) {
307 while ((p = readdir(d)) != NULL) {
3579deea 308 stream->printf("%s", lc(string(p->d_name)).c_str());
6d877d9b 309 if(p->d_isdir) {
3579deea 310 stream->printf("/");
6d877d9b 311 } else if(opts.find("-s", 0, 2) != string::npos) {
3579deea
JM
312 stream->printf(" %d", p->d_fsize);
313 }
314 stream->printf("\r\n");
9e403697 315 }
ed7c5844 316 closedir(d);
0325af12 317 } else {
3579deea 318 stream->printf("Could not open directory %s\r\n", path.c_str());
0325af12
AW
319 }
320}
321
3704585b 322extern SDFAT mounter;
323
324void SimpleShell::remount_command( string parameters, StreamOutput *stream )
325{
326 mounter.remount();
48afc62a 327 stream->printf("remounted\r\n");
12fb447a 328}
3704585b 329
9e403697
JM
330// Delete a file
331void SimpleShell::rm_command( string parameters, StreamOutput *stream )
332{
6d877d9b 333 const char *fn = absolute_from_relative(shift_parameter( parameters )).c_str();
9e403697
JM
334 int s = remove(fn);
335 if (s != 0) stream->printf("Could not delete %s \r\n", fn);
336}
337
6d877d9b
JM
338// Rename a file
339void SimpleShell::mv_command( string parameters, StreamOutput *stream )
340{
341 string from = absolute_from_relative(shift_parameter( parameters ));
a940483b 342 string to = absolute_from_relative(shift_parameter(parameters));
6d877d9b
JM
343 int s = rename(from.c_str(), to.c_str());
344 if (s != 0) stream->printf("Could not rename %s to %s\r\n", from.c_str(), to.c_str());
345 else stream->printf("renamed %s to %s\r\n", from.c_str(), to.c_str());
346}
347
04d608c4
MM
348// Create a new directory
349void SimpleShell::mkdir_command( string parameters, StreamOutput *stream )
350{
351 string path = absolute_from_relative(shift_parameter( parameters ));
352 int result = mkdir(path.c_str(), 0);
353 if (result != 0) stream->printf("could not create directory %s\r\n", path.c_str());
354 else stream->printf("created directory %s\r\n", path.c_str());
355}
356
0325af12 357// Change current absolute path to provided path
9e403697
JM
358void SimpleShell::cd_command( string parameters, StreamOutput *stream )
359{
75f4581c 360 string folder = absolute_from_relative( parameters );
6bcd4886 361
0325af12 362 DIR *d;
0325af12 363 d = opendir(folder.c_str());
9e403697 364 if (d == NULL) {
58baeec1 365 stream->printf("Could not open directory %s \r\n", folder.c_str() );
9e403697 366 } else {
75f4581c 367 THEKERNEL->current_path = folder;
ed7c5844 368 closedir(d);
0325af12
AW
369 }
370}
371
b7250484 372// Responds with the present working directory
9e403697
JM
373void SimpleShell::pwd_command( string parameters, StreamOutput *stream )
374{
75f4581c 375 stream->printf("%s\r\n", THEKERNEL->current_path.c_str());
b7250484
L
376}
377
0325af12 378// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
9e403697
JM
379void SimpleShell::cat_command( string parameters, StreamOutput *stream )
380{
58baeec1 381 // Get parameters ( filename and line limit )
75f4581c 382 string filename = absolute_from_relative(shift_parameter( parameters ));
1f61f177 383 string limit_parameter = shift_parameter( parameters );
0325af12 384 int limit = -1;
1f61f177 385 int delay= 0;
2ab3fca6 386 bool send_eof= false;
1f61f177
JM
387 if ( limit_parameter == "-d" ) {
388 string d= shift_parameter( parameters );
9e403697 389 char *e = NULL;
1f61f177 390 delay = strtol(d.c_str(), &e, 10);
01e97c58 391 if (e <= d.c_str()) {
1f61f177
JM
392 delay = 0;
393
01e97c58
JM
394 } else {
395 send_eof= true; // we need to terminate file send with an eof
396 }
397
1f61f177
JM
398 }else if ( limit_parameter != "" ) {
399 char *e = NULL;
400 limit = strtol(limit_parameter.c_str(), &e, 10);
401 if (e <= limit_parameter.c_str())
f7e6f459
MM
402 limit = -1;
403 }
58baeec1 404
1f61f177 405 // we have been asked to delay before cat, probably to allow time to issue upload command
1aa3d42f 406 if(delay > 0) {
be7f67cd 407 safe_delay_ms(delay*1000);
1f61f177
JM
408 }
409
58baeec1 410 // Open file
0325af12 411 FILE *lp = fopen(filename.c_str(), "r");
9e403697 412 if (lp == NULL) {
58baeec1
MM
413 stream->printf("File not found: %s\r\n", filename.c_str());
414 return;
9ed670c5 415 }
0325af12
AW
416 string buffer;
417 int c;
58baeec1 418 int newlines = 0;
6d877d9b 419 int linecnt = 0;
0325af12 420 // Print each line of the file
9e403697 421 while ((c = fgetc (lp)) != EOF) {
58baeec1 422 buffer.append((char *)&c, 1);
2ab3fca6
JM
423 if ( c == '\n' || ++linecnt > 80) {
424 if(c == '\n') newlines++;
d728799b 425 stream->puts(buffer.c_str());
58baeec1 426 buffer.clear();
6d877d9b 427 if(linecnt > 80) linecnt = 0;
6757ce1a
JM
428 // we need to kick things or they die
429 THEKERNEL->call_event(ON_IDLE);
68b7afb4 430 }
9e403697
JM
431 if ( newlines == limit ) {
432 break;
433 }
58baeec1 434 };
0325af12 435 fclose(lp);
2ab3fca6
JM
436
437 if(send_eof) {
438 stream->puts("\032"); // ^Z terminates the upload
439 }
6d877d9b
JM
440}
441
442void SimpleShell::upload_command( string parameters, StreamOutput *stream )
443{
444 // this needs to be a hack. it needs to read direct from serial and not allow on_main_loop run until done
445 // NOTE this will block all operation until the upload is complete, so do not do while printing
7baa50df 446 if(!THECONVEYOR->is_idle()) {
6d877d9b
JM
447 stream->printf("upload not allowed while printing or busy\n");
448 return;
449 }
450
451 // open file to upload to
452 string upload_filename = absolute_from_relative( parameters );
453 FILE *fd = fopen(upload_filename.c_str(), "w");
454 if(fd != NULL) {
455 stream->printf("uploading to file: %s, send control-D or control-Z to finish\r\n", upload_filename.c_str());
456 } else {
457 stream->printf("failed to open file: %s.\r\n", upload_filename.c_str());
458 return;
459 }
0325af12 460
6d877d9b
JM
461 int cnt = 0;
462 bool uploading = true;
463 while(uploading) {
464 if(!stream->ready()) {
465 // we need to kick things or they die
466 THEKERNEL->call_event(ON_IDLE);
467 continue;
468 }
469
470 char c = stream->_getc();
471 if( c == 4 || c == 26) { // ctrl-D or ctrl-Z
472 uploading = false;
473 // close file
474 fclose(fd);
475 stream->printf("uploaded %d bytes\n", cnt);
476 return;
477
478 } else {
479 // write character to file
480 cnt++;
481 if(fputc(c, fd) != c) {
482 // error writing to file
483 stream->printf("error writing to file. ignoring all characters until EOF\r\n");
484 fclose(fd);
485 fd = NULL;
486 uploading= false;
487
488 } else {
489 if ((cnt%400) == 0) {
490 // HACK ALERT to get around fwrite corruption close and re open for append
491 fclose(fd);
492 fd = fopen(upload_filename.c_str(), "a");
6757ce1a
JM
493 // we need to kick things or they die
494 THEKERNEL->call_event(ON_IDLE);
6d877d9b
JM
495 }
496 }
497 }
498 }
499 // we got an error so ignore everything until EOF
500 char c;
501 do {
502 if(stream->ready()) {
503 c= stream->_getc();
504 }else{
505 THEKERNEL->call_event(ON_IDLE);
506 c= 0;
507 }
508 } while(c != 4 && c != 26);
0325af12
AW
509}
510
618c9b0f
JM
511// loads the specified config-override file
512void SimpleShell::load_command( string parameters, StreamOutput *stream )
513{
514 // Get parameters ( filename )
75f4581c 515 string filename = absolute_from_relative(parameters);
618c9b0f
JM
516 if(filename == "/") {
517 filename = THEKERNEL->config_override_filename();
518 }
519
6d877d9b 520 FILE *fp = fopen(filename.c_str(), "r");
618c9b0f
JM
521 if(fp != NULL) {
522 char buf[132];
523 stream->printf("Loading config override file: %s...\n", filename.c_str());
524 while(fgets(buf, sizeof buf, fp) != NULL) {
525 stream->printf(" %s", buf);
526 if(buf[0] == ';') continue; // skip the comments
c0b50fa8
JM
527 // NOTE only Gcodes and Mcodes can be in the config-override
528 Gcode *gcode = new Gcode(buf, &StreamOutput::NullStream);
529 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode);
530 delete gcode;
531 THEKERNEL->call_event(ON_IDLE);
618c9b0f
JM
532 }
533 stream->printf("config override file executed\n");
534 fclose(fp);
535
6d877d9b 536 } else {
618c9b0f
JM
537 stream->printf("File not found: %s\n", filename.c_str());
538 }
539}
540
541// saves the specified config-override file
542void SimpleShell::save_command( string parameters, StreamOutput *stream )
543{
544 // Get parameters ( filename )
75f4581c 545 string filename = absolute_from_relative(parameters);
618c9b0f
JM
546 if(filename == "/") {
547 filename = THEKERNEL->config_override_filename();
548 }
549
be7f67cd 550 THECONVEYOR->wait_for_idle(); //just to be safe as it can take a while to run
23eb804b 551
06afe68b
JM
552 //remove(filename.c_str()); // seems to cause a hang every now and then
553 {
554 FileStream fs(filename.c_str());
555 fs.printf("; DO NOT EDIT THIS FILE\n");
556 // this also will truncate the existing file instead of deleting it
557 }
558
23eb804b 559 // stream that appends to file
4fed9ba1
JM
560 AppendFileStream *gs = new AppendFileStream(filename.c_str());
561 // if(!gs->is_open()) {
562 // stream->printf("Unable to open File %s for write\n", filename.c_str());
563 // return;
564 // }
618c9b0f 565
7acfedab 566 __disable_irq();
618c9b0f
JM
567 // issue a M500 which will store values in the file stream
568 Gcode *gcode = new Gcode("M500", gs);
569 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
570 delete gs;
571 delete gcode;
7acfedab 572 __enable_irq();
618c9b0f
JM
573
574 stream->printf("Settings Stored to %s\r\n", filename.c_str());
575}
576
6187a020 577// show free memory
9e403697
JM
578void SimpleShell::mem_command( string parameters, StreamOutput *stream)
579{
078f76e0 580 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos;
9e403697
JM
581 unsigned long heap = (unsigned long)_sbrk(0);
582 unsigned long m = g_maximumHeapAddress - heap;
ecc610a4
JM
583 stream->printf("Unused Heap: %lu bytes\r\n", m);
584
6d877d9b 585 uint32_t f = heapWalk(stream, verbose);
0c683b26 586 stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
a200fc31 587
0c683b26 588 stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
6d877d9b 589 if (verbose) {
1803076a
MM
590 AHB0.debug(stream);
591 AHB1.debug(stream);
592 }
8a9f9313 593
781dcb02 594 stream->printf("Block size: %u bytes, Tickinfo size: %u bytes\n", sizeof(Block), sizeof(Block::tickinfo_t) * Block::n_actuators);
6187a020
JM
595}
596
9e403697
JM
597static uint32_t getDeviceType()
598{
599#define IAP_LOCATION 0x1FFF1FF1
01f35bcc
JM
600 uint32_t command[1];
601 uint32_t result[5];
9e403697 602 typedef void (*IAP)(uint32_t *, uint32_t *);
01f35bcc
JM
603 IAP iap = (IAP) IAP_LOCATION;
604
605 __disable_irq();
606
607 command[0] = 54;
608 iap(command, result);
609
610 __enable_irq();
611
612 return result[1];
613}
614
d4ee6ee2
JM
615// get network config
616void SimpleShell::net_command( string parameters, StreamOutput *stream)
617{
618 void *returned_data;
6d877d9b 619 bool ok = PublicData::get_value( network_checksum, get_ipconfig_checksum, &returned_data );
d4ee6ee2 620 if(ok) {
6d877d9b 621 char *str = (char *)returned_data;
d4ee6ee2
JM
622 stream->printf("%s\r\n", str);
623 free(str);
624
6d877d9b 625 } else {
d4ee6ee2
JM
626 stream->printf("No network detected\n");
627 }
628}
629
582559c6 630// print out build version
9e403697
JM
631void SimpleShell::version_command( string parameters, StreamOutput *stream)
632{
582559c6 633 Version vers;
9e403697
JM
634 uint32_t dev = getDeviceType();
635 const char *mcu = (dev & 0x00100000) ? "LPC1769" : "LPC1768";
01f35bcc 636 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);
599cb4bd 637 #ifdef CNC
37b7b898 638 stream->printf(" CNC Build ");
599cb4bd 639 #endif
52d0c799 640 #ifdef DISABLEMSD
1189e9cd 641 stream->printf(" NOMSD Build\r\n");
52d0c799 642 #endif
37b7b898 643 stream->printf("%d axis\n", MAX_ROBOT_ACTUATORS);
582559c6
JM
644}
645
77983aa1 646// Reset the system
9e403697
JM
647void SimpleShell::reset_command( string parameters, StreamOutput *stream)
648{
ead17727 649 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
7e81f138 650 reset_delay_secs = 5; // reboot in 5 seconds
2742fca9
JM
651}
652
653// go into dfu boot mode
9e403697
JM
654void SimpleShell::dfu_command( string parameters, StreamOutput *stream)
655{
ed7c5844
JM
656 stream->printf("Entering boot mode...\r\n");
657 system_reset(true);
77983aa1
L
658}
659
0f0b1656 660// Break out into the MRI debugging system
9e403697
JM
661void SimpleShell::break_command( string parameters, StreamOutput *stream)
662{
0f0b1656
L
663 stream->printf("Entering MRI debug mode...\r\n");
664 __debugbreak();
665}
666
40843ebc
JM
667static int get_active_tool()
668{
335957f5 669 void *returned_data;
40843ebc
JM
670 bool ok = PublicData::get_value(tool_manager_checksum, get_active_tool_checksum, &returned_data);
671 if (ok) {
335957f5
JM
672 int active_tool= *static_cast<int *>(returned_data);
673 return active_tool;
40843ebc
JM
674 } else {
675 return 0;
676 }
677}
678
6c0193b3
JM
679void SimpleShell::grblDP_command( string parameters, StreamOutput *stream)
680{
681 /*
682 [G54:95.000,40.000,-23.600]
683 [G55:0.000,0.000,0.000]
684 [G56:0.000,0.000,0.000]
685 [G57:0.000,0.000,0.000]
686 [G58:0.000,0.000,0.000]
687 [G59:0.000,0.000,0.000]
688 [G28:0.000,0.000,0.000]
689 [G30:0.000,0.000,0.000]
690 [G92:0.000,0.000,0.000]
691 [TLO:0.000]
692 [PRB:0.000,0.000,0.000:0]
693 */
078f76e0
JM
694
695 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos;
696
c8bac202 697 std::vector<Robot::wcs_t> v= THEROBOT->get_wcs_state();
078f76e0
JM
698 if(verbose) {
699 char current_wcs= std::get<0>(v[0]);
700 stream->printf("[current WCS: %s]\n", wcs2gcode(current_wcs).c_str());
701 }
702
6c0193b3
JM
703 int n= std::get<1>(v[0]);
704 for (int i = 1; i <= n; ++i) {
32bc8b54 705 stream->printf("[%s:%1.4f,%1.4f,%1.4f]\n", wcs2gcode(i-1).c_str(),
c8bac202
JM
706 THEROBOT->from_millimeters(std::get<0>(v[i])),
707 THEROBOT->from_millimeters(std::get<1>(v[i])),
708 THEROBOT->from_millimeters(std::get<2>(v[i])));
6c0193b3
JM
709 }
710
9339253b
JM
711 float *rd;
712 PublicData::get_value( endstops_checksum, saved_position_checksum, &rd );
32bc8b54 713 stream->printf("[G28:%1.4f,%1.4f,%1.4f]\n",
c8bac202
JM
714 THEROBOT->from_millimeters(rd[0]),
715 THEROBOT->from_millimeters(rd[1]),
716 THEROBOT->from_millimeters(rd[2]));
32bc8b54 717
31c6c2c2 718 stream->printf("[G30:%1.4f,%1.4f,%1.4f]\n", 0.0F, 0.0F, 0.0F); // not implemented
9339253b 719
32bc8b54 720 stream->printf("[G92:%1.4f,%1.4f,%1.4f]\n",
c8bac202
JM
721 THEROBOT->from_millimeters(std::get<0>(v[n+1])),
722 THEROBOT->from_millimeters(std::get<1>(v[n+1])),
723 THEROBOT->from_millimeters(std::get<2>(v[n+1])));
32bc8b54 724
078f76e0 725 if(verbose) {
32bc8b54 726 stream->printf("[Tool Offset:%1.4f,%1.4f,%1.4f]\n",
c8bac202
JM
727 THEROBOT->from_millimeters(std::get<0>(v[n+2])),
728 THEROBOT->from_millimeters(std::get<1>(v[n+2])),
729 THEROBOT->from_millimeters(std::get<2>(v[n+2])));
078f76e0 730 }else{
c8bac202 731 stream->printf("[TL0:%1.4f]\n", THEROBOT->from_millimeters(std::get<2>(v[n+2])));
078f76e0 732 }
6c0193b3 733
4440e123
JM
734 // this is the last probe position, updated when a probe completes, also stores the number of steps moved after a homing cycle
735 float px, py, pz;
736 uint8_t ps;
c8bac202
JM
737 std::tie(px, py, pz, ps) = THEROBOT->get_last_probe_position();
738 stream->printf("[PRB:%1.4f,%1.4f,%1.4f:%d]\n", THEROBOT->from_millimeters(px), THEROBOT->from_millimeters(py), THEROBOT->from_millimeters(pz), ps);
6c0193b3
JM
739}
740
9e403697
JM
741void SimpleShell::get_command( string parameters, StreamOutput *stream)
742{
7e81f138 743 string what = shift_parameter( parameters );
c4e56997 744
7e81f138 745 if (what == "temp") {
3bfb2639 746 struct pad_temperature temp;
9e403697 747 string type = shift_parameter( parameters );
56a6c8c1
JM
748 if(type.empty()) {
749 // scan all temperature controls
750 std::vector<struct pad_temperature> controllers;
751 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
752 if (ok) {
753 for (auto &c : controllers) {
754 stream->printf("%s (%d) temp: %f/%f @%d\r\n", c.designator.c_str(), c.id, c.current_temperature, c.target_temperature, c.pwm);
755 }
b55cfff1 756
56a6c8c1
JM
757 } else {
758 stream->printf("no heaters found\r\n");
759 }
760
761 }else{
762 bool ok = PublicData::get_value( temperature_control_checksum, current_temperature_checksum, get_checksum(type), &temp );
763
764 if (ok) {
765 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
766 } else {
767 stream->printf("%s is not a known temperature device\r\n", type.c_str());
768 }
b55cfff1 769 }
c4e56997 770
70021ec9 771 } else if (what == "fk" || what == "ik") {
2577a122 772 string p= shift_parameter( parameters );
70021ec9 773 bool move= false;
8ff7a960 774 if(p == "-m") {
70021ec9
JM
775 move= true;
776 p= shift_parameter( parameters );
777 }
778
779 std::vector<float> v= parse_number_list(p.c_str());
586cc733
JM
780 if(p.empty() || v.size() < 1) {
781 stream->printf("error:usage: get [fk|ik] [-m] x[,y,z]\n");
70021ec9
JM
782 return;
783 }
2577a122 784
70021ec9 785 float x= v[0];
586cc733
JM
786 float y= (v.size() > 1) ? v[1] : x;
787 float z= (v.size() > 2) ? v[2] : y;
70021ec9
JM
788
789 if(what == "fk") {
8ff7a960 790 // do forward kinematics on the given actuator position and display the cartesian coordinates
70021ec9
JM
791 ActuatorCoordinates apos{x, y, z};
792 float pos[3];
c8bac202 793 THEROBOT->arm_solution->actuator_to_cartesian(apos, pos);
1c658603 794 stream->printf("cartesian= X %f, Y %f, Z %f\n", pos[0], pos[1], pos[2]);
70021ec9
JM
795 x= pos[0];
796 y= pos[1];
797 z= pos[2];
8ff7a960 798
70021ec9 799 }else{
8ff7a960 800 // do inverse kinematics on the given cartesian position and display the actuator coordinates
70021ec9
JM
801 float pos[3]{x, y, z};
802 ActuatorCoordinates apos;
c8bac202 803 THEROBOT->arm_solution->cartesian_to_actuator(pos, apos);
1c658603 804 stream->printf("actuator= X %f, Y %f, Z %f\n", apos[0], apos[1], apos[2]);
70021ec9
JM
805 }
806
807 if(move) {
8ff7a960 808 // move to the calculated, or given, XYZ
70021ec9
JM
809 char cmd[64];
810 snprintf(cmd, sizeof(cmd), "G53 G0 X%f Y%f Z%f", x, y, z);
811 struct SerialMessage message;
812 message.message = cmd;
813 message.stream = &(StreamOutput::NullStream);
814 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
be7f67cd 815 THECONVEYOR->wait_for_idle();
70021ec9 816 }
2577a122
JM
817
818 } else if (what == "pos") {
12ce413f 819 // convenience to call all the various M114 variants, shows ABC axis where relevant
cb6bfefa
JM
820 std::string buf;
821 THEROBOT->print_position(0, buf); stream->printf("last %s\n", buf.c_str()); buf.clear();
822 THEROBOT->print_position(1, buf); stream->printf("realtime %s\n", buf.c_str()); buf.clear();
823 THEROBOT->print_position(2, buf); stream->printf("%s\n", buf.c_str()); buf.clear();
824 THEROBOT->print_position(3, buf); stream->printf("%s\n", buf.c_str()); buf.clear();
825 THEROBOT->print_position(4, buf); stream->printf("%s\n", buf.c_str()); buf.clear();
826 THEROBOT->print_position(5, buf); stream->printf("%s\n", buf.c_str()); buf.clear();
34210908
JM
827
828 } else if (what == "wcs") {
829 // print the wcs state
078f76e0 830 grblDP_command("-v", stream);
40843ebc
JM
831
832 } else if (what == "state") {
6c0193b3 833 // also $G
40843ebc 834 // [G0 G54 G17 G21 G90 G94 M0 M5 M9 T0 F0.]
73cc27d2 835 stream->printf("[G%d %s G%d G%d G%d G94 M0 M5 M9 T%d F%1.4f S%1.4f]\n",
40843ebc 836 THEKERNEL->gcode_dispatch->get_modal_command(),
c8bac202
JM
837 wcs2gcode(THEROBOT->get_current_wcs()).c_str(),
838 THEROBOT->plane_axis_0 == X_AXIS && THEROBOT->plane_axis_1 == Y_AXIS && THEROBOT->plane_axis_2 == Z_AXIS ? 17 :
839 THEROBOT->plane_axis_0 == X_AXIS && THEROBOT->plane_axis_1 == Z_AXIS && THEROBOT->plane_axis_2 == Y_AXIS ? 18 :
840 THEROBOT->plane_axis_0 == Y_AXIS && THEROBOT->plane_axis_1 == Z_AXIS && THEROBOT->plane_axis_2 == X_AXIS ? 19 : 17,
841 THEROBOT->inch_mode ? 20 : 21,
842 THEROBOT->absolute_mode ? 90 : 91,
40843ebc 843 get_active_tool(),
73cc27d2
JM
844 THEROBOT->from_millimeters(THEROBOT->get_feed_rate()),
845 THEROBOT->get_s_value());
6c0193b3 846
a9e8c04b
JM
847 } else if (what == "status") {
848 // also ? on serial and usb
849 stream->printf("%s\n", THEKERNEL->get_query_string().c_str());
850
6c0193b3 851 } else {
07186543 852 stream->printf("error:unknown option %s\n", what.c_str());
b55cfff1 853 }
8293d443
JM
854}
855
77047e76 856// used to test out the get public data events
9e403697
JM
857void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
858{
859 string type = shift_parameter( parameters );
860 string temp = shift_parameter( parameters );
04211969 861 float t = temp.empty() ? 0.0 : strtof(temp.c_str(), NULL);
75e6428d 862 bool ok = PublicData::set_value( temperature_control_checksum, get_checksum(type), &t );
991d98cc 863
9e403697 864 if (ok) {
991d98cc 865 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
9e403697 866 } else {
991d98cc
JM
867 stream->printf("%s is not a known temperature device\r\n", type.c_str());
868 }
77047e76
JM
869}
870
4c8f5447
JM
871void SimpleShell::print_thermistors_command( string parameters, StreamOutput *stream)
872{
8b260c2c 873 #ifndef NO_TOOLS_TEMPERATURECONTROL
4c8f5447 874 Thermistor::print_predefined_thermistors(stream);
8b260c2c 875 #endif
4c8f5447
JM
876}
877
1f8dab1a
JM
878void SimpleShell::calc_thermistor_command( string parameters, StreamOutput *stream)
879{
8b260c2c 880 #ifndef NO_TOOLS_TEMPERATURECONTROL
bbb839c1
JM
881 string s = shift_parameter( parameters );
882 int saveto= -1;
883 // see if we have -sn as first argument
884 if(s.find("-s", 0, 2) != string::npos) {
885 // save the results to thermistor n
886 saveto= strtol(s.substr(2).c_str(), nullptr, 10);
887 }else{
888 parameters= s;
889 }
890
1f8dab1a
JM
891 std::vector<float> trl= parse_number_list(parameters.c_str());
892 if(trl.size() == 6) {
893 // calculate the coefficients
894 float c1, c2, c3;
895 std::tie(c1, c2, c3) = Thermistor::calculate_steinhart_hart_coefficients(trl[0], trl[1], trl[2], trl[3], trl[4], trl[5]);
896 stream->printf("Steinhart Hart coefficients: I%1.18f J%1.18f K%1.18f\n", c1, c2, c3);
bbb839c1
JM
897 if(saveto == -1) {
898 stream->printf(" Paste the above in the M305 S0 command, then save with M500\n");
899 }else{
900 char buf[80];
901 int n = snprintf(buf, sizeof(buf), "M305 S%d I%1.18f J%1.18f K%1.18f", saveto, c1, c2, c3);
902 string g(buf, n);
903 Gcode gcode(g, &(StreamOutput::NullStream));
904 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode );
905 stream->printf(" Setting Thermistor %d to those settings, save with M500\n", saveto);
906 }
1f8dab1a
JM
907
908 }else{
909 // give help
910 stream->printf("Usage: calc_thermistor T1,R1,T2,R2,T3,R3\n");
911 }
8b260c2c 912 #endif
1f8dab1a
JM
913}
914
ae91dea4
JM
915// used to test out the get public data events for switch
916void SimpleShell::switch_command( string parameters, StreamOutput *stream)
917{
918 string type = shift_parameter( parameters );
919 string value = shift_parameter( parameters );
6d877d9b 920 bool ok = false;
ae91dea4 921 if(value == "on" || value == "off") {
6d877d9b 922 bool b = value == "on";
ae91dea4 923 ok = PublicData::set_value( switch_checksum, get_checksum(type), state_checksum, &b );
6d877d9b 924 } else {
ae91dea4
JM
925 float v = strtof(value.c_str(), NULL);
926 ok = PublicData::set_value( switch_checksum, get_checksum(type), value_checksum, &v );
927 }
928 if (ok) {
929 stream->printf("switch %s set to: %s\r\n", type.c_str(), value.c_str());
930 } else {
931 stream->printf("%s is not a known switch device\r\n", type.c_str());
932 }
933}
934
d55d551b
JM
935void SimpleShell::md5sum_command( string parameters, StreamOutput *stream )
936{
937 string filename = absolute_from_relative(parameters);
938
939 // Open file
940 FILE *lp = fopen(filename.c_str(), "r");
941 if (lp == NULL) {
942 stream->printf("File not found: %s\r\n", filename.c_str());
943 return;
944 }
945 MD5 md5;
946 uint8_t buf[64];
947 do {
948 size_t n= fread(buf, 1, sizeof buf, lp);
949 if(n > 0) md5.update(buf, n);
2a95b07e 950 THEKERNEL->call_event(ON_IDLE);
d55d551b
JM
951 } while(!feof(lp));
952
953 stream->printf("%s %s\n", md5.finalize().hexdigest().c_str(), filename.c_str());
954 fclose(lp);
955}
956
518b5c4c
JM
957// runs several types of test on the mechanisms
958void SimpleShell::test_command( string parameters, StreamOutput *stream)
959{
960 string what = shift_parameter( parameters );
961
962 if (what == "jog") {
963 // jogs back and forth usage: axis distance iterations [feedrate]
964 string axis = shift_parameter( parameters );
965 string dist = shift_parameter( parameters );
966 string iters = shift_parameter( parameters );
967 string speed = shift_parameter( parameters );
968 if(axis.empty() || dist.empty() || iters.empty()) {
969 stream->printf("error: Need axis distance iterations\n");
970 return;
971 }
972 float d= strtof(dist.c_str(), NULL);
c8bac202 973 float f= speed.empty() ? THEROBOT->get_feed_rate() : strtof(speed.c_str(), NULL);
518b5c4c
JM
974 uint32_t n= strtol(iters.c_str(), NULL, 10);
975
976 bool toggle= false;
977 for (uint32_t i = 0; i < n; ++i) {
978 char cmd[64];
a276de9c
JM
979 snprintf(cmd, sizeof(cmd), "G91 G0 %c%f F%f G90", toupper(axis[0]), toggle ? -d : d, f);
980 stream->printf("%s\n", cmd);
518b5c4c
JM
981 struct SerialMessage message{&StreamOutput::NullStream, cmd};
982 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
a276de9c 983 if(THEKERNEL->is_halted()) break;
518b5c4c
JM
984 toggle= !toggle;
985 }
a276de9c 986 stream->printf("done\n");
518b5c4c
JM
987
988 }else if (what == "circle") {
fb4c9d09 989 // draws a circle around origin. usage: radius iterations [feedrate]
e0b7aa0e 990 string radius = shift_parameter( parameters );
e0b7aa0e
JM
991 string iters = shift_parameter( parameters );
992 string speed = shift_parameter( parameters );
fb4c9d09
JM
993 if(radius.empty() || iters.empty()) {
994 stream->printf("error: Need radius iterations\n");
e0b7aa0e
JM
995 return;
996 }
997
998 float r= strtof(radius.c_str(), NULL);
e0b7aa0e 999 uint32_t n= strtol(iters.c_str(), NULL, 10);
c8bac202 1000 float f= speed.empty() ? THEROBOT->get_feed_rate() : strtof(speed.c_str(), NULL);
fb4c9d09 1001
70057bbd 1002 THEROBOT->push_state();
ee602f65 1003 char cmd[64];
ec3cac28 1004 snprintf(cmd, sizeof(cmd), "G91 G0 X%f F%f G90", -r, f);
fb4c9d09 1005 stream->printf("%s\n", cmd);
ee602f65
JM
1006 struct SerialMessage message{&StreamOutput::NullStream, cmd};
1007 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
e0b7aa0e
JM
1008
1009 for (uint32_t i = 0; i < n; ++i) {
a276de9c 1010 if(THEKERNEL->is_halted()) break;
ec3cac28 1011 snprintf(cmd, sizeof(cmd), "G2 I%f J0 F%f", r, f);
fb4c9d09
JM
1012 stream->printf("%s\n", cmd);
1013 message.message= cmd;
1014 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
e0b7aa0e 1015 }
ec3cac28
JM
1016
1017 // leave it where it started
1018 if(!THEKERNEL->is_halted()) {
1019 snprintf(cmd, sizeof(cmd), "G91 G0 X%f F%f G90", r, f);
1020 stream->printf("%s\n", cmd);
1021 struct SerialMessage message{&StreamOutput::NullStream, cmd};
1022 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
1023 }
1024
5daa28cd 1025 THEROBOT->pop_state();
a276de9c 1026 stream->printf("done\n");
518b5c4c
JM
1027
1028 }else if (what == "square") {
1029 // draws a square usage: size iterations [feedrate]
1030 string size = shift_parameter( parameters );
1031 string iters = shift_parameter( parameters );
1032 string speed = shift_parameter( parameters );
1033 if(size.empty() || iters.empty()) {
1034 stream->printf("error: Need size iterations\n");
1035 return;
1036 }
1037 float d= strtof(size.c_str(), NULL);
c8bac202 1038 float f= speed.empty() ? THEROBOT->get_feed_rate() : strtof(speed.c_str(), NULL);
518b5c4c
JM
1039 uint32_t n= strtol(iters.c_str(), NULL, 10);
1040
1041 for (uint32_t i = 0; i < n; ++i) {
1042 char cmd[64];
1043 {
1044 snprintf(cmd, sizeof(cmd), "G91 G0 X%f F%f", d, f);
a276de9c 1045 stream->printf("%s\n", cmd);
518b5c4c
JM
1046 struct SerialMessage message{&StreamOutput::NullStream, cmd};
1047 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
1048 }
1049 {
e0b7aa0e 1050 snprintf(cmd, sizeof(cmd), "G0 Y%f", d);
a276de9c 1051 stream->printf("%s\n", cmd);
518b5c4c
JM
1052 struct SerialMessage message{&StreamOutput::NullStream, cmd};
1053 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
1054 }
1055 {
e0b7aa0e 1056 snprintf(cmd, sizeof(cmd), "G0 X%f", -d);
a276de9c 1057 stream->printf("%s\n", cmd);
518b5c4c
JM
1058 struct SerialMessage message{&StreamOutput::NullStream, cmd};
1059 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
1060 }
1061 {
e0b7aa0e 1062 snprintf(cmd, sizeof(cmd), "G0 Y%f G90", -d);
a276de9c 1063 stream->printf("%s\n", cmd);
518b5c4c
JM
1064 struct SerialMessage message{&StreamOutput::NullStream, cmd};
1065 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
1066 }
a276de9c 1067 if(THEKERNEL->is_halted()) break;
5daa28cd 1068 }
be7f67cd
JM
1069 stream->printf("done\n");
1070
1071 }else if (what == "raw") {
1072 // issues raw steps to the specified axis usage: axis steps steps/sec
1073 string axis = shift_parameter( parameters );
1074 string stepstr = shift_parameter( parameters );
1075 string stepspersec = shift_parameter( parameters );
1076 if(axis.empty() || stepstr.empty() || stepspersec.empty()) {
1077 stream->printf("error: Need axis steps steps/sec\n");
1078 return;
1079 }
1080
5da4221b
JM
1081 char ax= toupper(axis[0]);
1082 uint8_t a= ax >= 'X' ? ax - 'X' : ax - 'A' + 3;
be7f67cd
JM
1083 int steps= strtol(stepstr.c_str(), NULL, 10);
1084 bool dir= steps >= 0;
1085 steps= std::abs(steps);
1086
5da4221b
JM
1087 if(a > C_AXIS) {
1088 stream->printf("error: axis must be x, y, z, a, b, c\n");
1089 return;
1090 }
1091
1092 if(a >= THEROBOT->get_number_registered_motors()) {
1093 stream->printf("error: axis is out of range\n");
be7f67cd
JM
1094 return;
1095 }
1096
1097 uint32_t sps= strtol(stepspersec.c_str(), NULL, 10);
1098 sps= std::max(sps, 1UL);
1099
1100 uint32_t delayus= 1000000.0F / sps;
1101 for(int s= 0;s<steps;s++) {
1102 if(THEKERNEL->is_halted()) break;
1103 THEROBOT->actuators[a]->manual_step(dir);
1104 // delay but call on_idle
1105 safe_delay_us(delayus);
518b5c4c 1106 }
df675215
JM
1107
1108 // reset the position based on current actuator position
1109 THEROBOT->reset_position_from_current_actuator_position();
1110
a276de9c 1111 stream->printf("done\n");
d55d551b 1112
a276de9c
JM
1113 }else {
1114 stream->printf("usage:\n test jog axis distance iterations [feedrate]\n");
1115 stream->printf(" test square size iterations [feedrate]\n");
fb4c9d09 1116 stream->printf(" test circle radius iterations [feedrate]\n");
be7f67cd 1117 stream->printf(" test raw axis steps steps/sec\n");
518b5c4c
JM
1118 }
1119}
d55d551b 1120
9e403697
JM
1121void SimpleShell::help_command( string parameters, StreamOutput *stream )
1122{
ed7c5844 1123 stream->printf("Commands:\r\n");
582559c6 1124 stream->printf("version\r\n");
ecc610a4 1125 stream->printf("mem [-v]\r\n");
3579deea 1126 stream->printf("ls [-s] [folder]\r\n");
ed7c5844 1127 stream->printf("cd folder\r\n");
c4e56997 1128 stream->printf("pwd\r\n");
6c0193b3 1129 stream->printf("cat file [limit] [-d 10]\r\n");
9e403697 1130 stream->printf("rm file\r\n");
6d877d9b 1131 stream->printf("mv file newfile\r\n");
12fb447a 1132 stream->printf("remount\r\n");
4eb0e279 1133 stream->printf("play file [-v]\r\n");
ed7c5844
JM
1134 stream->printf("progress - shows progress of current play\r\n");
1135 stream->printf("abort - abort currently playing file\r\n");
c4e56997
JM
1136 stream->printf("reset - reset smoothie\r\n");
1137 stream->printf("dfu - enter dfu boot loader\r\n");
1138 stream->printf("break - break into debugger\r\n");
ed7c5844
JM
1139 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
1140 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
a9e8c04b 1141 stream->printf("get [pos|wcs|state|status|fk|ik]\r\n");
5647f709 1142 stream->printf("get temp [bed|hotend]\r\n");
991d98cc 1143 stream->printf("set_temp bed|hotend 185\r\n");
d4ee6ee2 1144 stream->printf("net\r\n");
618c9b0f
JM
1145 stream->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
1146 stream->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");
bbb839c1
JM
1147 stream->printf("upload filename - saves a stream of text to the named file\r\n");
1148 stream->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
4c8f5447 1149 stream->printf("thermistors - print out the predefined thermistors\r\n");
d55d551b 1150 stream->printf("md5sum file - prints md5 sum of the given file\r\n");
235a7435
JM
1151}
1152