' ====================================================================
'     Name: Control_91_LeoProtoNewPen.txt            Date: 09 Mar 2021
'    Autor: Leonardo DE PALO
'    Scope: Test COREXY from Wingo
' Hardware: Standard Master.
'     Note: Proto version have the following rule:
'			MA= Blk Gre Red Blu
'			MB= Blu Red Gre Blk
'           MA=1000/0  MB=1000/0
'           * MB have the stepper connector rotated
' ====================================================================

Option Speed 9
Load Images\TabletTesterLeoProto.jpg
'Load Sound\gong.wav
COM Open 1 115200

' -------------------------------------- Options
KeyJog_Speed = 100

' -------------------------------------- Software limits
DestxMax =   245	' 246 before new pen system
DestxMin =     1
DestyMax =   138
DestyMin =     1

' -------------------------------------- MOTORS ASSIGNEMENTS
' Digital motors fine tuning set up
PEN_DOWN       = 895 ' 900 Motor  1
PEN_UP         = 860
POWER_IN       = 950 ' Motor 2 main power plug insert
POWER_OUT      = 100
ONOFF_IN       = 750 ' Motor 3
ONOFF_OUT      = 295
USBPWR_IN      = 588 ' Motor 4
USBPWR_OUT     =  95
HDMI_IN        = 980 ' Motor 5
HDMI_OUT       = 136
USB3_IN        = 940 ' Motor 6
USB3_OUT       =  79
USBC_IN        = 286 ' Motor 7
USBC_OUT       =  28
VOLUME_IN_UP   = 697 ' Motor 8
VOLUME_OUT     = 754
VOLUME_IN_DW   = 794
HEARSET_IN     = 985 ' Motor  9
HEARSET_OUT    =  76
MEMORY_IN      = 900 ' Motor 10
MEMORY_OUT     =  10

' ----------------------------------------- STEPPER SLOTS
Slot_MotorA        =  1			'1_TabTesterXY pin  1 STEPPER
Slot_MotorB        =  3			'1_TabTesterXY pin  3 STEPPER
Slot_SensorX       =  7			'1_TabTesterXY pin  7 DIG INPU
Slot_SensorY       =  8			'1_TabTesterXY pin  8 DIG INPU
Slot_EnableMotors  = 10			'1_TabTesterXY pin 10 DIG OUT

' ----------------------------------------- DIGITAL MOTOR SLOTS
SLOT_USB3          = 13 		'2_TabTesterMot pin 1 PWMF
SLOT_HDMI          = 15			'2_TabTesterMot pin 3 PWMF
SLOT_USBPWR        = 16			'2_TabTesterMot pin 4 PWMF
SLOT_ONOFF         = 17			'2_TabTesterMot pin 5 PWMF
SLOT_POWER 		   = 18			'2_TabTesterMot pin 6 software control SERVO16
SLOT_VOLUME        = 25			'3_TabTesterMot pin 1 PWMF
SLOT_MEMORY        = 26			'3_TabTesterMot pin 2 PWMF
SLOT_HEARSET       = 27			'3_TabTesterMot pin 3 PWMF
SLOT_PEN           = 29			'3_TabTesterMot pin 4 PWMF
SLOT_USBC          = 28         '2_TabTesterMot pin 2 PWMF

' ----------------------------------------- TABLET POWER
SLOT_TabletPower   =  9         ' 1000 = Tablet on

' ----------------------------------------- EMERGENCY and SPEED
Slot_Emergency     = 11
Slot_Speed         =  6

' ----------------------------------------- SLOTS FOR SOUND TESTS
Slot_RunScope          = 37
Slot_SoundLevel        = 41
Slot_SoundLevel2ndHarm = 43
Slot_SoundLevel3rdHarm = 45

' ----------------------------------------- JOG WITH MOUSE
Jog_RangeX        = 256        '288
Jog_RangeY        = 144
Jog_Enable_Slot   = 34

' ----------------------------------------- COLOR RECOGNITION
Slot_S0   =  24
Slot_S1   =  23
Slot_S2   =  21
Slot_S3   =  20
Slot_Freq =  22

' ----------------------------------------- FILE SET UP
' Variable String FileName
' FileName = "Test_Results.txt"

' ----------------------------------------- LOAD APPS
Load Theremino_HAL\Theremino_HAL.exe
Load Theremino_HAL\Theremino_SlotViewer.exe

'Load Theremino_MicroVideo\Theremino_MicroVideo.exe
'Load Theremino_AudioGenerator\Theremino_AudioGenerator.exe
'Load Theremino_AudioInput\Theremino_AudioInput.exe
'Load Theremino_HAL\Theremino_SignalScope.exe

' ----------------------------------------- KEYS
Key Right		Gosub ExecKeys
Key Left 		Gosub ExecKeys
Key Up 			Gosub ExecKeys
Key Down 		Gosub ExecKeys
Key Pageup		Gosub PenUp
Key Pagedown	Gosub PenDown
Key End     	Gosub PenSingle

' ----------------------------------------- START
Load Toc.wav

Slot Slot_S0 = 1000
Slot Slot_S1 = 1000

'Window MinSize

DestX = v1
DestY = v2

Gosub VolumeOut
Gosub PenUp
Gosub SleepAllServo
Gosub MainMenu
Load Hide
Stop												'

' -----------------------------------------------------------------
Variable Numeric DestxMax
Variable Numeric DestxMin
Variable Numeric DestyMax
Variable Numeric DestyMin

' -----------------------------------------------------------------
Label  ClearButtons
	For v1 = 1 To 32
		Button v1 Text ""
		Button v1 Color Moccasin
	Next
Return

Label  CloseAll
	Load CloseApps
Return

Label EventStop
	Window Sizable
End

' -----------------------------------------------------------------
'   MENU
' -----------------------------------------------------------------
Label MainMenu
	Gosub  ClearButtons
	Button 1 Text Display
	Button Display Text "DISPLAY"
	Button Display Color SkyBlue
	Button 2 Text Touch
	Button Touch Text "TOUCH"
	Button Touch Color LightSteelBlue
	Button 3 Text Audio
	Button Audio Text "AUDIO"
	Button Audio Color LightGreen
	
	Button 4 Text Plug
	Button Plug Text "PLUG"
	Button Plug Color MediumAquamarine
	Button 5 Text WiFi
	Button WiFi Text "Wi-Fi"
	Button WiFi Color HotPink
	Button 6 Text Bluetooth
	Button Bluetooth Text  "BLUETOOTH"
	Button Bluetooth Color RoyalBlue
	Button 7 Text Power
	Button Power Text "POWER"
	Button Power Color Chocolate
	Button 8 Text VirtualKey
	Button VirtualKey Text "VIRTUAL KEY"
	Button VirtualKey Color SlateGray
	Button 9 Text  Mechanical
	Button Mechanical Text "MECHANICAL"
	Button Mechanical Color Gainsboro
	Button 10 Text  Miscellanea
	Button Miscellanea Text "MISCELLANEA"
	Button Miscellanea Color DarkCyan
	Button 12 Text Zero
	Button Zero Text "ZERO"
	Button Zero Color Black
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
	Button 16 Text Emergency
	Button Emergency Text "EMERGENCY"
	Button Emergency Color Red
	Button Emergency Slot Slot_Emergency
