' ====================================================================
'     Name: Stitcher_0.1.txt                         Date: 05 Aug 2021
'   Author: Leonardo DE PALO Ph.D. & Roberto Fasciani Ms.C.
'    Scope: Early test of the stitcher usinng hal or ArduHAL
' Hardware: Arduino nano
'     note:
' ====================================================================
'   SWITCH SETTING LEADSHINE MA860
'  1    2    3    4    5    6    7    8
'                     ON   ON   ON   ON   400
'                    OFF   ON   ON   ON   800
'                     ON  OFF   ON   ON  1600   <<<<<
'                OFF                            power saving
'  ON   ON   ON                                 2.00 Amp
' OFF   ON   ON                                 2.57 Amp
'  ON  OFF   ON                                 3.14 Amp
' OFF  OFF   ON                                 3.71 Amp



'  ON  OFF  OFF                                 5.43 Amp
' ====================================================================

Option Speed 9
Option GlobalKeys Enabled
'Load "Media\Images\xxxxx.jpg"
'VarsFromFile ".\Theremino_HAL\Theremino_Notepad\ConfigVars.txt"

' -------------------------------------- LOAD EXTERNAL PROGRAMS

'Load Theremino_ArduHAL\Theremino_ArduHAL.exe
'Load Theremino_ArduHAL\Theremino_SlotViewer.exe
'Load Theremino_ArduHAL\Theremino_SignalScope.exe
'Load Theremino_ArduHAL\Theremino_DAA\DAA.exe

Load "Theremino_HAL\Theremino_HAL.exe"
Load "Theremino_HAL\Theremino_SlotViewer.exe"
Load "Theremino_HAL\Theremino_SignalScope.exe"

' ====================================================================
'  VARIABLES
' ====================================================================
Variable Numeric Slot_LedR
Variable Numeric Slot_LedG
Variable Numeric Slot_LedB
Variable Numeric Slot_EnableMotors
Variable Numeric Slot_MotorX
Variable Numeric Slot_MotorY

' -------------------------------------- Motors limits
Variable Numeric Xmax               = 320
Variable Numeric Xmin               =   2
Variable Numeric Ymax               = 220
Variable Numeric Ymin               =   2

' -------------------------------------- Key Jog Speed
Variable Numeric KeyJog_Speed       = 100

' ====================================================================
'  TEST MASTER
' ====================================================================
For v1 = 1 To 9e99
	For v2 = 1 To 20 ' <<< testing for 2 seconds
		If (Slot(0) >= 1 And Slot(0) <= 2) Or Key("CTRL")
			Exit 2
		EndIf
		Wait Seconds 0.1
	Next
	Print " Invalid master number: " + Slot(0) + CRLF
	Slot 0 = Recognize
	Beep
Next

' ====================================================================
'  INITIALIZATIONS
' ====================================================================
' -------------------------------------- Set slots
'Slot_LedR          = 3
'Slot_LedG          = 4
'Slot_LedB          = 5
Slot_MotorX         = 1
Slot_MotorY         = 3
Slot_EnableMotors   = 10

' ====================================================================
'  START
' ====================================================================
Gosub EnableMotors
Gosub SetButtonsAndKeys
' -------------------------------------- KEYS
Key Left  Gosub ExecKeys
Key Right Gosub ExecKeys
Key Up    Gosub ExecKeys
Key Down  Gosub ExecKeys
Key Space Gosub ExecKeys

Gosub UpdateButtons
Option GlobalKeys Disabled

Stop


' ====================================================================
'  BUTTONS
' ====================================================================
Label SetButtonsAndKeys
	Button 4 Text EditSequence
	Button EditSequence Text "Edit sequence"
	Button EditSequence Color PaleGreen
	
	Button 5 Text RunSequence
	Button RunSequence Text "RUN SEQUENCE"
	Button RunSequence Color Aqua
	
	Button 6 Text SelectSequence
	Button SelectSequence Text "Select Sequence"
	Button SelectSequence Color PowderBlue
	
	Button 8 Text EnableMotors
	Button EnableMotors Color Tan
	
	Button 9 Text DisableMotors
	Button DisableMotors Color Tan
	
	Button 11 Text "Rotation"
	Button Rotation Color Yellow
	Button Rotation Text "Rotation"
	
	Button 12 Text "BtnCoordX"
	Button BtnCoordX Text "X = ?"
	Button BtnCoordX Color Yellow
	
	Button 13 Text "BtnCoordY"
	Button BtnCoordY Text "Y = ?"
	Button BtnCoordY Color Yellow
	
	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
	
	Button 17 Text RedOn
	Button RedOn Color Red
	
	Button 18 Text MagentaOn
	Button MagentaOn Color Magenta
	
	Button 19 Text YellowOn
	Button YellowOn Color Yellow
	
	Button 20 Text GreenOn
	Button GreenOn Color Green
	
	Button 21 Text BlueOn
	Button BlueOn Color MediumBlue
	
	Button 22 Text WhiteOn
	Button GreenOn Color Green
	
	Button 23 Text RgbOff
	Button RgbOff Color Black
	
	Button 25 Text Zero_Degree
	Button Zero_Degree Text "Zero"
	Button Zero_Degree Color LightBlue
	
	Button 26 Text 90_Degree
	Button 90_Degree Text "90"
	Button 90_Degree Color LightBlue
	
	Button 27 Text 180_Degree
	Button 180_Degree Text "180"
	Button 180_Degree Color LightBlue
	
	Button 28 Text 270_Degree
	Button 270_Degree Text "270"
	Button 270_Degree Color LightBlue
	
	Button 29 Text 360_Degree
	Button 360_Degree Text "360"
	Button 360_Degree Color LightBlue
