B4R Question Little FS write 500 records in 1 second

tzfpg

Active Member
Licensed User
Longtime User
Hi,

I would like to ask is it possible if in 1 seconds, I want to write 500 records into Little FS?
I'm using WeMos Mini D1 R2 device
Below is my coding and I able to write 25 records only in 3 secords

B4X:
Sub Process_Globals
Dim start_status As Int = 0
Dim count As Long = 0
Private t1 As Timer
End Sub

Private Sub AppStart
Log("Initializing Filesystem...")
    If(lfs.Initialize())=False Then
        Log("Formatting Filesystem. This may take some time")
        lfs.Format
        If(lfs.Initialize())=True Then
            Log("Formatting succesful...")
        
        Else
            Log("Error formatting Filesystem...")
        End If
    End If
 
    Log("Total size: ", NumberFormat(lfs.TotalSize / 1024, 0, 0), " KB")
    Log("Used size: ", NumberFormat(lfs.UsedSize / 1024, 0, 0), " KB")
    data_default
   t1.Initialize("t1_Tick",3000)
    t1.Enabled=False
    AddLooper("Looper1")
End Sub

Sub data_default
    If lfs.Exists("/testpost") = False Then
        Dim Buffer() As Byte
        If lfs.OpenReadWrite("/testpost") = True Then
            lfs.Position = lfs.CurrentFile.Size
            Buffer = bc.StringToBytes(0)
            lfs.Stream.WriteBytes(Buffer,0,Buffer.Length)
            lfs.Stream.Flush
            lfs.Close
        End If
    End If
End Sub

Sub t1_Tick
      start_status=2
       t1.Enabled=False
       Dim f1 As String = ReadData("/testpost")
        Log("f1 ", f1)
        Dim file1 As String = ReadData("/test.txt")
        Log("file1 ", file1)
End Sub

Sub Looper1
    If start_status = 0 Then
        If lfs.Exists("/test.txt") Then
            lfs.Remove("/test.txt")
            Log("test.txt Deleted")
        End If
    
        start_status=1
        lfs.OpenReadWrite("/test.txt")
        t1.Enabled=True
    else if start_status = 1 Then
        count = count + 1
        SaveData1("/test.txt",count)
    else if start_status = 2 Then
        start_status = 99
        ListFiles
    End If
End Sub
 
Sub ReadData(data As String) As String
    Dim result As String = ""
    If lfs.OpenRead(data) = True Then
        Dim NByte As Int=lfs.Stream.BytesAvailable
        Dim Buffer(NByte) As Byte
        lfs.Stream.ReadBytes(Buffer, 0, NByte)
        lfs.Stream.Flush
        result = bc.StringFromBytes(Buffer)
        lfs.Close
    End If
    Return result
End Sub

private Sub SaveData1(filename As String,data As String)
    Dim s As String
    If data.Length = 1 Then
        s = JoinStrings(Array As String("00",data, CRLF))
    Else If data.Length = 2 Then
        s = JoinStrings(Array As String("0",data, CRLF))
    Else
        s = JoinStrings(Array As String(data, CRLF))
    End If

    Dim Buffer() As Byte
    Dim count1 As ULong
    If lfs.OpenReadWrite("/testpost") = True Then
        count1=ReadData("/testpost")
        lfs.Close
    End If
 
    If lfs.OpenReadWrite(filename) = True Then
        lfs.Position = count1
        Buffer = bc.StringToBytes(s)
        lfs.Stream.WriteBytes(Buffer,0,Buffer.Length)
        lfs.Stream.Flush
        lfs.Close
    End If
 
    count1=count1+5
    Dim lg As ULong =1800000
    If count1<=lg Then
        SaveData2(count1)
    Else
        count1=0
        SaveData2(0)
    End If
End Sub

private Sub SaveData2(count1 As ULong)
    Dim Buffer() As Byte
    lfs.Remove("/testpost")
    If lfs.OpenReadWrite("/testpost") = True Then
        lfs.Position = lfs.CurrentFile.Size
        Buffer = bc.StringToBytes(count1)
        lfs.Stream.WriteBytes(Buffer,0,Buffer.Length)
        lfs.Stream.Flush
        lfs.Close
    End If
End Sub

Is there any way to write 500 Records in 1 seconds
Thank for helping
 
Last edited:

emexes

Expert
Licensed User
I don't use B4R so perhaps this is irrelevant, but:

the bit where the testpost file is deleted, opened and closed EVERY time a record is written to it, might be part of the issue.

Or is it that lfs can only handle one open file at a time?

In which case, you might do better storing all your data in one file, that you open once, and use lfs.Position to write whichever bits need to be update (eg, count1?), and lfs.Stream.Flush after each update to make sure your data is safe on the file device.
 
Upvote 0

Num3

Active Member
Licensed User
Longtime User
Flash devices are not properly made for speed, but i guess you might reach it.
Do remember there is a finite write number of times, after that point the manufacturer does not offer any guaranties of data integrity
 
Upvote 0
Top