Merge pull request #145 from wolfmanjm/feature/add-reset
[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 "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"
17 #include "mri.h"
18
19
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;
24
25 register_for_event(ON_SECOND_TICK);
26 }
27
28 void SimpleShell::on_second_tick(void*) {
29 // we are timing out for the reset
30 if (this->reset_delay_secs > 0) {
31 if(--this->reset_delay_secs == 0){
32 system_reset(false);
33 }
34 }
35 }
36
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;
41
42 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
43
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
46
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 );
64 }
65
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;
71 }
72
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 );
77 DIR* d;
78 struct dirent* p;
79 d = opendir(folder.c_str());
80 if(d != NULL) {
81 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
82 closedir(d);
83 } else {
84 stream->printf("Could not open directory %s \r\n", folder.c_str());
85 }
86 }
87
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 += "/"; }
92 DIR *d;
93 d = opendir(folder.c_str());
94 if(d == NULL) {
95 stream->printf("Could not open directory %s \r\n", folder.c_str() );
96 }else{
97 this->current_path = folder;
98 closedir(d);
99 }
100 }
101
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());
105 }
106
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 ){
109
110 // Get parameters ( filename and line limit )
111 string filename = this->absolute_from_relative(shift_parameter( parameters ));
112 string limit_paramater = shift_parameter( parameters );
113 int limit = -1;
114 if( limit_paramater != "" )
115 {
116 char* e = NULL;
117 limit = strtol(limit_paramater.c_str(), &e, 10);
118 if (e <= limit_paramater.c_str())
119 limit = -1;
120 }
121
122 // Open file
123 FILE *lp = fopen(filename.c_str(), "r");
124 if(lp == NULL) {
125 stream->printf("File not found: %s\r\n", filename.c_str());
126 return;
127 }
128 string buffer;
129 int c;
130 int newlines = 0;
131
132 // Print each line of the file
133 while ((c = fgetc (lp)) != EOF){
134 buffer.append((char *)&c, 1);
135 if( char(c) == '\n' ){
136 newlines++;
137 stream->puts(buffer.c_str());
138 buffer.clear();
139 }
140 if( newlines == limit ){ break; }
141 };
142 fclose(lp);
143
144 }
145
146 // Reset the system
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= 5; // reboot in 5 seconds
150 }
151
152 // go into dfu boot mode
153 void SimpleShell::dfu_command( string parameters, StreamOutput* stream){
154 stream->printf("Entering boot mode...\r\n");
155 system_reset(true);
156 }
157
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");
161 __debugbreak();
162 }
163
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");
179 }
180