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