'╔═════════════════════════════════════════════════════════╗
'║   Name: Meti_Linear_V00.txt           Date: 15 Aug 2022 ║
'║ Author: Leonardo                                        ║
'║  Scope: Test overall functionality                      ║
'╠═════════════════════════════════════════════════════════╣
'║Hardware: STEPPER MOTOR DRIVEN in open loop              ║
'╟─────────────────────────────────────────────────────────╢
'║ Note: driver TB6600                                     ║
'║                                                         ║
'╚═════════════════════════════════════════════════════════╝
Option Speed 9
'Beep "2500 100, 1500 100"

'Variable Numeric Debug = 0 ' 1 = DEBUG   0 = NORMAL

RestoreVars

Load images\Meti_Linear.jpg
'Beep "2500 200, 1500 200, 2500 300, 0500, 1500 300"
'TTS SelectVoice Language
'TTS SetVolume  100    '0 = mute
'Wait Seconds 2
TTS SetSpeed -1

'Window MinSize


' ╔══════════════════════════════════════════╗ █   █ █▀▀█ █▀▀▀█
' ║ VARIABLE INITIALIZATIONS & ASSIGNEMENTS  ║ █   █ █▄▄█ █▄▄▄█
' ╚══════════════════════════════════════════╝  ▀▄▀  █  █ █  ▀▄
Variable Numeric Slot_M_1        =  1
Variable Numeric Slot_EnMotors   = 10
Variable Numeric Slot_HomeSensor =  6
Variable Numeric Slot_LedR       =  7
Variable Numeric Slot_LedG       =  8
Variable Numeric Slot_LedB       =  9
Variable Numeric Slot_SignScope  = 13

' -------------------------------------- Software limits
Variable Numeric M1max =  3600
Variable Numeric M1min =     0

Key 1 BtnCoord_M1

' ╔══════════════════════════════════════════╗ █▀▀▄ █  █ ▀▀█▀▀
' ║ BUTTONS                                  ║ █▀▀█ █  █   █
' ╚══════════════════════════════════════════╝ █▄▄█ █▄▄█   █
Button 1 Label Home
Button Home Color Aquamarine

Button 2 Label "BtnCoord_M1"
Button BtnCoord_M1 Text "M1 = ?"
Button BtnCoord_M1 Color LightBlue

Button 3 Label Position
Button Position Text "Position=?"
Button Position Color Lavender

Button 4 Label Position_300
Button Position_300 Text "Position=300"
Button Position_300 Color Aquamarine

Button 5 Label Position_250
Button Position_250 Text "Position=250"
Button Position_250 Color Aquamarine

Button 6 Label Position_200
Button Position_200 Text "Position=200"
Button Position_200 Color Aquamarine

Button 7 Label Position_150
Button Position_150 Text "Position=150"
Button Position_150 Color Aquamarine

Button 8 Label Position_100
Button Position_100 Text "Position=100"
Button Position_100 Color Aquamarine

Button 9 Label Position_50
Button Position_50 Text "Position=50"
Button Position_50 Color Aquamarine

Button 10 Label Position_2
Button Position_2 Text "Position=2"
Button Position_2 Color Aquamarine

Button 12 Label EnableMotors
Button EnableMotors Text "Enable Motors"
Button EnableMotors Color PaleGreen

Button 13 Label DisableMotors
Button DisableMotors Text "Disable Motors"
Button DisableMotors Color PaleGreen

Button 16 Label CloseAll
Button CloseAll Text "CLOSE ALL"
Button CloseAll Color Red


Button 20 Label MotorTest
Button MotorTest Text "MotorTest"
Button MotorTest Color Orange

Button 17 Label RepTest
Button RepTest Text " "REPET.TEST""
Button RepTest Color Khaki

'Button 16 Text Emergency
'Button Emergency Text "EMERGENCY"
'Button Emergency Color Red
'Button Emergency Slot  Slot_Emergency

Button 22 Label RedOn
Button RedOn Color Red
Button 23 Label GreenOn
Button GreenOn Color Green
Button 24 Label YellowOn
Button YellowOn Color Yellow
Button 25 Label BlueOn
Button BlueOn Color MediumBlue
Button 26 Label WhiteOn
Button GreenOn Color Green
Button 27 Label RgbOff
Button RgbOff Color Black

Button 28 Label EnableVoice
Button EnableVoice Text "EnableVoice"
Button EnableVoice Color SkyBlue
Button 29 Label DisableVoice
Button DisableVoice Text "Disable Motors"
Button DisableVoice Color SkyBlue