Return

Stop

' ====================================================================
'  EMERGENCY
' ====================================================================
Label Emergency
'	Gosub DisableAllButtons
'	Button 15 Enabled
'	Button 15 Color Flashing7 Yellow Red
'	For v1 = 1 To 9e99
'		Gosub RedOn
'		Wait Seconds 0.4
'		Gosub RgbOff
'		Wait Seconds 0.4
'		Load sound\Alarm10b.wav
'	Next
Stop

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

' ====================================================================
' ENABLE/DISABLE MOTORS
' ====================================================================
Label EnableMotors
	Slot(Slot_EnableMotors) = 0
Return

Label DisableMotors
	Gosub WaitAllMotors
	Slot(Slot_EnableMotors) = 1000
Return

' ====================================================================
'  CLOSING EVENTS
' ====================================================================
Label CloseAll
	'Gosub SaveVarsAndPositions
	Gosub DisableMotors
	Load CloseApps
Return

Label EventStop
	'Gosub SaveVarsAndPositions
	Gosub EnableAllButtons
	Window Sizable
	Gosub DisableMotors
    'Slot(Slot_SignalScope) = 0
	'Gosub AirOFF
	'Gosub WhiteLightOFF
End
' ====================================================================
'  LED COLORS
' ====================================================================
Label RedOn
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 0
	Slot Slot_LedB = 0
Return

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

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

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

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

Label WhiteOn
	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

'====================================================================
'  MOTOR POSITION
' ====================================================================
Label Zero_Degree
	Gosub EnableMotors
	Wait Seconds 0.01
	Slot Slot_MotorX = 0
	Gosub WaitAllMotors
	'Gosub DisableMotors
Return

Label 90_Degree
	Gosub EnableMotors
	Wait Seconds 0.01
	Slot Slot_MotorX = 90
	Gosub WaitAllMotors
	'Gosub DisableMotors
Return

Label 180_Degree
	Gosub EnableMotors
	Wait Seconds 0.01
	Slot Slot_MotorX = 180
	Gosub WaitAllMotors
	'Gosub DisableMotors
Return

Label 270_Degree
	Gosub EnableMotors
	Wait Seconds 0.01
	Slot Slot_MotorX = 270
	Gosub WaitAllMotors
	'Gosub DisableMotors
Return

Label 360_Degree
	Gosub EnableMotors
	Wait Seconds 0.01
	Slot Slot_MotorX = 360
	Gosub WaitAllMotors
	'Gosub DisableMotors
Return

' ====================================================================
'   Wait until all motors are stopped
' ====================================================================
Label WaitAllMotors
	Wait Seconds 0.05
	Variable Numeric WM1
	For WM1 = 1 To 9e99
		If Abs(Slot(Slot_MotorX + 1)) < 0.01
			If Abs(Slot(Slot_MotorY + 1)) < 0.01
				Exit
			EndIf
		EndIf
		Wait Seconds 0.005
	Next
Return

' ====================================================================
'  JOG with keyboard
' ====================================================================
Label ExecKeys
	Gosub EnableMotors
	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("Left")
		Slot(Slot_MotorX) = Limit(Slot(Slot_MotorX) + JogSpeed, Xmin, Xmax)
	EndIf
	If Key("Right")
		Slot(Slot_MotorX) = Limit(Slot(Slot_MotorX) - JogSpeed, Xmin, Xmax)
	EndIf
	If Key("Up")
		Slot(Slot_MotorY) = Limit(Slot(Slot_MotorY) + JogSpeed, Ymin, Ymax)
	EndIf
	If Key("Down")
		Slot(Slot_MotorY) = Limit(Slot(Slot_MotorY) - JogSpeed, Ymin, Ymax)
	EndIf
	
	If Key("Space")
		Gosub DisableMotors
	EndIf
	Gosub PrintValues
	Gosub UpdateButtons
Return

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

Label UpdateButtons
	Button BtnCoordX Text "X =" + Format(Slot(Slot_MotorX), "0.000")
	Button BtnCoordy Text "Y =" + Format(Slot(Slot_MotorY), "0.000")
Return


