#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Dim SD As SD
Dim GPIO4 = 4 As Int
Dim append = True As Boolean
Dim overwrite = False As Boolean
Dim BC As ByteConverter
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
SD.Initialize(GPIO4)
Log("SD initialised")
Log("Test SDwrite..................")
For a = 1 To 5
Log("---------------- test ", a, " ------------------------")
SDlist
Log("Writing to file")
SDwrite("NEW1.TXT", "testdata",append)
Next
Log("Test App finnishes ...")
End Sub
Sub SDlist
Log("SDlist here")
Dim a As Int
a= a+1
For Each f As File In SD.ListFiles("/")
Log(a, " Name: ", f.Name, ", Size: ", f.Size)
Next
End Sub
Sub SDwrite(fname As String, data As String, Append_flag As Boolean)
Dim buffer() As Byte
Log("SDwrite here")
Log(" - file - ", fname)
Log(" - data=", data)
Log(" - Append = ", Append_flag)
Dim OK As Boolean
Log(" - Test exists file ")
' -----------------------------------------------
' The following line causes NodeMCU to crash !!!!
'''''' OK = SD.Exists(fname)
'''''' report(OK)
'now open the file, append a line of text and then close it ..
OK = SD.OpenReadWrite(fname)
report(OK)
If Append_flag Then SD.Position = SD.CurrentFile .Size Else SD.Position=0
buffer = BC.StringToBytes(data)
SD.Stream.WriteBytes(buffer,0,buffer.Length)
buffer = BC.StringToBytes(CRLF)
SD.Stream.WriteBytes(buffer,0,buffer.Length)
Log("closing file")
SD.Close
End Sub
Sub report(value As Boolean)
If value Then Log("REPORT:Success") Else Log("REPORT:FAILED!")
End Sub