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