' ====================================================================
'     Name: WXM_CNC_Vslot_Automation_16_2.txt        Date: 17 Jun 2021
'    Autor: Leonardo DE PALO
'    Scope: Two gripper PWM, rotating with Protocol1 of FeeTech.
' Hardware: Standard Master e SM29.
' note:
' ====================================================================

Option Speed 9
Gosub RestoreVars
Gosub SetButtonsAndKeys
Gosub ShowSequenceName
Stop						 										'

Label EventStop
	Gosub SaveVars
End


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

Label RestoreVars
	Load Vars
	SequenceName = S1
Return


' ====================================================================
'  BUTTONS
' ====================================================================
Label SetButtonsAndKeys
	Button 5 Text EditSequence
	Button EditSequence Text "Edit sequence"
	Button EditSequence Color PaleGreen
	
	Button 6 Text RunSequence
	Button RunSequence Text "RUN SEQUENCE"
	Button RunSequence Color Aqua
	
	Button 7 Text SelectSequence
	Button SelectSequence Text "Select Sequence"
	Button SelectSequence Color PowderBlue
Return

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

Label InitSequencePaths
	Variable String SequenceName
	Variable String EditorFolder = ".\Theremino_HAL\Theremino_Editor\"
	Variable String EditorApp = EditorFolder + "Theremino_Editor.exe"
	Variable String 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
	Variable Numeric ExitFromSequenceLoop
	Variable Numeric NumCycle
	'
	Controls OpenTextBox
	Gosub InitSequencePaths
	'
	ExitFromSequenceLoop = 0
	SequenceToExecute = SequencePathAndName
	For NumCycle = NumCycle To Infinity
		Gosub ExecSequence
		Wait Seconds 0.5
		If ExitFromSequenceLoop > 0
			Exit
		EndIf
	Next
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 "Sequence " + GetFileNameWithoutPath(SequenceToExecute) + " started at " + Format(Now, "HH:mm:ss") + CRLF
	Print 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
		'
		Print " Exec: " + SequenceCmd + CRLF
		'
		SequenceCmd = RemoveComments(SequenceCmd)
		SequenceCmd = Trim(SequenceCmd)
		'
		If SequenceCmd <> ""
			'
			Select SequenceCmd
            	' ---------------------------------------------- End sequence
				Case "EndSeq"
					SequenceIndex = SequenceLength-1
				' ---------------------------------------------- Event STOPd sequence
					
				' ---------------------------------------------- With parameters
				CaseElse
					Select GetSeparatedString(SequenceCmd, 0)
						CaseElse
							If SequenceCmd <> ""
								Controls OpenTextBox
								Print CRLF + " Unrecognized command: " + SequenceCmd + CRLF
								Beep
								Stop
							EndIf
					EndSelect
			EndSelect
		EndIf
	Next
	Print Chars("-", 50) + CRLF
	Print "Sequence " + GetFileNameWithoutPath(SequenceToExecute) + " executed in " + Format(ElapsedTime - ExecSequenceStartTime, "0.00") + " seconds" + CRLF
Return


