
' ====================================================================
'   Demo for sequence SELECT / EDIT / RUN
' ====================================================================

' --------------------------------------------------------------------
'  PLEASE SELECT THE SEQUENCE "TestSequence"
' --------------------------------------------------------------------

Option Speed 9
Option GlobalKeys Enabled

' -------------------------------------- Set slots
Variable Numeric Slot_LedR       = 1
Variable Numeric Slot_LedG       = 2
Variable Numeric Slot_LedB       = 3
Variable Numeric Slot_WhiteLight = 4

Gosub SetButtons

Gosub RestoreVarsAndPositions
Gosub ShowSequenceName

Stop						 										'

' ====================================================================
'  BUTTONS
' ====================================================================
Label SetButtons
	Button 2 Text EditSequence
	Button EditSequence Text "Edit sequence"
	Button EditSequence Color PaleGreen
	
	Button 3 Text RunSequence
	Button RunSequence Text "RUN SEQUENCE"
	Button RunSequence Color Aqua
	
	Button 4 Text SelectSequence
	Button SelectSequence Text "Select Sequence"
	Button SelectSequence Color PowderBlue
	
	Button 7 Text Red
	Button Red Color Red
	
	Button 8 Text Magenta
	Button Magenta Color Magenta
	
	Button 9 Text Yellow
	Button Yellow Color Yellow
	
	Button 10 Text Green
	Button Green Color Green
	
	Button 11 Text Blu
	Button Blu Color MediumBlue
	
	Button 12 Text White
	Button White Color Green
	
	Button 13 Text RgbOff
	Button RgbOff Color Black
	
	Button 15 Text WhiteLightToggle
	Button WhiteLightToggle Text "WhiteLight ON"
	Button WhiteLightToggle Color LightCyan
Return


' ====================================================================
'  ENABLE/DISABLE BUTTONS
' ====================================================================
Label EnableAllButtons
	For v1 = 1 To 32
		Button v1 Enabled
	Next
Return

Label DisableAllButtons
	For v1 = 1 To 32
		Button v1 Disabled
	Next
Return

' ====================================================================
'  CLOSING EVENTS
' ====================================================================
Label CloseAll
	Gosub SaveVarsAndPositions
Return

Label EventStop
	Gosub SaveVarsAndPositions
	Gosub EnableAllButtons
End

' ====================================================================
'  SAVE LOAD VARS
' ====================================================================
Label SaveVarsAndPositions
	S1 = SequenceName
	Save Vars
Return

Label RestoreVarsAndPositions
	Load Vars
	SequenceName = S1
Return


' ====================================================================
'  LED COLORS
' ====================================================================
Label Red
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 0
	Slot Slot_LedB = 0
Return

Label Magenta
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 0
	Slot Slot_LedB = 1000
Return

Label Yellow
	Slot Slot_LedR = 900
	Slot Slot_LedG = 400
	Slot Slot_LedB = 0
Return

Label Green
	Slot Slot_LedR = 0
	Slot Slot_LedG = 1000
	Slot Slot_LedB = 0
Return

Label Blu
	Slot Slot_LedR = 0
	Slot Slot_LedG = 0
	Slot Slot_LedB = 1000
Return

Label White
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 1000
	Slot Slot_LedB = 1000
Return

Label RgbOff
	Slot Slot_LedR = 0
	Slot Slot_LedG = 0
	Slot Slot_LedB = 0
Return

' =====================================================================
'  WHITE LIGHT
' =====================================================================
Label WhiteLightToggle
	If Slot(Slot_WhiteLight) = 0
		Gosub WhiteLightON
	Else
		Gosub WhiteLightOFF
	EndIf
Return

Label WhiteLightON
	Slot Slot_WhiteLight = 1000
	Button WhiteLightToggle Text "WhiteLight OFF"
	Button WhiteLightToggle Color PowderBlue
Return

Label WhiteLightOFF
	Slot Slot_WhiteLight = 0
	Button WhiteLightToggle Text "WhiteLight ON"
	Button WhiteLightToggle Color LightCyan
Return


