smoothie port to mbed online compiler (smoothieware.org)
For documentation, license, ..., please check http://smoothieware.org/
This version has been tested with a 3 axis machine
modules/utils/simpleshell/SimpleShell.cpp@0:31e91bb0ef3c, 2012-07-31 (annotated)
- Committer:
- scachat
- Date:
- Tue Jul 31 21:11:18 2012 +0000
- Revision:
- 0:31e91bb0ef3c
smoothie port to mbed online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | /* |
scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | */ |
scachat | 0:31e91bb0ef3c | 7 | |
scachat | 0:31e91bb0ef3c | 8 | |
scachat | 0:31e91bb0ef3c | 9 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 10 | #include "SimpleShell.h" |
scachat | 0:31e91bb0ef3c | 11 | #include "libs/nuts_bolts.h" |
scachat | 0:31e91bb0ef3c | 12 | #include "libs/utils.h" |
scachat | 0:31e91bb0ef3c | 13 | #include "libs/SerialMessage.h" |
scachat | 0:31e91bb0ef3c | 14 | #include "libs/StreamOutput.h" |
scachat | 0:31e91bb0ef3c | 15 | #include "modules/robot/Player.h" |
scachat | 0:31e91bb0ef3c | 16 | |
scachat | 0:31e91bb0ef3c | 17 | |
scachat | 0:31e91bb0ef3c | 18 | void SimpleShell::on_module_loaded(){ |
scachat | 0:31e91bb0ef3c | 19 | this->current_path = "/"; |
scachat | 0:31e91bb0ef3c | 20 | this->playing_file = false; |
scachat | 0:31e91bb0ef3c | 21 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
scachat | 0:31e91bb0ef3c | 22 | this->register_for_event(ON_MAIN_LOOP); |
scachat | 0:31e91bb0ef3c | 23 | } |
scachat | 0:31e91bb0ef3c | 24 | |
scachat | 0:31e91bb0ef3c | 25 | // When a new line is received, check if it is a command, and if it is, act upon it |
scachat | 0:31e91bb0ef3c | 26 | void SimpleShell::on_console_line_received( void* argument ){ |
scachat | 0:31e91bb0ef3c | 27 | SerialMessage new_message = *static_cast<SerialMessage*>(argument); |
scachat | 0:31e91bb0ef3c | 28 | string possible_command = new_message.message; |
scachat | 0:31e91bb0ef3c | 29 | |
scachat | 0:31e91bb0ef3c | 30 | // We don't compare to a string but to a checksum of that string, this saves some space in flash memory |
scachat | 0:31e91bb0ef3c | 31 | unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient |
scachat | 0:31e91bb0ef3c | 32 | |
scachat | 0:31e91bb0ef3c | 33 | // Act depending on command |
scachat | 0:31e91bb0ef3c | 34 | switch( check_sum ){ |
scachat | 0:31e91bb0ef3c | 35 | case ls_command_checksum : this->ls_command( get_arguments(possible_command), new_message.stream ); break; |
scachat | 0:31e91bb0ef3c | 36 | case cd_command_checksum : this->cd_command( get_arguments(possible_command), new_message.stream ); break; |
scachat | 0:31e91bb0ef3c | 37 | case pwd_command_checksum : this->pwd_command( get_arguments(possible_command), new_message.stream ); break; |
scachat | 0:31e91bb0ef3c | 38 | case cat_command_checksum : this->cat_command( get_arguments(possible_command), new_message.stream ); break; |
scachat | 0:31e91bb0ef3c | 39 | case play_command_checksum : this->play_command(get_arguments(possible_command), new_message.stream ); break; |
scachat | 0:31e91bb0ef3c | 40 | case reset_command_checksum : this->reset_command(get_arguments(possible_command),new_message.stream ); break; |
scachat | 0:31e91bb0ef3c | 41 | } |
scachat | 0:31e91bb0ef3c | 42 | } |
scachat | 0:31e91bb0ef3c | 43 | |
scachat | 0:31e91bb0ef3c | 44 | // Convert a path indication ( absolute or relative ) into a path ( absolute ) |
scachat | 0:31e91bb0ef3c | 45 | string SimpleShell::absolute_from_relative( string path ){ |
scachat | 0:31e91bb0ef3c | 46 | if( path[0] == '/' ){ return path; } |
scachat | 0:31e91bb0ef3c | 47 | if( path[0] == '.' ){ return this->current_path; } |
scachat | 0:31e91bb0ef3c | 48 | return this->current_path + path; |
scachat | 0:31e91bb0ef3c | 49 | } |
scachat | 0:31e91bb0ef3c | 50 | |
scachat | 0:31e91bb0ef3c | 51 | // Act upon an ls command |
scachat | 0:31e91bb0ef3c | 52 | // Convert the first parameter into an absolute path, then list the files in that path |
scachat | 0:31e91bb0ef3c | 53 | void SimpleShell::ls_command( string parameters, StreamOutput* stream ){ |
scachat | 0:31e91bb0ef3c | 54 | string folder = this->absolute_from_relative( parameters ); |
scachat | 0:31e91bb0ef3c | 55 | DIR* d; |
scachat | 0:31e91bb0ef3c | 56 | struct dirent* p; |
scachat | 0:31e91bb0ef3c | 57 | d = opendir(folder.c_str()); |
scachat | 0:31e91bb0ef3c | 58 | if(d != NULL) { |
scachat | 0:31e91bb0ef3c | 59 | while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); } |
scachat | 0:31e91bb0ef3c | 60 | } else { |
scachat | 0:31e91bb0ef3c | 61 | stream->printf("Could not open directory %s \r\n", folder.c_str()); |
scachat | 0:31e91bb0ef3c | 62 | } |
scachat | 0:31e91bb0ef3c | 63 | } |
scachat | 0:31e91bb0ef3c | 64 | |
scachat | 0:31e91bb0ef3c | 65 | // Change current absolute path to provided path |
scachat | 0:31e91bb0ef3c | 66 | void SimpleShell::cd_command( string parameters, StreamOutput* stream ){ |
scachat | 0:31e91bb0ef3c | 67 | string folder = this->absolute_from_relative( parameters ); |
scachat | 0:31e91bb0ef3c | 68 | if( folder[folder.length()-1] != '/' ){ folder += "/"; } |
scachat | 0:31e91bb0ef3c | 69 | DIR *d; |
scachat | 0:31e91bb0ef3c | 70 | struct dirent *p; |
scachat | 0:31e91bb0ef3c | 71 | d = opendir(folder.c_str()); |
scachat | 0:31e91bb0ef3c | 72 | if(d == NULL) { |
scachat | 0:31e91bb0ef3c | 73 | stream->printf("Could not open directory %s \r\n", folder.c_str() ); |
scachat | 0:31e91bb0ef3c | 74 | }else{ |
scachat | 0:31e91bb0ef3c | 75 | this->current_path = folder; |
scachat | 0:31e91bb0ef3c | 76 | } |
scachat | 0:31e91bb0ef3c | 77 | } |
scachat | 0:31e91bb0ef3c | 78 | |
scachat | 0:31e91bb0ef3c | 79 | // Responds with the present working directory |
scachat | 0:31e91bb0ef3c | 80 | void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){ |
scachat | 0:31e91bb0ef3c | 81 | stream->printf("%s\r\n", this->current_path.c_str()); |
scachat | 0:31e91bb0ef3c | 82 | } |
scachat | 0:31e91bb0ef3c | 83 | |
scachat | 0:31e91bb0ef3c | 84 | // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output ) |
scachat | 0:31e91bb0ef3c | 85 | void SimpleShell::cat_command( string parameters, StreamOutput* stream ){ |
scachat | 0:31e91bb0ef3c | 86 | |
scachat | 0:31e91bb0ef3c | 87 | // Get parameters ( filename and line limit ) |
scachat | 0:31e91bb0ef3c | 88 | string filename = this->absolute_from_relative(shift_parameter( parameters )); |
scachat | 0:31e91bb0ef3c | 89 | string limit_paramater = shift_parameter( parameters ); |
scachat | 0:31e91bb0ef3c | 90 | int limit = -1; |
scachat | 0:31e91bb0ef3c | 91 | if( limit_paramater != "" ){ limit = int(atof(limit_paramater.c_str())); } |
scachat | 0:31e91bb0ef3c | 92 | |
scachat | 0:31e91bb0ef3c | 93 | // Open file |
scachat | 0:31e91bb0ef3c | 94 | FILE *lp = fopen(filename.c_str(), "r"); |
scachat | 0:31e91bb0ef3c | 95 | if(lp == NULL) { |
scachat | 0:31e91bb0ef3c | 96 | stream->printf("File not found: %s\r\n", filename.c_str()); |
scachat | 0:31e91bb0ef3c | 97 | return; |
scachat | 0:31e91bb0ef3c | 98 | } |
scachat | 0:31e91bb0ef3c | 99 | string buffer; |
scachat | 0:31e91bb0ef3c | 100 | int c; |
scachat | 0:31e91bb0ef3c | 101 | int newlines = 0; |
scachat | 0:31e91bb0ef3c | 102 | |
scachat | 0:31e91bb0ef3c | 103 | // Print each line of the file |
scachat | 0:31e91bb0ef3c | 104 | while ((c = fgetc (lp)) != EOF){ |
scachat | 0:31e91bb0ef3c | 105 | buffer.append((char *)&c, 1); |
scachat | 0:31e91bb0ef3c | 106 | if( char(c) == '\n' ){ |
scachat | 0:31e91bb0ef3c | 107 | newlines++; |
scachat | 0:31e91bb0ef3c | 108 | stream->printf("%s", buffer.c_str()); |
scachat | 0:31e91bb0ef3c | 109 | buffer.clear(); |
scachat | 0:31e91bb0ef3c | 110 | } |
scachat | 0:31e91bb0ef3c | 111 | if( newlines == limit ){ break; } |
scachat | 0:31e91bb0ef3c | 112 | }; |
scachat | 0:31e91bb0ef3c | 113 | fclose(lp); |
scachat | 0:31e91bb0ef3c | 114 | |
scachat | 0:31e91bb0ef3c | 115 | } |
scachat | 0:31e91bb0ef3c | 116 | |
scachat | 0:31e91bb0ef3c | 117 | // Play a gcode file by considering each line as if it was received on the serial console |
scachat | 0:31e91bb0ef3c | 118 | void SimpleShell::play_command( string parameters, StreamOutput* stream ){ |
scachat | 0:31e91bb0ef3c | 119 | // Get filename |
scachat | 0:31e91bb0ef3c | 120 | string filename = this->absolute_from_relative(shift_parameter( parameters )); |
scachat | 0:31e91bb0ef3c | 121 | this->current_file_handler = fopen( filename.c_str(), "r"); |
scachat | 0:31e91bb0ef3c | 122 | if(this->current_file_handler == NULL) |
scachat | 0:31e91bb0ef3c | 123 | { |
scachat | 0:31e91bb0ef3c | 124 | stream->printf("File not found: %s\r\n", filename.c_str()); |
scachat | 0:31e91bb0ef3c | 125 | return; |
scachat | 0:31e91bb0ef3c | 126 | } |
scachat | 0:31e91bb0ef3c | 127 | this->playing_file = true; |
scachat | 0:31e91bb0ef3c | 128 | this->current_stream = stream; |
scachat | 0:31e91bb0ef3c | 129 | } |
scachat | 0:31e91bb0ef3c | 130 | |
scachat | 0:31e91bb0ef3c | 131 | // Reset the system |
scachat | 0:31e91bb0ef3c | 132 | void SimpleShell::reset_command( string parameters, StreamOutput* stream){ |
scachat | 0:31e91bb0ef3c | 133 | stream->printf("Smoothie out. Peace.\r\n"); |
scachat | 0:31e91bb0ef3c | 134 | system_reset(); |
scachat | 0:31e91bb0ef3c | 135 | } |
scachat | 0:31e91bb0ef3c | 136 | |
scachat | 0:31e91bb0ef3c | 137 | void SimpleShell::on_main_loop(void* argument){ |
scachat | 0:31e91bb0ef3c | 138 | |
scachat | 0:31e91bb0ef3c | 139 | if( this->playing_file ){ |
scachat | 0:31e91bb0ef3c | 140 | string buffer; |
scachat | 0:31e91bb0ef3c | 141 | int c; |
scachat | 0:31e91bb0ef3c | 142 | // Print each line of the file |
scachat | 0:31e91bb0ef3c | 143 | while ((c = fgetc(this->current_file_handler)) != EOF){ |
scachat | 0:31e91bb0ef3c | 144 | if (c == '\n'){ |
scachat | 0:31e91bb0ef3c | 145 | this->current_stream->printf("%s\n", buffer.c_str()); |
scachat | 0:31e91bb0ef3c | 146 | struct SerialMessage message; |
scachat | 0:31e91bb0ef3c | 147 | message.message = buffer; |
scachat | 0:31e91bb0ef3c | 148 | message.stream = this->current_stream; |
scachat | 0:31e91bb0ef3c | 149 | // wait for the queue to have enough room that a serial message could still be received before sending |
scachat | 0:31e91bb0ef3c | 150 | this->kernel->player->wait_for_queue(2); |
scachat | 0:31e91bb0ef3c | 151 | this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message); |
scachat | 0:31e91bb0ef3c | 152 | buffer.clear(); |
scachat | 0:31e91bb0ef3c | 153 | return; |
scachat | 0:31e91bb0ef3c | 154 | }else{ |
scachat | 0:31e91bb0ef3c | 155 | buffer += c; |
scachat | 0:31e91bb0ef3c | 156 | } |
scachat | 0:31e91bb0ef3c | 157 | }; |
scachat | 0:31e91bb0ef3c | 158 | |
scachat | 0:31e91bb0ef3c | 159 | fclose(this->current_file_handler); |
scachat | 0:31e91bb0ef3c | 160 | this->playing_file = false; |
scachat | 0:31e91bb0ef3c | 161 | } |
scachat | 0:31e91bb0ef3c | 162 | } |