Return

Label Display
	Gosub  ClearButtons
	Button 1 Text Luminosity
	Button Luminosity Text "LUMINOSITY"
	Button Luminosity Color SkyBlue
	Button 2 Text Red
	Button Red Text "RED"
	Button Red Color Red
	Button 3 Text Green
	Button Green Text "GREEN"
	Button Green Color Green
	Button 4 Text Blu
	Button Blu Text "BLUE"
	Button Blu Color Blue
	Button 5 Text White
	Button White Text "WHITE"
	Button White Color White
	Button 6 Text Bitmap
	Button Bitmap Text "BITMAP"
	Button Bitmap Color SkyBlue
	Button 7 Text Photo
	Button Photo Text "PHOTO"
	Button Photo Color SkyBlue
	Button 8 Text Video
	Button Video Text "VIDEO"
	Button Video Color SkyBlue
	Button 9 Text NoColor
	Button NoColor Text "NO COLOR"
	Button NoColor Color SkyBlue
	Button 10 Text ColorTest
	Button ColorTest Text "COLOR TEST"
	Button ColorTest Color SkyBlue
	'Button 11 Text --------
	'Button -------- Text "--------"
	'Button -------- Color SkyBlue
	Button 12 Text TouchTest
	Button TouchTest Text "TOUCH TEST"
	Button TouchTest Color SkyBlue
	Button 13 Text ColorRecognition
	Button ColorRecognition Text "Color Recognition"
	Button ColorRecognition Color SkyBlue
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Touch
	Gosub  ClearButtons
	Button 1 Text PenUp
	Button PenUp Text "PEN UP"
	Button PenUp Color LightSteelBlue
	Button 2 Text PenDown
	Button PenDown Text "PEN DOWN"
	Button PenDown Color LightSteelBlue
	Button 3 Text PenSingle
	Button PenSingle Text "PEN SINGLE"
	Button PenSingle Color LightSteelBlue
	Button 4 Text PenDouble
	Button PenDouble Text "PEN DOUBLE"
	Button PenDouble Color LightSteelBlue
	Button 5 Text Slide
	Button Slide Text "SLIDE"
	Button Slide Color LightSteelBlue
	Button 6 Text SlidePSW
	Button SlidePSW Text "Slide PSW"
	Button SlidePSW Color LightSteelBlue
	Button 7 Text  TouchTest
	Button TouchTest Text  "TOUCH TEST"
	Button TouchTest Color LightSteelBlue
	Button 8 Text  JogWithMouse
	Button JogWithMouse Text "JOG with Mouse"
	Button JogWithMouse Color LightSteelBlue
	Button 10 Text  StartWxmAppTablet
	Button StartWxmAppTablet Text "Start WxmApp Tablet"
	Button StartWxmAppTablet Color LightSteelBlue
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Audio
	Gosub  ClearButtons
	Button 1 Text HearsetIn
	Button HearsetIn Text "HEARSET IN"
	Button HearsetIn Color LightGreen
	Button 2 Text HearsetOut
	Button HearsetOut Text "HEARSET OUT"
	Button HearsetOut Color LightGreen
	Button 3 Text VolumeOut
	Button VolumeOut Text "VOLUME OUT"
	Button VolumeOut Color LightGreen
	Button 4 Text VolumeUp
	Button VolumeUp Text "VOLUME UP"
	Button VolumeUp Color LightGreen
	Button 5 Text VolumeDw
	Button VolumeDw Text "VOLUME DW"
	Button VolumeDw Color LightGreen
	Button 6 Text VolumeTest
	Button VolumeTest Text "VOLUME TEST"
	Button VolumeTest Color LightGreen
	Button 7 Text SpeakerDist
	Button SpeakerDist Text "SPEAKER DIST."
	Button SpeakerDist Color LightGreen
	Button 8 Text TestHearset
	Button TestHearset Text "TEST HEARSET"
	Button TestHearset Color LightGreen
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Plug
	Gosub  ClearButtons
	Button 1 Text PowerIn
	Button PowerIn Text "POWER IN"
	Button PowerIn Color MediumAquamarine
	Button 2 Text PowerOut
	Button PowerOut Text "POWER OUT"
	Button PowerOut Color MediumAquamarine
	Button 3 Text MemoryIn
	Button MemoryIn Text "MEMORY IN"
	Button MemoryIn Color MediumAquamarine
	Button 4 Text MemoryOut
	Button MemoryOut Text "MEMORY OUT"
	Button MemoryOut Color MediumAquamarine
	Button 5 Text Usb3In
	Button Usb3In Text "USB3 IN"
	Button Usb3In Color MediumAquamarine
	Button 6 Text Usb3Out
	Button Usb3Out Text "USB3 OUT"
	Button Usb3Out Color MediumAquamarine
	Button 7 Text UsbcIn
	Button UsbcIn Text "USBC IN"
	Button UsbcIn Color MediumAquamarine
	Button 8 Text UsbcOut
	Button UsbcOut Text "USBC OUT"
	Button UsbcOut Color MediumAquamarine
	Button 9 Text HdmiIn
	Button HdmiIn Text "HDMI IN"
	Button HdmiIn Color MediumAquamarine
	Button 10 Text HdmiOut
	Button HdmiOut Text "HDMI OUT"
	Button HdmiOut Color MediumAquamarine
	Button 11 Text HearsetIn
	Button HearsetIn Text "HEARSET IN"
	Button HearsetIn Color MediumAquamarine
	Button 12 Text HearsetOut
	Button HearsetOut Text "HEARSET OUT"
	Button HearsetOut Color MediumAquamarine
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label WiFi
	Gosub  ClearButtons
	'Button 1 Text Empty1
	'Button Empty1 Text "EMPTY 1"
	'Button Empty1 Color HotPink
	'Button 2 Text Empty2
	'Button Empty2 Text "EMPTY 2"
	'Button Empty2 Color HotPink
	'Button 3 Text Empty3
	'Button Empty3 Text "EMPTY 3"
	'Button Empty3 Color HotPink
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Bluetooth
	Gosub  ClearButtons
	'Button 1 Text Empty1
	'Button Empty1 Text "EMPTY 1"
	'Button Empty1 Color RoyalBlue
	'Button 2 Text Empty2
	'Button Empty2 Text "EMPTY 2"
	'Button Empty2 Color RoyalBlue
	'Button 3 Text Empty3
	'Button Empty3 Text "EMPTY 3"
	'Button Empty3 Color RoyalBlue
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Power
	Gosub  ClearButtons
	Button 1 Text PowerIn
	Button PowerIn Text "POWER IN"
	Button PowerIn Color Chocolate
	Button 2 Text PowerOut
	Button PowerOut Text "POWER OUT"
	Button PowerOut Color Chocolate
	Button 4 Text UsbPwrIn
	Button UsbPwrIn Text "USB PWR IN"
	Button UsbPwrIn Color Yellow5
	Button 5 Text UsbPwrOut
	Button UsbPwrOut Text "USB PWR OUT"
	Button UsbPwrOut Color Yellow5
	Button 7 Text PowerOn
	Button PowerOn Text "POWER ON"
	Button PowerOn Color Yellow6
	Button 8 Text PowerOFF
	Button PowerOFF Text "POWER OFF"
	Button PowerOFF Color Yellow6
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label VirtualKey
	Gosub  ClearButtons
	Button 1 Text Numbers
	Button Numbers Text "NUMBERS"
	Button Numbers Color SlateGray
	Button 2 Text Letters
	Button Letters Text "LETTERS"
	Button Letters Color SlateGray
	Button 3 Text WindowsMenu
	Button WindowsMenu Text "WINDOWS MENU"
	Button WindowsMenu Color SlateGray
	Button 5 Text PsSequences
	Button PsSequences Text "PS SEQUENCES"
	Button PsSequences Color SlateGray
	Button 6 Text GetService
	Button GetService Text "Get-Service"
	Button GetService Color SlateGray
	Button 7 Text WinUpdateHistory
	Button WinUpdateHistory Text "Windows update history"
	Button WinUpdateHistory Color SlateGray
	Button 8 Text GetSystemInfo
	Button GetSystemInfo Text "GetSystemInfo"
	Button GetSystemInfo Color SlateGray
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return