' -------------------------------------- KEYS
Key Right		 ExecKeys
Key Left 		 ExecKeys

' ╔══════════════════════════════════════════╗ █   █▀▀█ █▀▀█ █▀▀▄
' ║ LOAD APPS                                ║ █   █  █ █▄▄█ █  █
' ╚══════════════════════════════════════════╝ █▄▄ █▄▄█ █  █ █▄▄█

Load Theremino_HAL.exe
Load Theremino_SlotViewer.exe
Load Theremino_Cobot.exe
Load Theremino_SignalScope.exe

' ╔══════════════════════════════════════════╗  █▀▀▀ ▀▀█▀▀ █▀▀▀█ █▀▀▀█ ▀▀█▀▀
' ║  START FROM HERE                         ║  ▀▀▀█   █   █▀▀▀█ █▀█▀▀   █
' ╚══════════════════════════════════════════╝  ▄▄▄█   █   █   █ █  ▀▄   █
EnableMotors
Controls OpenTextBox
DisableAllButtons
Controls ClearTextBox
GreenOn
Print TTSvoices
EnableAllButtons
UpdateButtons
Load Hide
'EnableVoice
DisableVoice
Loop
Stop

' ╔══════════════════════════════════════════╗ █▀▀ █   █▀▀█ █▀▀▀ █▀▀▀
' ║ CLOSE ALL                                ║ █   █   █  █ ▀▀▀█ █▀▀
' ╚══════════════════════════════════════════╝ █▄▄ █▄▄ █▄▄█ ▄▄▄█ █▄▄▄
Label CloseAll
	RedOn
	SaveVars
	DisableMotors
	Window Sizable
	Slot(Slot_SignScope) = 0
	TTS Speak "End Of Program."
	Load CloseApps
Return

' ╔════════════════════════════════════════════╗ █▀▀▀ ▀▀█▀▀ █▀▀█ █▀▀█
' ║ STOP EVENT (when pressing stop button)     ║ ▀▀▀█   █   █  █ █▄▄█
' ╚════════════════════════════════════════════╝ ▄▄▄█   █   █▄▄█ █
Label EventStop
	SaveVars
	DisableMotors
	RedOn
	EnableAllButtons
	Window Sizable
	Slot(Slot_SignScope) = 0
	TTS Speak "End Of Program."
End

' ╔══════════════════════════════════════════╗ █   █▀▀█ █▀▀█ █▀▀█
' ║ LOOP                                     ║ █   █  █ █  █ █▄▄█
' ╚══════════════════════════════════════════╝ █▄▄ █▄▄█ █▄▄█ █
Label Loop
	UpdateButtons
	' ------------------------------------------
	' 0.02 sec. or more to allow time for keys and other tasks
	Wait Seconds 0.02
	' ---------------------------------------------------------
Goto Loop

' ╔══════════════════════════════════════════╗
' ║ ENABLE/DISABLE BUTTONS                   ║
' ╚══════════════════════════════════════════╝
Label EnableAllButtons
	For v1 = 1 To 16
		Button v1 Enabled
	Next
Return

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

' ╔══════════════════════════════════════════╗
' ║ SAVE LOAD VARS                           ║
' ╚══════════════════════════════════════════╝
Label SaveVars
	Save Vars
Return

Label RestoreVars
	Load Vars
Return


' ╔════════════════════════════════════════════╗ █  █ █▀▀█ █▀▄▀█ █▀▀▀
' ║ HOMING STEPPERS                            ║ █▀▀█ █  █ █   █ █▀▀
' ╚════════════════════════════════════════════╝ █  █ █▄▄█ █   █ █▄▄▄
Label Home
	EnableMotors
	Slot(Slot_M_1) = Slot(Slot_M_1) -10
Goto Homing_M1

Label Homing_M1
	Slot(Slot_M_1) = Slot(Slot_M_1) + 0.5		' direction and speed goto switch
	Wait Seconds 0.02		              	' do not alter, loop time 0.02 Sec
	If Slot(Slot_HomeSensor) > 500
		Goto Homing_Mot_2
	EndIf
Goto Homing_M1

Label Homing_Mot_2
	Slot(Slot_M_1) = Slot(Slot_M_1) - 0.2	' 0.05 direction and speed return from switch
	Wait Seconds 0.02		             	' do not alter, loop time 0.02 Sec
	If Slot(Slot_HomeSensor) < 500
		Goto Homing_Mot_3
	EndIf
