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