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