SD config and data save
Posted: Wed Oct 04, 2023 8:41 am
Hello,
I'm the newby so excuse my too simple questions please, but still please help with them.
I need to open config file from SD and then write data to another file at SD.
I'm search at google and find pretty powerful sample but because of lack of experience at this area I just can't understand of how exactly does it work?? What exactly each stroke of this code are doing?! Why it have some "F()" operator? Why function "const __FlashStringHelper" multiply by text key?! Such monstrously intricate code is just confuse me!
https://arduinogetstarted.com/tutorials ... om-sd-card
Is it possible at Arduino IDE assign values from config file to firmware variables much easier, logically like:
Can you help with a such code, please!
And about data logging to SD is it possible to write data to one file all the time continuously and time-to-time as single action write data to another new files? For example all the time write "data_logging.txt" and once per 1min write "data_summary_DATATIME.txt" simultaneously without files reopening or it still require to open-close-open-write-reopen-write etc each file on each writing?? (OMG)
I'm the newby so excuse my too simple questions please, but still please help with them.
I need to open config file from SD and then write data to another file at SD.
I'm search at google and find pretty powerful sample but because of lack of experience at this area I just can't understand of how exactly does it work?? What exactly each stroke of this code are doing?! Why it have some "F()" operator? Why function "const __FlashStringHelper" multiply by text key?! Such monstrously intricate code is just confuse me!
https://arduinogetstarted.com/tutorials ... om-sd-card
Code: Select all
myInt_1 = SD_findInt(F("myInt_1"));
int SD_findInt(const __FlashStringHelper * key) {
char value_string[VALUE_MAX_LENGTH];
int value_length = SD_findKey(key, value_string);
return HELPER_ascii2Int(value_string, value_length);
}
int SD_findKey(const __FlashStringHelper * key, char * value) {
File configFile = SD.open(FILE_NAME);
if (!configFile) {
Serial.print(F("SD Card: error on opening file "));
Serial.println(FILE_NAME);
return;
}
char key_string[KEY_MAX_LENGTH];
char SD_buffer[KEY_MAX_LENGTH + VALUE_MAX_LENGTH + 1]; // 1 is = character
int key_length = 0;
int value_length = 0;
// Flash string to string
PGM_P keyPoiter;
keyPoiter = reinterpret_cast<PGM_P>(key);
byte ch;
do {
ch = pgm_read_byte(keyPoiter++);
if (ch != 0)
key_string[key_length++] = ch;
} while (ch != 0);
// check line by line
while (configFile.available()) {
int buffer_length = configFile.readBytesUntil('\n', SD_buffer, 100);
if (SD_buffer[buffer_length - 1] == '\r')
buffer_length--; // trim the \r
if (buffer_length > (key_length + 1)) { // 1 is = character
if (memcmp(SD_buffer, key_string, key_length) == 0) { // equal
if (SD_buffer[key_length] == '=') {
value_length = buffer_length - key_length - 1;
memcpy(value, SD_buffer + key_length + 1, value_length);
break;
}
}
}
}
configFile.close(); // close the file
return value_length;
}
Code: Select all
for ( int i = 1; while configFile line i exist ; 1) {
var_name = read text before symbol "=" from configFile line i;
var(var_name) = read text after symbol "=" from configFile line i;
}
And about data logging to SD is it possible to write data to one file all the time continuously and time-to-time as single action write data to another new files? For example all the time write "data_logging.txt" and once per 1min write "data_summary_DATATIME.txt" simultaneously without files reopening or it still require to open-close-open-write-reopen-write etc each file on each writing?? (OMG)