reformat and cleanup panel screen code
[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
AW
9#include "libs/Kernel.h"
10#include "SimpleShell.h"
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"
0325af12 20
47339e4a 21#include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
5647f709 22#include "modules/robot/RobotPublicAccess.h"
47339e4a 23
6187a020
JM
24extern unsigned int g_maximumHeapAddress;
25
ecc610a4
JM
26#include <malloc.h>
27#include <mri.h>
28#include <stdio.h>
29#include <stdint.h>
30
31extern "C" uint32_t __end__;
32extern "C" uint32_t __malloc_free_list;
33extern "C" uint32_t _sbrk(int size);
34
9e403697
JM
35#define get_temp_command_checksum CHECKSUM("temp")
36#define get_pos_command_checksum CHECKSUM("pos")
37
38// command lookup table
39SimpleShell::ptentry_t SimpleShell::commands_table[] = {
40 {CHECKSUM("ls"), &SimpleShell::ls_command},
41 {CHECKSUM("cd"), &SimpleShell::cd_command},
42 {CHECKSUM("pwd"), &SimpleShell::pwd_command},
43 {CHECKSUM("cat"), &SimpleShell::cat_command},
44 {CHECKSUM("rm"), &SimpleShell::rm_command},
45 {CHECKSUM("reset"), &SimpleShell::reset_command},
46 {CHECKSUM("dfu"), &SimpleShell::dfu_command},
47 {CHECKSUM("break"), &SimpleShell::break_command},
48 {CHECKSUM("help"), &SimpleShell::help_command},
49 {CHECKSUM("version"), &SimpleShell::version_command},
50 {CHECKSUM("mem"), &SimpleShell::mem_command},
51 {CHECKSUM("get"), &SimpleShell::get_command},
52 {CHECKSUM("set_temp"), &SimpleShell::set_temp_command},
862fc625 53 {CHECKSUM("test"), &SimpleShell::test_command},
9e403697
JM
54
55 // unknown command
56 {0, NULL}
57};
ecc610a4
JM
58
59// Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
9e403697 60static void heapWalk(StreamOutput *stream, bool verbose)
ecc610a4
JM
61{
62 uint32_t chunkNumber = 1;
63 // The __end__ linker symbol points to the beginning of the heap.
64 uint32_t chunkCurr = (uint32_t)&__end__;
65 // __malloc_free_list is the head pointer to newlib-nano's link list of free chunks.
66 uint32_t freeCurr = __malloc_free_list;
67 // Calling _sbrk() with 0 reserves no more memory but it returns the current top of heap.
68 uint32_t heapEnd = _sbrk(0);
69 // accumulate totals
9e403697
JM
70 uint32_t freeSize = 0;
71 uint32_t usedSize = 0;
ecc610a4
JM
72
73 stream->printf("Used Heap Size: %lu\n", heapEnd - chunkCurr);
74
75 // Walk through the chunks until we hit the end of the heap.
9e403697 76 while (chunkCurr < heapEnd) {
ecc610a4
JM
77 // Assume the chunk is in use. Will update later.
78 int isChunkFree = 0;
79 // The first 32-bit word in a chunk is the size of the allocation. newlib-nano over allocates by 8 bytes.
80 // 4 bytes for this 32-bit chunk size and another 4 bytes to allow for 8 byte-alignment of returned pointer.
9e403697 81 uint32_t chunkSize = *(uint32_t *)chunkCurr;
ecc610a4
JM
82 // The start of the next chunk is right after the end of this one.
83 uint32_t chunkNext = chunkCurr + chunkSize;
84
85 // The free list is sorted by address.
86 // Check to see if we have found the next free chunk in the heap.
9e403697 87 if (chunkCurr == freeCurr) {
ecc610a4
JM
88 // Chunk is free so flag it as such.
89 isChunkFree = 1;
90 // The second 32-bit word in a free chunk is a pointer to the next free chunk (again sorted by address).
9e403697 91 freeCurr = *(uint32_t *)(freeCurr + 4);
ecc610a4
JM
92 }
93
94 // Skip past the 32-bit size field in the chunk header.
95 chunkCurr += 4;
96 // 8-byte align the data pointer.
97 chunkCurr = (chunkCurr + 7) & ~7;
98 // newlib-nano over allocates by 8 bytes, 4 bytes for the 32-bit chunk size and another 4 bytes to allow for 8
99 // byte-alignment of the returned pointer.
100 chunkSize -= 8;
9e403697 101 if (verbose)
ecc610a4
JM
102 stream->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber, chunkCurr, chunkSize, isChunkFree ? "CHUNK FREE" : "");
103
9e403697 104 if (isChunkFree) freeSize += chunkSize;
ecc610a4
JM
105 else usedSize += chunkSize;
106
107 chunkCurr = chunkNext;
108 chunkNumber++;
109 }
110 stream->printf("Allocated: %lu, Free: %lu\r\n", usedSize, freeSize);
111}
112
113
9e403697
JM
114void SimpleShell::on_module_loaded()
115{
0325af12
AW
116 this->current_path = "/";
117 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
9e403697 118 this->reset_delay_secs = 0;
c4e56997
JM
119
120 this->register_for_event(ON_SECOND_TICK);
121 this->register_for_event(ON_GCODE_RECEIVED);
ead17727
JM
122}
123
9e403697
JM
124void SimpleShell::on_second_tick(void *)
125{
ead17727 126 // we are timing out for the reset
5895ead3 127 if (this->reset_delay_secs > 0) {
9e403697 128 if (--this->reset_delay_secs == 0) {
ead17727
JM
129 system_reset(false);
130 }
131 }
0325af12
AW
132}
133
9e403697
JM
134void SimpleShell::on_gcode_received(void *argument)
135{
136 Gcode *gcode = static_cast<Gcode *>(argument);
3a238fdc 137 string args= get_arguments(gcode->command);
c4e56997
JM
138
139 if (gcode->has_m) {
140 if (gcode->m == 20) { // list sd card
141 gcode->mark_as_taken();
142 gcode->stream->printf("Begin file list\r\n");
143 ls_command("/sd", gcode->stream);
144 gcode->stream->printf("End file list\r\n");
145 }
3a238fdc
CG
146 else if (gcode->m == 30) { // remove file
147 gcode->mark_as_taken();
148 rm_command("/sd/" + args, gcode->stream);
149 }
c4e56997
JM
150 }
151}
152
9e403697
JM
153bool SimpleShell::parse_command(unsigned short cs, string args, StreamOutput *stream)
154{
155 for (ptentry_t *p = commands_table; p->pfunc != NULL; ++p) {
156 if (cs == p->command_cs) {
157 PFUNC fnc= p->pfunc;
158 (this->*fnc)(args, stream);
159 return true;
160 }
161 }
162
163 return false;
164}
165
0325af12 166// When a new line is received, check if it is a command, and if it is, act upon it
9e403697
JM
167void SimpleShell::on_console_line_received( void *argument )
168{
169 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
7f613782
JM
170
171 // ignore comments
9e403697 172 if (new_message.message[0] == ';') return;
7f613782 173
b6c86164 174 string possible_command = new_message.message;
0325af12 175
3add9a23
AW
176 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
177
0325af12 178 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
9e403697 179 unsigned short check_sum = get_checksum( possible_command.substr(0, possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
6187a020 180
9e403697
JM
181 // find command and execute it
182 parse_command(check_sum, get_arguments(possible_command), new_message.stream);
0325af12
AW
183}
184
0325af12 185// Convert a path indication ( absolute or relative ) into a path ( absolute )
9e403697
JM
186string SimpleShell::absolute_from_relative( string path )
187{
188 if ( path[0] == '/' ) {
189 return path;
190 }
191 if ( path[0] == '.' ) {
192 return this->current_path;
193 }
0325af12
AW
194 return this->current_path + path;
195}
196
197// Act upon an ls command
198// Convert the first parameter into an absolute path, then list the files in that path
9e403697
JM
199void SimpleShell::ls_command( string parameters, StreamOutput *stream )
200{
0325af12 201 string folder = this->absolute_from_relative( parameters );
9e403697
JM
202 DIR *d;
203 struct dirent *p;
0325af12 204 d = opendir(folder.c_str());
9e403697
JM
205 if (d != NULL) {
206 while ((p = readdir(d)) != NULL) {
207 stream->printf("%s\r\n", lc(string(p->d_name)).c_str());
208 }
ed7c5844 209 closedir(d);
0325af12 210 } else {
b6c86164 211 stream->printf("Could not open directory %s \r\n", folder.c_str());
0325af12
AW
212 }
213}
214
9e403697
JM
215// Delete a file
216void SimpleShell::rm_command( string parameters, StreamOutput *stream )
217{
218 const char *fn= this->absolute_from_relative(shift_parameter( parameters )).c_str();
219 int s = remove(fn);
220 if (s != 0) stream->printf("Could not delete %s \r\n", fn);
221}
222
0325af12 223// Change current absolute path to provided path
9e403697
JM
224void SimpleShell::cd_command( string parameters, StreamOutput *stream )
225{
0325af12 226 string folder = this->absolute_from_relative( parameters );
9e403697
JM
227 if ( folder[folder.length() - 1] != '/' ) {
228 folder += "/";
229 }
0325af12 230 DIR *d;
0325af12 231 d = opendir(folder.c_str());
9e403697 232 if (d == NULL) {
58baeec1 233 stream->printf("Could not open directory %s \r\n", folder.c_str() );
9e403697 234 } else {
ed7c5844
JM
235 this->current_path = folder;
236 closedir(d);
0325af12
AW
237 }
238}
239
b7250484 240// Responds with the present working directory
9e403697
JM
241void SimpleShell::pwd_command( string parameters, StreamOutput *stream )
242{
b7250484
L
243 stream->printf("%s\r\n", this->current_path.c_str());
244}
245
0325af12 246// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
9e403697
JM
247void SimpleShell::cat_command( string parameters, StreamOutput *stream )
248{
58baeec1
MM
249
250 // Get parameters ( filename and line limit )
0325af12
AW
251 string filename = this->absolute_from_relative(shift_parameter( parameters ));
252 string limit_paramater = shift_parameter( parameters );
253 int limit = -1;
9e403697
JM
254 if ( limit_paramater != "" ) {
255 char *e = NULL;
f7e6f459
MM
256 limit = strtol(limit_paramater.c_str(), &e, 10);
257 if (e <= limit_paramater.c_str())
258 limit = -1;
259 }
58baeec1
MM
260
261 // Open file
0325af12 262 FILE *lp = fopen(filename.c_str(), "r");
9e403697 263 if (lp == NULL) {
58baeec1
MM
264 stream->printf("File not found: %s\r\n", filename.c_str());
265 return;
9ed670c5 266 }
0325af12
AW
267 string buffer;
268 int c;
58baeec1
MM
269 int newlines = 0;
270
0325af12 271 // Print each line of the file
9e403697 272 while ((c = fgetc (lp)) != EOF) {
58baeec1 273 buffer.append((char *)&c, 1);
9e403697 274 if ( char(c) == '\n' ) {
58baeec1 275 newlines++;
d728799b 276 stream->puts(buffer.c_str());
58baeec1 277 buffer.clear();
68b7afb4 278 }
9e403697
JM
279 if ( newlines == limit ) {
280 break;
281 }
58baeec1 282 };
0325af12
AW
283 fclose(lp);
284
285}
286
6187a020 287// show free memory
9e403697
JM
288void SimpleShell::mem_command( string parameters, StreamOutput *stream)
289{
290 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos ;
291 unsigned long heap = (unsigned long)_sbrk(0);
292 unsigned long m = g_maximumHeapAddress - heap;
ecc610a4
JM
293 stream->printf("Unused Heap: %lu bytes\r\n", m);
294
295 heapWalk(stream, verbose);
6187a020
JM
296}
297
9e403697
JM
298static uint32_t getDeviceType()
299{
300#define IAP_LOCATION 0x1FFF1FF1
01f35bcc
JM
301 uint32_t command[1];
302 uint32_t result[5];
9e403697 303 typedef void (*IAP)(uint32_t *, uint32_t *);
01f35bcc
JM
304 IAP iap = (IAP) IAP_LOCATION;
305
306 __disable_irq();
307
308 command[0] = 54;
309 iap(command, result);
310
311 __enable_irq();
312
313 return result[1];
314}
315
582559c6 316// print out build version
9e403697
JM
317void SimpleShell::version_command( string parameters, StreamOutput *stream)
318{
582559c6 319 Version vers;
9e403697
JM
320 uint32_t dev = getDeviceType();
321 const char *mcu = (dev & 0x00100000) ? "LPC1769" : "LPC1768";
01f35bcc 322 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
323}
324
77983aa1 325// Reset the system
9e403697
JM
326void SimpleShell::reset_command( string parameters, StreamOutput *stream)
327{
ead17727 328 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
9e403697 329 this->reset_delay_secs = 5; // reboot in 5 seconds
2742fca9
JM
330}
331
332// go into dfu boot mode
9e403697
JM
333void SimpleShell::dfu_command( string parameters, StreamOutput *stream)
334{
ed7c5844
JM
335 stream->printf("Entering boot mode...\r\n");
336 system_reset(true);
77983aa1
L
337}
338
0f0b1656 339// Break out into the MRI debugging system
9e403697
JM
340void SimpleShell::break_command( string parameters, StreamOutput *stream)
341{
0f0b1656
L
342 stream->printf("Entering MRI debug mode...\r\n");
343 __debugbreak();
344}
345
8293d443 346// used to test out the get public data events
9e403697
JM
347void SimpleShell::get_command( string parameters, StreamOutput *stream)
348{
349 int what = get_checksum(shift_parameter( parameters ));
b55cfff1 350 void *returned_data;
c4e56997 351
9e403697
JM
352 if (what == get_temp_command_checksum) {
353 string type = shift_parameter( parameters );
354 bool ok = this->kernel->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
b55cfff1 355
9e403697
JM
356 if (ok) {
357 struct pad_temperature temp = *static_cast<struct pad_temperature *>(returned_data);
b55cfff1 358 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
9e403697 359 } else {
b55cfff1
JM
360 stream->printf("%s is not a known temperature device\r\n", type.c_str());
361 }
c4e56997 362
9e403697
JM
363 } else if (what == get_pos_command_checksum) {
364 bool ok = this->kernel->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
b55cfff1 365
9e403697
JM
366 if (ok) {
367 double *pos = static_cast<double *>(returned_data);
b55cfff1 368 stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]);
c4e56997 369
9e403697 370 } else {
b55cfff1
JM
371 stream->printf("get pos command failed\r\n");
372 }
373 }
8293d443
JM
374}
375
77047e76 376// used to test out the get public data events
9e403697
JM
377void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
378{
379 string type = shift_parameter( parameters );
380 string temp = shift_parameter( parameters );
381 double t = temp.empty() ? 0.0 : strtod(temp.c_str(), NULL);
382 bool ok = this->kernel->public_data->set_value( temperature_control_checksum, get_checksum(type), &t );
991d98cc 383
9e403697 384 if (ok) {
991d98cc 385 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
9e403697 386 } else {
991d98cc
JM
387 stream->printf("%s is not a known temperature device\r\n", type.c_str());
388 }
77047e76
JM
389}
390
862fc625
JM
391#if 0
392#include "mbed.h"
393#include "BaseSolution.h"
394#include "RostockSolution.h"
395#include "JohannKosselSolution.h"
396#endif
397void SimpleShell::test_command( string parameters, StreamOutput *stream)
398{
399#if 0
400 double millimeters[3]= {100.0, 200.0, 300.0};
401 int steps[3];
402 BaseSolution* r= new RostockSolution(THEKERNEL->config);
403 BaseSolution* k= new JohannKosselSolution(THEKERNEL->config);
404 Timer timer;
405 timer.start();
406 for(int i=0;i<10;i++) r->millimeters_to_steps(millimeters, steps);
407 timer.stop();
408 float tr= timer.read();
409 timer.reset();
410 timer.start();
411 for(int i=0;i<10;i++) k->millimeters_to_steps(millimeters, steps);
412 timer.stop();
413 float tk= timer.read();
414 stream->printf("time RostockSolution: %f, time JohannKosselSolution: %f\n", tr, tk);
415 delete kr;
416 delete tk;
417#endif
418#if 0
419// time idle loop
420#include "mbed.h"
421static int tmin = 1000000;
422static int tmax = 0;
423void time_idle()
424void time_idle()
425{
426 Timer timer;
427 timer.start();
428 int begin, end;
429 for (int i = 0; i < 1000; ++i) {
430 begin = timer.read_us();
431 THEKERNEL->call_event(ON_IDLE);
432 end = timer.read_us();
433 int d = end - begin;
434 if (d < tmin) tmin = d;
435 if (d > tmax) tmax = d;
436 }
437}
438static Timer timer;
439static int lastt = 0;
440#endif
441}
442
9e403697
JM
443void SimpleShell::help_command( string parameters, StreamOutput *stream )
444{
ed7c5844 445 stream->printf("Commands:\r\n");
582559c6 446 stream->printf("version\r\n");
ecc610a4 447 stream->printf("mem [-v]\r\n");
ed7c5844
JM
448 stream->printf("ls [folder]\r\n");
449 stream->printf("cd folder\r\n");
c4e56997 450 stream->printf("pwd\r\n");
ed7c5844 451 stream->printf("cat file [limit]\r\n");
9e403697 452 stream->printf("rm file\r\n");
ed7c5844
JM
453 stream->printf("play file [-q]\r\n");
454 stream->printf("progress - shows progress of current play\r\n");
455 stream->printf("abort - abort currently playing file\r\n");
c4e56997
JM
456 stream->printf("reset - reset smoothie\r\n");
457 stream->printf("dfu - enter dfu boot loader\r\n");
458 stream->printf("break - break into debugger\r\n");
ed7c5844
JM
459 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
460 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
461 stream->printf("config-load [<file_name>]\r\n");
5647f709 462 stream->printf("get temp [bed|hotend]\r\n");
991d98cc 463 stream->printf("set_temp bed|hotend 185\r\n");
b55cfff1 464 stream->printf("get pos\r\n");
235a7435
JM
465}
466