Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Committer:
Bigcheese
Date:
Sun Mar 02 06:33:08 2014 +0000
Revision:
3:f151d08d335c
Parent:
2:1df0b61d3b5a
Bunch of stuff. Need to locally merge in updated USB changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael J. Spencer 2:1df0b61d3b5a 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.
Michael J. Spencer 2:1df0b61d3b5a 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 #include "libs/Kernel.h"
scachat 0:31e91bb0ef3c 9 #include "ConfigValue.h"
scachat 0:31e91bb0ef3c 10 #include "FileConfigSource.h"
scachat 0:31e91bb0ef3c 11 #include "ConfigCache.h"
Michael J. Spencer 2:1df0b61d3b5a 12
Michael J. Spencer 2:1df0b61d3b5a 13
scachat 0:31e91bb0ef3c 14 using namespace std;
scachat 0:31e91bb0ef3c 15 #include <string>
scachat 0:31e91bb0ef3c 16
Michael J. Spencer 2:1df0b61d3b5a 17
scachat 0:31e91bb0ef3c 18 FileConfigSource::FileConfigSource(string config_file, uint16_t name_checksum){
scachat 0:31e91bb0ef3c 19 this->name_checksum = name_checksum;
scachat 0:31e91bb0ef3c 20 this->config_file = config_file;
scachat 0:31e91bb0ef3c 21 this->config_file_found = false;
scachat 0:31e91bb0ef3c 22 }
scachat 0:31e91bb0ef3c 23
scachat 0:31e91bb0ef3c 24 // Transfer all values found in the file to the passed cache
scachat 0:31e91bb0ef3c 25 void FileConfigSource::transfer_values_to_cache( ConfigCache* cache ){
Michael J. Spencer 2:1df0b61d3b5a 26
Michael J. Spencer 2:1df0b61d3b5a 27 if( this->has_config_file() == false ){return;}
Michael J. Spencer 2:1df0b61d3b5a 28 // Open the config file ( find it if we haven't already found it )
scachat 0:31e91bb0ef3c 29 FILE *lp = fopen(this->get_config_file().c_str(), "r");
Michael J. Spencer 2:1df0b61d3b5a 30 int c;
Michael J. Spencer 2:1df0b61d3b5a 31 // For each line
scachat 0:31e91bb0ef3c 32 do {
Michael J. Spencer 2:1df0b61d3b5a 33 process_char_from_ascii_config(c = fgetc(lp), cache);
Michael J. Spencer 2:1df0b61d3b5a 34 } while (c != EOF);
scachat 0:31e91bb0ef3c 35 fclose(lp);
scachat 0:31e91bb0ef3c 36 }
scachat 0:31e91bb0ef3c 37
scachat 0:31e91bb0ef3c 38 // Return true if the check_sums match
scachat 0:31e91bb0ef3c 39 bool FileConfigSource::is_named( uint16_t check_sum ){
scachat 0:31e91bb0ef3c 40 return check_sum == this->name_checksum;
scachat 0:31e91bb0ef3c 41 }
scachat 0:31e91bb0ef3c 42
scachat 0:31e91bb0ef3c 43 // Write a config setting to the file
scachat 0:31e91bb0ef3c 44 void FileConfigSource::write( string setting, string value ){
scachat 0:31e91bb0ef3c 45 // Open the config file ( find it if we haven't already found it )
scachat 0:31e91bb0ef3c 46 FILE *lp = fopen(this->get_config_file().c_str(), "r+");
scachat 0:31e91bb0ef3c 47 string buffer;
scachat 0:31e91bb0ef3c 48 int c;
scachat 0:31e91bb0ef3c 49 // For each line
scachat 0:31e91bb0ef3c 50 do {
scachat 0:31e91bb0ef3c 51 c = fgetc (lp);
scachat 0:31e91bb0ef3c 52 if (c == '\n' || c == EOF){
scachat 0:31e91bb0ef3c 53 // We have a new line
scachat 0:31e91bb0ef3c 54 if( buffer[0] == '#' ){ buffer.clear(); continue; } // Ignore comments
scachat 0:31e91bb0ef3c 55 if( buffer.length() < 3 ){ buffer.clear(); continue; } //Ignore empty lines
Michael J. Spencer 2:1df0b61d3b5a 56 size_t begin_key = buffer.find_first_not_of(" \t");
Michael J. Spencer 2:1df0b61d3b5a 57 size_t begin_value = buffer.find_first_not_of(" \t", buffer.find_first_of(" \t", begin_key));
scachat 0:31e91bb0ef3c 58 // If this line matches the checksum
Michael J. Spencer 2:1df0b61d3b5a 59 string candidate = buffer.substr(begin_key, buffer.find_first_of(" \t", begin_key) - begin_key);
scachat 0:31e91bb0ef3c 60 if( candidate.compare(setting) != 0 ){ buffer.clear(); continue; }
scachat 0:31e91bb0ef3c 61 int free_space = int(int(buffer.find_first_of("\r\n#", begin_value+1))-begin_value);
scachat 0:31e91bb0ef3c 62 if( int(value.length()) >= free_space ){
Michael J. Spencer 2:1df0b61d3b5a 63 //THEKERNEL->streams->printf("ERROR: Not enough room for value\r\n");
scachat 0:31e91bb0ef3c 64 fclose(lp);
scachat 0:31e91bb0ef3c 65 return;
scachat 0:31e91bb0ef3c 66 }
scachat 0:31e91bb0ef3c 67 // Update value
scachat 0:31e91bb0ef3c 68 for( int i = value.length(); i < free_space; i++){ value += " "; }
scachat 0:31e91bb0ef3c 69 fpos_t pos;
Bigcheese 3:f151d08d335c 70 int position = fgetpos( lp, &pos );
Bigcheese 3:f151d08d335c 71 int start = position - buffer.length() + begin_value - 1;
scachat 0:31e91bb0ef3c 72 fseek(lp, start, SEEK_SET);
scachat 0:31e91bb0ef3c 73 fputs(value.c_str(), lp);
scachat 0:31e91bb0ef3c 74 fclose(lp);
scachat 0:31e91bb0ef3c 75 return;
scachat 0:31e91bb0ef3c 76 }else{
scachat 0:31e91bb0ef3c 77 buffer += c;
scachat 0:31e91bb0ef3c 78 }
scachat 0:31e91bb0ef3c 79 } while (c != EOF);
scachat 0:31e91bb0ef3c 80 fclose(lp);
Michael J. Spencer 2:1df0b61d3b5a 81 //THEKERNEL->streams->printf("ERROR: configuration key not found\r\n");
scachat 0:31e91bb0ef3c 82 }
scachat 0:31e91bb0ef3c 83
scachat 0:31e91bb0ef3c 84 // Return the value for a specific checksum
Michael J. Spencer 2:1df0b61d3b5a 85 string FileConfigSource::read( uint16_t check_sums[3] ){
scachat 0:31e91bb0ef3c 86
scachat 0:31e91bb0ef3c 87 string value = "";
scachat 0:31e91bb0ef3c 88
scachat 0:31e91bb0ef3c 89 if( this->has_config_file() == false ){return value;}
Michael J. Spencer 2:1df0b61d3b5a 90 // Open the config file ( find it if we haven't already found it )
scachat 0:31e91bb0ef3c 91 FILE *lp = fopen(this->get_config_file().c_str(), "r");
Michael J. Spencer 2:1df0b61d3b5a 92 int c;
Michael J. Spencer 2:1df0b61d3b5a 93 // For each line
scachat 0:31e91bb0ef3c 94 do {
scachat 0:31e91bb0ef3c 95 c = fgetc (lp);
Michael J. Spencer 2:1df0b61d3b5a 96 process_char_from_ascii_config(c, check_sums);
Michael J. Spencer 2:1df0b61d3b5a 97 } while (c != EOF);
scachat 0:31e91bb0ef3c 98 fclose(lp);
scachat 0:31e91bb0ef3c 99
scachat 0:31e91bb0ef3c 100 return value;
scachat 0:31e91bb0ef3c 101 }
scachat 0:31e91bb0ef3c 102
scachat 0:31e91bb0ef3c 103 // Return wether or not we have a readable config file
scachat 0:31e91bb0ef3c 104 bool FileConfigSource::has_config_file(){
scachat 0:31e91bb0ef3c 105 if( this->config_file_found ){ return true; }
scachat 0:31e91bb0ef3c 106 this->try_config_file(this->config_file);
scachat 0:31e91bb0ef3c 107 if( this->config_file_found ){
scachat 0:31e91bb0ef3c 108 return true;
scachat 0:31e91bb0ef3c 109 }else{
scachat 0:31e91bb0ef3c 110 return false;
scachat 0:31e91bb0ef3c 111 }
scachat 0:31e91bb0ef3c 112
scachat 0:31e91bb0ef3c 113 }
scachat 0:31e91bb0ef3c 114
scachat 0:31e91bb0ef3c 115 // Tool function for get_config_file
scachat 0:31e91bb0ef3c 116 inline void FileConfigSource::try_config_file(string candidate){
Michael J. Spencer 2:1df0b61d3b5a 117 if(file_exists(candidate)){ this->config_file_found = true; }
scachat 0:31e91bb0ef3c 118 }
scachat 0:31e91bb0ef3c 119
scachat 0:31e91bb0ef3c 120 // Get the filename for the config file
scachat 0:31e91bb0ef3c 121 string FileConfigSource::get_config_file(){
scachat 0:31e91bb0ef3c 122 if( this->config_file_found ){ return this->config_file; }
scachat 0:31e91bb0ef3c 123 if( this->has_config_file() ){
scachat 0:31e91bb0ef3c 124 return this->config_file;
scachat 0:31e91bb0ef3c 125 }else{
scachat 0:31e91bb0ef3c 126 printf("ERROR: no config file found\r\n");
Michael J. Spencer 2:1df0b61d3b5a 127 return "";
scachat 0:31e91bb0ef3c 128 }
scachat 0:31e91bb0ef3c 129 }
scachat 0:31e91bb0ef3c 130
scachat 0:31e91bb0ef3c 131
scachat 0:31e91bb0ef3c 132
scachat 0:31e91bb0ef3c 133
Michael J. Spencer 2:1df0b61d3b5a 134
Michael J. Spencer 2:1df0b61d3b5a 135