Merge pull request #228 from TopherMan/feature/M27
[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 -q")->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 == 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");
55 if(this->current_file_handler == NULL){
56 gcode->stream->printf("file.open failed: %s\r\n", filename.c_str());
57 }else{
58 gcode->stream->printf("File selected: %s\r\n", filename.c_str());
59 }
60
61 }else if (gcode->m == 24) { // start print
62 gcode->mark_as_taken();
63 if (this->current_file_handler != NULL) {
64 this->playing_file = true;
65 this->reply_stream= gcode->stream;
66 }
67
68 }else if (gcode->m == 25) { // pause print
69 gcode->mark_as_taken();
70 this->playing_file = false;
71
72 }else if (gcode->m == 32) { // select file and start print
73 gcode->mark_as_taken();
74 // Get filename
75 string filename= "/sd/" + this->absolute_from_relative(shift_parameter( args ));
76 this->current_stream = &(StreamOutput::NullStream);
77
78 if(this->current_file_handler != NULL) {
79 this->playing_file = false;
80 fclose(this->current_file_handler);
81 }
82
83 this->current_file_handler = fopen( filename.c_str(), "r");
84 if(this->current_file_handler == NULL){
85 gcode->stream->printf("file.open failed: %s\r\n", filename.c_str());
86 }else{
87 this->playing_file = true;
88 }
89 }else if (gcode->m == 27) { // report print progress
90 gcode->mark_as_taken();
91 progress_command("", gcode->stream);
92 }
93 }
94 }
95
96
97 // When a new line is received, check if it is a command, and if it is, act upon it
98 void Player::on_console_line_received( void* argument ){
99 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
100 string possible_command = new_message.message;
101
102 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
103
104 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
105 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
106
107 // Act depending on command
108 if (check_sum == play_command_checksum)
109 this->play_command( get_arguments(possible_command), new_message.stream );
110 else if (check_sum == progress_command_checksum)
111 this->progress_command(get_arguments(possible_command),new_message.stream );
112 else if (check_sum == abort_command_checksum)
113 this->abort_command(get_arguments(possible_command),new_message.stream );
114 else if (check_sum == cd_command_checksum)
115 this->cd_command( get_arguments(possible_command), new_message.stream );
116 }
117
118 // Play a gcode file by considering each line as if it was received on the serial console
119 void Player::play_command( string parameters, StreamOutput* stream ){
120
121 // Get filename
122 string filename = this->absolute_from_relative(shift_parameter( parameters ));
123 string options = shift_parameter( parameters );
124
125 this->current_file_handler = fopen( filename.c_str(), "r");
126 if(this->current_file_handler == NULL){
127 stream->printf("File not found: %s\r\n", filename.c_str());
128 return;
129 }
130
131 stream->printf("Playing %s\r\n", filename.c_str());
132
133 this->playing_file = true;
134
135 // Do not output to any stream if we were passed the -q ( quiet ) option
136 if( options.find_first_of("Qq") == string::npos ){
137 this->current_stream = stream;
138 }else{
139 this->current_stream = &(StreamOutput::NullStream);
140 }
141
142 // get size of file
143 int result = fseek(this->current_file_handler, 0, SEEK_END);
144 if (0 != result){
145 stream->printf("WARNING - Could not get file size\r\n");
146 file_size= -1;
147 }else{
148 file_size= ftell(this->current_file_handler);
149 fseek(this->current_file_handler, 0, SEEK_SET);
150 stream->printf(" File size %ld\r\n", file_size);
151 }
152 this->played_cnt= 0;
153 this->elapsed_secs= 0;
154 }
155
156 void Player::progress_command( string parameters, StreamOutput* stream ){
157 if(!playing_file) {
158 stream->printf("Not currently playing\r\n");
159 return;
160 }
161
162 if(file_size > 0) {
163 int est= -1;
164 if(this->elapsed_secs > 10) {
165 int bytespersec= played_cnt / this->elapsed_secs;
166 if(bytespersec > 0)
167 est= (file_size - played_cnt) / bytespersec;
168 }
169
170 int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
171 stream->printf("%d %% complete, elapsed time: %d s", pcnt, this->elapsed_secs);
172 if(est > 0){
173 stream->printf(", est time: %d s", est);
174 }
175 stream->printf("\r\n");
176
177 }else{
178 stream->printf("File size is unknown\r\n");
179 }
180 }
181
182 void Player::abort_command( string parameters, StreamOutput* stream ){
183 if(!playing_file) {
184 stream->printf("Not currently playing\r\n");
185 return;
186 }
187 playing_file = false;
188 played_cnt= 0;
189 file_size= 0;
190 fclose(current_file_handler);
191 stream->printf("Aborted playing file\r\n");
192 }
193
194 // Convert a path indication ( absolute or relative ) into a path ( absolute )
195 string Player::absolute_from_relative( string path ){
196 if( path[0] == '/' ){ return path; }
197 if( path[0] == '.' ){ return this->current_path; }
198 return this->current_path + path;
199 }
200
201 // Change current absolute path to provided path
202 void Player::cd_command( string parameters, StreamOutput* stream ){
203 string folder = this->absolute_from_relative( parameters );
204 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
205 DIR *d;
206 d = opendir(folder.c_str());
207 if(d == NULL) {
208 // stream->printf("Could not open directory %s \r\n", folder.c_str() );
209 }else{
210 this->current_path = folder;
211 closedir(d);
212 }
213 }
214
215 void Player::on_main_loop(void* argument){
216 if( !this->booted ) {
217 if( this->on_boot_gcode_enable ){
218 this->play_command(this->on_boot_gcode, this->kernel->serial);
219 }else{
220 //this->kernel->serial->printf("On boot gcode disabled! skipping...\n");
221 }
222 this->booted = true;
223 }
224
225 if( this->playing_file ){
226 string buffer;
227 int c;
228 buffer.reserve(20);
229 // Print each line of the file
230 while ((c = fgetc(this->current_file_handler)) != EOF){
231 if (c == '\n'){
232 this->current_stream->printf("%s\n", buffer.c_str());
233 struct SerialMessage message;
234 message.message = buffer;
235 message.stream = this->current_stream;
236 // wait for the queue to have enough room that a serial message could still be received before sending
237 this->kernel->conveyor->wait_for_queue(2);
238 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
239 played_cnt += buffer.size();
240 buffer.clear();
241 return;
242 }else{
243 buffer += c;
244 }
245 }
246
247 this->playing_file = false;
248 played_cnt= 0;
249 file_size= 0;
250 fclose(this->current_file_handler);
251
252 if(this->reply_stream != NULL) {
253 // if we were printing from an M command from pronterface we need to send this back
254 this->reply_stream->printf("Done printing file\r\n");
255 this->reply_stream= NULL;
256 }
257 }
258 }
259
260 void Player::on_get_public_data(void* argument) {
261 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
262
263 if(!pdr->starts_with(player_checksum)) return;
264
265 if(pdr->second_element_is(is_playing_checksum)) {
266 static bool bool_data;
267 bool_data= this->playing_file;
268 pdr->set_data_ptr(&bool_data);
269 pdr->set_taken();
270
271 }else if(pdr->second_element_is(get_progress_checksum)) {
272 static struct pad_progress p;
273 if(file_size > 0 && playing_file) {
274 p.elapsed_secs= this->elapsed_secs;
275 p.percent_complete= (this->file_size - (this->file_size - this->played_cnt)) * 100 / this->file_size;
276 pdr->set_data_ptr(&p);
277 pdr->set_taken();
278 }
279 }
280 }
281
282 void Player::on_set_public_data(void* argument) {
283 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
284
285 if(!pdr->starts_with(player_checksum)) return;
286
287 if(pdr->second_element_is(abort_play_checksum)) {
288 if(playing_file) {
289 playing_file = false;
290 played_cnt= 0;
291 file_size= 0;
292 fclose(current_file_handler);
293 pdr->set_taken();
294 }
295 }
296 }
297
298
299
300
301
302 /*
303 void Player::on_main_loop(void* argument){
304 if( !this->on_booted ){
305 this->play_command(this->on_boot_file_name, new StreamOutput());
306 this->on_booted = true;
307 }
308 if( this->playing_file ){
309 string buffer;
310 int c;
311 // Print each line of the file
312 while ((c = fgetc(this->current_file_handler)) != EOF){
313 if (c == '\n'){
314 this->current_stream->printf("%s\n", buffer.c_str());
315 struct SerialMessage message;
316 message.message = buffer;
317 message.stream = this->current_stream;
318 // wait for the queue to have enough room that a serial message could still be received before sending
319 this->kernel->conveyor->wait_for_queue(2);
320 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
321 buffer.clear();
322 return;
323 }else{
324 buffer += c;
325 }
326 };
327
328 fclose(this->current_file_handler);
329 this->playing_file = false;
330 }
331 }
332 */
333