cleanup white space tabs -> spaces
[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 #include "version.h"
19 #include "PublicDataRequest.h"
20
21 #include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
22
23 void SimpleShell::on_module_loaded(){
24 this->current_path = "/";
25 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
26 this->reset_delay_secs= 0;
27
28 register_for_event(ON_SECOND_TICK);
29 }
30
31 void SimpleShell::on_second_tick(void*) {
32 // we are timing out for the reset
33 if (this->reset_delay_secs > 0) {
34 if(--this->reset_delay_secs == 0){
35 system_reset(false);
36 }
37 }
38 }
39
40 // When a new line is received, check if it is a command, and if it is, act upon it
41 void SimpleShell::on_console_line_received( void* argument ){
42 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
43 string possible_command = new_message.message;
44
45 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
46
47 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
48 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
49
50 // Act depending on command
51 if (check_sum == ls_command_checksum)
52 this->ls_command( get_arguments(possible_command), new_message.stream );
53 else if (check_sum == cd_command_checksum)
54 this->cd_command( get_arguments(possible_command), new_message.stream );
55 else if (check_sum == pwd_command_checksum)
56 this->pwd_command( get_arguments(possible_command), new_message.stream );
57 else if (check_sum == cat_command_checksum)
58 this->cat_command( get_arguments(possible_command), new_message.stream );
59 else if (check_sum == break_command_checksum)
60 this->break_command(get_arguments(possible_command),new_message.stream );
61 else if (check_sum == reset_command_checksum)
62 this->reset_command(get_arguments(possible_command),new_message.stream );
63 else if (check_sum == dfu_command_checksum)
64 this->dfu_command(get_arguments(possible_command),new_message.stream );
65 else if (check_sum == help_command_checksum)
66 this->help_command(get_arguments(possible_command),new_message.stream );
67 else if (check_sum == version_command_checksum)
68 this->version_command(get_arguments(possible_command),new_message.stream );
69 else if (check_sum == get_temp_command_checksum)
70 this->get_temp_command(get_arguments(possible_command),new_message.stream );
71 else if (check_sum == set_temp_command_checksum)
72 this->set_temp_command(get_arguments(possible_command),new_message.stream );
73 }
74
75 // Convert a path indication ( absolute or relative ) into a path ( absolute )
76 string SimpleShell::absolute_from_relative( string path ){
77 if( path[0] == '/' ){ return path; }
78 if( path[0] == '.' ){ return this->current_path; }
79 return this->current_path + path;
80 }
81
82 // Act upon an ls command
83 // Convert the first parameter into an absolute path, then list the files in that path
84 void SimpleShell::ls_command( string parameters, StreamOutput* stream ){
85 string folder = this->absolute_from_relative( parameters );
86 DIR* d;
87 struct dirent* p;
88 d = opendir(folder.c_str());
89 if(d != NULL) {
90 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
91 closedir(d);
92 } else {
93 stream->printf("Could not open directory %s \r\n", folder.c_str());
94 }
95 }
96
97 // Change current absolute path to provided path
98 void SimpleShell::cd_command( string parameters, StreamOutput* stream ){
99 string folder = this->absolute_from_relative( parameters );
100 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
101 DIR *d;
102 d = opendir(folder.c_str());
103 if(d == NULL) {
104 stream->printf("Could not open directory %s \r\n", folder.c_str() );
105 }else{
106 this->current_path = folder;
107 closedir(d);
108 }
109 }
110
111 // Responds with the present working directory
112 void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){
113 stream->printf("%s\r\n", this->current_path.c_str());
114 }
115
116 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
117 void SimpleShell::cat_command( string parameters, StreamOutput* stream ){
118
119 // Get parameters ( filename and line limit )
120 string filename = this->absolute_from_relative(shift_parameter( parameters ));
121 string limit_paramater = shift_parameter( parameters );
122 int limit = -1;
123 if( limit_paramater != "" )
124 {
125 char* e = NULL;
126 limit = strtol(limit_paramater.c_str(), &e, 10);
127 if (e <= limit_paramater.c_str())
128 limit = -1;
129 }
130
131 // Open file
132 FILE *lp = fopen(filename.c_str(), "r");
133 if(lp == NULL) {
134 stream->printf("File not found: %s\r\n", filename.c_str());
135 return;
136 }
137 string buffer;
138 int c;
139 int newlines = 0;
140
141 // Print each line of the file
142 while ((c = fgetc (lp)) != EOF){
143 buffer.append((char *)&c, 1);
144 if( char(c) == '\n' ){
145 newlines++;
146 stream->puts(buffer.c_str());
147 buffer.clear();
148 }
149 if( newlines == limit ){ break; }
150 };
151 fclose(lp);
152
153 }
154
155 // print out build version
156 void SimpleShell::version_command( string parameters, StreamOutput* stream){
157 Version vers;
158 stream->printf("Build version: %s, Build date: %s, System Clock: %ldMHz\r\n", vers.get_build(), vers.get_build_date(), SystemCoreClock / 1000000);
159 }
160
161 // Reset the system
162 void SimpleShell::reset_command( string parameters, StreamOutput* stream){
163 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
164 this->reset_delay_secs= 5; // reboot in 5 seconds
165 }
166
167 // go into dfu boot mode
168 void SimpleShell::dfu_command( string parameters, StreamOutput* stream){
169 stream->printf("Entering boot mode...\r\n");
170 system_reset(true);
171 }
172
173 // Break out into the MRI debugging system
174 void SimpleShell::break_command( string parameters, StreamOutput* stream){
175 stream->printf("Entering MRI debug mode...\r\n");
176 __debugbreak();
177 }
178
179 // used to test out the get public data events
180 void SimpleShell::get_temp_command( string parameters, StreamOutput* stream){
181 string type= shift_parameter( parameters );
182 void *returned_data;
183 bool ok= this->kernel->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
184
185 if(ok) {
186 struct pad_temperature temp= *static_cast<struct pad_temperature*>(returned_data);
187 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
188 }else{
189 stream->printf("%s is not a known temperature device\r\n", type.c_str());
190 }
191 }
192
193 // used to test out the get public data events
194 void SimpleShell::set_temp_command( string parameters, StreamOutput* stream){
195 string type= shift_parameter( parameters );
196 string temp= shift_parameter( parameters );
197 double t= temp.empty() ? 0.0 : strtod(temp.c_str(), NULL);
198 bool ok= this->kernel->public_data->set_value( temperature_control_checksum, get_checksum(type), &t );
199
200 if(ok) {
201 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
202 }else{
203 stream->printf("%s is not a known temperature device\r\n", type.c_str());
204 }
205 }
206
207 void SimpleShell::help_command( string parameters, StreamOutput* stream ){
208 stream->printf("Commands:\r\n");
209 stream->printf("version\r\n");
210 stream->printf("ls [folder]\r\n");
211 stream->printf("cd folder\r\n");
212 stream->printf("pwd\r\n");
213 stream->printf("cat file [limit]\r\n");
214 stream->printf("play file [-q]\r\n");
215 stream->printf("progress - shows progress of current play\r\n");
216 stream->printf("abort - abort currently playing file\r\n");
217 stream->printf("reset - reset smoothie\r\n");
218 stream->printf("dfu - enter dfu boot loader\r\n");
219 stream->printf("break - break into debugger\r\n");
220 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
221 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
222 stream->printf("config-load [<file_name>]\r\n");
223 stream->printf("get_temp bed|hotend\r\n");
224 stream->printf("set_temp bed|hotend 185\r\n");
225 }
226