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