Use a struct to return public data requests, see example in temerature control
[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"
172d42d9 16#include "DirHandle.h"
0f0b1656 17#include "mri.h"
582559c6 18#include "version.h"
8293d443 19#include "PublicDataRequest.h"
0325af12 20
47339e4a
JM
21#include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
22
0325af12
AW
23void SimpleShell::on_module_loaded(){
24 this->current_path = "/";
25 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
ead17727
JM
26 this->reset_delay_secs= 0;
27
5895ead3 28 register_for_event(ON_SECOND_TICK);
ead17727
JM
29}
30
5895ead3 31void SimpleShell::on_second_tick(void*) {
ead17727 32 // we are timing out for the reset
5895ead3
JM
33 if (this->reset_delay_secs > 0) {
34 if(--this->reset_delay_secs == 0){
ead17727
JM
35 system_reset(false);
36 }
37 }
0325af12
AW
38}
39
40// When a new line is received, check if it is a command, and if it is, act upon it
41void SimpleShell::on_console_line_received( void* argument ){
b6c86164
AW
42 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
43 string possible_command = new_message.message;
0325af12 44
3add9a23
AW
45 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
46
0325af12 47 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
7b49793d 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
cc63083e 49
0325af12 50 // Act depending on command
d9664db4
AG
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 );
b34c7b9b
L
59 else if (check_sum == break_command_checksum)
60 this->break_command(get_arguments(possible_command),new_message.stream );
d9664db4
AG
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)
2742fca9 64 this->dfu_command(get_arguments(possible_command),new_message.stream );
ed7c5844
JM
65 else if (check_sum == help_command_checksum)
66 this->help_command(get_arguments(possible_command),new_message.stream );
582559c6
JM
67 else if (check_sum == version_command_checksum)
68 this->version_command(get_arguments(possible_command),new_message.stream );
8293d443
JM
69 else if (check_sum == get_temp_command_checksum)
70 this->get_temp_command(get_arguments(possible_command),new_message.stream );
0325af12
AW
71}
72
0325af12
AW
73// Convert a path indication ( absolute or relative ) into a path ( absolute )
74string SimpleShell::absolute_from_relative( string path ){
75 if( path[0] == '/' ){ return path; }
58baeec1 76 if( path[0] == '.' ){ return this->current_path; }
0325af12
AW
77 return this->current_path + path;
78}
79
80// Act upon an ls command
81// Convert the first parameter into an absolute path, then list the files in that path
838b33b4 82void SimpleShell::ls_command( string parameters, StreamOutput* stream ){
0325af12
AW
83 string folder = this->absolute_from_relative( parameters );
84 DIR* d;
85 struct dirent* p;
86 d = opendir(folder.c_str());
87 if(d != NULL) {
ed7c5844
JM
88 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
89 closedir(d);
0325af12 90 } else {
b6c86164 91 stream->printf("Could not open directory %s \r\n", folder.c_str());
0325af12
AW
92 }
93}
94
95// Change current absolute path to provided path
838b33b4 96void SimpleShell::cd_command( string parameters, StreamOutput* stream ){
0325af12
AW
97 string folder = this->absolute_from_relative( parameters );
98 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
99 DIR *d;
0325af12 100 d = opendir(folder.c_str());
58baeec1
MM
101 if(d == NULL) {
102 stream->printf("Could not open directory %s \r\n", folder.c_str() );
0325af12 103 }else{
ed7c5844
JM
104 this->current_path = folder;
105 closedir(d);
0325af12
AW
106 }
107}
108
b7250484
L
109// Responds with the present working directory
110void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){
111 stream->printf("%s\r\n", this->current_path.c_str());
112}
113
0325af12 114// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
838b33b4 115void SimpleShell::cat_command( string parameters, StreamOutput* stream ){
58baeec1
MM
116
117 // Get parameters ( filename and line limit )
0325af12
AW
118 string filename = this->absolute_from_relative(shift_parameter( parameters ));
119 string limit_paramater = shift_parameter( parameters );
120 int limit = -1;
f7e6f459
MM
121 if( limit_paramater != "" )
122 {
123 char* e = NULL;
124 limit = strtol(limit_paramater.c_str(), &e, 10);
125 if (e <= limit_paramater.c_str())
126 limit = -1;
127 }
58baeec1
MM
128
129 // Open file
0325af12 130 FILE *lp = fopen(filename.c_str(), "r");
9ed670c5 131 if(lp == NULL) {
58baeec1
MM
132 stream->printf("File not found: %s\r\n", filename.c_str());
133 return;
9ed670c5 134 }
0325af12
AW
135 string buffer;
136 int c;
58baeec1
MM
137 int newlines = 0;
138
0325af12
AW
139 // Print each line of the file
140 while ((c = fgetc (lp)) != EOF){
58baeec1 141 buffer.append((char *)&c, 1);
68b7afb4 142 if( char(c) == '\n' ){
58baeec1 143 newlines++;
d728799b 144 stream->puts(buffer.c_str());
58baeec1 145 buffer.clear();
68b7afb4 146 }
0325af12 147 if( newlines == limit ){ break; }
58baeec1 148 };
0325af12
AW
149 fclose(lp);
150
151}
152
582559c6
JM
153// print out build version
154void SimpleShell::version_command( string parameters, StreamOutput* stream){
155 Version vers;
156 stream->printf("Build version: %s, Build date: %s, System Clock: %ldMHz\r\n", vers.get_build(), vers.get_build_date(), SystemCoreClock / 1000000);
157}
158
77983aa1
L
159// Reset the system
160void SimpleShell::reset_command( string parameters, StreamOutput* stream){
ead17727 161 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
5895ead3 162 this->reset_delay_secs= 5; // reboot in 5 seconds
2742fca9
JM
163}
164
165// go into dfu boot mode
166void SimpleShell::dfu_command( string parameters, StreamOutput* stream){
ed7c5844
JM
167 stream->printf("Entering boot mode...\r\n");
168 system_reset(true);
77983aa1
L
169}
170
0f0b1656
L
171// Break out into the MRI debugging system
172void SimpleShell::break_command( string parameters, StreamOutput* stream){
173 stream->printf("Entering MRI debug mode...\r\n");
174 __debugbreak();
175}
176
8293d443
JM
177// used to test out the get public data events
178void SimpleShell::get_temp_command( string parameters, StreamOutput* stream){
47339e4a 179 string type= shift_parameter( parameters );
f2cf03f7
JM
180 void *returned_data;
181 bool ok= this->kernel->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
47339e4a
JM
182
183 if(ok) {
f2cf03f7
JM
184 struct pad_temperature temp= *static_cast<struct pad_temperature*>(returned_data);
185 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
47339e4a
JM
186 }else{
187 stream->printf("%s is not a known temperature device\r\n", type.c_str());
188 }
8293d443
JM
189}
190
235a7435 191void SimpleShell::help_command( string parameters, StreamOutput* stream ){
ed7c5844 192 stream->printf("Commands:\r\n");
582559c6 193 stream->printf("version\r\n");
ed7c5844
JM
194 stream->printf("ls [folder]\r\n");
195 stream->printf("cd folder\r\n");
196 stream->printf("pwd\r\n");
197 stream->printf("cat file [limit]\r\n");
198 stream->printf("play file [-q]\r\n");
199 stream->printf("progress - shows progress of current play\r\n");
200 stream->printf("abort - abort currently playing file\r\n");
201 stream->printf("reset - reset smoothie\r\n");
202 stream->printf("dfu - enter dfu boot loader\r\n");
ead17727 203 stream->printf("break - break into debugger\r\n");
ed7c5844
JM
204 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
205 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
206 stream->printf("config-load [<file_name>]\r\n");
235a7435
JM
207}
208