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