Label Mechanical
	Gosub  ClearButtons
	Button 1 Text Zero
	Button Zero Text "ZERO"
	Button Zero Color Gainsboro
	Button 2 Text EnableMotors
	Button EnableMotors Text "ENAB. MOTORS"
	Button EnableMotors Color Gainsboro
	Button 3 Text DisableMotors
	Button DisableMotors Text "DISAB. MOTORS"
	Button DisableMotors Color Gainsboro
	
	Button 9 Text BurnIn_7-24
	Button BurnIn_7-24 Text "BurnIn 7-24"
	Button BurnIn_7-24 Color Gainsboro
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Miscellanea
	Gosub  ClearButtons
	Button 1 Text StandBy
	Button StandBy Text "STAND BY"
	Button StandBy Color DarkCyan
	Button 2 Text SafeRemove
	Button SafeRemove Text "SAFE REMOVE"
	Button SafeRemove Color DarkCyan
	Button 3 Text AllIn
	Button AllIn Text "ALL IN"
	Button AllIn Color DarkCyan
	Button 4 Text AllOut
	Button AllOut Text "ALL OUT"
	Button AllOut Color DarkCyan
	Button 5 Text VirtualKeyboard
	Button VirtualKeyboard Text "V KEYBOARD"
	Button VirtualKeyboard Color DarkCyan
	Button 6 Text JogWithMouse
	Button JogWithMouse Text "Jog with Mouse"
	Button JogWithMouse Color DarkCyan
	Button 7 Text Calibrate
	Button Calibrate Text "CALIBRATE"
	Button Calibrate Color DarkCyan
	Button 8 Text UsbPwrIn
	Button UsbPwrIn Text "USB PWR IN"
	Button UsbPwrIn Color DarkCyan
	Button 9 Text UsbPwrOut
	Button UsbPwrOut Text "USB PWR OUT"
	Button UsbPwrOut Color DarkCyan
	
	Button 11 Text GetSystemInfo
	Button GetSystemInfo Text "Get System Info"
	Button GetSystemInfo Color DarkCyan
	
	Button 14 Text MainMenu
	Button MainMenu Text "MAIN MENU"
	Button MainMenu Color Yellow
	Button 15 Text CloseAll
	Button CloseAll Text "CLOSE ALL"
	Button CloseAll Color Red
Return

Label Numbers
	PressKeys ALT Tab
	Wait Seconds 0.5
	s1 =  "SPACE"
	PressKeys s1
	PressKeys 1 2 3 4 5 6 7 8 9 0 Enter
Return

Label Letters
	PressKeys ALT Tab
	Wait Seconds 0.5
	PressKeys A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Enter
	PressKeys B C C  Enter
Return

Label WindowsMenu
	'PressKey Space
	PressKeys WINDOWS X
	Wait Seconds 2
Return

Label PsSequences
	SequenceFileName = "SEQUENCES\Sequence_Dir.seq"
	Gosub ExecSequence
Return

Label GetService
	SequenceFileName = "SEQUENCES\Sequence_Get-Service.seq"
	Gosub ExecSequence
Return

Label WinUpdateHistory
	SequenceFileName = "SEQUENCES\Sequence_Win10-update-history.seq"
	Gosub ExecSequence
Return

Label GetSystemInfo
	COM DiscardOutBuffer
	COM WriteString "SendSystemInfo" + CRLF
	FileName = "info.csv"
	Gosub ReceiveFileFromCOM
Return


Label Luminosity
	Button 1 Color Flashing8 Black Gray5
	Beep
	Wait Key("Space")
	SequenceFileName = "SEQUENCES\Sequence_LuminosityBigPen.seq"
	Gosub ExecSequence
	Button 1 Color SkyBlue
Return

Label VirtualKeyboard
	'Beep
	'Wait Key("Space")
	Button 29 Color Flashing8 Blue9  Cyan9
	SequenceFileName = "SEQUENCES\Sequence_VirtualKeyboard.seq"
	Gosub ExecSequence
	Wait Seconds 2
	Button 29 Color MediumBlue
Return

Label Red
	COM WriteString "RED" + CRLF
Return

Label Green
	COM WriteString "GREEN" + CRLF
Return

Label Blu
	COM WriteString "BLU" + CRLF
Return

Label White
	COM WriteString "WHITE" + CRLF
Return

Label NoColor
	COM WriteString "NO COLOR" + CRLF
Return

Label Bitmap
	COM WriteString "BITMAP" + CRLF
Return

Label Photo
	COM WriteString "PHOTO" + CRLF
Return

Label Video
	COM WriteString "VIDEO" + CRLF
Return

Label StandBy
	Gosub  Zero
	SequenceFileName = "SEQUENCES\Sequence_SafeRemove.seq"
	Gosub ExecSequence
	Gosub PowerIn
	Gosub Usb3In
	Gosub MemoryIn
	Gosub UsbcOut
	Gosub HdmiOut
	Gosub UsbPwrOut
	Gosub HearsetOut
	Gosub OnOffOut
	Gosub VolumeOut
	Gosub DisableMotors
Return

Label AllIn
	Gosub PowerIn
	Wait Seconds 0.2
	Gosub Usb3In
	Wait Seconds 0.2
	Gosub UsbcIn
	Wait Seconds 0.2
	Gosub HdmiIn
	Wait Seconds 0.2
	Gosub UsbPwrIn
	Wait Seconds 0.2
	Gosub HearsetIn
	Wait Seconds 0.2
	Gosub MemoryIn
	Wait Seconds 0.2
	'Gosub OnOffIn
	'Gosub VolumeInUp
	'Wait Seconds 0.5
	Gosub VolumeOut
	Wait Seconds 0.2
Return

Label AllOut
	Gosub PowerOut
	Wait Seconds 0.2
	Gosub Usb3Out
	Wait Seconds 0.2
	Gosub UsbcOut
	Wait Seconds 0.2
	Gosub HdmiOut
	Wait Seconds 0.2
	Gosub UsbPwrOut
	Wait Seconds 0.2
	Gosub HearsetOut
	Wait Seconds 0.2
	Gosub MemoryOut
	Wait Seconds 0.2
	Gosub OnOffOut
	Wait Seconds 0.2
	'Gosub VolumeInDw
	'Wait Seconds 0.5
	Gosub VolumeOut
	Wait Seconds 0.2
