' =====================================
'         COSMIC RAY DETECTOR
' =====================================

' ------------------------------------- Affected by Load Vars

' -------------------------------------
Load Vars
' ------------------------------------- Not affected by Load Vars
Numeric SoundFrequency = 1000
Numeric SoundMillisec = 10
Numeric FilesWithHeader = 1
Numeric FirstOutputSlot = 1
'
Array1 = 0.5,1,2,5,10,20,50,100,200  ' Test frequency
Array2 = 0,12,20,50,100,200,500,1000 ' Coincidence time uS
Array3 = 1,10,30,60,120,180,300,600  ' Log time Sec
String Header = "DateTime;A;B:C;D;AB;AC;AD;BC;BD;CD;ABC;ABD;ACD;BCD;ABCD"
' -------------------------------------
'
' ------------------------------------- LOAD APPS
Load Theremino_SlotViewer.exe
Load Theremino_FilterFIR.exe
Load Theremino_Graphs.exe
'
' ------------------------------------- BUTTONS
Button 1 Label TestOnOff
Button 1 Color Gold
Button 1 Image0S Button3.png
Button 2 Label TestFrequency
Button 2 Color Gold
Button 2 Image0S Button3.png
Button 4 Label TimeUS
Button 4 Color Gold
Button 4 Image0S Button3.png
Button 6 Label "ResetCounters"
Button 6 Text "Reset counters"
Button 6 Color Gold
Button 6 Image0S Button3.png
Button 8 Label CtrA
Button 9 Label CtrB
Button 10 Label CtrC
Button 11 Label CtrD
Button 17 Label CtrAB
Button 18 Label CtrAC
Button 19 Label CtrAD
Button 20 Label CtrBC
Button 21 Label CtrBD
Button 22 Label CtrCD
Button 23 Label CtrABC
Button 24 Label CtrABD
Button 25 Label CtrACD
Button 26 Label CtrBCD
Button 27 Label CtrABCD
For v1 = 8 To 11
	Button v1 Color Gray9
	Button v1 Image0S Button2.png
Next
For v1 = 17 To 27
	Button v1 Color Gray9
	Button v1 Image0S Button1.png
Next
Button 13 Label "SoundOnOff"
Button 13 Color Gold
Button 13 Image0S Button3.png
Button 29 Label "LogOnOff"
Button 29 Color Gold
Button 29 Image0S Button3.png
Button 30 Label LogTimeSec
Button 30 Color Gold
Button 30 Image0S Button3.png
Controls SetBackColor LightGoldenrodYellow
' ----------------------------------------------- START ALL
Option Speed 9
OpenComPort
UpdateButtons
SendOptions
If LogEnabled <> 0
	InitLog
EndIf

ResetLabels
'ResetCounters
Loop
Stop

' -----------------------------------------------
'  EVENT STOP
' -----------------------------------------------
Label EventStop
	Save Vars
	For v1 = 8 To 11
		Button v1 Color Gray9
	Next
	For v1 = 17 To 27
		Button v1 Color Gray9
	Next
End

' -----------------------------------------------
'  BUTTONS
' -----------------------------------------------
Numeric TestEnabled
Numeric TestFreq
Label TestOnOff
	If TestEnabled <> 0
		TestEnabled = 0
	Else
		TestEnabled = 1
	EndIf
	UpdateButtons
	SendOptions
Return

Label TestFrequency
	Numeric Index1
	Index1 = Mod(Index1 + 1, ArrayLength(1))
	UpdateButtons
	SendOptions
Return

Label TimeUS
	Numeric Index2
	Index2 = Mod(Index2 + 1, ArrayLength(2))
	UpdateButtons
	SendOptions
Return

Label LogTimeSec
	Numeric Index3
	Index3 = Mod(Index3 + 1, ArrayLength(3))
	UpdateButtons
	SendOptions
Return

Numeric LogEnabled
Label LogOnOff
	If LogEnabled <> 0
		LogEnabled = 0
	Else
		LogEnabled = 1
		InitLog
	EndIf
	UpdateButtons
Return

Numeric SoundEnabled
Label SoundOnOff
	If SoundEnabled <> 0
		SoundEnabled = 0
	Else
		SoundEnabled = 1
	EndIf
	UpdateButtons
Return

Label UpdateButtons
	If TestEnabled > 0
		Button TestOnOff Text "Test enabled"
		Button TestOnOff Color Orange
		TestFreq = Array1(Index1)
	Else
		Button TestOnOff Text "Test disabled"
		Button TestOnOff Color 200,250,150
		TestFreq = 0
	EndIf
	If LogEnabled > 0
		Button LogOnOff Text "Log enabled"
		Button LogOnOff Color Orange
		LogSeconds = Array3(Index3)
	Else
		Button LogOnOff Text "Log disabled"
		Button LogOnOff Color 200,250,150
	EndIf
	If SoundEnabled > 0
		Button SoundOnOff Text "Sound enabled"
		Button SoundOnOff Color Orange
	Else
		Button SoundOnOff Text "Sound disabled"
		Button SoundOnOff Color 200,250,150
	EndIf
	Button TestFrequency Text "Test " + Array1(Index1) + " Hz"
	Button TimeUS Text "Time " + Array2(Index2) + " uS"
	Button LogTimeSec Text "LogTime " + Array3(Index3) + " Sec"
Return

Label ResetCounters
	COM WriteString "RESET" + CRLF
	COM DiscardInBuffer
Return

Label ResetLabels
	For v1 = 0 To 14
		Array9(v1) = Infinity
	Next
	UpdateLabels
	UpdateTimer = 3
