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" |
7fccecc2 | 15 | #include "modules/robot/Player.h" |
0325af12 AW |
16 | |
17 | ||
18 | void SimpleShell::on_module_loaded(){ | |
19 | this->current_path = "/"; | |
423df6df | 20 | this->playing_file = false; |
0325af12 | 21 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
423df6df | 22 | this->register_for_event(ON_MAIN_LOOP); |
0325af12 AW |
23 | } |
24 | ||
25 | // When a new line is received, check if it is a command, and if it is, act upon it | |
26 | void SimpleShell::on_console_line_received( void* argument ){ | |
b6c86164 AW |
27 | SerialMessage new_message = *static_cast<SerialMessage*>(argument); |
28 | string possible_command = new_message.message; | |
0325af12 | 29 | |
3add9a23 AW |
30 | //new_message.stream->printf("Received %s\r\n", possible_command.c_str()); |
31 | ||
0325af12 | 32 | // We don't compare to a string but to a checksum of that string, this saves some space in flash memory |
7b49793d | 33 | 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 | 34 | |
0325af12 | 35 | // Act depending on command |
d9664db4 AG |
36 | if (check_sum == ls_command_checksum) |
37 | this->ls_command( get_arguments(possible_command), new_message.stream ); | |
38 | else if (check_sum == cd_command_checksum) | |
39 | this->cd_command( get_arguments(possible_command), new_message.stream ); | |
40 | else if (check_sum == pwd_command_checksum) | |
41 | this->pwd_command( get_arguments(possible_command), new_message.stream ); | |
42 | else if (check_sum == cat_command_checksum) | |
43 | this->cat_command( get_arguments(possible_command), new_message.stream ); | |
44 | else if (check_sum == play_command_checksum) | |
45 | this->play_command(get_arguments(possible_command), new_message.stream ); | |
46 | else if (check_sum == reset_command_checksum) | |
47 | this->reset_command(get_arguments(possible_command),new_message.stream ); | |
48 | else if (check_sum == dfu_command_checksum) | |
49 | this->reset_command(get_arguments(possible_command),new_message.stream ); | |
0325af12 AW |
50 | } |
51 | ||
0325af12 AW |
52 | // Convert a path indication ( absolute or relative ) into a path ( absolute ) |
53 | string SimpleShell::absolute_from_relative( string path ){ | |
54 | if( path[0] == '/' ){ return path; } | |
58baeec1 | 55 | if( path[0] == '.' ){ return this->current_path; } |
0325af12 AW |
56 | return this->current_path + path; |
57 | } | |
58 | ||
59 | // Act upon an ls command | |
60 | // Convert the first parameter into an absolute path, then list the files in that path | |
838b33b4 | 61 | void SimpleShell::ls_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
62 | string folder = this->absolute_from_relative( parameters ); |
63 | DIR* d; | |
64 | struct dirent* p; | |
65 | d = opendir(folder.c_str()); | |
66 | if(d != NULL) { | |
ab2f6a3e JM |
67 | while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); } |
68 | closedir(d); | |
0325af12 | 69 | } else { |
b6c86164 | 70 | stream->printf("Could not open directory %s \r\n", folder.c_str()); |
0325af12 AW |
71 | } |
72 | } | |
73 | ||
74 | // Change current absolute path to provided path | |
838b33b4 | 75 | void SimpleShell::cd_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
76 | string folder = this->absolute_from_relative( parameters ); |
77 | if( folder[folder.length()-1] != '/' ){ folder += "/"; } | |
78 | DIR *d; | |
0325af12 | 79 | d = opendir(folder.c_str()); |
58baeec1 MM |
80 | if(d == NULL) { |
81 | stream->printf("Could not open directory %s \r\n", folder.c_str() ); | |
0325af12 | 82 | }else{ |
ab2f6a3e JM |
83 | this->current_path = folder; |
84 | closedir(d); | |
0325af12 AW |
85 | } |
86 | } | |
87 | ||
b7250484 L |
88 | // Responds with the present working directory |
89 | void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){ | |
90 | stream->printf("%s\r\n", this->current_path.c_str()); | |
91 | } | |
92 | ||
0325af12 | 93 | // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output ) |
838b33b4 | 94 | void SimpleShell::cat_command( string parameters, StreamOutput* stream ){ |
58baeec1 MM |
95 | |
96 | // Get parameters ( filename and line limit ) | |
0325af12 AW |
97 | string filename = this->absolute_from_relative(shift_parameter( parameters )); |
98 | string limit_paramater = shift_parameter( parameters ); | |
99 | int limit = -1; | |
f7e6f459 MM |
100 | if( limit_paramater != "" ) |
101 | { | |
102 | char* e = NULL; | |
103 | limit = strtol(limit_paramater.c_str(), &e, 10); | |
104 | if (e <= limit_paramater.c_str()) | |
105 | limit = -1; | |
106 | } | |
58baeec1 MM |
107 | |
108 | // Open file | |
0325af12 | 109 | FILE *lp = fopen(filename.c_str(), "r"); |
9ed670c5 | 110 | if(lp == NULL) { |
58baeec1 MM |
111 | stream->printf("File not found: %s\r\n", filename.c_str()); |
112 | return; | |
9ed670c5 | 113 | } |
0325af12 AW |
114 | string buffer; |
115 | int c; | |
58baeec1 MM |
116 | int newlines = 0; |
117 | ||
0325af12 AW |
118 | // Print each line of the file |
119 | while ((c = fgetc (lp)) != EOF){ | |
58baeec1 | 120 | buffer.append((char *)&c, 1); |
68b7afb4 | 121 | if( char(c) == '\n' ){ |
58baeec1 MM |
122 | newlines++; |
123 | stream->printf("%s", buffer.c_str()); | |
124 | buffer.clear(); | |
68b7afb4 | 125 | } |
0325af12 | 126 | if( newlines == limit ){ break; } |
58baeec1 | 127 | }; |
0325af12 AW |
128 | fclose(lp); |
129 | ||
130 | } | |
131 | ||
cc63083e | 132 | // Play a gcode file by considering each line as if it was received on the serial console |
838b33b4 | 133 | void SimpleShell::play_command( string parameters, StreamOutput* stream ){ |
58baeec1 | 134 | |
cc63083e | 135 | // Get filename |
9ed670c5 | 136 | string filename = this->absolute_from_relative(shift_parameter( parameters )); |
3add9a23 | 137 | stream->printf("Playing %s\r\n", filename.c_str()); |
7790a7c4 | 138 | string options = shift_parameter( parameters ); |
58baeec1 | 139 | |
9ed670c5 BD |
140 | this->current_file_handler = fopen( filename.c_str(), "r"); |
141 | if(this->current_file_handler == NULL) | |
142 | { | |
58baeec1 MM |
143 | stream->printf("File not found: %s\r\n", filename.c_str()); |
144 | return; | |
9ed670c5 | 145 | } |
423df6df | 146 | this->playing_file = true; |
7790a7c4 AW |
147 | if( options.find_first_of("Qq") == string::npos ){ |
148 | this->current_stream = stream; | |
149 | }else{ | |
836222e7 | 150 | this->current_stream = kernel->streams; |
7790a7c4 | 151 | } |
423df6df | 152 | } |
77983aa1 L |
153 | |
154 | // Reset the system | |
155 | void SimpleShell::reset_command( string parameters, StreamOutput* stream){ | |
156 | stream->printf("Smoothie out. Peace.\r\n"); | |
4de76b51 | 157 | system_reset(); |
77983aa1 L |
158 | } |
159 | ||
423df6df AW |
160 | void SimpleShell::on_main_loop(void* argument){ |
161 | ||
58baeec1 | 162 | if( this->playing_file ){ |
423df6df AW |
163 | string buffer; |
164 | int c; | |
09ad665f | 165 | buffer.reserve(20); |
423df6df AW |
166 | // Print each line of the file |
167 | while ((c = fgetc(this->current_file_handler)) != EOF){ | |
168 | if (c == '\n'){ | |
169 | this->current_stream->printf("%s\n", buffer.c_str()); | |
58baeec1 | 170 | struct SerialMessage message; |
423df6df AW |
171 | message.message = buffer; |
172 | message.stream = this->current_stream; | |
8bcf74dc | 173 | // wait for the queue to have enough room that a serial message could still be received before sending |
3d560dd6 | 174 | this->kernel->player->wait_for_queue(2); |
58baeec1 | 175 | this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message); |
423df6df AW |
176 | buffer.clear(); |
177 | return; | |
178 | }else{ | |
179 | buffer += c; | |
180 | } | |
58baeec1 | 181 | }; |
423df6df AW |
182 | |
183 | fclose(this->current_file_handler); | |
184 | this->playing_file = false; | |
185 | } | |
2bb8b390 | 186 | } |