Return

Label Multi
	v2 = Input " How many loops? "
	If v2 = 0
		v2 = 10
	EndIf
	For v1 = 1 To v2
		s1 = Now
		s1 = s1 + "     Cycle in progress = " + v1
		s1 = s1 + "     Cycle to do = " + v2
		Print s1
		'Gosub RUN TEST
	Next
Return

Label SafeRemove
	Gosub Zero
	Gosub PowerOut
	Gosub Usb3Out
	Gosub UsbcOut
	Gosub HdmiOut
	Gosub UsbPwrOut
	Gosub HearsetOut
	Gosub OnOffOut
	Gosub VolumeOut
	Gosub MemoryOut
	SequenceFileName = "SEQUENCES\Sequence_SafeRemove.seq"
	Gosub ExecSequence
Return

Label ButtonFast
	SequenceFileName = "SEQUENCES\Sequence_ButtonFast.seq"
	Gosub ExecSequence
Return

Label  BurnIn_7-24
	For v9 = 1 To 10000
		SequenceFileName = "SEQUENCES\BurnIn7-24.seq"
		Gosub ExecSequence
		Print "Cycle number:" + v9
'		Wait Seconds 1.5
		Slot(100) = Slot(100) + 1
		Beep
	Next
Return

Label Calibrate
	'Beep
	'Wait Key("Space")
	Gosub  Zero
	'SequenceFileName = "SEQUENCES\Sequence_Calibrate.seq"
	SequenceFileName = "SEQUENCES\Sequence_SpeedTest.seq"
	Gosub ExecSequence
Return

' ===========================================================
'   AUDIO TESTS
' ===========================================================
Variable Numeric Slot_RunScope
Variable Numeric Slot_SoundLevel
Variable Numeric Slot_SoundLevel2ndHarm
Variable Numeric Slot_SoundLevel3rdHarm
Variable Numeric SoundLevelMin
Variable Numeric SoundNoiseMax
Variable Numeric SoundDist2ndHarmMax ' Max level for the 2nd harmonics
Variable Numeric SoundDist3rdHarmMax ' Max level for the 3rd harmonics

Label SpeakerDist
	Window MinSize
	Wait Key("Space")
	Button 25 Color Flashing8 Gold DarkGreen
	SoundLevelMin = 650
	SoundNoiseMax = 350
	SoundDist2ndHarmMax = 400
	SoundDist3rdHarmMax = 400
	Gosub AudioTest
	Window Sizable
	Button 25 Color LightSteelBlue
	'Gosub EventStop
Return

Label TestHearset
	Window MinSize
	Button 26 Color Flashing8 Gold DarkGreen
	SoundLevelMin = 50
	SoundNoiseMax = 30
	Gosub AudioTest
	Window Sizable
	Button 26 Color LightSteelBlue
	Gosub EventStop
Return

Label AudioTest
	'Gosub "ZERO"
	Slot 39 = 1000              '<<<<<<<<<<<<<< manual sound start
    'Beep
	'wait Seconds 2
	'Wait Key("Space")
	s1 = ""
	'---------------------- test audio left
	DestX = 014
	DestY = 108.8
	Gosub CoreXY_MoveToDestXY_AndWait
	Gosub PenSingle
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.5
	v1 = Slot(Slot_SoundLevel)
	If v1 < SoundLevelMin
		s1 = s1 + "ERROR: Left level too low" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel2ndHarm)
	If v1 > SoundDist2ndHarmMax
		s1 = s1 + "ERROR: Left speaker. Too much distortion on the 2nd harmonics" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel3rdHarm)
	If v1 > SoundDist3rdHarmMax
		s1 = s1 + "ERROR: Left speaker. Too much distortion on the 3rd harmonics" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	
	'---------------------- test audio right
	DestX = 014
	DestY = 102.5
	Gosub CoreXY_MoveToDestXY_AndWait
	Gosub PenSingle
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.5
	v1 = Slot(Slot_SoundLevel)
	If v1 < SoundLevelMin
		s1 = s1 + "ERROR: Right level too low" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel2ndHarm)
	If v1 > SoundDist2ndHarmMax
		s1 = s1 + "ERROR: Right speaker. Too much distortion on the 2nd harmonics" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel3rdHarm)
	If v1 > SoundDist3rdHarmMax
		s1 = s1 + "ERROR: Right speaker. Too much distortion on the 3rd harmonics" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	
	Slot 39 = 0                 '<<<<<<<<<<<<<< manual sound stop
	
	'---------------------- test audio no sound left/right
	DestX = 014
	DestY = 096.2
	Gosub CoreXY_MoveToDestXY_AndWait
	Gosub PenSingle
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.5
	v1 = Slot(Slot_SoundLevel)
	If v1 > SoundNoiseMax
		s1 = s1 + "ERROR: Too much noise" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	
	If s1 = ""
		s1 = "TEST PASSED OK"
	EndIf
	Print s1
Return

Label VolumeTest
	'Gosub "ZERO"
	Window MinSize
	Button 24 Color Flashing8 LightSteelBlue DarkGreen
	SoundLevelMin = 650
	SoundNoiseMax = 350
	Gosub VolumeTest2
	Window Sizable
	Button 24 Color LightSteelBlue
	'SequenceFileName = "SEQUENCES\Sequence_SafeRemove.seq"
	'Gosub ExecSequence
	Gosub StandBy
	Gosub EventStop
Return

Label VolumeTest2
	'Beep
	'wait Seconds 2
	'Wait Key("Space")
	s1 = ""
	'---------------------- test audio volume level high
	Gosub VolumeUp
	Wait Seconds 4
	Gosub VolumeOut
	Slot 39 = 1000              '<<<<<<<<<<<<<< manual sound start
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.23
	v1 = Slot(Slot_SoundLevel)
	If v1 < SoundLevelMin
		s1 = s1 + "ERROR: Left level too low" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	'Slot 39 = 0                 '<<<<<<<<<<<<<< manual sound stop
	
	'---------------------- test audio no sound left/right
	Gosub VolumeDw
	Wait Seconds 4
	Gosub VolumeOut
	'Slot 39 = 1000              '<<<<<<<<<<<<<< manual sound start
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.23
	v1 = Slot(Slot_SoundLevel)
	If v1 > SoundNoiseMax
		s1 = s1 + "ERROR: Too much noise" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	Slot 39 = 0                 '<<<<<<<<<<<<<< manual sound stop
	If s1 = ""
		s1 = "TEST PASSED OK"
	EndIf
	Print s1
Return

' =====================================================================
'  COLOR RECOGNITION
' =====================================================================
Variable Numeric Slot_S0
Variable Numeric Slot_S1
Variable Numeric Slot_S2
Variable Numeric Slot_S3
Variable Numeric Slot_Freq

