Commit | Line | Data |
---|---|---|
cd011f58 AW |
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 | ||
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" |
0325af12 AW |
15 | |
16 | ||
17 | void SimpleShell::on_module_loaded(){ | |
18 | this->current_path = "/"; | |
423df6df | 19 | this->playing_file = false; |
0325af12 | 20 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
423df6df | 21 | this->register_for_event(ON_MAIN_LOOP); |
0325af12 AW |
22 | } |
23 | ||
24 | // When a new line is received, check if it is a command, and if it is, act upon it | |
25 | void SimpleShell::on_console_line_received( void* argument ){ | |
b6c86164 AW |
26 | SerialMessage new_message = *static_cast<SerialMessage*>(argument); |
27 | string possible_command = new_message.message; | |
0325af12 AW |
28 | |
29 | // We don't compare to a string but to a checksum of that string, this saves some space in flash memory | |
30 | 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 | 31 | |
0325af12 AW |
32 | // Act depending on command |
33 | switch( check_sum ){ | |
b6c86164 AW |
34 | case ls_command_checksum : this->ls_command( get_arguments(possible_command), new_message.stream ); break; |
35 | case cd_command_checksum : this->cd_command( get_arguments(possible_command), new_message.stream ); break; | |
36 | case cat_command_checksum : this->cat_command( get_arguments(possible_command), new_message.stream ); break; | |
37 | case play_command_checksum : this->play_command(get_arguments(possible_command), new_message.stream ); break; | |
0325af12 AW |
38 | } |
39 | } | |
40 | ||
0325af12 AW |
41 | // Convert a path indication ( absolute or relative ) into a path ( absolute ) |
42 | string SimpleShell::absolute_from_relative( string path ){ | |
43 | if( path[0] == '/' ){ return path; } | |
44 | if( path[0] == '.' ){ return this->current_path; } | |
45 | return this->current_path + path; | |
46 | } | |
47 | ||
48 | // Act upon an ls command | |
49 | // Convert the first parameter into an absolute path, then list the files in that path | |
838b33b4 | 50 | void SimpleShell::ls_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
51 | string folder = this->absolute_from_relative( parameters ); |
52 | DIR* d; | |
53 | struct dirent* p; | |
54 | d = opendir(folder.c_str()); | |
55 | if(d != NULL) { | |
b6c86164 | 56 | while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); } |
0325af12 | 57 | } else { |
b6c86164 | 58 | stream->printf("Could not open directory %s \r\n", folder.c_str()); |
0325af12 AW |
59 | } |
60 | } | |
61 | ||
62 | // Change current absolute path to provided path | |
838b33b4 | 63 | void SimpleShell::cd_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
64 | string folder = this->absolute_from_relative( parameters ); |
65 | if( folder[folder.length()-1] != '/' ){ folder += "/"; } | |
66 | DIR *d; | |
67 | struct dirent *p; | |
68 | d = opendir(folder.c_str()); | |
69 | if(d == NULL) { | |
b6c86164 | 70 | stream->printf("Could not open directory %s \r\n", folder.c_str() ); |
0325af12 AW |
71 | }else{ |
72 | this->current_path = folder; | |
73 | } | |
74 | } | |
75 | ||
76 | // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output ) | |
838b33b4 | 77 | void SimpleShell::cat_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
78 | |
79 | // Get parameters ( filename and line limit ) | |
80 | string filename = this->absolute_from_relative(shift_parameter( parameters )); | |
81 | string limit_paramater = shift_parameter( parameters ); | |
82 | int limit = -1; | |
83 | if( limit_paramater != "" ){ limit = int(atof(limit_paramater.c_str())); } | |
84 | ||
85 | // Open file | |
86 | FILE *lp = fopen(filename.c_str(), "r"); | |
87 | string buffer; | |
88 | int c; | |
89 | int newlines = 0; | |
90 | ||
91 | // Print each line of the file | |
92 | while ((c = fgetc (lp)) != EOF){ | |
93 | if( char(c) == '\n' ){ newlines++; } | |
838b33b4 | 94 | stream->printf("%c",c); |
0325af12 AW |
95 | if( newlines == limit ){ break; } |
96 | }; | |
97 | fclose(lp); | |
98 | ||
99 | } | |
100 | ||
cc63083e | 101 | // Play a gcode file by considering each line as if it was received on the serial console |
838b33b4 | 102 | void SimpleShell::play_command( string parameters, StreamOutput* stream ){ |
cc63083e | 103 | // Get filename |
423df6df AW |
104 | this->current_file_handler = fopen( this->absolute_from_relative(shift_parameter( parameters )).c_str(), "r"); |
105 | this->playing_file = true; | |
106 | this->current_stream = stream; | |
107 | } | |
108 | void SimpleShell::on_main_loop(void* argument){ | |
109 | ||
110 | if( this->playing_file ){ | |
111 | string buffer; | |
112 | int c; | |
113 | // Print each line of the file | |
114 | while ((c = fgetc(this->current_file_handler)) != EOF){ | |
115 | if (c == '\n'){ | |
116 | this->current_stream->printf("%s\n", buffer.c_str()); | |
117 | struct SerialMessage message; | |
118 | message.message = buffer; | |
119 | message.stream = this->current_stream; | |
120 | this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message); | |
121 | buffer.clear(); | |
122 | return; | |
123 | }else{ | |
124 | buffer += c; | |
125 | } | |
126 | }; | |
127 | ||
128 | fclose(this->current_file_handler); | |
129 | this->playing_file = false; | |
130 | } | |
2bb8b390 | 131 | } |