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/>.
9 #include "libs/Kernel.h"
10 #include "SimpleShell.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"
20 void SimpleShell::on_module_loaded(){
21 this->current_path
= "/";
22 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
23 this->reset_delay_secs
= 0;
25 register_for_event(ON_IDLE
);
28 void SimpleShell::on_idle(void*) {
29 // we are timing out for the reset
30 if (this->reset_delay_secs
) {
31 if(time(NULL
) >= this->reset_delay_secs
){
37 // When a new line is received, check if it is a command, and if it is, act upon it
38 void SimpleShell::on_console_line_received( void* argument
){
39 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
40 string possible_command
= new_message
.message
;
42 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
44 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
45 unsigned short check_sum
= get_checksum( possible_command
.substr(0,possible_command
.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
47 // Act depending on command
48 if (check_sum
== ls_command_checksum
)
49 this->ls_command( get_arguments(possible_command
), new_message
.stream
);
50 else if (check_sum
== cd_command_checksum
)
51 this->cd_command( get_arguments(possible_command
), new_message
.stream
);
52 else if (check_sum
== pwd_command_checksum
)
53 this->pwd_command( get_arguments(possible_command
), new_message
.stream
);
54 else if (check_sum
== cat_command_checksum
)
55 this->cat_command( get_arguments(possible_command
), new_message
.stream
);
56 else if (check_sum
== break_command_checksum
)
57 this->break_command(get_arguments(possible_command
),new_message
.stream
);
58 else if (check_sum
== reset_command_checksum
)
59 this->reset_command(get_arguments(possible_command
),new_message
.stream
);
60 else if (check_sum
== dfu_command_checksum
)
61 this->dfu_command(get_arguments(possible_command
),new_message
.stream
);
62 else if (check_sum
== help_command_checksum
)
63 this->help_command(get_arguments(possible_command
),new_message
.stream
);
66 // Convert a path indication ( absolute or relative ) into a path ( absolute )
67 string
SimpleShell::absolute_from_relative( string path
){
68 if( path
[0] == '/' ){ return path
; }
69 if( path
[0] == '.' ){ return this->current_path
; }
70 return this->current_path
+ path
;
73 // Act upon an ls command
74 // Convert the first parameter into an absolute path, then list the files in that path
75 void SimpleShell::ls_command( string parameters
, StreamOutput
* stream
){
76 string folder
= this->absolute_from_relative( parameters
);
79 d
= opendir(folder
.c_str());
81 while((p
= readdir(d
)) != NULL
) { stream
->printf("%s\r\n", lc(string(p
->d_name
)).c_str()); }
84 stream
->printf("Could not open directory %s \r\n", folder
.c_str());
88 // Change current absolute path to provided path
89 void SimpleShell::cd_command( string parameters
, StreamOutput
* stream
){
90 string folder
= this->absolute_from_relative( parameters
);
91 if( folder
[folder
.length()-1] != '/' ){ folder
+= "/"; }
93 d
= opendir(folder
.c_str());
95 stream
->printf("Could not open directory %s \r\n", folder
.c_str() );
97 this->current_path
= folder
;
102 // Responds with the present working directory
103 void SimpleShell::pwd_command( string parameters
, StreamOutput
* stream
){
104 stream
->printf("%s\r\n", this->current_path
.c_str());
107 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
108 void SimpleShell::cat_command( string parameters
, StreamOutput
* stream
){
110 // Get parameters ( filename and line limit )
111 string filename
= this->absolute_from_relative(shift_parameter( parameters
));
112 string limit_paramater
= shift_parameter( parameters
);
114 if( limit_paramater
!= "" )
117 limit
= strtol(limit_paramater
.c_str(), &e
, 10);
118 if (e
<= limit_paramater
.c_str())
123 FILE *lp
= fopen(filename
.c_str(), "r");
125 stream
->printf("File not found: %s\r\n", filename
.c_str());
132 // Print each line of the file
133 while ((c
= fgetc (lp
)) != EOF
){
134 buffer
.append((char *)&c
, 1);
135 if( char(c
) == '\n' ){
137 stream
->puts(buffer
.c_str());
140 if( newlines
== limit
){ break; }
147 void SimpleShell::reset_command( string parameters
, StreamOutput
* stream
){
148 stream
->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
149 this->reset_delay_secs
= time(NULL
) + 5; // reboot in 5 seconds
152 // go into dfu boot mode
153 void SimpleShell::dfu_command( string parameters
, StreamOutput
* stream
){
154 stream
->printf("Entering boot mode...\r\n");
158 // Break out into the MRI debugging system
159 void SimpleShell::break_command( string parameters
, StreamOutput
* stream
){
160 stream
->printf("Entering MRI debug mode...\r\n");
164 void SimpleShell::help_command( string parameters
, StreamOutput
* stream
){
165 stream
->printf("Commands:\r\n");
166 stream
->printf("ls [folder]\r\n");
167 stream
->printf("cd folder\r\n");
168 stream
->printf("pwd\r\n");
169 stream
->printf("cat file [limit]\r\n");
170 stream
->printf("play file [-q]\r\n");
171 stream
->printf("progress - shows progress of current play\r\n");
172 stream
->printf("abort - abort currently playing file\r\n");
173 stream
->printf("reset - reset smoothie\r\n");
174 stream
->printf("dfu - enter dfu boot loader\r\n");
175 stream
->printf("break - break into debugger\r\n");
176 stream
->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
177 stream
->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
178 stream
->printf("config-load [<file_name>]\r\n");