Rev 89 | Rev 92 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 89 | Rev 90 | ||
---|---|---|---|
Line 63... | Line 63... | ||
63 | #include "uart1.h" |
63 | #include "uart1.h" |
Line 64... | Line 64... | ||
64 | 64 | ||
65 | typedef struct |
65 | typedef struct |
66 | { |
66 | { |
67 | ParamId_t ParamId; |
67 | ParamId_t ParamId; |
68 | s8 Name[16]; |
68 | s8 Name[17]; // 0 terminator is the last byte |
69 | u8 Group; // 0 = reserved 1,2,3,4 |
69 | u8 Group; // 0 = reserved 1,2,3,4 |
70 | u16 Value; |
70 | u16 Value; |
71 | u16 Default; |
71 | u16 Default; |
72 | u16 Min; |
72 | u16 Min; |
Line 93... | Line 93... | ||
93 | } |
93 | } |
94 | } |
94 | } |
Line 95... | Line 95... | ||
95 | 95 | ||
96 | //---------------------------------------------------------------------------------- |
96 | //---------------------------------------------------------------------------------- |
97 | // set parameter from string based name and value |
97 | // set parameter from string based name and value |
98 | void Settings_SetParameterFromString(s8 *name, s8 *value) |
98 | u8 Settings_SetParameterFromString(s8 *name, s8 *value) |
99 | { |
99 | { |
100 | u8 i; |
100 | u8 i, retval = 0; |
101 | s8 string[] = " \0"; // null terminated string of 16 characters |
101 | s8 string[] = " \0"; // null terminated string of 16 characters |
- | 102 | s8 text[64]; |
|
- | 103 | ||
Line 102... | Line 104... | ||
102 | s8 text[32]; |
104 | if((name == NULL) || (value == NULL)) return(retval); |
103 | 105 | ||
Line 104... | Line 106... | ||
104 | i = strlen(name); // lenght of the parameter name |
106 | i = strlen(name); // lenght of the parameter name |
105 | if (i > 16) i = 16; // cut off at 16 |
107 | if (i > 16) i = 16; // cut off at 16 |
Line 106... | Line 108... | ||
106 | 108 | ||
107 | memcpy(string, name, i); // copy name to local buffer |
109 | memcpy(string, name, i); // copy name to local buffer |
108 | //sprintf(text,"#%s#%s#\n\r", name, string); SerialPutString(text); |
110 | //sprintf(text,"#%s#%s#\n\r", name, string); SerialPutString(text); |
109 | - | ||
110 | // search name in parameter list |
111 | |
111 | for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++) |
112 | // search name in parameter list |
112 | { |
113 | for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++) |
113 | //sprintf(text,"#%s#\n\r", CFG_Parameter[i].Name); SerialPutString(text); |
114 | { |
114 | if(strncmp(string, CFG_Parameter[i].Name, 16) == 0) // names are matching |
115 | if(strncmp(string, CFG_Parameter[i].Name, 16) == 0) // names are matching |
115 | { |
- | |
- | 116 | { |
|
116 | CFG_Parameter[i].Value = (u16)atoi(value); // convert string to number and set value |
117 | CFG_Parameter[i].Value = (u16)atoi(value); // convert string to number and set value |
117 | // range within min/max |
118 | // out of range ? |
- | 119 | if((CFG_Parameter[i].Value < CFG_Parameter[i].Min) || (CFG_Parameter[i].Value > CFG_Parameter[i].Max)) |
|
- | 120 | { // print a warning |
|
- | 121 | sprintf(text,"\r\n%s <-- %d is out of range [%d...%d]", string, CFG_Parameter[i].Value, CFG_Parameter[i].Min, CFG_Parameter[i].Max); |
|
118 | if(CFG_Parameter[i].Value < CFG_Parameter[i].Min) CFG_Parameter[i].Value = CFG_Parameter[i].Min; |
122 | SerialPutString(text); |
119 | 123 | CFG_Parameter[i].Value = CFG_Parameter[i].Default; // fallback to default |
|
120 | sprintf(text,"\r\n%s = %d", string, CFG_Parameter[i].Value); |
124 | } |
- | 125 | retval = 1; // value in range |
|
- | 126 | break; // end loop |
|
- | 127 | } |
|
- | 128 | } |
|
- | 129 | if(!retval) |
|
- | 130 | { |
|
121 | SerialPutString(text); |
131 | sprintf(text,"\r\n%s <-- unknown parameter\r\n", string); |
Line 122... | Line 132... | ||
122 | break; // end loop |
132 | SerialPutString(text); |
123 | } |
133 | } |
124 | } |
134 | return(retval); |
125 | } |
135 | } |
126 | 136 | ||
127 | //---------------------------------------------------------------------------------- |
137 | //---------------------------------------------------------------------------------- |
128 | // read settings from file on sd-card |
138 | // read settings from file on sd-card |
129 | void Settings_Init(void) |
139 | void Settings_Init(void) |
- | 140 | { |
|
Line -... | Line 141... | ||
- | 141 | #define LINE_MAX 32 |
|
130 | { |
142 | File_t *fp; |
131 | #define LINE_MAX 32 |
- | |
132 | File_t *fp; |
- | |
133 | s8 text[LINE_MAX]; |
143 | s8 text[LINE_MAX]; |
134 | s8 *name, *value; |
- | |
135 | - | ||
136 | Settings_SetDefaultValues(); |
- | |
137 | SerialPutString("\n\rReading settings from file..."); |
- | |
138 | fp = fopen_("settings.ini", 'r'); |
- | |
139 | 144 | s8 *name, *value; |
|
140 | if (fp == NULL) // could not open the file |
- | |
141 | { |
- | |
142 | SerialPutString("ERROR: File not found !"); |
145 | u8 i; |
143 | return; |
- | |
144 | } |
- | |
145 | // read all lines from file |
- | |
146 | while(fgets_(text, LINE_MAX, fp) != 0) |
146 | |
147 | { |
- | |
148 | if ( // ignorelines starting with \r,\n,' ',';','#' |
- | |
149 | (text[0] != '\n') && |
147 | SerialPutString("\n\rSettings init..."); |
150 | (text[0] != '\r') && |
- | |
151 | (text[0] != ' ' ) && |
148 | Settings_SetDefaultValues(); |
152 | (text[0] != ';' ) && |
149 | |
153 | (text[0] != '#' ) |
150 | if(Fat16_IsValid()) |
154 | ) |
151 | { // check if settings file is existing |
- | 152 | if(fexist_("settings.ini")) |
|
155 | { |
153 | { |
- | 154 | fp = fopen_("settings.ini", 'r'); // try to open the file |
|
- | 155 | if (fp == NULL) // could not open the file |
|
- | 156 | { |
|
- | 157 | SerialPutString("ERROR: Opening settings file!"); |
|
- | 158 | return; |
|
- | 159 | } |
|
- | 160 | // read all lines from file |
|
- | 161 | while(fgets_(text, LINE_MAX, fp) != 0) |
|
- | 162 | { |
|
- | 163 | if ( // ignorelines starting with \r,\n,' ',';','#' |
|
- | 164 | (text[0] != '\n') && |
|
- | 165 | (text[0] != '\r') && |
|
- | 166 | (text[0] != ' ' ) && |
|
- | 167 | (text[0] != ';' ) && |
|
- | 168 | (text[0] != '#' ) |
|
- | 169 | ) |
|
- | 170 | { |
|
- | 171 | name = strtok(text, "="); // get name |
|
- | 172 | value = strtok(NULL, "="); // get value |
|
- | 173 | if ((name != NULL) && (value != NULL)) |
|
- | 174 | { |
|
- | 175 | Settings_SetParameterFromString(name, value); |
|
- | 176 | } |
|
- | 177 | } |
|
- | 178 | } |
|
- | 179 | fclose_(fp); |
|
- | 180 | SerialPutString("ok"); |
|
- | 181 | return; |
|
- | 182 | } |
|
- | 183 | else // settings file does not exist |
|
- | 184 | { // create default settings file |
|
- | 185 | fp = fopen_("settings.ini", 'w'); // try to open the file |
|
- | 186 | if(fp == NULL) |
|
- | 187 | { |
|
- | 188 | SerialPutString("ERROR: Creating default settings file!"); |
|
- | 189 | return; |
|
- | 190 | } |
|
- | 191 | // run thrue all parameters and set value to default |
|
- | 192 | for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++) |
|
- | 193 | { |
|
- | 194 | sprintf(text, "%s = %d\r\n", CFG_Parameter[i].Name, CFG_Parameter[i].Default); |
|
156 | name = strtok(text, "="); // get name |
195 | fputs_(text, fp); // write to file |
157 | value = strtok(NULL, "="); // get value |
196 | } |
- | 197 | fputs_("\r\n", fp); // newline at the end of file |
|
158 | if ((name != NULL) && (value != NULL)) // if '=' has been found |
198 | fclose_(fp); |
159 | { |
199 | SerialPutString("Default settings file created!"); |
160 | Settings_SetParameterFromString(name, value); |
200 | return; |
- | 201 | } |
|
161 | } |
202 | } |
Line 162... | Line 203... | ||
162 | } |
203 | else // no acces to fat 16 filesystem |
163 | } |
204 | { |