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/>.
9 #include "libs/Kernel.h"
10 #include "SimpleShell.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/Player.h"
16 #include "system_LPC17xx.h"
19 void SimpleShell::on_module_loaded(){
20 this->current_path
= "/";
21 this->playing_file
= false;
22 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
23 this->register_for_event(ON_MAIN_LOOP
);
26 // When a new line is received, check if it is a command, and if it is, act upon it
27 void SimpleShell::on_console_line_received( void* argument
){
28 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
29 string possible_command
= new_message
.message
;
31 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
32 unsigned short check_sum
= get_checksum( possible_command
.substr(0,possible_command
.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
34 // Act depending on command
36 case ls_command_checksum
: this->ls_command( get_arguments(possible_command
), new_message
.stream
); break;
37 case cd_command_checksum
: this->cd_command( get_arguments(possible_command
), new_message
.stream
); break;
38 case cat_command_checksum
: this->cat_command( get_arguments(possible_command
), new_message
.stream
); break;
39 case play_command_checksum
: this->play_command(get_arguments(possible_command
), new_message
.stream
); break;
40 case reset_command_checksum
: this->reset_command(get_arguments(possible_command
),new_message
.stream
); break;
44 // Convert a path indication ( absolute or relative ) into a path ( absolute )
45 string
SimpleShell::absolute_from_relative( string path
){
46 if( path
[0] == '/' ){ return path
; }
47 if( path
[0] == '.' ){ return this->current_path
; }
48 return this->current_path
+ path
;
51 // Act upon an ls command
52 // Convert the first parameter into an absolute path, then list the files in that path
53 void SimpleShell::ls_command( string parameters
, StreamOutput
* stream
){
54 string folder
= this->absolute_from_relative( parameters
);
57 d
= opendir(folder
.c_str());
59 while((p
= readdir(d
)) != NULL
) { stream
->printf("%s\r\n", lc(string(p
->d_name
)).c_str()); }
61 stream
->printf("Could not open directory %s \r\n", folder
.c_str());
65 // Change current absolute path to provided path
66 void SimpleShell::cd_command( string parameters
, StreamOutput
* stream
){
67 string folder
= this->absolute_from_relative( parameters
);
68 if( folder
[folder
.length()-1] != '/' ){ folder
+= "/"; }
71 d
= opendir(folder
.c_str());
73 stream
->printf("Could not open directory %s \r\n", folder
.c_str() );
75 this->current_path
= folder
;
79 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
80 void SimpleShell::cat_command( string parameters
, StreamOutput
* stream
){
82 // Get parameters ( filename and line limit )
83 string filename
= this->absolute_from_relative(shift_parameter( parameters
));
84 string limit_paramater
= shift_parameter( parameters
);
86 if( limit_paramater
!= "" ){ limit
= int(atof(limit_paramater
.c_str())); }
89 FILE *lp
= fopen(filename
.c_str(), "r");
91 stream
->printf("File not found: %s\r\n", filename
.c_str());
98 // Print each line of the file
99 while ((c
= fgetc (lp
)) != EOF
){
100 buffer
.append((char *)&c
, 1);
101 if( char(c
) == '\n' ){
103 stream
->printf("%s", buffer
.c_str());
106 if( newlines
== limit
){ break; }
112 // Play a gcode file by considering each line as if it was received on the serial console
113 void SimpleShell::play_command( string parameters
, StreamOutput
* stream
){
115 string filename
= this->absolute_from_relative(shift_parameter( parameters
));
116 this->current_file_handler
= fopen( filename
.c_str(), "r");
117 if(this->current_file_handler
== NULL
)
119 stream
->printf("File not found: %s\r\n", filename
.c_str());
122 this->playing_file
= true;
123 this->current_stream
= stream
;
127 void SimpleShell::reset_command( string parameters
, StreamOutput
* stream
){
128 stream
->printf("Smoothie out. Peace.\r\n");
130 LPC_WDT
->WDCLKSEL
= 0x1; // Set CLK src to PCLK
131 uint32_t clk
= SystemCoreClock
/ 16; // WD has a fixed /4 prescaler, PCLK default is /4
132 LPC_WDT
->WDTC
= 1 * (float)clk
; // Reset in 1 second
133 LPC_WDT
->WDMOD
= 0x3; // Enabled and Reset
134 LPC_WDT
->WDFEED
= 0xAA; // Kick the dog!
135 LPC_WDT
->WDFEED
= 0x55;
138 void SimpleShell::on_main_loop(void* argument
){
140 if( this->playing_file
){
143 // Print each line of the file
144 while ((c
= fgetc(this->current_file_handler
)) != EOF
){
146 this->current_stream
->printf("%s\n", buffer
.c_str());
147 struct SerialMessage message
;
148 message
.message
= buffer
;
149 message
.stream
= this->current_stream
;
150 // wait for the queue to have enough room that a serial message could still be received before sending
151 this->kernel
->player
->wait_for_queue(2);
152 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
160 fclose(this->current_file_handler
);
161 this->playing_file
= false;