#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private bc As ByteConverter
' Private astream As AsyncStreams
Private lfs As LittleFS
' Private pinbutton As Pin
Private task0 As Boolean = False
Private task1 As Boolean = False
Private task2 As Boolean = False
Private task3 As Boolean = False
Private task4 As Boolean = False
Private task5 As Boolean = False
Private task6 As Boolean = False
Dim str(10) As String
Dim LF() As Byte = Array As Byte(10)
str(0) = "Hello "
str(1) ="World "
str(2) = "I am "
str(3) = "from "
str(4) = "France "
str(5) = "and "
str(6) = "i like "
str(7) ="B4X "
str(8) ="LittleFS "
str(9) ="exemple "
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
'example of connecting to a local network
If wifi.Connect2("Livebox-7b06", "7FC5C5CF1EA762DFE469A36349") Then
Log("Connected to network")
CallSubPlus("ListCleanWrite_FS",5000,0)
Else
Log("Failed to connect to network")
End If
End Sub
Sub ListCleanWrite_FS(tag As Byte)
If Not(lfs.Initialize) Then
Log("LITTLEFS Mount Failed")
Else
Log("LITTLEFS Mount OK")
Log("***ListDIR:")
listdir("/",7)
Log("end listdir"):Log(" ")
clean
Log("end clean"):Log(" ")
write_1
Log("end write_1"):Log(" ")
Log("***ListDIR:")
listdir("/",5)
Log("end phase 1.0"):Log(" ")
End If
task0 = True
CallSubPlus("ReadRemoveList_FS",10000,0)
End Sub
Sub ReadRemoveList_FS(tag As Byte)
read_1
Log("end read 1"):Log(" ")
remove_1
Log("end remove 1"):Log(" ")
Log("***ListDIR:")
listdir("/",5)
Log("end phase 1.1"):Log(" ")
task1 = True
CallSubPlus("MkdirWriteListRemoveRmdirList_FS",10000,0)
End Sub
Sub MkdirWriteListRemoveRmdirList_FS(tag As Byte)
createDIR_2
write_2
listdir("/", 1)
Log("end phase 2.1"):Log(" ")
remove_2
removeDIR_2
Log("***ListDIR:")
listdir("/", 1)
Log("end phase 2.2"):Log(" ")
task2 = True
CallSubPlus("WriteReadRenameReadRemove_FS",10000,0)
End Sub
Sub WriteReadRenameReadRemove_FS(tag As Byte)
write_3
Log("end write_3")
append_3
Log("end append_3")
read_3
Log("end read_3")
rename_3
Log("end rename_3")
read2_3
Log("end read2_3")
remove_3
Log("***ListDIR:")
listdir("/",3)
Log("end remove_3")
Log("end phase 3"):Log(" ")
task3=True
CallSubPlus("WriteUlongReadUlong_FS",10000,0)
End Sub
Sub WriteUlongReadUlong_FS(tag As Byte)
Log("test Write Ulong in a file")
WriteUlong
ReadUlong
Log("end write 10000 Ulong")
task4=True
CallSubPlus("WriteStringReadString_FS",10000,0)
End Sub
Sub WriteStringReadString_FS(tag As Byte)
Log("test Read/Check String in a file")
lfs.Remove("/StringFile.txt")
writeString
readString
Log("end read/check text sentences")
task5=True
CallSubPlus("End_FS",10000,0)
End Sub
Sub End_FS(tag As Byte)
Log("Test completed" )
Private task1 As Boolean = False
Private task2 As Boolean = False
Private task3 As Boolean = False
Private task4 As Boolean = False
Private task5 As Boolean = False
Private task6 As Boolean = False
End Sub
Sub clean
remove_1
remove_2
removeDIR_2
remove_3
lfs.Remove("/new1/new2/new3")
lfs.RMDir("/new1/new2")
' lfs.Format
End Sub
Sub write_1
lfs.OpenReadWrite("/new1/new2/new3/hello3.txt")
Dim txt() As Byte = "Hello3 a tous"
lfs.Stream.WriteBytes(txt,0,txt.Length)
lfs.close
Log("file hello3.txt created")
End Sub
Sub read_1
If lfs.Exists("/new1/new2/new3/hello3.txt") Then
lfs.OpenRead("/new1/new2/new3/hello3.txt")
Dim txt(lfs.CurrentFile.Size) As Byte
lfs.Stream.ReadBytes(txt,0,lfs.CurrentFile.Size)
lfs.close
Log("file content : ",txt)
Else
Log(" file /new1/new2/new3/hello3.txt not created")
End If
End Sub
Sub remove_1
If lfs.Exists("/new1/new2/new3/hello3.txt") Then
lfs.Remove("/new1/new2/new3/hello3.txt")
Log("file /new1/new2/new3/hello3.txt removed")
Else
Log("file /new1/new2/new3/hello3.txt don't exist")
End If
End Sub
Sub createDIR_2
If Not(lfs.Exists("/mydir")) Then
lfs.MKDir("/mydir")
Else
Log("directory /mydir already exists")
End If
End Sub
Sub write_2
lfs.OpenReadWrite("/mydir/hello2.txt")
Dim txt() As Byte = "Hello2"
lfs.Stream.WriteBytes(txt,0,txt.Length)
lfs.close
Log("directory mydir created, file hello3.txt created, file content : ",txt)
End Sub
Sub remove_2
If lfs.Exists("/mydir/hello2.txt") Then
lfs.Remove("/mydir/hello2.txt")
Log("/mydir/hello2.txt removed")
Else
Log("file /mydir/hello2.txt don't exist")
End If
End Sub
Sub removeDIR_2
If lfs.exists("/mydir") Then
lfs.RMDir("/mydir")
Log("directory mydir removed removed")
Else
Log("directory mydir don't exist")
End If
End Sub
Sub write_3
lfs.OpenReadWrite("/hello.txt")
Dim txt() As Byte =JoinStrings(Array As String("Hello! ",CRLF))
lfs.Stream.WriteBytes(txt,0,txt.Length)
lfs.Close
End Sub
Sub append_3
lfs.OpenAppend("/hello.txt")
Dim txt() As Byte =JoinStrings(Array As String("World!",CRLF))
lfs.Stream.WriteBytes(txt,0,txt.Length)
lfs.Close
End Sub
Sub read_3
If lfs.Exists("/hello.txt") Then
lfs.OpenRead("/hello.txt")
Dim txt(lfs.CurrentFile.Size) As Byte
lfs.Stream.ReadBytes(txt,0,lfs.CurrentFile.Size)
lfs.close
Log("text after append : ",txt)
Else
Log("file /hello.txt not created")
End If
End Sub
Sub rename_3
If lfs.Exists("/hello.txt") Then
lfs.Rename("/hello.txt", "/foo.txt")
Else
Log("file /hello.txt not created before rename")
End If
End Sub
Sub read2_3
If lfs.Exists("/foo.txt") Then
lfs.OpenRead("/foo.txt")
Dim txt(lfs.CurrentFile.Size) As Byte
lfs.Stream.ReadBytes(txt,0,lfs.CurrentFile.Size)
lfs.close
Log("text after rename : ",txt)
Else
Log("file /foo.txt not created before rename")
End If
End Sub
Sub remove_3
If lfs.Exists("/foo.txt") Then
lfs.Remove("/foo.txt")
Else
Log("file /foo.txt not created before remove")
End If
End Sub
Sub listdir(path As String, nblevel As Byte)
' Log("stack: ",StackBufferUsage)
If nblevel > 6 Then nblevel = 6
Dim ppath() As Byte
Dim level() As Byte = DIRlevel(bc.StringToBytes(path))
For Each f As File In lfs.ListFiles(path)
If f.isdirectory Then
If path <> "/" Then
ppath = JoinBytes(Array(path.getbytes,"/".getbytes,f.name.getbytes))
Else
ppath = JoinBytes(Array(path.getbytes,f.name.getbytes))
End If
Log(level,"Dir : ",ppath)
If nblevel-1>0 Then
If nblevel > 1 Then listdir(bc.stringfrombytes(ppath),nblevel-1)
End If
Else
Log(level,"Name: ",f.name," - Size: ",f.size)
End If
Next
End Sub
Sub DIRlevel(lpath() As Byte) As Byte()
Dim level As Byte = 0
If lpath.Length >1 Then lpath = JoinBytes(Array(lpath,"/".getbytes))
For Each f As Byte In bc.substring(lpath,1)
If f = 47 Then level = level +1
Next
Dim tmp() As Byte = "- - - - - - - -"
Return bc.SubString2(tmp,0,(level*4)+1)
End Sub
Sub WriteUlong
Log("test writebytes Ulong 0<10000")
If lfs.OpenReadWrite("/Ulongfile.txt") Then
Dim i As ULong
For i = 0 To 10000
writefile(i)
Next
lfs.Close
Else
Log("error open /Ulongsfile.txt")
End If
End Sub
Sub writefile(nb As ULong)
lfs.stream.WriteBytes( bc.ULongsToBytes(Array As ULong(nb)),0,4)
End Sub
Sub ReadUlong
Log("test readbytes Ulong 0<10000")
If lfs.Exists("/Ulongfile.txt") Then
lfs.OpenRead("/Ulongfile.txt")
Dim i As ULong
For i = 0 To 10000
readfile(i)
Next
lfs.Close
End If
End Sub
Sub readfile(nb As ULong)
Dim buff(4) As Byte : Dim j As UInt
j =lfs.stream.readBytes(buff,0,4)
If j = 0 Then
Log("error readbytes ", nb)
End If
If bc.ULongsfromBytes(buff)(0) <> nb Then Log("wrong value at read ",nb)
End Sub
Sub writeString
Log("test writeString 100 sentences")
If lfs.OpenReadWrite("/StringFile.txt") Then
Log(StackBufferUsage)
RndSeed(Millis)
For i = 0 To 100
writestring2(i)
Next
Log(StackBufferUsage)
lfs.Close
Else
Log("error open /StringFile.txt")
End If
End Sub
Sub writestring2(nb As Byte)
Dim stra() As Byte= JoinStrings(Array As String(nb," ",str(Rnd(0,10)),str(Rnd(0,10)),str(Rnd(0,10)),str(Rnd(0,10)),str(Rnd(0,10)),str(Rnd(0,10)),str(Rnd(0,10)),".",CRLF))
lfs.stream.WriteBytes(stra,0,stra.Length)
End Sub
Sub readString
Log("test readStrings 100 sentences")
Dim buff(256) As Byte : Dim Rbuff As UInt:Dim Lbuff As UInt:Dim NBstay As UInt = 0
If lfs.Exists("/StringFile.txt") Then
lfs.OpenRead("/StringFile.txt")
Log("start - bytesavailable",lfs.Stream.BytesAvailable)
Do While lfs.Stream.BytesAvailable > 0
Log(" "):Log(" bytesavailable ",lfs.Stream.BytesAvailable)
Rbuff =NBstay+ lfs.Stream.ReadBytes(buff,NBstay,Min(256-NBstay,lfs.Stream.BytesAvailable))
If Rbuff = 256 Then
Lbuff = bc.LastIndexOf(buff,CRLF)
Else
Lbuff = Rbuff-2
End If
Log(" bytesavailable",lfs.Stream.BytesAvailable)
NBstay = Rbuff-Lbuff-2
Log("lbuff: ",Lbuff, " Rbuff: ",Rbuff," NBstay: ",NBstay):Log(" ")
readString2(bc.SubString2(buff,0,Lbuff+2))
If NBstay >0 Then
bc.ArrayCopy2(buff,Lbuff+2,buff,0,NBstay)
End If
Loop 'Next
lfs.Close
Else
Log("error open /StringFile.txt")
End If
End Sub
Sub readString2(buff() As Byte)
For Each s() As Byte In bc.Split(buff, LF)
If s.Length <> 0 Then
Log(s)
End If
Next
End Sub