6c22f60afced226bd5b593979ca90035376abe8c
[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
18 void Player::on_module_loaded(){
19 this->playing_file = false;
20 this->booted = false;
21 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
22 this->register_for_event(ON_MAIN_LOOP);
23 this->register_for_event(ON_SECOND_TICK);
24
25 this->on_boot_file_name = this->kernel->config->value(on_boot_gcode_checksum)->by_default("/sd/on_boot.gcode -q")->as_string();
26 this->on_boot_enable = this->kernel->config->value(on_boot_gcode_enable_checksum)->by_default(true)->as_bool();
27 this->elapsed_secs= 0;
28 }
29
30 void Player::on_second_tick(void*) {
31 if (!kernel->pauser->paused()) this->elapsed_secs++;
32 }
33
34 // When a new line is received, check if it is a command, and if it is, act upon it
35 void Player::on_console_line_received( void* argument ){
36 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
37 string possible_command = new_message.message;
38
39 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
40
41 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
42 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
43
44 // Act depending on command
45 if (check_sum == play_command_checksum)
46 this->play_command( get_arguments(possible_command), new_message.stream );
47 else if (check_sum == progress_command_checksum)
48 this->progress_command(get_arguments(possible_command),new_message.stream );
49 else if (check_sum == abort_command_checksum)
50 this->abort_command(get_arguments(possible_command),new_message.stream );
51 else if (check_sum == cd_command_checksum)
52 this->cd_command( get_arguments(possible_command), new_message.stream );
53 }
54
55 // Play a gcode file by considering each line as if it was received on the serial console
56 void Player::play_command( string parameters, StreamOutput* stream ){
57
58 // Get filename
59 string filename = this->absolute_from_relative(shift_parameter( parameters ));
60 stream->printf("Playing %s\r\n", filename.c_str());
61 string options = shift_parameter( parameters );
62
63 this->current_file_handler = fopen( filename.c_str(), "r");
64 if(this->current_file_handler == NULL){
65 stream->printf("File not found: %s\r\n", filename.c_str());
66 return;
67 }
68
69 this->playing_file = true;
70
71 // Do not output to any stream if we were passed the -q ( quiet ) option
72 if( options.find_first_of("Qq") == string::npos ){
73 this->current_stream = stream;
74 }else{
75 this->current_stream = &(StreamOutput::NullStream);
76 }
77
78 // get size of file
79 int result = fseek(this->current_file_handler, 0, SEEK_END);
80 if (0 != result){
81 stream->printf("WARNING - Could not get file size\r\n");
82 file_size= -1;
83 }else{
84 file_size= ftell(this->current_file_handler);
85 fseek(this->current_file_handler, 0, SEEK_SET);
86 stream->printf(" File size %ld\r\n", file_size);
87 }
88 this->played_cnt= 0;
89 this->elapsed_secs= 0;
90 }
91
92 void Player::progress_command( string parameters, StreamOutput* stream ){
93 if(!playing_file) {
94 stream->printf("Not currently playing\r\n");
95 return;
96 }
97
98 if(file_size > 0) {
99 int est= -1;
100 if(this->elapsed_secs > 10) {
101 int bytespersec= played_cnt / this->elapsed_secs;
102 if(bytespersec > 0)
103 est= (file_size - played_cnt) / bytespersec;
104 }
105
106 int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
107 stream->printf("%d %% complete, elapsed time: %d s", pcnt, this->elapsed_secs);
108 if(est > 0){
109 stream->printf(", est time: %d s", est);
110 }
111 stream->printf("\r\n");
112
113 }else{
114 stream->printf("File size is unknown\r\n");
115 }
116 }
117
118 void Player::abort_command( string parameters, StreamOutput* stream ){
119 if(!playing_file) {
120 stream->printf("Not currently playing\r\n");
121 return;
122 }
123 playing_file = false;
124 played_cnt= 0;
125 file_size= 0;
126 fclose(current_file_handler);
127 stream->printf("Aborted playing file\r\n");
128 }
129
130 // Convert a path indication ( absolute or relative ) into a path ( absolute )
131 string Player::absolute_from_relative( string path ){
132 if( path[0] == '/' ){ return path; }
133 if( path[0] == '.' ){ return this->current_path; }
134 return this->current_path + path;
135 }
136
137 // Change current absolute path to provided path
138 void Player::cd_command( string parameters, StreamOutput* stream ){
139 string folder = this->absolute_from_relative( parameters );
140 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
141 DIR *d;
142 d = opendir(folder.c_str());
143 if(d == NULL) {
144 // stream->printf("Could not open directory %s \r\n", folder.c_str() );
145 }else{
146 this->current_path = folder;
147 closedir(d);
148 }
149 }
150
151 void Player::on_main_loop(void* argument){
152 if( !this->booted ) {
153 if( this->on_boot_enable ){
154 this->play_command(this->on_boot_file_name, this->kernel->serial);
155 }else{
156 //this->kernel->serial->printf("On boot gcode disabled! skipping...\n");
157 }
158 this->booted = true;
159 }
160
161 if( this->playing_file ){
162 string buffer;
163 int c;
164 buffer.reserve(20);
165 // Print each line of the file
166 while ((c = fgetc(this->current_file_handler)) != EOF){
167 if (c == '\n'){
168 this->current_stream->printf("%s\n", buffer.c_str());
169 struct SerialMessage message;
170 message.message = buffer;
171 message.stream = this->current_stream;
172 // wait for the queue to have enough room that a serial message could still be received before sending
173 this->kernel->conveyor->wait_for_queue(2);
174 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
175 played_cnt += buffer.size();
176 buffer.clear();
177 return;
178 }else{
179 buffer += c;
180 }
181 };
182 this->playing_file = false;
183 played_cnt= 0;
184 file_size= 0;
185 fclose(this->current_file_handler);
186 }
187 }
188
189 /*
190 void Player::on_main_loop(void* argument){
191 if( !this->on_booted ){
192 this->play_command(this->on_boot_file_name, new StreamOutput());
193 this->on_booted = true;
194 }
195 if( this->playing_file ){
196 string buffer;
197 int c;
198 // Print each line of the file
199 while ((c = fgetc(this->current_file_handler)) != EOF){
200 if (c == '\n'){
201 this->current_stream->printf("%s\n", buffer.c_str());
202 struct SerialMessage message;
203 message.message = buffer;
204 message.stream = this->current_stream;
205 // wait for the queue to have enough room that a serial message could still be received before sending
206 this->kernel->conveyor->wait_for_queue(2);
207 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
208 buffer.clear();
209 return;
210 }else{
211 buffer += c;
212 }
213 };
214
215 fclose(this->current_file_handler);
216 this->playing_file = false;
217 }
218 }
219 */
220