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