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 "SerialConsole.h"
14 #include "libs/SerialMessage.h"
15 #include "libs/StreamOutputPool.h"
16 #include "libs/StreamOutput.h"
18 #include "checksumm.h"
22 #include "modules/robot/Conveyor.h"
23 #include "DirHandle.h"
24 #include "PublicDataRequest.h"
25 #include "PlayerPublicAccess.h"
27 #define play_command_checksum CHECKSUM("play")
28 #define progress_command_checksum CHECKSUM("progress")
29 #define abort_command_checksum CHECKSUM("abort")
30 #define on_boot_gcode_checksum CHECKSUM("on_boot_gcode")
31 #define on_boot_gcode_enable_checksum CHECKSUM("on_boot_gcode_enable")
33 void Player::on_module_loaded(){
34 this->playing_file
= false;
36 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
37 this->register_for_event(ON_MAIN_LOOP
);
38 this->register_for_event(ON_SECOND_TICK
);
39 this->register_for_event(ON_GET_PUBLIC_DATA
);
40 this->register_for_event(ON_SET_PUBLIC_DATA
);
41 this->register_for_event(ON_GCODE_RECEIVED
);
43 this->on_boot_gcode
= THEKERNEL
->config
->value(on_boot_gcode_checksum
)->by_default("/sd/on_boot.gcode")->as_string();
44 this->on_boot_gcode_enable
= THEKERNEL
->config
->value(on_boot_gcode_enable_checksum
)->by_default(true)->as_bool();
45 this->elapsed_secs
= 0;
46 this->reply_stream
= NULL
;
49 void Player::on_second_tick(void*) {
50 if (!THEKERNEL
->pauser
->paused()) this->elapsed_secs
++;
53 void Player::on_gcode_received(void *argument
) {
54 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
55 string args
= get_arguments(gcode
->command
);
57 if (gcode
->m
== 21) { // Dummy code; makes Octoprint happy -- supposed to initialize SD card
58 gcode
->mark_as_taken();
59 gcode
->stream
->printf("SD card ok\r\n");
61 }else if (gcode
->m
== 23) { // select file
62 gcode
->mark_as_taken();
64 this->filename
= "/sd/" + shift_parameter( args
);
65 this->current_stream
= &(StreamOutput::NullStream
);
67 if(this->current_file_handler
!= NULL
) {
68 this->playing_file
= false;
69 fclose(this->current_file_handler
);
71 this->current_file_handler
= fopen( this->filename
.c_str(), "r");
73 if(this->current_file_handler
== NULL
){
74 gcode
->stream
->printf("file.open failed: %s\r\n", this->filename
.c_str());
79 int result
= fseek(this->current_file_handler
, 0, SEEK_END
);
83 this->file_size
= ftell(this->current_file_handler
);
84 fseek(this->current_file_handler
, 0, SEEK_SET
);
86 gcode
->stream
->printf("File opened:%s Size:%ld\r\n", this->filename
.c_str(), this->file_size
);
87 gcode
->stream
->printf("File selected\r\n");
92 this->elapsed_secs
= 0;
94 }else if (gcode
->m
== 24) { // start print
95 gcode
->mark_as_taken();
96 if (this->current_file_handler
!= NULL
) {
97 this->playing_file
= true;
98 // this would be a problem if the stream goes away before the file has finished,
99 // so we attach it to the kernel stream, however network connections from pronterface
100 // do not connect to the kernel streams so won't see this FIXME
101 this->reply_stream
= THEKERNEL
->streams
;
104 }else if (gcode
->m
== 25) { // pause print
105 gcode
->mark_as_taken();
106 this->playing_file
= false;
108 }else if (gcode
->m
== 26) { // Reset print. Slightly different than M26 in Marlin and the rest
109 gcode
->mark_as_taken();
110 if(this->current_file_handler
!= NULL
){
111 string currentfn
= this->filename
.c_str();
112 unsigned long old_size
= this->file_size
;
115 abort_command("", gcode
->stream
);
117 if(!currentfn
.empty()) {
118 // reload the last file opened
119 this->current_file_handler
= fopen(currentfn
.c_str() , "r");
121 if(this->current_file_handler
== NULL
){
122 gcode
->stream
->printf("file.open failed: %s\r\n", currentfn
.c_str());
124 this->filename
= currentfn
;
125 this->file_size
= old_size
;
126 this->current_stream
= &(StreamOutput::NullStream
);
131 gcode
->stream
->printf("No file loaded\r\n");
134 }else if (gcode
->m
== 27) { // report print progress, in format used by Marlin
135 gcode
->mark_as_taken();
136 progress_command("-b", gcode
->stream
);
138 }else if (gcode
->m
== 32) { // select file and start print
139 gcode
->mark_as_taken();
141 this->filename
= "/sd/" + shift_parameter( args
);
142 this->current_stream
= &(StreamOutput::NullStream
);
144 if(this->current_file_handler
!= NULL
) {
145 this->playing_file
= false;
146 fclose(this->current_file_handler
);
149 this->current_file_handler
= fopen( this->filename
.c_str(), "r");
150 if(this->current_file_handler
== NULL
){
151 gcode
->stream
->printf("file.open failed: %s\r\n", this->filename
.c_str());
153 this->playing_file
= true;
160 // When a new line is received, check if it is a command, and if it is, act upon it
161 void Player::on_console_line_received( void* argument
){
162 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
165 if(new_message
.message
[0] == ';') return;
167 string possible_command
= new_message
.message
;
169 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
171 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
172 unsigned short check_sum
= get_checksum( possible_command
.substr(0,possible_command
.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
174 // Act depending on command
175 if (check_sum
== play_command_checksum
)
176 this->play_command( get_arguments(possible_command
), new_message
.stream
);
177 else if (check_sum
== progress_command_checksum
)
178 this->progress_command(get_arguments(possible_command
),new_message
.stream
);
179 else if (check_sum
== abort_command_checksum
)
180 this->abort_command(get_arguments(possible_command
),new_message
.stream
);
183 // Play a gcode file by considering each line as if it was received on the serial console
184 void Player::play_command( string parameters
, StreamOutput
* stream
){
187 this->filename
= absolute_from_relative(shift_parameter( parameters
));
188 string options
= shift_parameter( parameters
);
190 this->current_file_handler
= fopen( this->filename
.c_str(), "r");
191 if(this->current_file_handler
== NULL
){
192 stream
->printf("File not found: %s\r\n", this->filename
.c_str());
196 stream
->printf("Playing %s\r\n", this->filename
.c_str());
198 this->playing_file
= true;
200 // Output to the current stream if we were passed the -v ( verbose ) option
201 if( options
.find_first_of("Vv") == string::npos
){
202 this->current_stream
= &(StreamOutput::NullStream
);
204 // we send to the kernels stream as it cannot go away
205 this->current_stream
= THEKERNEL
->streams
;
209 int result
= fseek(this->current_file_handler
, 0, SEEK_END
);
211 stream
->printf("WARNING - Could not get file size\r\n");
214 file_size
= ftell(this->current_file_handler
);
215 fseek(this->current_file_handler
, 0, SEEK_SET
);
216 stream
->printf(" File size %ld\r\n", file_size
);
219 this->elapsed_secs
= 0;
222 void Player::progress_command( string parameters
, StreamOutput
* stream
){
225 string options
= shift_parameter( parameters
);
228 stream
->printf("Not currently playing\r\n");
233 unsigned long est
= 0;
234 if(this->elapsed_secs
> 10) {
235 unsigned long bytespersec
= played_cnt
/ this->elapsed_secs
;
237 est
= (file_size
- played_cnt
) / bytespersec
;
240 unsigned int pcnt
= (file_size
- (file_size
- played_cnt
)) * 100 / file_size
;
241 // If -b or -B is passed, report in the format used by Marlin and the others.
242 if ( options
.find_first_of("Bb") == string::npos
){
243 stream
->printf("%u %% complete, elapsed time: %lu s", pcnt
, this->elapsed_secs
);
245 stream
->printf(", est time: %lu s", est
);
247 stream
->printf("\r\n");
249 stream
->printf("SD printing byte %lu/%lu\r\n", played_cnt
, file_size
);
253 stream
->printf("File size is unknown\r\n");
257 void Player::abort_command( string parameters
, StreamOutput
* stream
){
259 stream
->printf("Not currently playing\r\n");
262 playing_file
= false;
266 this->current_stream
= NULL
;
267 fclose(current_file_handler
);
268 stream
->printf("Aborted playing file\r\n");
271 void Player::on_main_loop(void* argument
){
272 if( !this->booted
) {
274 if( this->on_boot_gcode_enable
){
275 this->play_command(this->on_boot_gcode
, THEKERNEL
->serial
);
277 //THEKERNEL->serial->printf("On boot gcode disabled! skipping...\n");
281 if( this->playing_file
){
286 // Print each line of the file
287 while ((c
= fgetc(this->current_file_handler
)) != EOF
){
290 // we hit a long line and discarded it
293 this->current_stream
->printf("Warning: Discarded long line\n");
296 this->current_stream
->printf("%s\n", buffer
.c_str());
297 struct SerialMessage message
;
298 message
.message
= buffer
;
299 message
.stream
= &(StreamOutput::NullStream
); // we don't really need to see the ok
300 // wait for the queue to have enough room that a serial message could still be received before sending
301 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
302 played_cnt
+= buffer
.size();
306 }else if(buffer
.size() > 128) {
307 // discard rest of line
315 this->playing_file
= false;
319 fclose(this->current_file_handler
);
320 this->current_stream
= NULL
;
322 if(this->reply_stream
!= NULL
) {
323 // if we were printing from an M command from pronterface we need to send this back
324 this->reply_stream
->printf("Done printing file\r\n");
325 this->reply_stream
= NULL
;
330 void Player::on_get_public_data(void* argument
) {
331 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
333 if(!pdr
->starts_with(player_checksum
)) return;
335 if(pdr
->second_element_is(is_playing_checksum
)) {
336 static bool bool_data
;
337 bool_data
= this->playing_file
;
338 pdr
->set_data_ptr(&bool_data
);
341 }else if(pdr
->second_element_is(get_progress_checksum
)) {
342 static struct pad_progress p
;
343 if(file_size
> 0 && playing_file
) {
344 p
.elapsed_secs
= this->elapsed_secs
;
345 p
.percent_complete
= (this->file_size
- (this->file_size
- this->played_cnt
)) * 100 / this->file_size
;
346 p
.filename
= this->filename
;
347 pdr
->set_data_ptr(&p
);
353 void Player::on_set_public_data(void* argument
) {
354 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
356 if(!pdr
->starts_with(player_checksum
)) return;
358 if(pdr
->second_element_is(abort_play_checksum
)) {
359 abort_command("", &(StreamOutput::NullStream
));