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