Label ColorTest
	FileHeaderTestName = "COLOR TEST"
	Gosub WriteFileHeader
	Beep
	Wait Key("Space")
	Button 10 Color Flashing8 Red SkyBlue
	
	Gosub Red
	Wait Seconds 1
	Gosub  ColorRecognition
	If s1 = "RED"
		s1 = "Red color = Passed"
	Else
		s1 = "Red color = Fail"
	EndIf
	Save StringToFile s1 FileName
	
	Gosub Green
	Wait Seconds 1
	Gosub  ColorRecognition
	If s1 = "GREEN"
		s1 = "Green color = Passed"
	Else
		s1 = "Green color = Fail"
	EndIf
	Save StringToFile s1 FileName
	
	Gosub Blu
	Wait Seconds 1
	Gosub  ColorRecognition
	If s1 = "BLU"
		s1 = "Blu color = Passed"
	Else
		s1 = "Blu color = Fail"
	EndIf
	Save StringToFile s1 FileName
	
	Button 10 Color SkyBlue
	
	Gosub NoColor
Return

Label ColorRecognition
	' ------------------------- RED filter
	Slot Slot_S2 = 0
	Slot Slot_S3 = 0
	Wait Seconds 0.1
	v1 = Slot(Slot_Freq)
	' ------------------------- GREEN filter
	Slot Slot_S2 = 1000
	Slot Slot_S3 = 1000
	Wait Seconds 0.1
	v2 = Slot(Slot_Freq)
	' ------------------------- BLU filter
	Slot Slot_S2 = 0
	Slot Slot_S3 = 1000
	Wait Seconds 0.1
	v3 = Slot(Slot_Freq)
	'' ------------------------- CLEAR filter
	'Slot Slot_S2 = 1000
	'Slot Slot_S3 = 0
	'Wait Seconds 0.2
	'v4 = Slot(Slot_Freq)
    ' ------------------------- RECOGNIZE COLOR
	If v1 > v2 And v1 > v3
		s1 = "RED"
	Else
		If v2 > v1 And v2 > v3
			s1 = "GREEN"
		Else
			If v3 > v1 And v3 > v2
				s1 = "BLU"
			EndIf
		EndIf
	EndIf
	Print v1 + " " + v2 + " " + v3 + " " + s1
Return

Label TouchTest
	Window MinSize
	Beep
	Wait Key("Space")
	SequenceFileName = "SEQUENCES\Sequence_TouchTest.seq"
	Gosub ExecSequence
Return

Label StartWxmAppTablet
	SequenceFileName = "SEQUENCES\StartWXM_AppTablet.seq"
	Gosub ExecSequence
Return

Label SlidePSW
	SequenceFileName = "SEQUENCES\Sequence_SlidePsw.seq"
	Gosub ExecSequence
Return

Variable String FileHeaderTestName
Label WriteFileHeader
	s1 = "-------------------------" + CRLF
	s1 = s1 + FileHeaderTestName + CRLF
	s1 = s1 + Format(Now, "dd/MMM/yyyy HH:mm:ss") + CRLF
	s1 = s1 + "-------------------------"
	Save StringToFile s1 FileName
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
		' ------------------------------------------
	Controls OpenTextBox
	Controls ClearTextBox
	' ------------------------------------------
	Variable Numeric ExecSequenceStartTime
	ExecSequenceStartTime = ElapsedTime
	' ------------------------------------------
	SequenceLength = GetSeparatedStringCount(Sequence, CRLF)
	For SequenceIndex = 0 To SequenceLength-1
		Gosub ExecSpeed
		SequenceCmd = GetSeparatedString(Sequence, SequenceIndex, CRLF)
		SequenceCmd = Replace(SequenceCmd, Chr(9), " ") 'TABS to SPACES
		Print " Sequence: " + SequenceCmd + CRLF
		SequenceCmd = RemoveComments(SequenceCmd)
		SequenceCmd = Trim(SequenceCmd)
		'
		Select UCase(SequenceCmd)
            ' ------------------- exit from the sequence -------------
			Case "ENDSEQ"
				SequenceIndex = SequenceLength-1
			' ------------------- special multiple actions -----------
			Case "SLIDE"		     ' slide before insert PSW
				Gosub Slide
			Case "ZERO"              ' move pen to X=0 and Y=0 position
				Gosub Zero
			Case "ALL OUT"           ' all plug out
				Gosub AllOut
			Case "ALL IN"            ' all plug in
				Gosub AllIn
		   ' --------------------- single action ----------------------
			Case "POWER IN"          ' power plug in
				Gosub PowerIn
			Case "POWER OUT"         ' power plug out
				Gosub PowerOut
			Case "ENABLE MOTORS"     'enable movement motors
				Gosub EnableMotors
			Case "DISABLE MOTORS"    ' disable movement motors
				Gosub DisableMotors
			Case "PEN UP"            ' move pen up
				Gosub PenUp
			Case "PEN DOWN"          ' move pen down
				Gosub PenDown
			Case "PEN SINGLE"        ' single click
				Gosub PenSingle
			Case "PEN DOUBLE"        ' double click
				Gosub PenDouble
			Case "POWER CYCLE"       ' push power button, wait then release
				'Gosub PowerCycle
			Case "HDMI IN"
				Gosub HdmiIn
			Case "HDMI OUT"
				Gosub HdmiOut
			Case "USB3 IN"
				Gosub Usb3In
			Case "USB3 OUT"
				Gosub Usb3Out
			Case "USBC IN"
				Gosub UsbcIn
			Case "USBC OUT"
				Gosub UsbcOut
			Case "USBPWR IN"
				Gosub UsbPwrIn
			Case "USBPWR OUT"
				Gosub UsbPwrOut
			Case "MEMORY IN"
				Gosub MemoryIn
			Case "MEMORY OUT"
				Gosub MemoryOut
			Case "HEARSET IN"
				Gosub HearsetIn
			Case "HEARSET OUT"
				Gosub HearsetOut
			Case "CLOSE ALL"
				Gosub CloseAll
			' ------------------- display actions ---------------------
			Case "BITMAP"
				Gosub  Bitmap
			Case "PHOTO"
				Gosub  Photo
			Case "VIDEO"
				Gosub  Video
			Case "RED"
				Gosub  Red
			Case "GREEN"
				Gosub  Green
			Case "BLU"
				Gosub  Blu
			Case "NO COLOR"
				Gosub  NoColor
			' --------------------- WAIT MOTORS -----------------------
			Case "WAIT MOTORS"
				Gosub WaitMotors
			' --------------------- GET SYSTEM INFO -------------------
			Case "GetSystemInfo"
				Gosub  GetSystemInfo
			' --------------------- Istruction with parameters --------
			CaseElse
				s1 = GetSeparatedString(SequenceCmd, 0)
				Select UCase(s1)
					Case "MOVE"
						DestX = GetSeparatedString(SequenceCmd, 1)
						DestY = GetSeparatedString(SequenceCmd, 2)
						Gosub CoreXY_MoveToDestXY_AndWait
					Case "MOVESLOW"
						MoveSlowXY_DestX = GetSeparatedString(SequenceCmd, 1)
						MoveSlowXY_DestY = GetSeparatedString(SequenceCmd, 2)
						MoveSlowXY_Speed = GetSeparatedString(SequenceCmd, 3)
						Gosub MoveSlowXY
					Case "DRAWCIRCLE"
						DrawCircle_X = GetSeparatedString(SequenceCmd, 1)
						DrawCircle_Y = GetSeparatedString(SequenceCmd, 2)
						DrawCircle_Radius = GetSeparatedString(SequenceCmd, 3)
						DrawCircle_Speed = GetSeparatedString(SequenceCmd, 4)
						Gosub DrawCircle
					Case "PAUSE"
						Wait Seconds GetSeparatedString(SequenceCmd, 1)
					Case "PRESSKEYS"
						SequenceCmd = Mid(SequenceCmd, 11)
						PressKeys SequenceCmd
					Case "SENDKEYS"
						SequenceCmd = Mid(SequenceCmd, 10)
						SendKeys SequenceCmd
					Case "PENTYPING"
						PEN_TOUCH_TIME = GetSeparatedString(SequenceCmd, 1)
						Gosub PenTyping
					CaseElse
						If s1 <> ""
							Controls OpenTextBox
							Print CRLF + " Unrecognized command: " + SequenceCmd + CRLF
							Beep
							Stop
						EndIf
				EndSelect
		EndSelect
	Next
	SequenceFileName = Replace(SequenceFileName,".seq","")
	Print SequenceFileName + " executed in " + Format(ElapsedTime - ExecSequenceStartTime, "0.00") + " seconds" + CRLF
