fix current tool returned in get state
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
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/>.
6 */
7
8
9 #include "SimpleShell.h"
10 #include "libs/Kernel.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/utils.h"
13 #include "libs/SerialMessage.h"
14 #include "libs/StreamOutput.h"
15 #include "modules/robot/Conveyor.h"
16 #include "DirHandle.h"
17 #include "mri.h"
18 #include "version.h"
19 #include "PublicDataRequest.h"
20 #include "AppendFileStream.h"
21 #include "FileStream.h"
22 #include "checksumm.h"
23 #include "PublicData.h"
24 #include "Gcode.h"
25 #include "Robot.h"
26 #include "ToolManagerPublicAccess.h"
27 #include "GcodeDispatch.h"
28
29 #include "TemperatureControlPublicAccess.h"
30 #include "NetworkPublicAccess.h"
31 #include "platform_memory.h"
32 #include "SwitchPublicAccess.h"
33 #include "SDFAT.h"
34 #include "Thermistor.h"
35 #include "md5.h"
36
37 #include "system_LPC17xx.h"
38 #include "LPC17xx.h"
39
40 #include "mbed.h" // for wait_ms()
41
42 extern unsigned int g_maximumHeapAddress;
43
44 #include <malloc.h>
45 #include <mri.h>
46 #include <stdio.h>
47 #include <stdint.h>
48
49 extern "C" uint32_t __end__;
50 extern "C" uint32_t __malloc_free_list;
51 extern "C" uint32_t _sbrk(int size);
52
53 // command lookup table
54 const 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},
60 {"mv", SimpleShell::mv_command},
61 {"upload", SimpleShell::upload_command},
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},
71 {"switch", SimpleShell::switch_command},
72 {"net", SimpleShell::net_command},
73 {"load", SimpleShell::load_command},
74 {"save", SimpleShell::save_command},
75 {"remount", SimpleShell::remount_command},
76 {"calc_thermistor", SimpleShell::calc_thermistor_command},
77 {"thermistors", SimpleShell::print_thermistors_command},
78 {"md5sum", SimpleShell::md5sum_command},
79
80 // unknown command
81 {NULL, NULL}
82 };
83
84 int SimpleShell::reset_delay_secs = 0;
85
86 // Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
87 static uint32_t heapWalk(StreamOutput *stream, bool verbose)
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
97 uint32_t freeSize = 0;
98 uint32_t usedSize = 0;
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.
103 while (chunkCurr < heapEnd) {
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.
108 uint32_t chunkSize = *(uint32_t *)chunkCurr;
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.
114 if (chunkCurr == freeCurr) {
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).
118 freeCurr = *(uint32_t *)(freeCurr + 4);
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;
128 if (verbose)
129 stream->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber, chunkCurr, chunkSize, isChunkFree ? "CHUNK FREE" : "");
130
131 if (isChunkFree) freeSize += chunkSize;
132 else usedSize += chunkSize;
133
134 chunkCurr = chunkNext;
135 chunkNumber++;
136 }
137 stream->printf("Allocated: %lu, Free: %lu\r\n", usedSize, freeSize);
138 return freeSize;
139 }
140
141
142 void SimpleShell::on_module_loaded()
143 {
144 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
145 this->register_for_event(ON_GCODE_RECEIVED);
146 this->register_for_event(ON_SECOND_TICK);
147
148 reset_delay_secs = 0;
149 }
150
151 void SimpleShell::on_second_tick(void *)
152 {
153 // we are timing out for the reset
154 if (reset_delay_secs > 0) {
155 if (--reset_delay_secs == 0) {
156 system_reset(false);
157 }
158 }
159 }
160
161 void SimpleShell::on_gcode_received(void *argument)
162 {
163 Gcode *gcode = static_cast<Gcode *>(argument);
164 string args = get_arguments(gcode->get_command());
165
166 if (gcode->has_m) {
167 if (gcode->m == 20) { // list sd card
168 gcode->stream->printf("Begin file list\r\n");
169 ls_command("/sd", gcode->stream);
170 gcode->stream->printf("End file list\r\n");
171
172 } else if (gcode->m == 30) { // remove file
173 rm_command("/sd/" + args, gcode->stream);
174
175 } else if(gcode->m == 501) { // load config override
176 if(args.empty()) {
177 load_command("/sd/config-override", gcode->stream);
178 } else {
179 load_command("/sd/config-override." + args, gcode->stream);
180 }
181
182 } else if(gcode->m == 504) { // save to specific config override file
183 if(args.empty()) {
184 save_command("/sd/config-override", gcode->stream);
185 } else {
186 save_command("/sd/config-override." + args, gcode->stream);
187 }
188 }
189 }
190 }
191
192 bool SimpleShell::parse_command(const char *cmd, string args, StreamOutput *stream)
193 {
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);
197 return true;
198 }
199 }
200
201 return false;
202 }
203
204 // When a new line is received, check if it is a command, and if it is, act upon it
205 void SimpleShell::on_console_line_received( void *argument )
206 {
207 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
208
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;
212
213 string possible_command = new_message.message;
214
215 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
216 string cmd = shift_parameter(possible_command);
217
218 // find command and execute it
219 parse_command(cmd.c_str(), possible_command, new_message.stream);
220 }
221
222 // Act upon an ls command
223 // Convert the first parameter into an absolute path, then list the files in that path
224 void SimpleShell::ls_command( string parameters, StreamOutput *stream )
225 {
226 string path, opts;
227 while(!parameters.empty()) {
228 string s = shift_parameter( parameters );
229 if(s.front() == '-') {
230 opts.append(s);
231 } else {
232 path = s;
233 if(!parameters.empty()) {
234 path.append(" ");
235 path.append(parameters);
236 }
237 break;
238 }
239 }
240
241 path = absolute_from_relative(path);
242
243 DIR *d;
244 struct dirent *p;
245 d = opendir(path.c_str());
246 if (d != NULL) {
247 while ((p = readdir(d)) != NULL) {
248 stream->printf("%s", lc(string(p->d_name)).c_str());
249 if(p->d_isdir) {
250 stream->printf("/");
251 } else if(opts.find("-s", 0, 2) != string::npos) {
252 stream->printf(" %d", p->d_fsize);
253 }
254 stream->printf("\r\n");
255 }
256 closedir(d);
257 } else {
258 stream->printf("Could not open directory %s\r\n", path.c_str());
259 }
260 }
261
262 extern SDFAT mounter;
263
264 void SimpleShell::remount_command( string parameters, StreamOutput *stream )
265 {
266 mounter.remount();
267 stream->printf("remounted\r\n");
268 }
269
270 // Delete a file
271 void SimpleShell::rm_command( string parameters, StreamOutput *stream )
272 {
273 const char *fn = absolute_from_relative(shift_parameter( parameters )).c_str();
274 int s = remove(fn);
275 if (s != 0) stream->printf("Could not delete %s \r\n", fn);
276 }
277
278 // Rename a file
279 void SimpleShell::mv_command( string parameters, StreamOutput *stream )
280 {
281 string from = absolute_from_relative(shift_parameter( parameters ));
282 string to = absolute_from_relative(shift_parameter(parameters));
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
288 // Change current absolute path to provided path
289 void SimpleShell::cd_command( string parameters, StreamOutput *stream )
290 {
291 string folder = absolute_from_relative( parameters );
292
293 DIR *d;
294 d = opendir(folder.c_str());
295 if (d == NULL) {
296 stream->printf("Could not open directory %s \r\n", folder.c_str() );
297 } else {
298 THEKERNEL->current_path = folder;
299 closedir(d);
300 }
301 }
302
303 // Responds with the present working directory
304 void SimpleShell::pwd_command( string parameters, StreamOutput *stream )
305 {
306 stream->printf("%s\r\n", THEKERNEL->current_path.c_str());
307 }
308
309 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
310 void SimpleShell::cat_command( string parameters, StreamOutput *stream )
311 {
312 // Get parameters ( filename and line limit )
313 string filename = absolute_from_relative(shift_parameter( parameters ));
314 string limit_paramater = shift_parameter( parameters );
315 int limit = -1;
316 if ( limit_paramater != "" ) {
317 char *e = NULL;
318 limit = strtol(limit_paramater.c_str(), &e, 10);
319 if (e <= limit_paramater.c_str())
320 limit = -1;
321 }
322
323 // Open file
324 FILE *lp = fopen(filename.c_str(), "r");
325 if (lp == NULL) {
326 stream->printf("File not found: %s\r\n", filename.c_str());
327 return;
328 }
329 string buffer;
330 int c;
331 int newlines = 0;
332 int linecnt = 0;
333 // Print each line of the file
334 while ((c = fgetc (lp)) != EOF) {
335 buffer.append((char *)&c, 1);
336 if ( char(c) == '\n' || ++linecnt > 80) {
337 newlines++;
338 stream->puts(buffer.c_str());
339 buffer.clear();
340 if(linecnt > 80) linecnt = 0;
341 // we need to kick things or they die
342 THEKERNEL->call_event(ON_IDLE);
343 }
344 if ( newlines == limit ) {
345 break;
346 }
347 };
348 fclose(lp);
349 }
350
351 void SimpleShell::upload_command( string parameters, StreamOutput *stream )
352 {
353 // this needs to be a hack. it needs to read direct from serial and not allow on_main_loop run until done
354 // NOTE this will block all operation until the upload is complete, so do not do while printing
355 if(!THEKERNEL->conveyor->is_queue_empty()) {
356 stream->printf("upload not allowed while printing or busy\n");
357 return;
358 }
359
360 // open file to upload to
361 string upload_filename = absolute_from_relative( parameters );
362 FILE *fd = fopen(upload_filename.c_str(), "w");
363 if(fd != NULL) {
364 stream->printf("uploading to file: %s, send control-D or control-Z to finish\r\n", upload_filename.c_str());
365 } else {
366 stream->printf("failed to open file: %s.\r\n", upload_filename.c_str());
367 return;
368 }
369
370 int cnt = 0;
371 bool uploading = true;
372 while(uploading) {
373 if(!stream->ready()) {
374 // we need to kick things or they die
375 THEKERNEL->call_event(ON_IDLE);
376 continue;
377 }
378
379 char c = stream->_getc();
380 if( c == 4 || c == 26) { // ctrl-D or ctrl-Z
381 uploading = false;
382 // close file
383 fclose(fd);
384 stream->printf("uploaded %d bytes\n", cnt);
385 return;
386
387 } else {
388 // write character to file
389 cnt++;
390 if(fputc(c, fd) != c) {
391 // error writing to file
392 stream->printf("error writing to file. ignoring all characters until EOF\r\n");
393 fclose(fd);
394 fd = NULL;
395 uploading= false;
396
397 } else {
398 if ((cnt%400) == 0) {
399 // HACK ALERT to get around fwrite corruption close and re open for append
400 fclose(fd);
401 fd = fopen(upload_filename.c_str(), "a");
402 // we need to kick things or they die
403 THEKERNEL->call_event(ON_IDLE);
404 }
405 }
406 }
407 }
408 // we got an error so ignore everything until EOF
409 char c;
410 do {
411 if(stream->ready()) {
412 c= stream->_getc();
413 }else{
414 THEKERNEL->call_event(ON_IDLE);
415 c= 0;
416 }
417 } while(c != 4 && c != 26);
418 }
419
420 // loads the specified config-override file
421 void SimpleShell::load_command( string parameters, StreamOutput *stream )
422 {
423 // Get parameters ( filename )
424 string filename = absolute_from_relative(parameters);
425 if(filename == "/") {
426 filename = THEKERNEL->config_override_filename();
427 }
428
429 FILE *fp = fopen(filename.c_str(), "r");
430 if(fp != NULL) {
431 char buf[132];
432 stream->printf("Loading config override file: %s...\n", filename.c_str());
433 while(fgets(buf, sizeof buf, fp) != NULL) {
434 stream->printf(" %s", buf);
435 if(buf[0] == ';') continue; // skip the comments
436 struct SerialMessage message = {&(StreamOutput::NullStream), buf};
437 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
438 }
439 stream->printf("config override file executed\n");
440 fclose(fp);
441
442 } else {
443 stream->printf("File not found: %s\n", filename.c_str());
444 }
445 }
446
447 // saves the specified config-override file
448 void SimpleShell::save_command( string parameters, StreamOutput *stream )
449 {
450 // Get parameters ( filename )
451 string filename = absolute_from_relative(parameters);
452 if(filename == "/") {
453 filename = THEKERNEL->config_override_filename();
454 }
455
456 THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run
457
458 //remove(filename.c_str()); // seems to cause a hang every now and then
459 {
460 FileStream fs(filename.c_str());
461 fs.printf("; DO NOT EDIT THIS FILE\n");
462 // this also will truncate the existing file instead of deleting it
463 }
464
465 // stream that appends to file
466 AppendFileStream *gs = new AppendFileStream(filename.c_str());
467 // if(!gs->is_open()) {
468 // stream->printf("Unable to open File %s for write\n", filename.c_str());
469 // return;
470 // }
471
472 __disable_irq();
473 // issue a M500 which will store values in the file stream
474 Gcode *gcode = new Gcode("M500", gs);
475 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
476 delete gs;
477 delete gcode;
478 __enable_irq();
479
480 stream->printf("Settings Stored to %s\r\n", filename.c_str());
481 }
482
483 // show free memory
484 void SimpleShell::mem_command( string parameters, StreamOutput *stream)
485 {
486 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos ;
487 unsigned long heap = (unsigned long)_sbrk(0);
488 unsigned long m = g_maximumHeapAddress - heap;
489 stream->printf("Unused Heap: %lu bytes\r\n", m);
490
491 uint32_t f = heapWalk(stream, verbose);
492 stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
493
494 stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
495 if (verbose) {
496 AHB0.debug(stream);
497 AHB1.debug(stream);
498 }
499 }
500
501 static uint32_t getDeviceType()
502 {
503 #define IAP_LOCATION 0x1FFF1FF1
504 uint32_t command[1];
505 uint32_t result[5];
506 typedef void (*IAP)(uint32_t *, uint32_t *);
507 IAP iap = (IAP) IAP_LOCATION;
508
509 __disable_irq();
510
511 command[0] = 54;
512 iap(command, result);
513
514 __enable_irq();
515
516 return result[1];
517 }
518
519 // get network config
520 void SimpleShell::net_command( string parameters, StreamOutput *stream)
521 {
522 void *returned_data;
523 bool ok = PublicData::get_value( network_checksum, get_ipconfig_checksum, &returned_data );
524 if(ok) {
525 char *str = (char *)returned_data;
526 stream->printf("%s\r\n", str);
527 free(str);
528
529 } else {
530 stream->printf("No network detected\n");
531 }
532 }
533
534 // print out build version
535 void SimpleShell::version_command( string parameters, StreamOutput *stream)
536 {
537 Version vers;
538 uint32_t dev = getDeviceType();
539 const char *mcu = (dev & 0x00100000) ? "LPC1769" : "LPC1768";
540 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);
541 }
542
543 // Reset the system
544 void SimpleShell::reset_command( string parameters, StreamOutput *stream)
545 {
546 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
547 reset_delay_secs = 5; // reboot in 5 seconds
548 }
549
550 // go into dfu boot mode
551 void SimpleShell::dfu_command( string parameters, StreamOutput *stream)
552 {
553 stream->printf("Entering boot mode...\r\n");
554 system_reset(true);
555 }
556
557 // Break out into the MRI debugging system
558 void SimpleShell::break_command( string parameters, StreamOutput *stream)
559 {
560 stream->printf("Entering MRI debug mode...\r\n");
561 __debugbreak();
562 }
563
564 static int get_active_tool()
565 {
566 void *returned_data;
567 bool ok = PublicData::get_value(tool_manager_checksum, get_active_tool_checksum, &returned_data);
568 if (ok) {
569 int active_tool= *static_cast<int *>(returned_data);
570 return active_tool;
571 } else {
572 return 0;
573 }
574 }
575
576 // used to test out the get public data events
577 void SimpleShell::get_command( string parameters, StreamOutput *stream)
578 {
579 string what = shift_parameter( parameters );
580
581 if (what == "temp") {
582 struct pad_temperature temp;
583 string type = shift_parameter( parameters );
584 if(type.empty()) {
585 // scan all temperature controls
586 std::vector<struct pad_temperature> controllers;
587 bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
588 if (ok) {
589 for (auto &c : controllers) {
590 stream->printf("%s (%d) temp: %f/%f @%d\r\n", c.designator.c_str(), c.id, c.current_temperature, c.target_temperature, c.pwm);
591 }
592
593 } else {
594 stream->printf("no heaters found\r\n");
595 }
596
597 }else{
598 bool ok = PublicData::get_value( temperature_control_checksum, current_temperature_checksum, get_checksum(type), &temp );
599
600 if (ok) {
601 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
602 } else {
603 stream->printf("%s is not a known temperature device\r\n", type.c_str());
604 }
605 }
606
607 } else if (what == "pos") {
608 // convenience to call all the various M114 variants
609 char buf[64];
610 THEKERNEL->robot->print_position(0, buf, sizeof buf); stream->printf("last %s\n", buf);
611 THEKERNEL->robot->print_position(1, buf, sizeof buf); stream->printf("realtime %s\n", buf);
612 THEKERNEL->robot->print_position(2, buf, sizeof buf); stream->printf("%s\n", buf);
613 THEKERNEL->robot->print_position(3, buf, sizeof buf); stream->printf("%s\n", buf);
614 THEKERNEL->robot->print_position(4, buf, sizeof buf); stream->printf("%s\n", buf);
615 THEKERNEL->robot->print_position(5, buf, sizeof buf); stream->printf("%s\n", buf);
616
617 } else if (what == "wcs") {
618 // print the wcs state
619 std::vector<Robot::wcs_t> v= THEKERNEL->robot->get_wcs_state();
620 char current_wcs= std::get<0>(v[0]);
621 stream->printf("current WCS: %s\n", wcs2gcode(current_wcs).c_str());
622 int n= std::get<1>(v[0]);
623 for (int i = 1; i <= n; ++i) {
624 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]));
625 }
626
627 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]));
628 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]));
629
630 } else if (what == "state") {
631 // [G0 G54 G17 G21 G90 G94 M0 M5 M9 T0 F0.]
632 stream->printf("[G%d %s G%d G%d G%d G94 T%d F%1.1f]\n",
633 THEKERNEL->gcode_dispatch->get_modal_command(),
634 wcs2gcode(THEKERNEL->robot->get_current_wcs()).c_str(),
635 THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Y_AXIS && THEKERNEL->robot->plane_axis_2 == Z_AXIS ? 17 :
636 THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == Y_AXIS ? 18 :
637 THEKERNEL->robot->plane_axis_0 == Y_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == X_AXIS ? 19 : 17,
638 THEKERNEL->robot->inch_mode ? 20 : 21,
639 THEKERNEL->robot->absolute_mode ? 90 : 91,
640 get_active_tool(),
641 THEKERNEL->robot->get_feed_rate());
642 }
643 }
644
645 // used to test out the get public data events
646 void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
647 {
648 string type = shift_parameter( parameters );
649 string temp = shift_parameter( parameters );
650 float t = temp.empty() ? 0.0 : strtof(temp.c_str(), NULL);
651 bool ok = PublicData::set_value( temperature_control_checksum, get_checksum(type), &t );
652
653 if (ok) {
654 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
655 } else {
656 stream->printf("%s is not a known temperature device\r\n", type.c_str());
657 }
658 }
659
660 void SimpleShell::print_thermistors_command( string parameters, StreamOutput *stream)
661 {
662 Thermistor::print_predefined_thermistors(stream);
663 }
664
665 void SimpleShell::calc_thermistor_command( string parameters, StreamOutput *stream)
666 {
667 string s = shift_parameter( parameters );
668 int saveto= -1;
669 // see if we have -sn as first argument
670 if(s.find("-s", 0, 2) != string::npos) {
671 // save the results to thermistor n
672 saveto= strtol(s.substr(2).c_str(), nullptr, 10);
673 }else{
674 parameters= s;
675 }
676
677 std::vector<float> trl= parse_number_list(parameters.c_str());
678 if(trl.size() == 6) {
679 // calculate the coefficients
680 float c1, c2, c3;
681 std::tie(c1, c2, c3) = Thermistor::calculate_steinhart_hart_coefficients(trl[0], trl[1], trl[2], trl[3], trl[4], trl[5]);
682 stream->printf("Steinhart Hart coefficients: I%1.18f J%1.18f K%1.18f\n", c1, c2, c3);
683 if(saveto == -1) {
684 stream->printf(" Paste the above in the M305 S0 command, then save with M500\n");
685 }else{
686 char buf[80];
687 int n = snprintf(buf, sizeof(buf), "M305 S%d I%1.18f J%1.18f K%1.18f", saveto, c1, c2, c3);
688 string g(buf, n);
689 Gcode gcode(g, &(StreamOutput::NullStream));
690 THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode );
691 stream->printf(" Setting Thermistor %d to those settings, save with M500\n", saveto);
692 }
693
694 }else{
695 // give help
696 stream->printf("Usage: calc_thermistor T1,R1,T2,R2,T3,R3\n");
697 }
698 }
699
700 // used to test out the get public data events for switch
701 void SimpleShell::switch_command( string parameters, StreamOutput *stream)
702 {
703 string type = shift_parameter( parameters );
704 string value = shift_parameter( parameters );
705 bool ok = false;
706 if(value == "on" || value == "off") {
707 bool b = value == "on";
708 ok = PublicData::set_value( switch_checksum, get_checksum(type), state_checksum, &b );
709 } else {
710 float v = strtof(value.c_str(), NULL);
711 ok = PublicData::set_value( switch_checksum, get_checksum(type), value_checksum, &v );
712 }
713 if (ok) {
714 stream->printf("switch %s set to: %s\r\n", type.c_str(), value.c_str());
715 } else {
716 stream->printf("%s is not a known switch device\r\n", type.c_str());
717 }
718 }
719
720 void SimpleShell::md5sum_command( string parameters, StreamOutput *stream )
721 {
722 string filename = absolute_from_relative(parameters);
723
724 // Open file
725 FILE *lp = fopen(filename.c_str(), "r");
726 if (lp == NULL) {
727 stream->printf("File not found: %s\r\n", filename.c_str());
728 return;
729 }
730 MD5 md5;
731 uint8_t buf[64];
732 do {
733 size_t n= fread(buf, 1, sizeof buf, lp);
734 if(n > 0) md5.update(buf, n);
735 } while(!feof(lp));
736
737 stream->printf("%s %s\n", md5.finalize().hexdigest().c_str(), filename.c_str());
738 fclose(lp);
739 }
740
741
742
743 void SimpleShell::help_command( string parameters, StreamOutput *stream )
744 {
745 stream->printf("Commands:\r\n");
746 stream->printf("version\r\n");
747 stream->printf("mem [-v]\r\n");
748 stream->printf("ls [-s] [folder]\r\n");
749 stream->printf("cd folder\r\n");
750 stream->printf("pwd\r\n");
751 stream->printf("cat file [limit]\r\n");
752 stream->printf("rm file\r\n");
753 stream->printf("mv file newfile\r\n");
754 stream->printf("remount\r\n");
755 stream->printf("play file [-v]\r\n");
756 stream->printf("progress - shows progress of current play\r\n");
757 stream->printf("abort - abort currently playing file\r\n");
758 stream->printf("reset - reset smoothie\r\n");
759 stream->printf("dfu - enter dfu boot loader\r\n");
760 stream->printf("break - break into debugger\r\n");
761 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
762 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
763 stream->printf("get temp [bed|hotend]\r\n");
764 stream->printf("set_temp bed|hotend 185\r\n");
765 stream->printf("get pos\r\n");
766 stream->printf("net\r\n");
767 stream->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
768 stream->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");
769 stream->printf("upload filename - saves a stream of text to the named file\r\n");
770 stream->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
771 stream->printf("thermistors - print out the predefined thermistors\r\n");
772 stream->printf("md5sum file - prints md5 sum of the given file\r\n");
773 }
774