#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 raf As RandomAccessFile
Private LFS As LittleFS
Private Calibration_File As String = "/TouchCalData1"
Private Repeat_Cal As bool = False
Public TFT As TFT_eSPI
Public TFTid As Long
Private clBkg As UInt = TFT.TFT_BLACK
Private clButton As UInt = TFT.TFT_BLUE
Private clText As UInt = TFT.TFT_YELLOW
Public kwota(6) As Int = Array As Int(5,10,20,50,100,200)
' Public Btn(9) As ButtonWidget
Public div As ULong = 0
Public scantime As ULong = 0
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
' If wifi.Connect2("N_EXT", "Q7JT5FLVVMRM") Then
' Log("Connected to network")
' Else
' Log("Failed to connect to network")
' End If
TFTid = TFT.Initialize(-1,-1)
TFT.begin(0xFF)
TFT.fillScreen(clBkg)
touch_calibrate
LCDStartScreen
AddLooper("LocalLooper")
End Sub
Public Sub LocalLooper
TFT.setCursor(10,10)
TFT.setTextColor1(clText,clBkg,False)
TFT.setTextSize(2)
TFT.println(Millis/1000) 'visualization of a running loop
Dim xy(2) As UInt
Dim pressed As Byte = TFT.getTouch(xy,500)
If pressed=1 Then
Log(pressed,": ",xy(0)," / ",xy(1)," / ",TFT.getTouchRawZ)
End If
End Sub
Public Sub LCDStartScreen
TFT.fillScreen(clBkg)
TFT.setTextColor(clText)
TFT.setTextSize(1)
For x=0 To 2
For y=0 To 2
TFT.fillRect(x*105+5,y*105+160,100,100,clButton)
Next
Next
End Sub
Sub touch_calibrate
Dim calDatab(14) As Byte
Dim calData(7) As UInt
Dim calDataOK As bool = False
'// check file system exists
If (Not(LFS.Initialize)) Then
Log("Formating file system")
LFS.format
If Not(LFS.Initialize) Then
Do While True : Delay(100) : Loop
End If
End If
'// check If calibration file exists And size is correct
If (LFS.exists(Calibration_File)) Then
' Log("sprawdzanie kalibracji")
If (Repeat_Cal) Then
'// Delete If we want To re-calibrate
LFS.remove(Calibration_File)
Else
Dim f As bool = LFS.OpenRead(Calibration_File)
If (f=True) Then
If (LFS.Stream.ReadBytes(calDatab,0, 14) = 14) Then
setUInt(calDatab,calData)
calDataOK = True
End If
LFS.close
End If
End If
End If
If calDataOK And Not(Repeat_Cal) Then
'// calibration data valid
TFT.setTouch(calData)
Else
'// data Not valid so recalibrate
TFT.fillScreen(TFT.TFT_BLACK)
TFT.setCursor(20, 0)
TFT.setTextFont(2)
TFT.setTextSize(1)
TFT.setTextColor1(TFT.TFT_WHITE, TFT.TFT_BLACK,False) '????
TFT.println("Touch corners as indicated")
TFT.setTextFont(1)
TFT.println("")
If (Repeat_Cal) Then
TFT.setTextColor1(TFT.TFT_RED, TFT.TFT_BLACK,False)
TFT.println("Set REPEAT_CAL to false to stop this running again!")
End If
TFT.calibrateTouch(calData, TFT.TFT_MAGENTA, TFT.TFT_BLACK, 15)
TFT.setTextColor1(TFT.TFT_GREEN, TFT.TFT_BLACK,False)
TFT.println("Calibration complete!")
'// store data
Dim f As bool = LFS.OpenReadWrite(Calibration_File)
If (f=True) Then
' f.write((const unsigned char *)calData, 14)
setByte(calData,calDatab)
LFS.stream.WriteBytes(calDatab,0, 14)
LFS.close()
End If
End If
End Sub
Sub setByte(in() As UInt, out() As Byte)
For i = 0 To 6
out(i*2) = Bit.HighByte(in(i))
out(i*2+1) = Bit.LowByte(in(i))
Next
End Sub
Sub setUInt(in() As Byte, out() As UInt)
Dim b(2) As Byte
raf.Initialize(b,False)
For i = 0 To 6
raf.WriteByte(in(i*2),0)
raf.WriteByte(in(i*2+1),8)
out(i)=raf.ReadUInt16(0)
Next
End Sub