- Fri Jan 14, 2022 1:33 pm
#93360
Just a little update, this is the process I'm using to read the language configuration file; I think this is necessary because when YOU READ or WRITE the "language", in your own language, it is read and written differently ... you know, have you tried to "READ" a word or phrase using the "accent" of another language, sounds weird right?
I know that this code needs many improvements, that's why I put it here, waiting for your suggestions, corrections, critics and improvements.
Code: Select all#include <SD.h>
#include <SPI.h>
//-----------------------------------------------------------------------
// This portion of code is only for testing
// using the messages through the serial port.
#define debugprogram 1 // 1: Send messages to serial port
// 0: Disable serial messaging
#if debugprogram == 1
#define PRINT(x) Serial.print(x)
#define PRINTLN(x) Serial.println(x)
#else
#define PRINT(x)
#define PRINTLN(x)
#endif
//-----------------------------------------------------------------------
File root; // Needed to use the SD library
int tasks = 0; // Multipurpose variable to control the "ReadFile"
// function and other aspects of module programming
int FileLines = 0; // variable that stores the number of lines in a file
String chosenlanguage; // Variable that stores the selected language
//------------------------[ void ReadFile ]------------------------------
// General purpose function, which encompasses
// everything related to reading and writing files
//-----------------------------------------------------------------------
void ReadFile( String FileName, int ReadFileOpcion ) {
// FileName: Function target file name
// ReadFileOption: option to use only one function and simplify programming
root = SD.open( "/" + FileName + ".txt", FILE_READ );
int LineNumRead = 1;
bool found = false; //
if ( root.available() > 0 ) {
PRINTLN ( "File: \" " + FileName + " \" exist, accessing ...\n" );
while (root.available() > 0 ) {
switch ( ReadFileOpcion ) {
case 1: { // This function only serves to "count" the lines in the desired file
String FL = root.readStringUntil( '\n' ); // Read the selected file "line-by-line" using line feed as limit
LineNumRead += 1; // Every time it reaches the end of a line, we increase the value of "lines read" counter.
}
FileLines = LineNumRead - 1;
break;
case 2: {
String FL = root.readStringUntil( '\n' ); // Read the selected file "line-by-line" using line feed as limit
int init = FL.indexOf('-');
String lang1 = FL.substring( 0, init ); // Get the content of the entire line
PRINTLN("Actual line: " + String(LineNumRead));
if ( lang1 == chosenlanguage ) {
found = true; //
PRINTLN("- CODE: " + chosenlanguage + ", exist in line: " + LineNumRead + ", inside of file: " + FileName);
PRINTLN("- The languages translated for this code are as follows:");
while ( found == true ) { //init > 0 ){
int last = FL.indexOf( ',',init + 1 );
if ( last < 0){ break; } // Force to etect if there are no more separators, and we exit the process
String lang2 = FL.substring( init+1, last ); // Detect the following subString within the current reading
// line and pass it to the serial port (or for any other use,
// for example: enter these text strings in a menu).
PRINTLN ( " * " + lang2 );
init = last; // We assign the next "initial" position, the "end" of the previous one,
// in this way we can reuse the code without adding anything new
}
}
}
LineNumRead += 1;
}
if ( found == true ) { // If the language CODE is found, please stop reading / searching, so we make this process
// faster and we don't waste time reading the rest of the file
PRINTLN("CODE language found!");
break;
}
}
if ( found != true && ReadFileOpcion == 2){ // If we read the "languages" file and we do not find the CODE, we launch an alert
PRINTLN("- The selected CODE language: " + chosenlanguage + " is not present in file: " + FileName);
}
} else { // If the "languages" file is not found, we launch an alert, this can be used later to "download" this file from the internet cloud
PRINTLN("The file cannot be read or does not exist\n");
}
LineNumRead = 1;
root.close(); // Force to close any open file, in this case "root"
}
void setup(){
Serial.begin(112500); // Initialize the serial port
SD.begin(5); // Initialize the SD module
chosenlanguage = "en"; // We select the default language
tasks = 1;
}
// We use the variable "tasks", to tell the program where to start
void loop(){
switch (tasks) {
case 1:
ReadFile("languages",1); // Count the lines present in the file "languages.txt"
ReadFile("languages",2); // Find the language CODE in the file, and load the translated languages for it
tasks += 1;
break;
}
}
Language File:
After running the program on the ESP32:
I'm trying not to use "Strings" and use "string" objects, but I have to learn its correct handling better, my programming knowledge is all self-learning, so if your eyes burn when you see my way of programming, I'm sorry
.
I have tried to use the BBCODE [spoiler] [/spoiler], but it is disabled in the forum?