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/>.
9 #include "libs/Kernel.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/utils.h"
13 #include "libs/SerialMessage.h"
14 #include "libs/StreamOutput.h"
15 #include "modules/robot/Conveyor.h"
16 #include "DirHandle.h"
18 void Player::on_module_loaded(){
19 this->playing_file
= false;
21 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
22 this->register_for_event(ON_MAIN_LOOP
);
23 this->register_for_event(ON_SECOND_TICK
);
25 this->on_boot_file_name
= this->kernel
->config
->value(on_boot_gcode_checksum
)->by_default("/sd/on_boot.gcode -q")->as_string();
26 this->on_boot_enable
= this->kernel
->config
->value(on_boot_gcode_enable_checksum
)->by_default(true)->as_bool();
27 this->elapsed_secs
= 0;
30 void Player::on_second_tick(void*) {
31 if (!kernel
->pauser
->paused()) this->elapsed_secs
++;
34 // When a new line is received, check if it is a command, and if it is, act upon it
35 void Player::on_console_line_received( void* argument
){
36 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
37 string possible_command
= new_message
.message
;
39 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
41 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
42 unsigned short check_sum
= get_checksum( possible_command
.substr(0,possible_command
.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
44 // Act depending on command
45 if (check_sum
== play_command_checksum
)
46 this->play_command( get_arguments(possible_command
), new_message
.stream
);
47 else if (check_sum
== progress_command_checksum
)
48 this->progress_command(get_arguments(possible_command
),new_message
.stream
);
49 else if (check_sum
== abort_command_checksum
)
50 this->abort_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
);
55 // Play a gcode file by considering each line as if it was received on the serial console
56 void Player::play_command( string parameters
, StreamOutput
* stream
){
59 string filename
= this->absolute_from_relative(shift_parameter( parameters
));
60 stream
->printf("Playing %s\r\n", filename
.c_str());
61 string options
= shift_parameter( parameters
);
63 this->current_file_handler
= fopen( filename
.c_str(), "r");
64 if(this->current_file_handler
== NULL
){
65 stream
->printf("File not found: %s\r\n", filename
.c_str());
69 this->playing_file
= true;
71 // Do not output to any stream if we were passed the -q ( quiet ) option
72 if( options
.find_first_of("Qq") == string::npos
){
73 this->current_stream
= stream
;
75 this->current_stream
= &(StreamOutput::NullStream
);
79 int result
= fseek(this->current_file_handler
, 0, SEEK_END
);
81 stream
->printf("WARNING - Could not get file size\r\n");
84 file_size
= ftell(this->current_file_handler
);
85 fseek(this->current_file_handler
, 0, SEEK_SET
);
86 stream
->printf(" File size %ld\r\n", file_size
);
89 this->elapsed_secs
= 0;
92 void Player::progress_command( string parameters
, StreamOutput
* stream
){
94 stream
->printf("Not currently playing\r\n");
100 if(this->elapsed_secs
> 10) {
101 int bytespersec
= played_cnt
/ this->elapsed_secs
;
103 est
= (file_size
- played_cnt
) / bytespersec
;
106 int pcnt
= (file_size
- (file_size
- played_cnt
)) * 100 / file_size
;
107 stream
->printf("%d %% complete, elapsed time: %d s", pcnt
, this->elapsed_secs
);
109 stream
->printf(", est time: %d s", est
);
111 stream
->printf("\r\n");
114 stream
->printf("File size is unknown\r\n");
118 void Player::abort_command( string parameters
, StreamOutput
* stream
){
120 stream
->printf("Not currently playing\r\n");
123 playing_file
= false;
126 fclose(current_file_handler
);
127 stream
->printf("Aborted playing file\r\n");
130 // Convert a path indication ( absolute or relative ) into a path ( absolute )
131 string
Player::absolute_from_relative( string path
){
132 if( path
[0] == '/' ){ return path
; }
133 if( path
[0] == '.' ){ return this->current_path
; }
134 return this->current_path
+ path
;
137 // Change current absolute path to provided path
138 void Player::cd_command( string parameters
, StreamOutput
* stream
){
139 string folder
= this->absolute_from_relative( parameters
);
140 if( folder
[folder
.length()-1] != '/' ){ folder
+= "/"; }
142 d
= opendir(folder
.c_str());
144 // stream->printf("Could not open directory %s \r\n", folder.c_str() );
146 this->current_path
= folder
;
151 void Player::on_main_loop(void* argument
){
152 if( !this->booted
) {
153 if( this->on_boot_enable
){
154 this->play_command(this->on_boot_file_name
, this->kernel
->serial
);
156 //this->kernel->serial->printf("On boot gcode disabled! skipping...\n");
161 if( this->playing_file
){
165 // Print each line of the file
166 while ((c
= fgetc(this->current_file_handler
)) != EOF
){
168 this->current_stream
->printf("%s\n", buffer
.c_str());
169 struct SerialMessage message
;
170 message
.message
= buffer
;
171 message
.stream
= this->current_stream
;
172 // wait for the queue to have enough room that a serial message could still be received before sending
173 this->kernel
->conveyor
->wait_for_queue(2);
174 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
175 played_cnt
+= buffer
.size();
182 this->playing_file
= false;
185 fclose(this->current_file_handler
);
190 void Player::on_main_loop(void* argument){
191 if( !this->on_booted ){
192 this->play_command(this->on_boot_file_name, new StreamOutput());
193 this->on_booted = true;
195 if( this->playing_file ){
198 // Print each line of the file
199 while ((c = fgetc(this->current_file_handler)) != EOF){
201 this->current_stream->printf("%s\n", buffer.c_str());
202 struct SerialMessage message;
203 message.message = buffer;
204 message.stream = this->current_stream;
205 // wait for the queue to have enough room that a serial message could still be received before sending
206 this->kernel->conveyor->wait_for_queue(2);
207 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
215 fclose(this->current_file_handler);
216 this->playing_file = false;