' =====================================================================
'  SEQUENCES
' =====================================================================
Label InitSequencePaths
	Variable String EditorFolder
	Variable String EditorApp
	Variable String SequencePathAndName
	Variable String SequenceName
	'
	EditorFolder = ".\Apps\Theremino_Editor\"
	'
	EditorApp = EditorFolder + "Theremino_Editor.exe"
	'
	SequencePathAndName = EditorFolder + "Sequences\" + SequenceName
Return

Label EditSequence
	Gosub InitSequencePaths
	If FileExists(SequencePathAndName)
		Load EditorApp + " " + SequencePathAndName
	EndIf
Return

Label SelectSequence
	Gosub InitSequencePaths
	s1 = SelectFile(EditorFolder + "Sequences\")
	If s1 <> ""
		SequenceName = s1
		Gosub ShowSequenceName
		Gosub EditSequence
	EndIf
Return

Label ShowSequenceName
	Button SelectSequence Text "Sequence" + CRLF + SequenceName
Return

' ====================================================================
'  RUN sequence
' ====================================================================
Label RunSequence
	Gosub DisableAllButtons
	Button EditSequence Enabled
	Controls OpenTextBox
	Gosub InitSequencePaths
	'
	SequenceToExecute = SequencePathAndName
	Variable Numeric NumCycle
	For NumCycle = NumCycle To Infinity
		Gosub ExecSequence
	Next
	Gosub EnableAllButtons
Return

' ====================================================================
'  SEQUENCE LOADING AND EXECUTION
' ====================================================================
Variable String SequenceToExecute
Variable String Sequence
Variable String SequenceCmd
Variable Numeric SequenceIndex
Variable Numeric SequenceLength

Label ExecSequence
	Load Sequence SequenceToExecute
	If Len(Sequence) = 0
		Controls OpenTextBox
		Print CRLF + " Sequence file not found: " + SequenceToExecute
		Beep
		Stop
	EndIf
	'
	Print CRLF + "Sequence " + GetFileNameWithoutPath(SequenceToExecute)
	Print " started at " + Format(Now, "HH:mm:ss") + CRLF + Chars("-", 50) + CRLF
	' ----------------------------------------------------------
	Variable Numeric ExecSequenceStartTime
	ExecSequenceStartTime = ElapsedTime
	' ----------------------------------------------------------
	SequenceLength = GetSeparatedStringCount(Sequence, CRLF)
	For SequenceIndex = 0 To SequenceLength - 1
		' ------------------------------------------------------
		SequenceCmd = GetSeparatedString(Sequence, SequenceIndex, CRLF)
		SequenceCmd = Replace(SequenceCmd, Chr(9), " ") 'TABS to SPACES
		' ------------------------------------------------------ Do not print lines containing ''
		If GetSeparatedStringCount(SequenceCmd, "''") < 2
			Print " Exec: " + SequenceCmd + CRLF
		EndIf
		' ------------------------------------------------------ Trim and remove comments
		SequenceCmd = Trim(RemoveComments(SequenceCmd))
		' ------------------------------------------------------
		If SequenceCmd <> ""
			Select GetSeparatedString(SequenceCmd, 0)
				' ---------------------------------------------- LED COLORS
				Case "Red"
					Gosub Red
				Case "Magenta"
					Gosub Magenta
				Case "Yellow"
					Gosub Yellow
				Case "Green"
					Gosub Green
				Case "Blu"
					Gosub Blu
				Case "White"
					Gosub White
				Case "RgbOFF"
					Gosub RgbOff
				' ---------------------------------------------- White light
				Case "WhiteLightON"
					Gosub WhiteLightON
				Case "WhiteLightOFF"
					Gosub WhiteLightOFF
				' ---------------------------------------------- End sequence
				Case "EndSeq"
					SequenceIndex = SequenceLength-1
				' ---------------------------------------------- With parameters
				Case "Pause"
					Wait Seconds GetSeparatedString(SequenceCmd, 1)
				' ---------------------------------------------- Errors
				CaseElse
					Controls OpenTextBox
					Print CRLF + " Unrecognized command: " + SequenceCmd + CRLF
					Beep
					Stop
			EndSelect
		EndIf
	Next
	Print Chars("-", 50) + CRLF + "Sequence " + GetFileNameWithoutPath(SequenceToExecute)
	Print " executed in " + Format(ElapsedTime - ExecSequenceStartTime, "0.00") + " sec" + CRLF
Return


