
Variable Numeric Slot_SolenoidVoltage
Slot_SolenoidVoltage    =   2 ' The solenoid voltage 1000 = +4V / -1000 = -4V

Gosub TEST
'Gosub PenSingleAndWaitSolenoidON
Stop



' ====================================================================
'  BUTTONS AND KEYS
' ====================================================================
Label SetButtonsAndKeys
	
	Button 8 Text TEST
	Button TEST Text "TEST"
	Button TEST Color Aqua
	
Return



' ====================================================================
'  PEN UP/DOWN
' ====================================================================
Label PenUp
	'Slot(Slot_PEN) = PenUp_Value
	Wait Seconds 0.1
Return

Label PenDown
	'Slot(Slot_PEN) = PenDown_Value
	Wait Seconds 0.1
Return

Label PenSingle
	Gosub PenDown
	Load sound\Click3.wav
	'Load "media\sound\laser.wav"
	Wait Seconds 0.5
	Gosub PenUp
Return


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

Label ExecSequence
	Load Sequence SequenceFileName
	If Len(Sequence) = 0
		Controls OpenTextBox
		Print CRLF + " Sequence file not found: " + SequenceFileName
		Beep
		Stop
	EndIf
	'
	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
		SequenceCmd = RemoveComments(SequenceCmd)
		SequenceCmd = Trim(SequenceCmd)
		'
		If SequenceCmd <> ""
			Print " Executing command: " + SequenceCmd + CRLF
			'
			Select SequenceCmd
            	' ---------------------------------------------- End sequence
				Case "EndSeq"
					SequenceIndex = SequenceLength-1
				' ---------------------------------------------- Pen actions
				Case "PenUp"            ' move pen up
					Gosub PenUp
				Case "PenDown"          ' move pen down
					Gosub PenDown
				Case "PenSingle"        ' single click
					Gosub PenSingle
				' ---------------------------------------------- With parameters
				CaseElse
					s1 = GetSeparatedString(SequenceCmd, 0)
					Select s1
						Case "PenSingleAndWaitSolenoidON"
							TestVolt_TimeSec = GetSeparatedString(SequenceCmd, 1)
							Gosub PenSingleAndWaitSolenoidON
						Case "Pause"
							Wait Seconds GetSeparatedString(SequenceCmd, 1)
						CaseElse
							If s1 <> ""
								Controls OpenTextBox
								Print CRLF + " Unrecognized command: " + SequenceCmd + CRLF
								Beep
								Stop
							EndIf
					EndSelect
			EndSelect
			'Gosub UpdateButtons
		EndIf
	Next
	Print s1 + " executed in " + Format(ElapsedTime - ExecSequenceStartTime, "0.00") + " seconds" + CRLF
Return


' ====================================================================
'  UNLOCK-LOCK sequence
' ====================================================================
Variable Numeric NumCycle

Label OpenUnLockLockSeq
	Load "Sequences\UnLock-Lock.seq"
Return

Label TEST
	Controls OpenTextBox
	SequenceFileName = "SEQUENCES\UnLock-Lock.seq"
	Variable Numeric nn
	nn = 100'Input "How many cycles?"
	If nn <> 0
		For NumCycle = 1 To nn
			Gosub ExecSequence
			Wait Seconds 0.5
		Next
	EndIf
Return

Label ALARM
	Variable Numeric AL
	For AL = 1 To 1
		Load "sound/Alarm10b.wav"
		Wait Not MediaPlaying
		Wait Seconds 0.5
	Next
Return

Variable Numeric TestVolt_TimeSec

' ====================================================================
'  SOLENOID
' ====================================================================
Label PenSingleAndWaitSolenoidON
	Variable Numeric WS1
	Variable Numeric WS2
	For WS2 = 1 To Infinity
		Gosub PenSingle
		For WS1 = 1 To Infinity
			If Slot(Slot_SolenoidVoltage) > 700
				Exit 2			'exit from two for loops
			EndIf
			If WS1 > TestVolt_TimeSec * 2	' timeout 2 Sec
				If WS2 > 3					' number of trials
					Gosub ALARM
					End
				Else
					Exit 1
				EndIf
			EndIf
			Wait Seconds 0.1
		Next
	Next
Return


'WATCH WS1
'WATCH WS2
'WATCH SequenceIndex
'WATCH GetSeparatedString
'EXEC DestX = 12