Goto Homing_Mot_2

Label Homing_Mot_3
	Wait Seconds 0.1
	Slot Slot_M_1 = Reset				' Reset the slot value
	Wait Seconds 0.2
	Slot Slot_M_1 = 301                  ' Declared position in mm
	Wait Seconds 0.2
	Slot Slot_M_1 = 300					' move to this position
	WaitAllMotors
Return

' ╔════════════════════════════════════════════╗ █▀▄▀█ █▀▀█ █   █ █▀▀▀
' ║ MOVE STEPPERS                              ║ █   █ █  █ █   █ █▀▀
' ╚════════════════════════════════════════════╝ █   █ █▄▄█  ▀▄▀  █▄▄▄
Label Position_300
	TTS Speak "Motor go at 300 Position."
	Slot(Slot_M_1) = 300
	UpdateButtons
	Wait TTSready
	RedOn
	WaitAllMotors
Return

Label Position_250
	TTS Speak "Motor go at 250 Position."
	Slot(Slot_M_1) = 250
	UpdateButtons
	Wait TTSready
	RedOn
	WaitAllMotors
Return

Label Position_200
	TTS Speak "Motor go at 200 Position."
	Slot(Slot_M_1) = 200
	UpdateButtons
	Wait TTSready
	GreenOn
	WaitAllMotors
Return

Label Position_150
	TTS Speak "Motor go at 150 Position."
	Slot(Slot_M_1) = 150
	UpdateButtons
	Wait TTSready
	YellowOn
	WaitAllMotors
Return

Label Position_100
	TTS Speak "Motor go at 100 Position."
	Slot(Slot_M_1) = 100
	UpdateButtons
	Wait TTSready
	BlueOn
	WaitAllMotors
Return

Label Position_50
	TTS Speak "Motor go at 50 Position."
	Slot(Slot_M_1) = 50
	UpdateButtons
	Wait TTSready
	WhiteOn
	WaitAllMotors
Return

Label Position_2
	TTS Speak "Motor go at 2 Position."
	Slot(Slot_M_1) = 2
	UpdateButtons
	Wait TTSready
	RedOn
	WaitAllMotors
Return

Label Position
	V1 = Input "Write the destination."
	'TTS Speak "Motor go at 50 Position."
	Slot(Slot_M_1) = v1
	UpdateButtons
	Wait TTSready
	WhiteOn
	WaitAllMotors
Return

' ╔══════════════════════════════════════════╗ █   █ █▀▀█ █ ▀▀█▀▀
' ║ WAIT ALL MOTOR until stopped             ║ █   █ █▄▄█ █   █
' ╚══════════════════════════════════════════╝ ▀▄▀▄▀ █  █ █   █
Label WaitAllMotors
	
	Variable Numeric WAM1
	Wait Seconds 0.1
	For WAM1 = 1 To Infinity
		If Abs(Slot(Slot_M_1 + 1)) < 0.01
			Exit
		EndIf
		Wait Seconds 0.05
		UpdateButtons
	Next
Return

' ╔══════════════════════════════════════════╗
' ║ EMERGENCY                                ║
' ╚══════════════════════════════════════════╝
Label Emergency
'	DisableAllButtons
'	Button 15 Enabled
'	Button 15 Color Flashing7 Yellow Red
'	For v1 = 1 To Infinity
'		RedOn
'		Wait Seconds 0.4
'		RgbOff
'		Wait Seconds 0.4
'		Load sound\Alarm10b.wav
'	Next
Stop

' ╔════════════════════════════════════════════╗ █▀▀▀ █  █ █▀█
' ║  S U B R O U T I N E   A R E A             ║ ▀▀▀█ █  █ █▀▀█
' ╚════════════════════════════════════════════╝ ▄▄▄█ █▄▄█ █▄▄█
Label EnableVoice
	TTS SetVolume 100
	Button EnableVoice Disabled
	Button DisableVoice Enabled
	
Return

Label DisableVoice
	TTS SetVolume 0
	Button EnableVoice Enabled
	Button DisableVoice Disabled
Return



Label EnableMotors
	Slot(Slot_EnMotors) = 1000
	Button EnableMotors Disabled
	Button DisableMotors Enabled
	Wait Seconds 0.01
Return

Label DisableMotors
	Slot(Slot_EnMotors) = 0
	Button DisableMotors Disabled
	Button EnableMotors Enabled
	Wait Seconds 0.01
