Yanis Jossart
Member
Hello, I have a bug with the teensy 4.1 I'm using. It always freezes when it has to close a file.
Here is the code that I use, it's only a part of it, it worked well and I don't know why but it just stopped working. I changed sd card and the sd is not the problem. I freezes when it has to close the file. Does anyone have any idea on what could be happening? Thank very much in advance. what is very very strange is that it perfectly works whenever I totally isolate the code from the main one. Even with all the main libraries imported. I'm also using teensythreads. Is it possible that it causes some issues?
C++:
void ChangeSettings(int newSpeed, int newPower) {
dbSerialPrintln(newSpeed);
dbSerialPrintln(newPower);
// Initialize the SD card
if (!sd.begin(SD_CONFIG)) {
dbSerialPrintln("Sd couldnt be initialized");
return;
}
for(int i=0;i<100;i++)
{
char settingsFileName[10];
sprintf(settingsFileName, "%d.nc", i); // Create the filename as "0.nc", "1.nc", "2.nc", etc.
dbSerialPrintln(settingsFileName);
ChangeSettingsFile(settingsFileName, newSpeed, newPower);
}
char defaultFileName[] = "sd_default.nc";
ChangeSettingsFile(defaultFileName, newSpeed, newPower);
progressBar.setValue(100);
dbSerialPrintln("G-code modification complete.");
}
void ChangeSettingsFile(char settingsFileName[], int newSpeed, int newPower){
settingsFile = sd.open(settingsFileName, FILE_READ);
if (!settingsFile) {
dbSerialPrintln("Error opening the G-code file.");
return;
}
int lineCount = 0;
String fileContent = "";
while (settingsFile.available()) {
String line = settingsFile.readStringUntil('\n');
if (line.indexOf("F") >= 0 && lineCount != 0) {
line = modifySpeed(line, newSpeed);
dbSerialPrintln("Modifying speed with line : ");
dbSerialPrintln(line);
}
if (line.indexOf("S") >= 0 && lineCount != 0) {
line = modifyPower(line, newPower);
dbSerialPrintln("Modifying power with line : ");
dbSerialPrintln(line);
}
fileContent += line + "\n";
lineCount++;
}
dbSerialPrintln("closing the file");
settingsFile.close();
delay(50);
//sd.remove(settingsFileName);
// if (!sd.exists(settingsFileName)) {
// dbSerialPrintln("File does not exist, attempting to create it.");
// }
dbSerialPrintln("test");
settingsFile = sd.open(settingsFileName, O_WRITE | O_CREAT | O_TRUNC);
dbSerialPrintln("passed");
if (!settingsFile) {
dbSerialPrintln("Error opening the file for writing.");
return;
}
settingsFile.print(fileContent);
settingsFile.close();
uint32_t progressBarValue = 0;
progressBar.getValue(&progressBarValue);
int progressBarValueInt = (int)progressBarValue;
progressBar.setValue(min(progressBarValueInt+1,100));
}
// Function to modify the laser speed (F value)
String modifySpeed(String line, int newSpeed) {
String newLine = line;
int index = line.indexOf("F");
if (index >= 0) {
newLine = line.substring(0, index + 1) + String(newSpeed); // Replace the speed value
}
return newLine;
}
// Function to modify the laser power (S value)
String modifyPower(String line, int newPower) {
String newLine = line;
int index = line.indexOf("S");
if (index >= 0) {
newLine = line.substring(0, index + 1) + String(newPower); // Replace the power value
}
return newLine;
}
Here is the code that I use, it's only a part of it, it worked well and I don't know why but it just stopped working. I changed sd card and the sd is not the problem. I freezes when it has to close the file. Does anyone have any idea on what could be happening? Thank very much in advance. what is very very strange is that it perfectly works whenever I totally isolate the code from the main one. Even with all the main libraries imported. I'm also using teensythreads. Is it possible that it causes some issues?
Last edited: