Merge branch 'edge' into onboot
[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");
57 if(this->current_file_handler == NULL)
58 {
59 stream->printf("File not found: %s\r\n", filename.c_str());
60 return;
61 }
62 this->playing_file = true;
63 if( options.find_first_of("Qq") == string::npos ){
64 this->current_stream = stream;
65 }else{
addfb453
L
66 this->current_stream = kernel->streams;
67 }
68
69 // get size of file
70 int result = fseek(this->current_file_handler, 0, SEEK_END);
71 if (0 != result){
72 stream->printf("WARNING - Could not get file size\r\n");
73 file_size= -1;
74 }else{
75 file_size= ftell(this->current_file_handler);
76 fseek(this->current_file_handler, 0, SEEK_SET);
77 stream->printf(" File size %ld\r\n", file_size);
78 }
79 played_cnt= 0;
80}
81
82void Player::progress_command( string parameters, StreamOutput* stream ){
83 if(!playing_file) {
84 stream->printf("Not currently playing\r\n");
85 return;
86 }
87
88 if(file_size > 0) {
89 int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
90 stream->printf("%d %% complete\r\n", pcnt);
91 }else{
92 stream->printf("File size is unknown\r\n");
93 }
94}
95
96void Player::abort_command( string parameters, StreamOutput* stream ){
97 if(!playing_file) {
98 stream->printf("Not currently playing\r\n");
99 return;
100 }
101 playing_file = false;
102 played_cnt= 0;
103 file_size= 0;
104 fclose(current_file_handler);
105 stream->printf("Aborted playing file\r\n");
106}
107
108// Convert a path indication ( absolute or relative ) into a path ( absolute )
109string Player::absolute_from_relative( string path ){
110 if( path[0] == '/' ){ return path; }
111 if( path[0] == '.' ){ return this->current_path; }
112 return this->current_path + path;
113}
114
115// Change current absolute path to provided path
116void Player::cd_command( string parameters, StreamOutput* stream ){
117 string folder = this->absolute_from_relative( parameters );
118 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
119 DIR *d;
120 d = opendir(folder.c_str());
121 if(d == NULL) {
122// stream->printf("Could not open directory %s \r\n", folder.c_str() );
123 }else{
124 this->current_path = folder;
125 closedir(d);
126 }
127}
128
129void Player::on_main_loop(void* argument){
130 if( !this->booted && this->on_boot_enable ){
131 this->play_command(this->on_boot_file_name, NULL);
132 this->booted = true;
133 }
134
135 if( this->playing_file ){
136 string buffer;
137 int c;
138 buffer.reserve(20);
139 // Print each line of the file
140 while ((c = fgetc(this->current_file_handler)) != EOF){
141 if (c == '\n'){
142 this->current_stream->printf("%s\n", buffer.c_str());
143 struct SerialMessage message;
144 message.message = buffer;
145 message.stream = this->current_stream;
146 // wait for the queue to have enough room that a serial message could still be received before sending
147 this->kernel->conveyor->wait_for_queue(2);
148 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
149 played_cnt += buffer.size();
150 buffer.clear();
151 return;
152 }else{
153 buffer += c;
154 }
155 };
156 this->playing_file = false;
157 played_cnt= 0;
158 file_size= 0;
159 fclose(this->current_file_handler);
663d7943
L
160 }
161}
162
addfb453 163/*
663d7943
L
164void Player::on_main_loop(void* argument){
165 if( !this->on_booted ){
166 this->play_command(this->on_boot_file_name, new StreamOutput());
167 this->on_booted = true;
168 }
169 if( this->playing_file ){
170 string buffer;
171 int c;
172 // Print each line of the file
173 while ((c = fgetc(this->current_file_handler)) != EOF){
174 if (c == '\n'){
175 this->current_stream->printf("%s\n", buffer.c_str());
176 struct SerialMessage message;
177 message.message = buffer;
178 message.stream = this->current_stream;
179 // wait for the queue to have enough room that a serial message could still be received before sending
180 this->kernel->conveyor->wait_for_queue(2);
181 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
182 buffer.clear();
183 return;
184 }else{
185 buffer += c;
186 }
187 };
188
189 fclose(this->current_file_handler);
190 this->playing_file = false;
191 }
192}
addfb453
L
193*/
194