Private Sub SaveToLog As Boolean
Log("Wait..")
Dim millis1 As ULong = Millis
Dim j, i As UInt
For j = 1 To 3
Dim fn As String = GenerateFileName(j)
For i = 1 To 5000
Dim a() As Byte = GenerateFileLine(i)
WriteString(fn, a)
Next
Log(fn, " saved OK")
Next
Dim millis2 As ULong = Millis
Dim delta As Float = (millis2 - millis1)/1000
Log("Saved log file OK: ", NumberFormat(delta, 1, 3))
Return True
End Sub
Sub GenerateFileName(i As UInt) As String
Dim fn As String = JoinStrings(Array As String("/log_", NumberFormat(i,4,0), ".dat"))
Return fn
End Sub
Sub GenerateFileLine(i As UInt) As String
Dim a As String = JoinStrings(Array As String(NumberFormat(i,10,0), ";", "Column_", NumberFormat(i, 2, 0), CRLF))
Return a
End Sub
Sub WriteString(key_string() As Byte, value() As Byte)
Dim source As KeyValueType
source.key = key_string
source.value = value
RunNative("save_csv_line", source)
'Log("Saved to file")
End Sub
#if C
#include "FS.h"
#include "SD.h"
#include "SPI.h"
B4R::Object returnvalue_esp_sp;
void writeFile(fs::FS &fs, const char *path, const char *message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char *path, const char *message) {
//Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message)) {
//Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
B4R::Object* setup_sd(B4R::Object* o){
bool begun = SD.begin();
if (!begun) {
//Serial.println("Card Mount Failed");
return returnvalue_esp_sp.wrapNumber((byte)0);
}else{
return returnvalue_esp_sp.wrapNumber((byte)1);
}
}
void save_csv_line(B4R::Object* passedvar){
_keyvaluetype* tmp = (_keyvaluetype*)B4R::Object::toPointer(passedvar); //structure from B4R
B4R::Array* b = tmp->key;
char* c = (char*)b->data; //file name, do not forget slash "/file.txt"
//String key = (String)c;
B4R::Array* b2 = tmp->value;
char* c2 = (char*)b2->data; //test line for saving to file
//String value = (String)c2;
appendFile(SD, c, c2);
}
B4R::Object* freespace_sd(B4R::Object* o){
float result;
uint64_t total = SD.totalBytes() / (1024 * 1024);
Serial.printf("totalBytes: %llu MB\n", total);
uint64_t used = SD.usedBytes() / (1024 * 1024);
Serial.printf("usedBytes: %llu MB\n", used);
result = ((float)total - (float)used) / (float)total * 100;
return returnvalue_esp_sp.wrapNumber((float)result);
}
#End If