Return

' =====================================================================
'  CORE XY
' =====================================================================
Variable Numeric DestX				' destination in mm coords
Variable Numeric DestY				' destination in mm coords

Variable Numeric Slot_MotorA
Variable Numeric Slot_MotorB
Variable Numeric Slot_EnableMotors
Variable Numeric Slot_SensorX
Variable Numeric Slot_SensorY

Label CoreXY_MoveToDestXY_AndWait   ' move to destX & destY and wait stop motors
	Gosub CoreXY_MoveToDestXY
	Gosub WaitMotors
Return

Label CoreXY_MoveToDestXY			' move to destX & destY (with limits)
	DestX = Limit(DestX, DestxMin, DestxMax)
	DestY = Limit(DestY, DestyMin, DestyMax)
	If LimitReached
		Load sound\GeigerClick_16.wav
	EndIf
	Gosub CoreXY_PrepareCoreAB
	Slot(Slot_MotorA) = CoreA
	Slot(Slot_MotorB) = CoreB
Return

Label CoreXY_MoveToDestXY_NoLimits	' move to destX & destY (without using limits)
	Gosub CoreXY_PrepareCoreAB
	Slot(Slot_MotorA) = CoreA
	Slot(Slot_MotorB) = CoreB
Return

Label CoreXY_SetNewZero
    ' ------------------------- Reset axis X and Y
	Slot(Slot_MotorA) = Reset
	Slot(Slot_MotorB) = Reset
	Wait Seconds 0.15
	' ------------------------- Set reset values with CoreXY correction
	DestX = 0
	DestY = 0
	Gosub CoreXY_MoveToDestXY_NoLimits
	Gosub WaitMotors
Return

Variable Numeric CoreA
Variable Numeric CoreB

Label CoreXY_PrepareCoreAB
	CoreA = (DestX - DestY) / 2
	CoreB = (DestX + DestY) / 2
Return

' =====================================================================
'  HOME
' =====================================================================
Variable Numeric HomeAxis
Variable Numeric SlotSensor
Variable Numeric DetachMM
Variable Numeric SpeedSearch
Variable Numeric SpeedReturn
Variable Numeric InitialPosition

Label Zero
	Gosub CoreXY_SetNewZero
	Gosub EnableMotors
	Gosub PenUp
	Gosub HomeX
	Gosub HomeY
	Gosub CoreXY_SetNewZero
	Gosub PrintValues
	'Gosub "DISAB.MOTORS"
Return

Label HomeX
	HomeAxis = 0        	  ' 0 = Axis X
	SlotSensor = Slot_SensorX ' home sensor slot
	DetachMM = 1			  ' initial detach mm (neg or pos)
	SpeedSearch = -0.6		  ' direction and speed
	SpeedReturn = +0.05		  ' detach direction and speed
	InitialPosition = 1       ' 8 move to MM
	Gosub  Home
Return


Label HomeY
	HomeAxis = 1        	  ' 1 = Axis Y
	SlotSensor = Slot_SensorY ' home sensor slot
	DetachMM = 1			  ' initial detach mm (neg or pos)
	SpeedSearch = -0.6		  ' direction and speed
	SpeedReturn = +0.05		  ' detach direction and speed
	InitialPosition = 1       ' move to MM
	Gosub Home
Return

Label Home
	' -------------------------- detach if sensor is active
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		If HomeAxis = 0
			DestX = DestX + DetachMM
		Else
			DestY = DestY + DetachMM
		EndIf
		Gosub CoreXY_MoveToDestXY_NoLimits
		Gosub WaitMotors
	EndIf
    ' -------------------------- search the sensor
Label SearchHome
	If HomeAxis = 0
		DestX = DestX + SpeedSearch
	Else
		DestY = DestY + SpeedSearch
	EndIf
	Gosub CoreXY_MoveToDestXY_NoLimits
	'
	Wait Seconds 0.005
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		Gosub CoreXY_SetNewZero
		Goto DetachSensor
	EndIf
Goto SearchHome
    ' -------------------------- detach from the sensor
Label DetachSensor
	If HomeAxis = 0
		DestX = DestX + SpeedReturn
	Else
		DestY = DestY + SpeedReturn
	EndIf
	Gosub CoreXY_MoveToDestXY_NoLimits
	Wait Seconds 0.005
	If Slot (SlotSensor) < 500 Or Key("CTRL")
		Gosub WaitMotors
		Gosub CoreXY_SetNewZero
		Goto MoveToInitialPosition
	EndIf
Goto DetachSensor
    ' -------------------------- move to InitialPosition
Label MoveToInitialPosition
	If HomeAxis = 0
		DestX = InitialPosition
	Else
		DestY = InitialPosition
	EndIf
	Gosub CoreXY_MoveToDestXY_NoLimits
	Gosub WaitMotors
Return

' =====================================================================
'  WAIT until the motors A and B are stopped
' =====================================================================
Label WaitMotors
	Wait Seconds 0.1         ' this ensures that the motors are running
	Label WaitMotors1
		If Abs(Slot(Slot_MotorA + 1)) < 0.05
			If Abs(Slot(Slot_MotorB + 1)) < 0.05
				Return
			EndIf
		EndIf
		Wait Seconds 0.005
	Goto WaitMotors1
Goto WaitMotors

' =====================================================================
'  DIGITAL MOTORS MOVEMENTS
' =====================================================================
Variable Numeric PEN_DOWN
Variable Numeric PEN_UP
Variable Numeric PEN_TOUCH_TIME
Variable Numeric POWER_IN
Variable Numeric POWER_OUT
Variable Numeric ONOFF_IN
Variable Numeric ONOFF_OUT
Variable Numeric USBPWR_IN
Variable Numeric USBPWR_OUT
Variable Numeric HDMI_IN
Variable Numeric HDMI_OUT
Variable Numeric USB3_IN
Variable Numeric USB3_OUT
Variable Numeric USBC_IN
Variable Numeric USBC_OUT
Variable Numeric VOLUME_IN_UP
Variable Numeric VOLUME_OUT
Variable Numeric VOLUME_IN_DW
Variable Numeric HEARSET_IN
Variable Numeric HEARSET_OUT
Variable Numeric MEMORY_IN
Variable Numeric MEMORY_OUT



