added a reset command to SimpleShell that triggers a watchdog reset
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.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 "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"
17
18
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);
24 }
25
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;
30
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
33
34 // Act depending on command
35 switch( check_sum ){
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;
41 }
42 }
43
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;
49 }
50
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 );
55 DIR* d;
56 struct dirent* p;
57 d = opendir(folder.c_str());
58 if(d != NULL) {
59 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
60 } else {
61 stream->printf("Could not open directory %s \r\n", folder.c_str());
62 }
63 }
64
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 += "/"; }
69 DIR *d;
70 struct dirent *p;
71 d = opendir(folder.c_str());
72 if(d == NULL) {
73 stream->printf("Could not open directory %s \r\n", folder.c_str() );
74 }else{
75 this->current_path = folder;
76 }
77 }
78
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 ){
81
82 // Get parameters ( filename and line limit )
83 string filename = this->absolute_from_relative(shift_parameter( parameters ));
84 string limit_paramater = shift_parameter( parameters );
85 int limit = -1;
86 if( limit_paramater != "" ){ limit = int(atof(limit_paramater.c_str())); }
87
88 // Open file
89 FILE *lp = fopen(filename.c_str(), "r");
90 if(lp == NULL) {
91 stream->printf("File not found: %s\r\n", filename.c_str());
92 return;
93 }
94 string buffer;
95 int c;
96 int newlines = 0;
97
98 // Print each line of the file
99 while ((c = fgetc (lp)) != EOF){
100 buffer.append((char *)&c, 1);
101 if( char(c) == '\n' ){
102 newlines++;
103 stream->printf("%s", buffer.c_str());
104 buffer.clear();
105 }
106 if( newlines == limit ){ break; }
107 };
108 fclose(lp);
109
110 }
111
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 ){
114 // Get filename
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)
118 {
119 stream->printf("File not found: %s\r\n", filename.c_str());
120 return;
121 }
122 this->playing_file = true;
123 this->current_stream = stream;
124 }
125
126 // Reset the system
127 void SimpleShell::reset_command( string parameters, StreamOutput* stream){
128 stream->printf("Smoothie out. Peace.\r\n");
129
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;
136 }
137
138 void SimpleShell::on_main_loop(void* argument){
139
140 if( this->playing_file ){
141 string buffer;
142 int c;
143 // Print each line of the file
144 while ((c = fgetc(this->current_file_handler)) != EOF){
145 if (c == '\n'){
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);
153 buffer.clear();
154 return;
155 }else{
156 buffer += c;
157 }
158 };
159
160 fclose(this->current_file_handler);
161 this->playing_file = false;
162 }
163 }