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