Variable Numeric SLOT_PEN			' digital motor 1
Variable Numeric SLOT_POWER			' digital motor 2
Variable Numeric SLOT_ONOFF			' digital motor 3
Variable Numeric SLOT_USBPWR		' digital motor 4
Variable Numeric SLOT_HDMI			' digital motor 5
Variable Numeric SLOT_USB3			' digital motor 6
Variable Numeric SLOT_USBC			' digital motor 7
Variable Numeric SLOT_VOLUME		' digital motor 8
Variable Numeric SLOT_HEARSET		' digital motor 9
Variable Numeric SLOT_MEMORY		' digital motor 10
Variable Numeric SLOT_TabletPower   ' Tabllet power sensing

Label PowerOut
	Slot(SLOT_POWER) = POWER_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
	Button 1 Color Chocolate
	Button 2 Color Red
Return

Label PowerIn
	Slot(SLOT_POWER) = POWER_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
	Button 1 Color Lime
	Button 2 Color Chocolate
Return

Label Usb3Out
	Slot(SLOT_USB3) = USB3_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label Usb3In
	Slot(SLOT_USB3) = USB3_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label UsbcIn
	Slot(SLOT_USBC) = USBC_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label UsbcOut
	Slot(SLOT_USBC) = USBC_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label HdmiOut
	Slot(SLOT_HDMI) = HDMI_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label HdmiIn
	Slot(SLOT_HDMI) = HDMI_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label UsbPwrOut
	Slot(SLOT_USBPWR) = USBPWR_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label UsbPwrIn
	Slot(SLOT_USBPWR) = USBPWR_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return


Label ReceiveFileFromCOM
	Variable String FileName
	Variable String Str1
	'
	COM DiscardInBuffer
	'
	Str1 = ""
	For v1 = 1 To 9e99
		s1 = COM_Received
		If s1 <> ""
			s1 = Replace(s1, "{{{CRLF}}}", CRLF)
			Str1 = Str1 + s1
			If s1 = "{{{FileEndMarker}}}"
				Str1 = Replace(Str1, "{{{FileEndMarker}}}", "")
				Save DeleteFile FileName
				Save StringToFile Str1 FileName
				Exit
			EndIf
		EndIf
		Wait Seconds 0.01
	Next
Return



Label OnOffOut
	Slot(SLOT_ONOFF) = ONOFF_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label OnOffIn
	Slot(SLOT_ONOFF) = ONOFF_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label VolumeOut
	Slot(SLOT_VOLUME) = VOLUME_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label VolumeUp
	Slot(SLOT_VOLUME) = VOLUME_IN_UP
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label VolumeDw
	Slot(SLOT_VOLUME) = VOLUME_IN_DW
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label MemoryOut
	Slot(SLOT_MEMORY) = MEMORY_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label MemoryIn
	Slot(SLOT_MEMORY) = MEMORY_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label HearsetOut
	Slot(SLOT_HEARSET) = HEARSET_OUT
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label HearsetIn
	Slot(SLOT_HEARSET) = HEARSET_IN
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label PenUp
	Slot(SLOT_PEN) = PEN_UP
	Wait Seconds 0.1
	'Gosub SleepAllServo
Return

Label PenDown
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.5
	'Gosub SleepAllServo
Return

Label PenSingle
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.35
	Slot(SLOT_PEN) = PEN_UP
	Wait Seconds 0.1
	Gosub SleepAllServo
Return

Label PenDouble
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.09
	Slot(SLOT_PEN) = PEN_UP
	Wait Seconds 0.1
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.09
	Slot(SLOT_PEN) = PEN_UP
	Wait Seconds 0.5
	Gosub SleepAllServo
Return

Label PenTyping
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds PEN_TOUCH_TIME
	Slot(SLOT_PEN) = PEN_UP
Return

Label PowerOn
	Gosub  Usb3In
	Button 7 Color Flashing8 Yellow5 Red5
	Gosub  OnOffIn
	Wait Seconds 3
	If Slot(SLOT_TabletPower) > 500
		Button 7 Color PaleGreen
		Button 8 Color Red
	Else
		Print "POWER ON FAILURE"
	EndIf
	Gosub OnOffOut
Return


Label PowerOFF
	Gosub  Usb3In
	Button 7 Color Yellow6
	Button 8 Color Flashing8 Yellow5 Red5
	Gosub  OnOffIn
	Wait Seconds 10
	If Slot(SLOT_TabletPower) < 500
		Button 7 Color Black
		Button 8 Color Black
	EndIf
	Gosub OnOffOut
Return

Label EnableMotors
	Gosub WaitMotors
	Slot(Slot_EnableMotors) = 1000
Return

Label DisableMotors
	Gosub WaitMotors
	Slot(Slot_EnableMotors) = 0
Return

Label SleepAllServo
	Slot(SLOT_USB3) = Sleep
	Slot(SLOT_HDMI) = Sleep
	Slot(SLOT_USBPWR) = Sleep
	Slot(SLOT_ONOFF) = Sleep
	Slot(SLOT_POWER) = Sleep
	Slot(SLOT_VOLUME) = Sleep
	Slot(SLOT_MEMORY) = Sleep
	Slot(SLOT_HEARSET) = Sleep
	Slot(SLOT_PEN) = Sleep
	Slot(SLOT_USBC) = Sleep
Return

' ====================================================================
'  EMERGENCY & SPEED
' ====================================================================
Variable Numeric Slot_Emergency
Variable Numeric Slot_Speed

Label Emergency
	Gosub DisableAllButtons
	Button 16 Enabled
	Button 16 Color Flashing7 Yellow Red
	KeyJog_Speed = 0
	For v1 = 1 To 9e99
		Wait Seconds 0.2	 '1.1
		'Load sound\Alarm10b.wav
		Load sound\Click1.wav
	Next
Stop

Label ExecSpeed
	v9 = Slot(Slot_Speed)
	v9 = 3 + 6 * v9 / 1000
	Option Speed V9
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

' =====================================================================
'  STEPPER-MOTORS MOVE SLOW
' =====================================================================
Variable Numeric MoveSlowXY_DestX 	' destination in mm
Variable Numeric MoveSlowXY_DestY 	' destination in mm
Variable Numeric MoveSlowXY_Speed 	' speed in mm/sec

Variable Numeric MSV1
Variable Numeric MSV2
Variable Numeric MSV3
Variable Numeric MSV4