Return

' -----------------------------------------------
'  SEND OPTIONS TO HARDWARE
' -----------------------------------------------
Label SendOptions
	String Options = "OPTIONS " + TestFreq + " " + Array2(Index2) + CRLF
	COM WriteString Options
Return

' -----------------------------------------------
'  LOOP
' -----------------------------------------------
Label Loop
	Numeric UpdateTimer
	Numeric Retry
	String ReceivedLine
	For Retry = 1 To Infinity
		S1 = COM_ReceiveUntil(CRLF)
		If s1 <> ""
			ReceivedLine = s1
		    'Print ReceivedLine + CRLF
			For v1 = 0 To 19
				Array8(v1) = GetSeparatedString(ReceivedLine, v1)
				Slot(FirstOutputSlot + v1) = Array8(v1)
			Next
			UpdateTimer = 3
		Else
			Exit
		EndIf
	Next
	' ----------------------------------------- Update labels
	If UpdateTimer = 3
		UpdateLabels
		If SoundEnabled = 1
			Beep SoundFrequency SoundMillisec
		EndIf
	EndIf
	' ----------------------------------------- Clear colors
	If UpdateTimer >= 0
		UpdateTimer = UpdateTimer - 1
		If UpdateTimer < 0
			For v1 = 7 To 11
				Button v1 Color Gray9
			Next
			For v1 = 17 To 27
				Button v1 Color Gray9
			Next
		EndIf
	EndIf
	' ----------------------------------------- Log
	If LogEnabled > 0
		ExecuteLog
	EndIf
	' ----------------------------------------- Loop time
	Wait Seconds 0.01
Goto Loop

' -----------------------------------------------
'  UPDATE LABELS
' -----------------------------------------------
Label UpdateLabels
	String LBL1 = ",    A ,    B ,    C ,    D ,"
	String LBL2 = ",AB ,AC ,AD ,BC ,BD ,CD "
	LBL2 = LBL2 + ", ABC , ABD , ACD , BCD , ABCD ,"
	Numeric UL1
	String txt
	For UL1 = 0 To 3
		If Array8(UL1) <> Array9(UL1)
			Button UL1+8 Color Coral
			txt = GetSeparatedString(LBL1, UL1+1, ",")
			Button UL1+8 Text txt + Array8(UL1)
		EndIf
	Next
	For UL1 = 4 To 14
		If Array8(UL1) <> Array9(UL1)
			Button UL1+13 Color Coral
			txt = GetSeparatedString(LBL2, UL1-3, ",")
			Button UL1+13 Text txt + Array8(UL1)
		EndIf
	Next
	For v1 = 0 To 14
		Array9(v1) = Array8(v1)
	Next
Return

' -----------------------------------------------
'  OPEN COM
' -----------------------------------------------
Label OpenComPort
	String  DeviceID = "CosmicRayDetector1"
	Numeric Bauds    = 1000000
	String  ComPort
	Numeric ComIndex
	Numeric ComRetry1
	Numeric ComRetry2
	For ComRetry1 = 1 To 10
		For ComIndex = 0 To GetSeparatedStringCount(COM_PortNames) - 1
			ComPort = GetSeparatedString(COM_PortNames, ComIndex)
			COM Open ComPort Bauds
			If COM_Status = "OPENED"
				For ComRetry2 = 1 To 3
					COM DiscardOutBuffer
					COM DiscardInBuffer
					COM WriteString "GetID" + CRLF
					Wait Seconds 0.01
					If COM_ReceiveUntil(CRLF) = DeviceID + CRLF
						ComIndex = -1
						Beep "440-1,0-80,880-1"
						Return
					EndIf
				Next
			EndIf
		Next
	Next
	If ComIndex >= 0
		Print "COM PORT NOT FOUND"
		TTS SelectVoice "ENGF"
		TTS Speak "Com port not found."
		TTS Speak "Please test connections and retry."
		Wait Not TTSspeaking
		End
	EndIf
Return

' -----------------------------------------------
'  LOG
' -----------------------------------------------
Label InitLog
	String LogFile = ".\LOGS\LOG_FILE.txt"
	String LogFileCopy = ".\LOGS\OLD_FILES\LOG_" + Format(Now, "yyyy-MM-dd_HH-mm-ss") + ".txt"
	If FileExists(LogFile)
		Save StringToFile GetTextFile(LogFile) LogFileCopy
		Save DeleteFile LogFile
	EndIf
	If FilesWithHeader = 1
		Save StringToFile Header LogFile
	EndIf
	Numeric LogSeconds
	Numeric LogOldTime = -Infinity
	Numeric LogNewTime
Return


Label ExecuteLog
	LogNewTime = ElapsedTime
	If LogNewTime - LogOldTime >= LogSeconds
		LogOldTime = LogNewTime
		' --------------------------------------- Prepare LogLine
		String LogLine = ""
		For v2 = 0 To 14
			LogLine = LogLine + GetSeparatedString(ReceivedLine, v2) + " "
		Next
		LogLine = Trim(LogLine)
		LogLine = Replace(LogLine, " ", ";")
		'LogLine = Format(Now, "yyyy/MM/dd HH:mm:ss.fff") + ";" + LogLine
		LogLine = Format(Now, "yyyy/MM/dd HH:mm:ss") + ";" + LogLine
		' --------------------------------------- Save LogLine
		Save StringToFile LogLine LogFile
	EndIf
Return


'BOOKMARKS 

'EDITEDLINES 196 1 223 2 346 4 340 4 3 5 2 6 1 7 333 9 17 10 

'WATCH dummy
