' =====================================
'             SIMULATOR
' =====================================

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

' -------------------------------------
Load Vars
' ------------------------------------- Not affected by Load Vars

'----------------------------------------
'                   Values in CPM
'----------------------------------------
'A B C D single        25 .. 50
'AB,AC,AD,BC,BD,CD   0.25 .. 10
'ABC,ABD,ACD,BCD        2 ... 4
'ABCD                 0.5 ... 5
'----------------------------------------

Numeric Probability_Single    = 30
Numeric Probability_Double    = 5
Numeric Probability_Triple    = 3
Numeric Probability_Quadruple = 2

Numeric SoundFrequency = 1000
Numeric SoundMillisec = 10
Numeric FilesWithHeader = 1
Numeric FirstOutputSlot = 1
'
Array3 = 1,10,30,60,120,180,300,600  ' Log time Sec
String Header = "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 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
UpdateButtons
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 SymEnabled
Label TestOnOff
	If SymEnabled <> 0
		SymEnabled = 0
	Else
		SymEnabled = 1
	EndIf
	UpdateButtons
Return

Label LogTimeSec
	Numeric Index3
	Index3 = Mod(Index3 + 1, ArrayLength(3))
	UpdateButtons
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 SymEnabled > 0
		Button TestOnOff Text "SYM enabled"
		Button TestOnOff Color Orange
	Else
		Button TestOnOff Text "SYM disabled"
		Button TestOnOff Color 200,250,150
	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 LogTimeSec Text "LogTime " + Array3(Index3) + " Sec"
Return

Label ResetCounters
	For v1 = 0 To 14
		Array8(v1) = 0
		Slot(FirstOutputSlot + v1) = 0
	Next
	ResetLabels
Return

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

' -----------------------------------------------
'  LOOP
' -----------------------------------------------
Label Loop
	Numeric UpdateTimer
	String ReceivedLine
	If SymEnabled > 0
		For v1 = 0 To 3
			SymPulse(v1)(Probability_Single)
		Next
		For v1 = 4 To 9
			SymPulse(v1)(Probability_Double)
		Next
		For v1 = 10 To 13
			SymPulse(v1)(Probability_Triple)
		Next
		For v1 = 14 To 14
			SymPulse(v1)(Probability_Quadruple)
		Next
	EndIf
	' ----------------------------------------- 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.085 ' 0.085 instead of 0.1 to get about 10 Hz
Goto Loop

' -----------------------------------------------
'  PULSE SIMULATION - Probability in CPM
'  Probability is divided by 60 (CPM)
'   and further divided by 10 (loop frequency)
' -----------------------------------------------
Label SymPulse
	Numeric SymPulseIndex
	Numeric SymPulseProbability
	If SymPulseProbability / 600 > Rnd
		Array8(SymPulseIndex) = Mod(Array8(SymPulseIndex) + 1, 65536)
		Slot(FirstOutputSlot + SymPulseIndex) = Array8(SymPulseIndex)
		UpdateTimer = 3
	EndIf
Return

' -----------------------------------------------
'  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

' -----------------------------------------------
'  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 22 9 16 9 194 2 197 5 200 5 203 5 236 9 230 9 241 8 234 9 173 10 

'WATCH dummy