Label MoveSlowXY
	' ----------------------------------- test for very low speed
	If MoveSlowXY_Speed < 0.1
		MoveSlowXY_Speed = 1
	EndIf
	' ----------------------------------- calc the movement in mm
	MSV1 = MoveSlowXY_DestX - DestX
	MSV2 = MoveSlowXY_DestY - DestY
	MSV3 = MSV1 * MSV1
	MSV3 = MSV3 + MSV2 * MSV2
	MSV3 = Sqrt(MSV3)
	' ----------------------------------- calc the loops count
	MSV4 = MSV3 / 0.01 '<<< loop time
	MSV4 = Int(MSV4 / MoveSlowXY_Speed)
	' ----------------------------------- too much speed ?
	If MSV4 = 0
		DestX = MoveSlowXY_DestX
		DestY = MoveSlowXY_DestY
		Gosub CoreXY_MoveToDestXY_AndWait
		Return
	EndIf
	' ----------------------------------- prepare the increments
	MSV1 = (MoveSlowXY_DestX - DestX) / MSV4
	MSV2 = (MoveSlowXY_DestY - DestY) / MSV4
	' ----------------------------------- loop for v4 times
	For MSV3 = 1 To MSV4
		' ------------------------------- increment, move and wait
		DestX = DestX + MSV1
		DestY = DestY + MSV2
		Gosub CoreXY_MoveToDestXY
		Wait Seconds 0.01 '<<< loop time
	Next
	Gosub WaitMotors
Return

' =====================================================================
'  STEPPER-MOTORS DRAW CIRCLE
' =====================================================================
Variable Numeric DrawCircle_X 	    ' Center X in mm
Variable Numeric DrawCircle_Y 	    ' Center Y in mm
Variable Numeric DrawCircle_Radius  ' Radius in mm
Variable Numeric DrawCircle_Speed 	' Speed in mm/sec

Variable Numeric DCV1
Variable Numeric DCV2
Variable Numeric DCV3
Variable Numeric DCV4

Label DrawCircle
	' ----------------------------------- test for very low speed
	If DrawCircle_Speed < 0.1
		DrawCircle_Speed = 1
	EndIf
	' ----------------------------------- calc the movement in mm
	DCV3 = DrawCircle_Radius * Math_PI * 2
	' ----------------------------------- calc the loops count
	DCV4 = DCV3 / 0.01 '<<< loop time
	DCV4 = Int(DCV4 / DrawCircle_Speed)
	' ----------------------------------- too much speed ?
	If DCV4 < 16
		DCV4 = 16
	EndIf
	' ----------------------------------- prepare the increments
	DCV1 = 2 * Math_PI / DCV4
	' ----------------------------------- loop for v4 times
	For DCV3 = 0 To DCV4
		' ------------------------------- calc circle
		DCV2 = DCV3 * DCV1
		DestX = DrawCircle_X + DrawCircle_Radius * Sin(DCV2)
		DestY = DrawCircle_Y + DrawCircle_Radius * Cos(DCV2)
		' ------------------------------- move and wait
		Gosub CoreXY_MoveToDestXY
		Wait Seconds 0.01 '<<< loop time
	Next
	Gosub WaitMotors
Return

' =====================================================================
'  STEPPER-MOTORS and PEN action SLIDE
' =====================================================================
Variable Numeric Slide_DestX 	 ' destination in mm
Variable Numeric Slide_DestY 	 ' destination in mm
Variable Numeric Slide_Speed 	 ' speed in mm/sec
Variable Numeric Slide_PenUpTime ' pen-up time in mS from start

Variable Numeric SLV1
Variable Numeric SLV2
Variable Numeric SLV3
Variable Numeric SLV4
Variable Numeric SLV5

Label Slide
	Gosub CoreXY_MoveToDestXY_AndWait
	Slide_DestX = 50                  '50
	Slide_DestY = 130                 '130
	Slide_Speed = 200                 '200
	Slide_PenUpTime = 500             '500
	' ----------------------------------- calc the movement in mm
	SLV1 = Slide_DestX - DestX
	SLV2 = Slide_DestY - DestY
	SLV3 = SLV1 * SLV1
	SLV3 = SLV3 + SLV2 * SLV2
	SLV3 = Sqrt(SLV3)
	' ----------------------------------- calc the loops count
	SLV4 = SLV3 / 0.01 '<<< loop time
	SLV4 = Int(SLV4 / Slide_Speed)
	' ----------------------------------- prepare the increments
	SLV1 = (Slide_DestX - DestX) / SLV4
	SLV2 = (Slide_DestY - DestY) / SLV4
	' ----------------------------------- prepare the pen-up time
	SLV5 = Slide_PenUpTime / 1000
	SLV5 = Int(SLV5 / 0.01) '<<< loop time
	' ----------------------------------- pen down
	Slot(SLOT_PEN) = PEN_DOWN
	' ----------------------------------- loop for v4 times
	For SLV3 = 1 To SLV4
		' ------------------------------- increment, move and wait
		DestX = DestX + SLV1
		DestY = DestY + SLV2
		Gosub CoreXY_MoveToDestXY
		Wait Seconds 0.01 '<<< loop time
		' ------------------------------- pen up
		If SLV3 = SLV5
			Slot(SLOT_PEN) = PEN_UP
		EndIf
	Next
	Gosub WaitMotors
Return

' ================================================
'  JOG WITH MOUSE
' ================================================
Variable Numeric Jog_Enable_Slot
Variable Numeric Jog_RangeX
Variable Numeric Jog_RangeY
Variable Numeric Jog_Active

Label JogWithMouse
	Gosub EnableMotors
	Button 8 Text "JOG STOP"
	Jog_Active = 1
	Label Jog2
		If Slot(Jog_Enable_Slot) > 500
			DestX = MouseX * Jog_RangeX
			DestY = MouseY * Jog_RangeY
			Gosub CoreXY_MoveToDestXY
			Gosub PrintValues
		EndIf
		If Jog_Active = 0
			Return
		EndIf
	Goto Jog2
Return

Label JogStop
	Button 8 Text "JOG with Mouse"
	Jog_Active = 0
Return

' ====================================================================
'  JOG with keyboard
' ====================================================================
Label ExecKeys
	Variable Numeric KeyJog_Speed
	Variable Numeric JogSpeed
	JogSpeed = KeyJog_Speed / 100
	Select True
		Case Key("SHIFT")
			JogSpeed = JogSpeed * 0.1
		Case Key("CTRL")
			JogSpeed = JogSpeed * 0.01
		Case Key("ALT")
			JogSpeed = JogSpeed * 0.001
	EndSelect
	'
	If Key("Right")
		DestX = DestX + JogSpeed
	EndIf
	If Key("Left")
		DestX = DestX - JogSpeed
	EndIf
	'
	If Key("Up")
		DestY = DestY + JogSpeed		'proto
		'DestY = DestY - JogSpeed		'Leo
	EndIf
	If Key("Down")
		DestY = DestY - JogSpeed		'proto
'		DestY = DestY - JogSpeed		'Leo
	EndIf
	'
	'If Key("PageUp")
	'	Slot Slot_Z = Limit(Slot(Slot_Z) + JogSpeed, Zmin, Zmax)
	'EndIf
	'If Key("PageDown")
	'	Slot Slot_Z = Limit(Slot(Slot_Z) - JogSpeed, Zmin, Zmax)
	'EndIf
	'
	Gosub CoreXY_MoveToDestXY
	Gosub PrintValues
Return

' ====================================================================
'  PRINT COORDINATES
' ====================================================================
Label PrintValues
	Controls OpenTextBox
	s1 =       "DestX=" + Format(DestX, "0.000")
	s1 = s1 + " DestY=" + Format(DestY, "0.000")
	Print CRLF + s1
Return


'WATCH v1
'EXEC DestX = 12
