Merge pull request #231 from TopherMan/fix/M23
[clinton/Smoothieware.git] / src / modules / utils / player / Player.cpp
CommitLineData
663d7943
L
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
9#include "libs/Kernel.h"
10#include "Player.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"
172d42d9 16#include "DirHandle.h"
35089dc7
JM
17#include "PublicDataRequest.h"
18#include "PlayerPublicAccess.h"
663d7943 19
663d7943
L
20void Player::on_module_loaded(){
21 this->playing_file = false;
addfb453 22 this->booted = false;
663d7943
L
23 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
24 this->register_for_event(ON_MAIN_LOOP);
63888663 25 this->register_for_event(ON_SECOND_TICK);
58d6d841
JM
26 this->register_for_event(ON_GET_PUBLIC_DATA);
27 this->register_for_event(ON_SET_PUBLIC_DATA);
c4e56997 28 this->register_for_event(ON_GCODE_RECEIVED);
8fef244a 29
7a42f8a2
MM
30 this->on_boot_gcode = this->kernel->config->value(on_boot_gcode_checksum)->by_default("/sd/on_boot.gcode -q")->as_string();
31 this->on_boot_gcode_enable = this->kernel->config->value(on_boot_gcode_enable_checksum)->by_default(true)->as_bool();
63888663 32 this->elapsed_secs= 0;
c4e56997 33 this->reply_stream= NULL;
63888663
JM
34}
35
36void Player::on_second_tick(void*) {
0a3f2832 37 if (!kernel->pauser->paused()) this->elapsed_secs++;
663d7943
L
38}
39
c4e56997
JM
40void Player::on_gcode_received(void *argument) {
41 Gcode *gcode = static_cast<Gcode*>(argument);
42 string args= get_arguments(gcode->command);
43 if (gcode->has_m) {
44 if (gcode->m == 23) { // select file
45 gcode->mark_as_taken();
46 // Get filename
47 string filename= "/sd/" + this->absolute_from_relative(shift_parameter( args ));
48 this->current_stream = &(StreamOutput::NullStream);
49
50 if(this->current_file_handler != NULL) {
51 this->playing_file = false;
52 fclose(this->current_file_handler);
53 }
54 this->current_file_handler = fopen( filename.c_str(), "r");
fbd92b6a
CG
55 // get size of file
56 int result = fseek(this->current_file_handler, 0, SEEK_END);
57 if (0 != result){
58 gcode->stream->printf("WARNING - Could not get file size\r\n");
59 file_size= -1;
60 }else{
61 file_size= ftell(this->current_file_handler);
62 fseek(this->current_file_handler, 0, SEEK_SET);
63 }
64
c4e56997
JM
65 if(this->current_file_handler == NULL){
66 gcode->stream->printf("file.open failed: %s\r\n", filename.c_str());
67 }else{
fbd92b6a
CG
68 gcode->stream->printf("File opened:%s Size:%ld\r\n", filename.c_str(),file_size);
69 gcode->stream->printf("File selected\r\n");
c4e56997
JM
70 }
71
fbd92b6a
CG
72 this->played_cnt= 0;
73 this->elapsed_secs= 0;
74
c4e56997
JM
75 }else if (gcode->m == 24) { // start print
76 gcode->mark_as_taken();
77 if (this->current_file_handler != NULL) {
78 this->playing_file = true;
79 this->reply_stream= gcode->stream;
80 }
81
82 }else if (gcode->m == 25) { // pause print
83 gcode->mark_as_taken();
84 this->playing_file = false;
85
86 }else if (gcode->m == 32) { // select file and start print
87 gcode->mark_as_taken();
88 // Get filename
89 string filename= "/sd/" + this->absolute_from_relative(shift_parameter( args ));
90 this->current_stream = &(StreamOutput::NullStream);
91
92 if(this->current_file_handler != NULL) {
93 this->playing_file = false;
94 fclose(this->current_file_handler);
95 }
96
97 this->current_file_handler = fopen( filename.c_str(), "r");
98 if(this->current_file_handler == NULL){
99 gcode->stream->printf("file.open failed: %s\r\n", filename.c_str());
100 }else{
101 this->playing_file = true;
102 }
43d26cb1 103 }else if (gcode->m == 27) { // report print progress, in format used by Marlin
12ce77b7 104 gcode->mark_as_taken();
43d26cb1 105 progress_command("-b", gcode->stream);
3661ec47
CG
106 }else if (gcode->m == 21) { // Dummy code; makes Octoprint happy
107 gcode->mark_as_taken();
108 gcode->stream->printf("SD card ok\r\n");
c4e56997
JM
109 }
110 }
111}
112
113
663d7943
L
114// When a new line is received, check if it is a command, and if it is, act upon it
115void Player::on_console_line_received( void* argument ){
116 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
117 string possible_command = new_message.message;
118
119 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
120
121 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
122 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
123
124 // Act depending on command
addfb453
L
125 if (check_sum == play_command_checksum)
126 this->play_command( get_arguments(possible_command), new_message.stream );
b41aedba
JM
127 else if (check_sum == progress_command_checksum)
128 this->progress_command(get_arguments(possible_command),new_message.stream );
129 else if (check_sum == abort_command_checksum)
130 this->abort_command(get_arguments(possible_command),new_message.stream );
addfb453
L
131 else if (check_sum == cd_command_checksum)
132 this->cd_command( get_arguments(possible_command), new_message.stream );
663d7943
L
133}
134
135// Play a gcode file by considering each line as if it was received on the serial console
136void Player::play_command( string parameters, StreamOutput* stream ){
137
138 // Get filename
addfb453 139 string filename = this->absolute_from_relative(shift_parameter( parameters ));
663d7943
L
140 string options = shift_parameter( parameters );
141
142 this->current_file_handler = fopen( filename.c_str(), "r");
fdbea18b 143 if(this->current_file_handler == NULL){
663d7943
L
144 stream->printf("File not found: %s\r\n", filename.c_str());
145 return;
146 }
fdbea18b 147
8fef244a
MM
148 stream->printf("Playing %s\r\n", filename.c_str());
149
663d7943 150 this->playing_file = true;
fdbea18b 151
d0ef6382 152 // Do not output to any stream if we were passed the -q ( quiet ) option
663d7943
L
153 if( options.find_first_of("Qq") == string::npos ){
154 this->current_stream = stream;
155 }else{
d0ef6382 156 this->current_stream = &(StreamOutput::NullStream);
fdbea18b
AW
157 }
158
159 // get size of file
160 int result = fseek(this->current_file_handler, 0, SEEK_END);
161 if (0 != result){
162 stream->printf("WARNING - Could not get file size\r\n");
163 file_size= -1;
164 }else{
165 file_size= ftell(this->current_file_handler);
166 fseek(this->current_file_handler, 0, SEEK_SET);
167 stream->printf(" File size %ld\r\n", file_size);
168 }
b41aedba 169 this->played_cnt= 0;
63888663 170 this->elapsed_secs= 0;
addfb453
L
171}
172
173void Player::progress_command( string parameters, StreamOutput* stream ){
43d26cb1
CG
174
175 // get options
176 string options = shift_parameter( parameters );
177
b41aedba
JM
178 if(!playing_file) {
179 stream->printf("Not currently playing\r\n");
180 return;
181 }
182
183 if(file_size > 0) {
b41aedba 184 int est= -1;
0a3f2832 185 if(this->elapsed_secs > 10) {
63888663 186 int bytespersec= played_cnt / this->elapsed_secs;
b41aedba
JM
187 if(bytespersec > 0)
188 est= (file_size - played_cnt) / bytespersec;
189 }
8fef244a 190
b41aedba 191 int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
43d26cb1
CG
192 // If -b or -B is passed, report in the format used by Marlin and the others.
193 if ( options.find_first_of("Bb") == string::npos ){
194 stream->printf("%d %% complete, elapsed time: %d s", pcnt, this->elapsed_secs);
195 if(est > 0){
196 stream->printf(", est time: %d s", est);
197 }
198 stream->printf("\r\n");
199 }else{
6d0e7c2e 200 stream->printf("SD printing byte %ld/%ld\r\n", played_cnt, file_size);
0a3f2832 201 }
8fef244a 202
b41aedba
JM
203 }else{
204 stream->printf("File size is unknown\r\n");
205 }
addfb453
L
206}
207
208void Player::abort_command( string parameters, StreamOutput* stream ){
b41aedba
JM
209 if(!playing_file) {
210 stream->printf("Not currently playing\r\n");
211 return;
212 }
213 playing_file = false;
214 played_cnt= 0;
215 file_size= 0;
216 fclose(current_file_handler);
217 stream->printf("Aborted playing file\r\n");
addfb453
L
218}
219
220// Convert a path indication ( absolute or relative ) into a path ( absolute )
221string Player::absolute_from_relative( string path ){
222 if( path[0] == '/' ){ return path; }
223 if( path[0] == '.' ){ return this->current_path; }
224 return this->current_path + path;
225}
226
227// Change current absolute path to provided path
228void Player::cd_command( string parameters, StreamOutput* stream ){
229 string folder = this->absolute_from_relative( parameters );
230 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
231 DIR *d;
232 d = opendir(folder.c_str());
233 if(d == NULL) {
234// stream->printf("Could not open directory %s \r\n", folder.c_str() );
235 }else{
b41aedba
JM
236 this->current_path = folder;
237 closedir(d);
addfb453
L
238 }
239}
240
241void Player::on_main_loop(void* argument){
38776a0b 242 if( !this->booted ) {
7a42f8a2
MM
243 if( this->on_boot_gcode_enable ){
244 this->play_command(this->on_boot_gcode, this->kernel->serial);
38776a0b 245 }else{
0472aec4 246 //this->kernel->serial->printf("On boot gcode disabled! skipping...\n");
38776a0b 247 }
addfb453
L
248 this->booted = true;
249 }
250
251 if( this->playing_file ){
252 string buffer;
253 int c;
254 buffer.reserve(20);
255 // Print each line of the file
256 while ((c = fgetc(this->current_file_handler)) != EOF){
257 if (c == '\n'){
258 this->current_stream->printf("%s\n", buffer.c_str());
259 struct SerialMessage message;
260 message.message = buffer;
261 message.stream = this->current_stream;
262 // wait for the queue to have enough room that a serial message could still be received before sending
263 this->kernel->conveyor->wait_for_queue(2);
b41aedba
JM
264 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
265 played_cnt += buffer.size();
addfb453
L
266 buffer.clear();
267 return;
268 }else{
269 buffer += c;
270 }
c4e56997
JM
271 }
272
b41aedba
JM
273 this->playing_file = false;
274 played_cnt= 0;
275 file_size= 0;
addfb453 276 fclose(this->current_file_handler);
c4e56997
JM
277
278 if(this->reply_stream != NULL) {
279 // if we were printing from an M command from pronterface we need to send this back
280 this->reply_stream->printf("Done printing file\r\n");
281 this->reply_stream= NULL;
282 }
663d7943
L
283 }
284}
285
35089dc7 286void Player::on_get_public_data(void* argument) {
58d6d841
JM
287 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
288
289 if(!pdr->starts_with(player_checksum)) return;
290
291 if(pdr->second_element_is(is_playing_checksum)) {
292 static bool bool_data;
293 bool_data= this->playing_file;
294 pdr->set_data_ptr(&bool_data);
295 pdr->set_taken();
c4e56997 296
58d6d841
JM
297 }else if(pdr->second_element_is(get_progress_checksum)) {
298 static struct pad_progress p;
299 if(file_size > 0 && playing_file) {
300 p.elapsed_secs= this->elapsed_secs;
301 p.percent_complete= (this->file_size - (this->file_size - this->played_cnt)) * 100 / this->file_size;
302 pdr->set_data_ptr(&p);
303 pdr->set_taken();
304 }
305 }
35089dc7
JM
306}
307
308void Player::on_set_public_data(void* argument) {
58d6d841
JM
309 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
310
311 if(!pdr->starts_with(player_checksum)) return;
312
313 if(pdr->second_element_is(abort_play_checksum)) {
314 if(playing_file) {
315 playing_file = false;
316 played_cnt= 0;
317 file_size= 0;
318 fclose(current_file_handler);
319 pdr->set_taken();
320 }
321 }
35089dc7
JM
322}
323
324
325
326
327
addfb453 328/*
663d7943
L
329void Player::on_main_loop(void* argument){
330 if( !this->on_booted ){
331 this->play_command(this->on_boot_file_name, new StreamOutput());
332 this->on_booted = true;
333 }
334 if( this->playing_file ){
335 string buffer;
336 int c;
337 // Print each line of the file
338 while ((c = fgetc(this->current_file_handler)) != EOF){
339 if (c == '\n'){
340 this->current_stream->printf("%s\n", buffer.c_str());
341 struct SerialMessage message;
342 message.message = buffer;
343 message.stream = this->current_stream;
344 // wait for the queue to have enough room that a serial message could still be received before sending
345 this->kernel->conveyor->wait_for_queue(2);
346 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
347 buffer.clear();
348 return;
349 }else{
350 buffer += c;
351 }
352 };
353
354 fclose(this->current_file_handler);
355 this->playing_file = false;
356 }
357}
addfb453
L
358*/
359