PID Autotune: M304 aborts
[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"
0f0b1656 16#include "mri.h"
0325af12
AW
17
18
19void SimpleShell::on_module_loaded(){
20 this->current_path = "/";
21 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
22}
23
24// When a new line is received, check if it is a command, and if it is, act upon it
25void SimpleShell::on_console_line_received( void* argument ){
b6c86164
AW
26 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
27 string possible_command = new_message.message;
0325af12 28
3add9a23
AW
29 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
30
0325af12 31 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
7b49793d 32 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
cc63083e 33
0325af12 34 // Act depending on command
d9664db4
AG
35 if (check_sum == ls_command_checksum)
36 this->ls_command( get_arguments(possible_command), new_message.stream );
37 else if (check_sum == cd_command_checksum)
38 this->cd_command( get_arguments(possible_command), new_message.stream );
39 else if (check_sum == pwd_command_checksum)
40 this->pwd_command( get_arguments(possible_command), new_message.stream );
41 else if (check_sum == cat_command_checksum)
42 this->cat_command( get_arguments(possible_command), new_message.stream );
b34c7b9b
L
43 else if (check_sum == break_command_checksum)
44 this->break_command(get_arguments(possible_command),new_message.stream );
d9664db4
AG
45 else if (check_sum == reset_command_checksum)
46 this->reset_command(get_arguments(possible_command),new_message.stream );
47 else if (check_sum == dfu_command_checksum)
48 this->reset_command(get_arguments(possible_command),new_message.stream );
235a7435
JM
49 else if (check_sum == help_command_checksum)
50 this->help_command(get_arguments(possible_command),new_message.stream );
0325af12
AW
51}
52
0325af12
AW
53// Convert a path indication ( absolute or relative ) into a path ( absolute )
54string SimpleShell::absolute_from_relative( string path ){
55 if( path[0] == '/' ){ return path; }
58baeec1 56 if( path[0] == '.' ){ return this->current_path; }
0325af12
AW
57 return this->current_path + path;
58}
59
60// Act upon an ls command
61// Convert the first parameter into an absolute path, then list the files in that path
838b33b4 62void SimpleShell::ls_command( string parameters, StreamOutput* stream ){
0325af12
AW
63 string folder = this->absolute_from_relative( parameters );
64 DIR* d;
65 struct dirent* p;
66 d = opendir(folder.c_str());
67 if(d != NULL) {
ab2f6a3e
JM
68 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
69 closedir(d);
0325af12 70 } else {
b6c86164 71 stream->printf("Could not open directory %s \r\n", folder.c_str());
0325af12
AW
72 }
73}
74
75// Change current absolute path to provided path
838b33b4 76void SimpleShell::cd_command( string parameters, StreamOutput* stream ){
0325af12
AW
77 string folder = this->absolute_from_relative( parameters );
78 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
79 DIR *d;
0325af12 80 d = opendir(folder.c_str());
58baeec1
MM
81 if(d == NULL) {
82 stream->printf("Could not open directory %s \r\n", folder.c_str() );
0325af12 83 }else{
ab2f6a3e
JM
84 this->current_path = folder;
85 closedir(d);
0325af12
AW
86 }
87}
88
b7250484
L
89// Responds with the present working directory
90void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){
91 stream->printf("%s\r\n", this->current_path.c_str());
92}
93
0325af12 94// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
838b33b4 95void SimpleShell::cat_command( string parameters, StreamOutput* stream ){
58baeec1
MM
96
97 // Get parameters ( filename and line limit )
0325af12
AW
98 string filename = this->absolute_from_relative(shift_parameter( parameters ));
99 string limit_paramater = shift_parameter( parameters );
100 int limit = -1;
f7e6f459
MM
101 if( limit_paramater != "" )
102 {
103 char* e = NULL;
104 limit = strtol(limit_paramater.c_str(), &e, 10);
105 if (e <= limit_paramater.c_str())
106 limit = -1;
107 }
58baeec1
MM
108
109 // Open file
0325af12 110 FILE *lp = fopen(filename.c_str(), "r");
9ed670c5 111 if(lp == NULL) {
58baeec1
MM
112 stream->printf("File not found: %s\r\n", filename.c_str());
113 return;
9ed670c5 114 }
0325af12
AW
115 string buffer;
116 int c;
58baeec1
MM
117 int newlines = 0;
118
0325af12
AW
119 // Print each line of the file
120 while ((c = fgetc (lp)) != EOF){
58baeec1 121 buffer.append((char *)&c, 1);
68b7afb4 122 if( char(c) == '\n' ){
58baeec1 123 newlines++;
d728799b 124 stream->puts(buffer.c_str());
58baeec1 125 buffer.clear();
68b7afb4 126 }
0325af12 127 if( newlines == limit ){ break; }
58baeec1 128 };
0325af12
AW
129 fclose(lp);
130
131}
132
77983aa1
L
133// Reset the system
134void SimpleShell::reset_command( string parameters, StreamOutput* stream){
135 stream->printf("Smoothie out. Peace.\r\n");
4de76b51 136 system_reset();
77983aa1
L
137}
138
0f0b1656
L
139// Break out into the MRI debugging system
140void SimpleShell::break_command( string parameters, StreamOutput* stream){
141 stream->printf("Entering MRI debug mode...\r\n");
142 __debugbreak();
143}
144
235a7435
JM
145void SimpleShell::help_command( string parameters, StreamOutput* stream ){
146 stream->printf("Commands:\r\n");
147 stream->printf("ls [folder]\r\n");
148 stream->printf("cd folder\r\n");
149 stream->printf("pwd\r\n");
150 stream->printf("cat file [limit]\r\n");
151 stream->printf("play file [-q]\r\n");
152 stream->printf("progress\r\n");
153 stream->printf("abort\r\n");
154 stream->printf("reset\r\n");
155 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
156 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
157 stream->printf("config-load [<file_name>]\r\n");
158}
159