Return


' ╔══════════════════════════════════════════╗ █   █▀▀ █▀▀▄
' ║ LED COLORS                               ║ █   █▀  █  █
' ╚══════════════════════════════════════════╝ █▄▄ █▄▄ █▄▄█
Label RedOn
	TTS Speak "Status red."
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 0
	Slot Slot_LedB = 0
	Wait TTSready
Return

Label MagentaOn
	TTS Speak "Status magenta."
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 0
	Slot Slot_LedB = 1000
Return

Label YellowOn
	TTS Speak "Status yellow."
	Slot Slot_LedR = 900
	Slot Slot_LedG = 400
	Slot Slot_LedB = 0
	Wait TTSready
Return

Label GreenOn
	TTS Speak "Status green."
	Slot Slot_LedR = 0
	Slot Slot_LedG = 1000
	Slot Slot_LedB = 0
	Wait TTSready
Return

Label BlueOn
	TTS Speak "Status blue."
	Slot Slot_LedR = 0
	Slot Slot_LedG = 0
	Slot Slot_LedB = 1000
	Wait TTSready
Return

Label WhiteOn
	TTS Speak "Status white."
	Slot Slot_LedR = 1000
	Slot Slot_LedG = 1000
	Slot Slot_LedB = 1000
	Wait TTSready
Return

Label RgbOff
	TTS Speak "Status black."
	Slot Slot_LedR = 0
	Slot Slot_LedG = 0
	Slot Slot_LedB = 0
	Wait TTSready
Return

Label RedBlink
	RedOn
	WhiteOn
Return

' ╔══════════════════════════════════════════╗ ▀▀█▀ █▀▀█ █▀▀█  █  █ █▀▀▀ █   █
' ║ JOG WITH KEYBOARD                        ║   █  █  █ █ ▄▄  █▄▀  █▀▀   ▀▄▀
' ╚══════════════════════════════════════════╝ █▄█  █▄▄█ █▄▄█  █ ▀▄ █▄▄▄   █
Label ExecKeys
	Variable Numeric JogSpeed = 0.1
	Select True
		Case Key("SHIFT")
			JogSpeed = JogSpeed * 0.01
		Case Key("CTRL")
			JogSpeed = JogSpeed * 0.001
	EndSelect
	'
	Select MotorToEdit
		Case 1
			If Key("Up")
				Slot(Slot_M_1) = Limit(Slot(Slot_M_1) - JogSpeed, M1min, M1max)
			EndIf
			If Key("Down")
				Slot(Slot_M_1) = Limit(Slot(Slot_M_1) + JogSpeed, M1min, M1max)
			EndIf
	EndSelect
	'
	PrintValues
	'EnableMotors
	'YellowOn
	UpdateButtons
Return

' ╔══════════════════════════════════════════╗ ▀▀█▀ █▀▀█ █▀▀▀   █▄ ▄█ █▀▀█ ▀▀█▀▀
' ║  MOTOR SELECTION FOR JOG                 ║   █  █  █ █ ▄▄   █ ▀ █ █  █   █
' ╚══════════════════════════════════════════╝ █▄█  █▄▄█ █▄▄█   █   █ █▄▄█   █
Variable Numeric MotorToEdit
Label defaultcolors
	Button BtnCoord_M1 Color LightBlue
Return

Label BtnCoord_M1
	MotorToEdit = 1
	defaultcolors
	Button BtnCoord_M1 Color Blue
Return

' ====================================================================
'  PRINT COORDINATES AND UPDATE COORDINATE BUTTONS
' ====================================================================
Label PrintValues
	s1 = "MoveTo " + Format(Slot(Slot_M_1), "0.00")
	Print CRLF + s1
Return

Label UpdateButtons
	Button BtnCoord_M1 Text "M1=" + Format(Slot(Slot_M_1) - Slot(Slot_M_1 + 1), "0.00") + "mm"
Return


'--------------------------------------------------------------
'Index  Name                          Culture  Gender   Age
'--------------------------------------------------------------
'1      Microsoft David Desktop       en-US    Male     Adult
'2      Microsoft Zira Desktop        en-US    Female   Adult
'3      Microsoft Elsa Desktop        it-IT    Female   Adult
'4      Microsoft Huihui Desktop      zh-CN    Female   Adult
'--------------------------------------------------------------


'WATCH Slot(Slot_HomeSensor)
