Allow TABS in config
[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 "SerialConsole.h"
14 #include "libs/SerialMessage.h"
15 #include "libs/StreamOutputPool.h"
16 #include "libs/StreamOutput.h"
17 #include "Gcode.h"
18 #include "checksumm.h"
19 #include "Pauser.h"
20 #include "Config.h"
21
22 #include "modules/robot/Conveyor.h"
23 #include "DirHandle.h"
24 #include "PublicDataRequest.h"
25 #include "PlayerPublicAccess.h"
26
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")
32
33 void Player::on_module_loaded(){
34 this->playing_file = false;
35 this->booted = 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);
42
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;
47 }
48
49 void Player::on_second_tick(void*) {
50 if (!THEKERNEL->pauser->paused()) this->elapsed_secs++;
51 }
52
53 void Player::on_gcode_received(void *argument) {
54 Gcode *gcode = static_cast<Gcode*>(argument);
55 string args= get_arguments(gcode->command);
56 if (gcode->has_m) {
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");
60
61 }else if (gcode->m == 23) { // select file
62 gcode->mark_as_taken();
63 // Get filename
64 this->filename= "/sd/" + shift_parameter( args );
65 this->current_stream = &(StreamOutput::NullStream);
66
67 if(this->current_file_handler != NULL) {
68 this->playing_file = false;
69 fclose(this->current_file_handler);
70 }
71 this->current_file_handler = fopen( this->filename.c_str(), "r");
72
73 if(this->current_file_handler == NULL){
74 gcode->stream->printf("file.open failed: %s\r\n", this->filename.c_str());
75 return;
76
77 }else{
78 // get size of file
79 int result = fseek(this->current_file_handler, 0, SEEK_END);
80 if (0 != result){
81 this->file_size= 0;
82 }else{
83 this->file_size= ftell(this->current_file_handler);
84 fseek(this->current_file_handler, 0, SEEK_SET);
85 }
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");
88 }
89
90
91 this->played_cnt= 0;
92 this->elapsed_secs= 0;
93
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;
102 }
103
104 }else if (gcode->m == 25) { // pause print
105 gcode->mark_as_taken();
106 this->playing_file = false;
107
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;
113
114 // abort the print
115 abort_command("", gcode->stream);
116
117 if(!currentfn.empty()) {
118 // reload the last file opened
119 this->current_file_handler = fopen(currentfn.c_str() , "r");
120
121 if(this->current_file_handler == NULL){
122 gcode->stream->printf("file.open failed: %s\r\n", currentfn.c_str());
123 }else{
124 this->filename= currentfn;
125 this->file_size= old_size;
126 this->current_stream = &(StreamOutput::NullStream);
127 }
128 }
129
130 }else{
131 gcode->stream->printf("No file loaded\r\n");
132 }
133
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);
137
138 }else if (gcode->m == 32) { // select file and start print
139 gcode->mark_as_taken();
140 // Get filename
141 this->filename= "/sd/" + shift_parameter( args );
142 this->current_stream = &(StreamOutput::NullStream);
143
144 if(this->current_file_handler != NULL) {
145 this->playing_file = false;
146 fclose(this->current_file_handler);
147 }
148
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());
152 }else{
153 this->playing_file = true;
154 }
155 }
156 }
157 }
158
159
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);
163
164 // ignore comments
165 if(new_message.message[0] == ';') return;
166
167 string possible_command = new_message.message;
168
169 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
170
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
173
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 );
181 }
182
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 ){
185
186 // Get filename
187 this->filename = absolute_from_relative(shift_parameter( parameters ));
188 string options = shift_parameter( parameters );
189
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());
193 return;
194 }
195
196 stream->printf("Playing %s\r\n", this->filename.c_str());
197
198 this->playing_file = true;
199
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);
203 }else{
204 // we send to the kernels stream as it cannot go away
205 this->current_stream = THEKERNEL->streams;
206 }
207
208 // get size of file
209 int result = fseek(this->current_file_handler, 0, SEEK_END);
210 if (0 != result){
211 stream->printf("WARNING - Could not get file size\r\n");
212 file_size= 0;
213 }else{
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);
217 }
218 this->played_cnt= 0;
219 this->elapsed_secs= 0;
220 }
221
222 void Player::progress_command( string parameters, StreamOutput* stream ){
223
224 // get options
225 string options = shift_parameter( parameters );
226
227 if(!playing_file) {
228 stream->printf("Not currently playing\r\n");
229 return;
230 }
231
232 if(file_size > 0) {
233 unsigned long est= 0;
234 if(this->elapsed_secs > 10) {
235 unsigned long bytespersec= played_cnt / this->elapsed_secs;
236 if(bytespersec > 0)
237 est= (file_size - played_cnt) / bytespersec;
238 }
239
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);
244 if(est > 0){
245 stream->printf(", est time: %lu s", est);
246 }
247 stream->printf("\r\n");
248 }else{
249 stream->printf("SD printing byte %lu/%lu\r\n", played_cnt, file_size);
250 }
251
252 }else{
253 stream->printf("File size is unknown\r\n");
254 }
255 }
256
257 void Player::abort_command( string parameters, StreamOutput* stream ){
258 if(!playing_file) {
259 stream->printf("Not currently playing\r\n");
260 return;
261 }
262 playing_file = false;
263 played_cnt= 0;
264 file_size= 0;
265 this->filename= "";
266 this->current_stream= NULL;
267 fclose(current_file_handler);
268 stream->printf("Aborted playing file\r\n");
269 }
270
271 void Player::on_main_loop(void* argument){
272 if( !this->booted ) {
273 this->booted = true;
274 if( this->on_boot_gcode_enable ){
275 this->play_command(this->on_boot_gcode, THEKERNEL->serial);
276 }else{
277 //THEKERNEL->serial->printf("On boot gcode disabled! skipping...\n");
278 }
279 }
280
281 if( this->playing_file ){
282 string buffer;
283 bool discard= false;
284 int c;
285 buffer.reserve(20);
286 // Print each line of the file
287 while ((c = fgetc(this->current_file_handler)) != EOF){
288 if (c == '\n'){
289 if(discard) {
290 // we hit a long line and discarded it
291 discard= false;
292 buffer.clear();
293 this->current_stream->printf("Warning: Discarded long line\n");
294 return;
295 }
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();
303 buffer.clear();
304 return;
305
306 }else if(buffer.size() > 128) {
307 // discard rest of line
308 discard= true;
309
310 }else{
311 buffer += c;
312 }
313 }
314
315 this->playing_file = false;
316 this->filename= "";
317 played_cnt= 0;
318 file_size= 0;
319 fclose(this->current_file_handler);
320 this->current_stream= NULL;
321
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;
326 }
327 }
328 }
329
330 void Player::on_get_public_data(void* argument) {
331 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
332
333 if(!pdr->starts_with(player_checksum)) return;
334
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);
339 pdr->set_taken();
340
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);
348 pdr->set_taken();
349 }
350 }
351 }
352
353 void Player::on_set_public_data(void* argument) {
354 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
355
356 if(!pdr->starts_with(player_checksum)) return;
357
358 if(pdr->second_element_is(abort_play_checksum)) {
359 abort_command("", &(StreamOutput::NullStream));
360 pdr->set_taken();
361 }
362 }