65,7 → 65,7 |
typedef struct |
{ |
ParamId_t ParamId; |
s8 Name[16]; |
s8 Name[17]; // 0 terminator is the last byte |
u8 Group; // 0 = reserved 1,2,3,4 |
u16 Value; |
u16 Default; |
95,12 → 95,14 |
|
//---------------------------------------------------------------------------------- |
// set parameter from string based name and value |
void Settings_SetParameterFromString(s8 *name, s8 *value) |
u8 Settings_SetParameterFromString(s8 *name, s8 *value) |
{ |
u8 i; |
u8 i, retval = 0; |
s8 string[] = " \0"; // null terminated string of 16 characters |
s8 text[32]; |
s8 text[64]; |
|
if((name == NULL) || (value == NULL)) return(retval); |
|
i = strlen(name); // lenght of the parameter name |
if (i > 16) i = 16; // cut off at 16 |
|
110,18 → 112,26 |
// search name in parameter list |
for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++) |
{ |
//sprintf(text,"#%s#\n\r", CFG_Parameter[i].Name); SerialPutString(text); |
if(strncmp(string, CFG_Parameter[i].Name, 16) == 0) // names are matching |
{ |
CFG_Parameter[i].Value = (u16)atoi(value); // convert string to number and set value |
// range within min/max |
if(CFG_Parameter[i].Value < CFG_Parameter[i].Min) CFG_Parameter[i].Value = CFG_Parameter[i].Min; |
|
sprintf(text,"\r\n%s = %d", string, CFG_Parameter[i].Value); |
SerialPutString(text); |
// out of range ? |
if((CFG_Parameter[i].Value < CFG_Parameter[i].Min) || (CFG_Parameter[i].Value > CFG_Parameter[i].Max)) |
{ // print a warning |
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); |
SerialPutString(text); |
CFG_Parameter[i].Value = CFG_Parameter[i].Default; // fallback to default |
} |
retval = 1; // value in range |
break; // end loop |
} |
} |
if(!retval) |
{ |
sprintf(text,"\r\n%s <-- unknown parameter\r\n", string); |
SerialPutString(text); |
} |
return(retval); |
} |
|
//---------------------------------------------------------------------------------- |
132,38 → 142,69 |
File_t *fp; |
s8 text[LINE_MAX]; |
s8 *name, *value; |
u8 i; |
|
SerialPutString("\n\rSettings init..."); |
Settings_SetDefaultValues(); |
SerialPutString("\n\rReading settings from file..."); |
fp = fopen_("settings.ini", 'r'); |
|
if (fp == NULL) // could not open the file |
{ |
SerialPutString("ERROR: File not found !"); |
return; |
} |
// read all lines from file |
while(fgets_(text, LINE_MAX, fp) != 0) |
{ |
if ( // ignorelines starting with \r,\n,' ',';','#' |
(text[0] != '\n') && |
(text[0] != '\r') && |
(text[0] != ' ' ) && |
(text[0] != ';' ) && |
(text[0] != '#' ) |
) |
{ |
name = strtok(text, "="); // get name |
value = strtok(NULL, "="); // get value |
if ((name != NULL) && (value != NULL)) // if '=' has been found |
|
if(Fat16_IsValid()) |
{ // check if settings file is existing |
if(fexist_("settings.ini")) |
{ |
fp = fopen_("settings.ini", 'r'); // try to open the file |
if (fp == NULL) // could not open the file |
{ |
Settings_SetParameterFromString(name, value); |
SerialPutString("ERROR: Opening settings file!"); |
return; |
} |
// read all lines from file |
while(fgets_(text, LINE_MAX, fp) != 0) |
{ |
if ( // ignorelines starting with \r,\n,' ',';','#' |
(text[0] != '\n') && |
(text[0] != '\r') && |
(text[0] != ' ' ) && |
(text[0] != ';' ) && |
(text[0] != '#' ) |
) |
{ |
name = strtok(text, "="); // get name |
value = strtok(NULL, "="); // get value |
if ((name != NULL) && (value != NULL)) |
{ |
Settings_SetParameterFromString(name, value); |
} |
} |
} |
fclose_(fp); |
SerialPutString("ok"); |
return; |
} |
else // settings file does not exist |
{ // create default settings file |
fp = fopen_("settings.ini", 'w'); // try to open the file |
if(fp == NULL) |
{ |
SerialPutString("ERROR: Creating default settings file!"); |
return; |
} |
// run thrue all parameters and set value to default |
for (i = 0; i < sizeof(CFG_Parameter) / sizeof(Parameter_t); i++) |
{ |
sprintf(text, "%s = %d\r\n", CFG_Parameter[i].Name, CFG_Parameter[i].Default); |
fputs_(text, fp); // write to file |
} |
fputs_("\r\n", fp); // newline at the end of file |
fclose_(fp); |
SerialPutString("Default settings file created!"); |
return; |
} |
} |
|
fclose_(fp); |
SerialPutString("ok"); |
else // no acces to fat 16 filesystem |
{ |
SerialPutString("Using default values!"); |
return; |
} |
} |
|
|