'╔════════════════════════════════════════════════════════════╗
'║     Name: WayPointer_04.txt              Date: 04 May 2025 ║
'║   Author: Leonardo                                         ║
'║    Scope: Managing the WayPointer                          ║
'╠════════════════════════════════════════════════════════════╣
'║ Configuration:  4 knob                                     ║
'║                10 buttons                                  ║
'╠════════════════════════════════════════════════════════════╣
'║ Hardware: Master                                           ║
'╠════════════════════════════════════════════════════════════╣
'║ISO   Culture  Gender   Age     Name                        ║
'╟────────────────────────────────────────────────────────────╢
'║ENG   EN-US    Male     Adult   Microsoft David Desktop     ║
'║ENG   EN-US    Female   Adult   Microsoft Zira Desktop      ║
'║ITA   IT-IT    Female   Adult   Microsoft Elsa Desktop      ║
'║JPN   JA-JP    Female   Adult   Microsoft Haruka Desktop    ║
'║ZHO   ZH-CN    Female   Adult   Microsoft Huihui Desktop    ║
'╚════════════════════════════════════════════════════════════╝
Option Speed 9
'Load images\WayPointer.jpg
'Beep "2500 20, 1500 100"
'TTS SelectVoice "ENG"
'TTS SelectVoice "ZHO"
's1 = TTSvoices
'TTS SetVolume 100      '100
'TTS SetSpeed 0
'TTS SelectVoice 5   '3

TTS SetVolume 0

' ╔════════════════════════════════════════════╗
' ║ SLOT TEXT for COMMANDS and RESPONSES       ║
' ╚════════════════════════════════════════════╝
'Variable Numeric Slot_CommandsToMotors      = 50
Variable Numeric Slot_CommandsToCobot       = 51
'Variable Numeric Slot_ResponsesFromCobot    = 52
Variable Numeric Slot_ExternalCommands      = 53
'Variable Numeric Slot_WordRecognized        = 55

Variable String WayPointStatus = 90
SlotText(WayPointStatus) = "OFF"


' ╔════════════════════════════════════════════╗ ▄▀▀▀ █   █▀▀█ ▀▀█▀▀
' ║ SLOT INITIALIZATIONS                       ║ ▀▀▀▄ █   █  █   █
' ╚════════════════════════════════════════════╝ ▄▄▄▀ █▄▄ █▄▄█   █
'Variable Numeric Slot_Pedal              =  10
'Variable Numeric Slot_SetWayPoint        =  814


'Variable Numeric Slot_SafeTorque          = 998

Variable Numeric Slot_MasterStatus        = 878

Variable Numeric Slot_SignalScope         = 883

Variable Numeric Slot_Encoder1            = 871
Variable Numeric Slot_Encoder2            = 872
Variable Numeric Slot_Encoder3            = 873
Variable Numeric Slot_Encoder4            = 874

Variable Numeric Slot_FirstFiveButton     = 876
Variable Numeric Slot_SecondFiveButton    = 877

'Variable Numeric Slot_MotorsFPS           =  56

Variable Numeric Slot_M1_Dest             = 158
Variable Numeric Slot_M2_Dest             = 258
Variable Numeric Slot_M3_Dest             = 358
Variable Numeric Slot_M4_Dest             = 458

Variable Numeric SText_WayPointStatus = 90
'SlotText(SText_WayPointStatus) = "OFF"    'Theremino WayPoint offline




' --------------------------------------------- MicroVideo
'Variable Numeric Slot_TakeImage           = 815
'Variable Numeric Slot_StartStopVideo      = 816
'Variable Numeric Slot_SaveVideo           = 817

Variable Numeric Slot_M1_Min              =   0
Variable Numeric Slot_M1_Max              = 270
Variable Numeric Slot_M2_Min              =   0
Variable Numeric Slot_M2_Max              = 270
Variable Numeric Slot_M3_Min  			  =  00
Variable Numeric Slot_M3_Max  			  = 270
Variable Numeric Slot_M4_Min  			  =   0
Variable Numeric Slot_M4_Max  			  = 355

Variable String CurrentLanguage           = "English" 'or "Chinese"

'╔═══════════════════════════════╤══════════╤════════╗
'║  type 5                       │  medium  │  press ║
'Variable Numeric AKT_5Y_Unp       = 858
'Variable Numeric AKT_5Y_Beg       = 605
'Variable Numeric AKT_5Y_Exe       = 402
'Variable Numeric AKT_5Y_Upp       = 226
'Variable Numeric AKT_5Y_Dow       =  68
''Variable Numeric AKT_5Y_Ins        =   0 ' not used
'Variable Numeric AKT_5C_Unp       = 861
'Variable Numeric AKT_5C_Red       = 607
'Variable Numeric AKT_5C_Gre       = 402
'Variable Numeric AKT_5C_Blk       = 226
'Variable Numeric AKT_5C_Yel       =  68
''Variable Numeric AKT_5C_Blu       =   0 ' not used


'╔═══════════════════════════════╤══════════╤════════╗
'║  type 5 underscored Black     │  medium  │  press ║
Variable Numeric AKT_5Y_Unp        = 670     '
Variable Numeric AKT_5Y_Beg        = 252     '  342
Variable Numeric AKT_5Y_Exe        = 123     '  161
Variable Numeric AKT_5Y_Upp        =  57     '   85
Variable Numeric AKT_5Y_Dow        =  15     '   29
'Variable Numeric AKT_5Y_Ins        =   0     '    0
Variable Numeric AKT_5C_Unp        = 670
Variable Numeric AKT_5C_Red        = 252     '  341
Variable Numeric AKT_5C_Gre        = 123     '  161
Variable Numeric AKT_5C_Blk        =  57     '   85
Variable Numeric AKT_5C_Yel        =  15     '   30
'Variable Numeric AKT_5C_Blu        =   0    '    0


' ╔═══════════════════════════════════╗ █▀▀▄ █  █ ▀▀█▀▀ ▀▀█▀▀ █▀▀█ █▄ █ ▄▀▀▀
' ║ BUTTONS                           ║ █▀▀█ █  █   █     █   █  █ █ ▀█ ▀▀▀▄
' ╚═══════════════════════════════════╝ █▄▄█ █▄▄█   █     █   █▄▄█ █  █ ▄▄▄▀

Button 1 Label ButtonsMeaning
Button ButtonsMeaning Text "Buttons Meaning"
Button ButtonsMeaning Color PeachPuff

Button 2 Label KnobController
Button KnobController  Text "Connect Knob Coltroller"
Button KnobController  Color LightBlue
'
'Button 3 Label Safe
'Button Safe Text "Safe"
'Button Safe Color Green9
'
'Button 4 Label Say
'Button Say Text "Say"
'Button Say Color NavajoWhite
'
Button 4 Label WayPoint
Button WayPoint Text "WayPoint OFF"
Button WayPoint Color Black

Button 5 Label Language
Button Language Text "English"
Button Language Color DodgerBlue

Button 6 Label ScopesActive
Button ScopesActive Text "ScopesActive"
Button ScopesActive Color LightSteelBlue

Button 7 Label RepeatOnOff
Button RepeatOnOff Text "Repeat OFF"
Button RepeatOnOff Color LightSteelBlue

Button 8 Label CollabOnOff
Button CollabOnOff Text "Collab OFF"
Button CollabOnOff Color LightSteelBlue

''Button 09 Label VoiceInput
''Button VoiceInput Text "Voice Input"
''Button VoiceInput Color SkyBlue
'
Button 10 Label VoiceOutputOnOff
Button VoiceOutputOnOff Text "VOICE"
Button VoiceOutputOnOff Color Green

'DisableAllButtons

' ╔════════════════════════════════════════════╗ █   █▀▀█ █▀▀█ █▀▀▄
' ║ LOAD APPS                                  ║ █   █  █ █▄▄█ █  █
' ╚════════════════════════════════════════════╝ █▄▄ █▄▄█ █  █ █▄▄█
'Safe
'ButtonsMeaningStatus = "Show OFF"

Load Theremino_HAL.exe
Load Theremino_SlotViewer_Master_Enc.exe
Load Theremino_SignalScope.exe
'Load Theremino_SlotViewer_STR.exe
'Load Theremino_SlotViewer_TSIO.exe

'WaitApplicationsStarted

'Load "..\Files\Analog_Button_Functions.txt"
Load Hide

' ╔════════════════════════════════════════════╗   █▀▀▀  ▀▀█▀▀  █▀▀▀█  █▀▀▀█  ▀▀█▀▀
' ║  EXECUTION START FROM HERE                 ║   ▀▀▀█    █    █▀▀▀█  █▀█▀▀    █
' ╚════════════════════════════════════════════╝   ▄▄▄█    █    █   █  █  ▀▄    █

ResetEncoders
Slot(Slot_SignalScope) = 1000
Controls OpenTextBox
Controls ClearTextBox

'VoiceCommandsStatus = "active"
'VoiceCommandsStatus = ""
'InterpretateVoiceCommands

SendCommand(Slot_CommandsToCobot)("SetTorqueMin 100")

DisableCollaborative
'StopVoice
'StartVoice
CurrentLanguage = "English"
'CurrentLanguage = "Chinese"
TTS Speak "Theremino Automation WayPoint run"

'EnableAllButton
Loop

' ╔════════════════════════════════════════════╗ █▀▀▀ ▀▀█▀▀ █▀▀█ █▀▀█
' ║ STOP EVENT (when pressing stop button)     ║ ▀▀▀█   █   █  █ █▄▄█
' ╚════════════════════════════════════════════╝ ▄▄▄█   █   █▄▄█ █
Label EventStop
	'Slot(Slot_SignalScope) = 0
End


' ╔════════════════════════════════════════════╗ █   █▀▀█ █▀▀█ █▀▀█
' ║ LOOP                                       ║ █   █  █ █  █ █▄▄█
' ╚════════════════════════════════════════════╝ █▄▄ █▄▄█ █▄▄█ █
Variable String LCM
Label Loop
	
	'StatusMaster
	If SlotText(SText_WayPointStatus) <> ""
		LCM = SlotText(SText_WayPointStatus)
		Print LCM
		If LCM = "OFF"
			Button WayPoint Text "WayPoint OFF"
			Button WayPoint Color Black
		Else
			Button WayPoint Text "WayPoint ON"
			Button WayPoint Color Flashing9 Green9 GreenYellow
		EndIf
		SlotText(SText_WayPointStatus) = ""
	EndIf
	If LCM = "ON"
		'Encoders
		'FirstFiveButtons
	EndIf
Goto Loop

' ===============================================
'  ENABLE - DISABLE Way Point Automation program
' ===============================================
Label WayPoint
	If	SlotText(SText_WayPointStatus) = "OFF"
		Button WayPoint Text "WayPoint ON"
		Button WayPoint Color Flashing8 Green9 GreenYellow
		SlotText(SText_WayPointStatus) = "ON"
	Else
		Button WayPoint Text "WayPoint OFF"
		Button WayPoint Color Black
		SlotText(SText_WayPointStatus) = "OFF"
	EndIf
Return


' ╔═════════════════════╗
' ║ MASTER STATUS QUERY ║
' ╚═════════════════════╝
Label StatusMaster
	Variable String Master
	If Slot(Slot_MasterStatus) <> 1000
		Master = "OFF"
		'Print "MASTER OFF  "
	Else
		Master = "ON"
		'Print "MASTER ON  "
	EndIf
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

'' ╔════════════════════════════════════════════╗
'' ║  WAIT COBOT READY                          ║
'' ╚════════════════════════════════════════════╝
'Label WaitCobotReady
'	Variable Numeric WCR1
'	For WCR1 = 1 To Infinity
'		Wait Seconds 0.05
'		If SlotText(Slot_ResponsesFromCobot) = "Ready"
'			Exit
'		EndIf
'	Next
'Return

'' ╔════════════════════════════════════════════╗
'' ║  WAIT APPS STARTED                         ║
'' ╚════════════════════════════════════════════╝
'Label WaitApplicationsStarted
'	Slot(Slot_MotorsFPS) = 0
'	Wait Slot(Slot_MotorsFPS) > 10
'	Wait Seconds 0.1
'Return



' =============================================================
'  SEND COMMAND (default timeout = 0.5 Sec)
' =============================================================
Label SendCommand			'(Destination Slot)(Command String)
	Variable Numeric SC1
	Variable String  SC2
	Variable Numeric SC3 = 0.7 '<-- The timeout in seconds was 0.5
	Variable Numeric SC4
	SlotText SC1 = SC2
	For SC4 = 0.1 To SC3 Step 0.1
		If SlotText(SC1) = ""
			Exit
		EndIf
		Wait Seconds 0.1
	Next
Return

'╔═════════════════╗
'║COMMANDS TO COBOT║
'╚═════════════════╝
' █▀▀▀ █▀▀█ █▄ ▄█ █▄ ▄█   ▀▀█▀▀ █▀▀█   █▀▀▀ █▀▀█ █▀█  █▀▀█ ▀▀█▀▀
' █    █  █ █ ▀ █ █ ▀ █     █   █  █   █    █  █ █▀▀█ █  █   █
' █▄▄▄ █▄▄█ █   █ █   █ ▄   █   █▄▄█   █▄▄▄ █▄▄█ █▄▄█ █▄▄█   █

Label ExecCobot
	'SendCommand(Slot_CommandsToCobot)("DisableRepeat")
	'SendCommand(Slot_CommandsToCobot)("EnableRepeat")
	SendCommand(Slot_CommandsToCobot)("SelectLine 1")
	SendCommand(Slot_CommandsToCobot)("StartExecution")
Return

Label StopCobot
	SendCommand(Slot_CommandsToCobot)("StopExecution")
Return

Label ArrowUpCobot
	SendCommand(Slot_CommandsToCobot)("ArrowUp")
Return

Label ArrowDownCobot
	SendCommand(Slot_CommandsToCobot)("ArrowDown")
Return

Label ExecuteCobotFromSelectedLine
	SendCommand(Slot_CommandsToCobot)("StartExecution")
Return

Label CorrectSelectedPosition
	If Master = "OFF"
		Return
	EndIf
	SendCommand(Slot_CommandsToCobot)("CorrectSelectedPosition")
Return

Label DeleteSelectedLine
	SendCommand(Slot_CommandsToCobot)("DeleteSelectedLine")
Return

Label AddNewPosition
	SendCommand(Slot_CommandsToCobot)("AddNewPosition")
Return

Label EnableRepeat
	SendCommand(Slot_CommandsToCobot)("EnableRepeat")
Return

Label DisableRepeat
	SendCommand(Slot_CommandsToCobot)("DisableRepeat")
Return

Label EnableCollaborative
	SendCommand(Slot_CommandsToCobot)("EnableCollaborative")
Return

Label DisableCollaborative
	SendCommand(Slot_CommandsToCobot)("DisableCollaborative")
Return

Label SetupCobot
	SendCommand(Slot_CommandsToCobot)("SetTorqueMin 331")
	SendCommand(Slot_CommandsToCobot)("SetTorqueMax 701")
	SendCommand(Slot_CommandsToCobot)("SetAcceleration 22")
	SendCommand(Slot_CommandsToCobot)("SetSpeed 1100")
Return


'' ╔══════════════════════════════════════════╗ █▀▀▀ █  █ █▀█
'' ║  S U B R O U T I N E   A R E A           ║ ▀▀▀█ █  █ █▀▀█
'' ╚══════════════════════════════════════════╝ ▄▄▄█ █▄▄█ █▄▄█
'Label EnableHomeButtons
'	For v1 = 17 To 22
'		Button v1 Enabled
'	Next
'Return
'
'Label DisableHomeButtons
'	For v1 = 17 To 22
'		Button v1 Disabled
'	Next
'Return

'Label ShowMotorFPS
'	Variable String MotorsAreEnabled
'	Variable Numeric SMF1 = Slot(Slot_MotorsFPS)
'	If SMF1 > 30
'		If MotorsAreEnabled <> "TRUE"
'			MotorsAreEnabled = "TRUE"
'			Button MotorFPS Color GreenYellow
'		EndIf
'		Button MotorFPS Text "FPS " + Str(SMF1)
'	Else
'		If MotorsAreEnabled = "TRUE"
'			MotorsAreEnabled = "FALSE"
'			Button MotorFPS Color Flashing7 Yellow DarkOrange
'			Button MotorFPS Text "NO MOTORS"
'		EndIf
'	EndIf
'Return

'Label MotorFPS
'	If MotorsAreEnabled = "TRUE"
'		SendCommand(Slot_CommandsToMotors)("DisableConnection")
'	Else
'		SendCommand(Slot_CommandsToMotors)("EnableConnection")
'	EndIf
'Return

Label ScopesActive
	If Slot(Slot_SignalScope) < 1000
		Button ScopesActive Text "Scopes ON"
		Button ScopesActive Color Flashing8 OrangeRed Moccasin
		Slot(Slot_SignalScope) = 1000
	Else
		Button ScopesActive Text "Scopes OFF"
		Button ScopesActive Color BurlyWood
		Slot(Slot_SignalScope) = 0
	EndIf
Return


' ╔═══════════╗ █   █ █▀▀█ █ █▀▀▀ █▀▀▀   █▀▀█ █  █ ▀▀█▀▀
' ║ Voice OUT ║  █ █  █  █ █ █    █▀▀    █  █ █  █   █
' ╚═══════════╝   █   █▄▄█ █ █▄▄▄ █▄▄▄   █▄▄█ █▄▄█   █
Variable String VoiceOutputStatus
Label StartVoice
	VoiceOutputStatus = ""
	VoiceOutputOnOff
Return

Label StopVoice
	VoiceOutputStatus = "active"
	VoiceOutputOnOff
Return

Label VoiceOutputOnOff
	If VoiceOutputStatus = "active"
		VoiceOutputStatus = ""
		Button VoiceOutputOnOff Text "Voice output OFF"
		Button VoiceOutputOnOff Color DarkSlateGray
		TTS SpeakWait Voice OFF
		TTS SetVolume 0
	Else
		TTS SetVolume 100
		TTS Speak Voice ON
		VoiceOutputStatus = "active"
		Button VoiceOutputOnOff Text "Voice output ON"
		Button VoiceOutputOnOff Color Lime
	EndIf
Return

Label Language
	If CurrentLanguage = "English"
		CurrentLanguage = "Chinese"
		Button Language Text "Chinese"
		Button Language Color Flashing  Firebrick  Thistle
		TTS SelectVoice "ZHO"
		TTS Speak 中文语言
	Else
		CurrentLanguage = "Chinese"
		CurrentLanguage = "English"
		Button Language Text "English"
		Button Language Color DodgerBlue
		TTS SelectVoice "ENG"
		TTS Speak English Language
	EndIf
Return

Label ClearCommand
	SlotText(Slot_ExternalCommands) = ""
Return

' ============ NICE BOX ==============
' ╔═════════════════════════╗ █▀▀▀   █▀▀    █▀█  █  █ ▀▀█▀▀ ▀▀█▀▀ █▀▀█ █▄ █ ▄▀▀▀
' ║ FIRST FIVE PUSH BUTTONS ║ █▀▀    ▀▀█    █▀▀█ █  █   █     █   █  █ █ ▀█ ▀▀▀▄
' ╚═════════════════════════╝ █  ▄   ▄▄█    █▄▄█ █▄▄█   █     █   █▄▄█ █  █ ▄▄▄▀
'for right value of analog keyboard read file: Analog_key_table.txt in /Programs

Variable String PressedButton
Label FirstFiveButtons
	Variable Numeric FFB0
	Variable Numeric FFB1
	Variable Numeric FFB2
	'' ------------------------------ check the presence of the Master
	If Slot(Slot_MasterStatus) <> 1000
		Return
	EndIf
	' ------------------------------ Read the value two times
	FFB1 = Slot(Slot_FirstFiveButton)
	' ------------------------------ If similar values
	If Abs(FFB2 - FFB1) < 10
		' -------------------------- If armed
		If FFB0 = 0
			FFB0 = 1
			Select True
				Case FFB1 > AKT_5Y_Unp
					'Print ""
					PressedButton = ""
				Case FFB1 > AKT_5Y_Beg
					'Print " Begin "
					PressedButton = "Begin"
					ExecCobot
					StartScope
				Case FFB1 > AKT_5Y_Exe
					'Print " Exec from line "
					PressedButton = "Exec from line"
					ExecuteCobotFromSelectedLine
					StartScope
				Case FFB1 > AKT_5Y_Upp
					'Print " Arrow up "
					PressedButton = "Up"
					ArrowUpCobot
					StartScope
				Case FFB1 > AKT_5Y_Dow
					'Print " Arrow down "
					PressedButton = "Down"
					ArrowDownCobot
					StartScope
				CaseElse
					'Print " Arrow inside "
					PressedButton = "Inside"
					StopCobot
					'CollabOnOff
					CorrectSelectedPosition
			EndSelect
		EndIf
	EndIf
	FFB2 = FFB1
    ' ------------------------------ Rearm buttons
	If FFB1 > 700
		FFB0 = 0
	EndIf
	SecondFiveButtons
Return

' ============ NICE BOX COLORED BUTTONS ==============
' ╔══════════════════════════╗ ▄▀▀▀    █▀▀    █▀█  █  █ ▀▀█▀▀ ▀▀█▀▀ █▀▀█ █▄ █ ▄▀▀▀
' ║ SECOND FIVE PUSH BUTTONS ║ ▀▀▀▄    ▀▀█    █▀▀█ █  █   █     █   █  █ █ ▀█ ▀▀▀▄
' ╚══════════════════════════╝ ▄▄▄▀ ▄  ▄▄█    █▄▄█ █▄▄█   █     █   █▄▄█ █  █ ▄▄▄▀
'for right valueof  analog keyboard read file: Analog_key_table.txt


Label SecondFiveButtons
	Variable Numeric SFB0
	Variable Numeric SFB1
	Variable Numeric SFB2
	'' ------------------------------ check the presence of the Master
	If Slot(Slot_MasterStatus) <> 1000
		Return
	EndIf
	' ------------------------------ Read the value two times
	SFB1 = Slot(Slot_SecondFiveButton)
	
	' ------------------------------ If similar values
	If Abs(SFB2 - SFB1) < 10
		' -------------------------- If armed
		If SFB0 = 0
			SFB0 = 1
			Select True
				Case SFB1 > AKT_5C_Unp
					'Print ""
					PressedButton = ""
					EncoderSpeed = ""
				Case SFB1 > AKT_5C_Red
					'Print " red "
					PressedButton = "red"
					EncoderSpeed = "high"
				Case SFB1 > AKT_5C_Gre
					'Print " green "
					PressedButton = "green"
					EncoderSpeed = "low"
				Case SFB1 > AKT_5C_Blk
					'Print " black "
					PressedButton = "black"
					DeleteSelectedLine
					TTS Speak Delete line
				Case SFB1 > AKT_5C_Yel
					'Print " yellow "
					PressedButton = "yellow"
					AddNewPosition
				CaseElse
					'Print " blue "
					PressedButton = "blue"
					RepeatOnOff
			EndSelect
		EndIf
	EndIf
	SFB2 = SFB1
    ' ------------------------------ Rearm buttons
	If SFB1 > 700
		SFB0 = 0
	EndIf
Return

Label  KnobController
	Slot(0) = Recognize
Return

' ╔════════════════════════╗ █▀▀▀ █▄ █ █▀▀▀ █▀▀█ █▀▀▄ █▀▀▀ █▀▀█ ▄▀▀▀
' ║ ENCODERS USB or  Wi-Fi ║ █▀▀  █ ▀█ █    █  █ █  █ █▀▀  █▄▄█ ▀▀▀█
' ╚════════════════════════╝ █▄▄▄ █  █ █▄▄▄ █▄▄█ █▄▄█ █▄▄▄ █ ▀▄ ▄▄▄▀
Variable Numeric EncoderSteps1
Variable Numeric OldEncoderSteps1 = Slot(Slot_Encoder1)
Variable Numeric Difference1
Variable Numeric EncoderSteps2
Variable Numeric OldEncoderSteps2 = Slot(Slot_Encoder2)
Variable Numeric Difference2
Variable Numeric EncoderSteps3
Variable Numeric OldEncoderSteps3 = Slot(Slot_Encoder3)
Variable Numeric Difference3
Variable Numeric EncoderSteps4
Variable Numeric OldEncoderSteps4 = Slot(Slot_Encoder4)
Variable Numeric Difference4

Label RecognizeEncoder
	Slot(0) = Recognize
Return

Variable String "EncoderSpeed" = ""
Label Encoders
	'------------------------------------------------ Encoder BASE
	EncoderSteps1 = Slot(Slot_Encoder1) / 4
	Difference1 = EncoderSteps1 - OldEncoderSteps1
	OldEncoderSteps1 = EncoderSteps1
	If Difference1 > 32000 / 4
		Difference1 = Difference1 - 65536 / 4
	EndIf
	If Difference1 < -32000 / 4
		Difference1 = Difference1 + 65536 / 4
	EndIf
	Difference1 = Difference1 * 1       'default speed
	If EncoderSpeed  = "low"
		Difference1 = Difference1 / 4
	EndIf
	If EncoderSpeed = "high"
		Difference1 = Difference1 * 2
	EndIf
	WriteSlotMinMax(Slot_M1_Dest)(Difference1)(Slot_M1_Min)(Slot_M1_Max)
	
    '------------------------------------------------ Encoder Inner
	EncoderSteps2 = Slot(Slot_Encoder2) / 4
	Difference2 = EncoderSteps2 - OldEncoderSteps2
	OldEncoderSteps2 = EncoderSteps2
	If Difference2 > 32000 / 4
		Difference2 = Difference2 - 65536 / 4
	EndIf
	If Difference2 < -32000 / 4
		Difference2 = Difference2 + 65536 / 4
	EndIf
	Difference2 = Difference2 * 1
	If EncoderSpeed  = "low"
		Difference2 = Difference2 / 4
	EndIf
	If EncoderSpeed  = "high"
		Difference2 = Difference2 * 2
	EndIf
	WriteSlotMinMax(Slot_M2_Dest)(Difference2)(Slot_M2_Min)(Slot_M2_Max)
	
    '------------------------------------------------ Encoder Outer
	EncoderSteps3 = Slot(Slot_Encoder3) / 4
	Difference3 = EncoderSteps3 - OldEncoderSteps3
	OldEncoderSteps3 = EncoderSteps3
	If Difference3 > 32000 / 4
		Difference3 = Difference3 - 65536 / 4
	EndIf
	If Difference3 < -32000 / 4
		Difference3 = Difference3 + 65536 / 4
	EndIf
	Difference3 = Difference3 * 1
	If EncoderSpeed  = "low"
		Difference3 = Difference3 / 4
	EndIf
	If EncoderSpeed  = "high"
		Difference3 = Difference3 * 2
	EndIf
	WriteSlotMinMax(Slot_M3_Dest)(Difference3)(Slot_M3_Min)(Slot_M3_Max)
	
	'------------------------------------------------ Encoder end
	EncoderSteps4 = Slot(Slot_Encoder4) / 4
	Difference4 = EncoderSteps4 - OldEncoderSteps4
	OldEncoderSteps4 = EncoderSteps4
	If Difference4 > 32000 / 4
		Difference4 = Difference4 - 65536 / 4
	EndIf
	If Difference4 < -32000 / 4
		Difference4 = Difference4 + 65536 / 4
	EndIf
	Difference4 = Difference4 * 1
	If EncoderSpeed  = "low"
		Difference4 = Difference4 / 4
	EndIf
	If EncoderSpeed  = "high"
		Difference4 = Difference4 * 2
	EndIf
	WriteSlotMinMax(Slot_M4_Dest)(Difference4)(Slot_M4_Min)(Slot_M4_Max)
	
		'------------------------------------------------
	If Difference1 <> 0 Or Difference2 <> 0 Or Difference3 <> 0 Or Difference4 <> 0
		CorrectSelectedPosition
	EndIf
Return

Label ResetEncoders
	OldEncoderSteps1 = Slot(Slot_Encoder1) / 4
	OldEncoderSteps2 = Slot(Slot_Encoder2) / 4
	OldEncoderSteps3 = Slot(Slot_Encoder3) / 4
	OldEncoderSteps4 = Slot(Slot_Encoder4) / 4
Return

Label WriteSlotMinMax
	Variable Numeric WSSlot
	Variable Numeric WSdiff
	Variable Numeric WSMin
	Variable Numeric WSMax
	WSdiff = Slot(WSSlot) + WSdiff
	If WSdiff > WSMax
		WSdiff = WSMax
		Beep(500,10)
	EndIf
	If WSdiff < WSMin
		WSdiff = WSMin
		Beep(500,10)
	EndIf
	Slot(WSSlot) = WSdiff
Return


' ╔═══════════════════════╗ █▀▀█ █▀▀▀ █▀▀█ █▀▀▀ █▀▀█ ▀▀█▀▀
' ║  REPEAT ON-OFF        ║ █▄▄█ █▀▀  █▄▄█ █▀▀  █▄▄█   █
' ╚═══════════════════════╝ █ ▀▄ █▄▄▄ █    █▄▄▄ █  █   █
Variable String RepeatStatus = "Repeat Off"
Label RepeatOnOff
	If RepeatStatus = "Repeat Off"
		RepeatStatus = "Repeat On"
		EnableRepeat
		TTS Speak RepeatStatus
		Button RepeatOnOff Text "REPEAT ON"
		Button RepeatOnOff Color Flashing7 Yellow Red
	Else
		DisableRepeat
		RepeatStatus = "Repeat Off"
		TTS Speak RepeatStatus
		Button RepeatOnOff Text "REPEAT OFF"
		Button RepeatOnOff Color DarkSlateGray
	EndIf
Return


'' ╔════════════════════════╗ █▀▀▀ █▀▀█ █    █    █▀▀█ █▀▀▄
'' ║  COLLABORATIVE ON-OFF  ║ █    █  █ █    █    █▄▄█ █▀▀█
'' ╚════════════════════════╝ █▄▄▄ █▄▄█ █▄▄▄ █▄▄▄ █  █ █▄▄█ ▄
'Variable String CollabStatus = "Collab Off"
'Label CollabOnOff
'	If CollabStatus = "Collab Off"
'		CollabStatus = "Collab On"
'		EnableCollaborative
'		TTS Speak Wait Collaborative status active
'		Button CollabOnOff Text "COLLAB ON"
'		Button CollabOnOff Color Flashing7 Yellow Red
'	Else
'		DisableCollaborative
'		CollabStatus = "Collab Off"
'		TTS Speak Wait Collaborative status is inactive
'		Button CollabOnOff Text "Collab Off"
'		Button CollabOnOff Color DarkSlateGray
'	EndIf
'Return
'
Variable String ButtonsMeaningStatus = "Show OFF"
Label ButtonsMeaning
	If ButtonsMeaningStatus = "Show OFF"
		ButtonsMeaningStatus = "Show ON"
		Window Sizable
		Load "Images\Button_meanings.jpg"
		Button ButtonsMeaning Text "Show ON"
	Else
		Load Hide
		ButtonsMeaningStatus = "Show OFF"
		Button ButtonsMeaning Text "Show OFF"
		Load Hide
	EndIf
Return

'' ╔═══════════════════════════╗ █▀▀▀ █▀▀ █▀▀█ █▀▀█ █▀▀ █▀▀▀
'' ║  S C O P E S              ║ ▀▀▀█ █   █  █ █▄▄█ █▀▀ ▀▀▀█
'' ╚═══════════════════════════╝ ▄▄▄█ █▄▄ █▄▄█ █    █▄▄ ▄▄▄█
Label StartScope
	Slot(Slot_SignalScope) = 1000
Return

Label StopScope
	Slot(Slot_SignalScope) = 0
Return

'' ====================================================================
''  PRINT COORDINATES
'' ====================================================================
'Label PrintValues
'	's1 =       "X=" + Format(Slot(Slot_M1_Dest_X), "0.000")
'	's1 = s1 + " Y=" + Format(Slot(Slot_M2_Dest_Y), "0.000")
'	's1 = s1 + " Z=" + Format(Slot(Slot_M3_Dest_Z), "0.000")
'	'Print CRLF + s1
'Return

' ====================================================================
'  re-set home position. Before use, all motor must be to 180
' ====================================================================
'Label ReSetHomePosition
'	ZeroFeetech(1)
'	ZeroFeetech(2)
'	ZeroFeetech(3)
'	ZeroFeetech(4)
'Return

'Label ZeroFeetech
'	' ----------------------------- initialize motor address
'	Variable Numeric ZF1
'	ZF1 = (ZF1 + 1) * 50
'	' -----------------------------
'	Slot(ZF1 + 23) = 128  'align current position to 2048
'	Wait Seconds 0.05
'	Slot(ZF1 + 23) = 1    'restore normal function
'	Wait Seconds 0.05
'	' ----------------------------- copy position to destination
'	'Slot(ZF1 + 1) = Slot(ZF1 + 2)
'	Slot Slot_M1_Dest = Slot(Slot_M1_Pos)
'	Slot Slot_M2_Dest = Slot(Slot_M2_Pos)
'	Slot Slot_M3_Dest = Slot(Slot_M3_Pos)
'	Slot Slot_M4_Dest = Slot(Slot_M4_Pos)
'
'	Wait Seconds 0.05
'	''' ----------------------------- move to desidered position
'	Slot(ZF1 + 1) = Slot(ZF1 + 2) + 10
'	For v1 = 0 To 1000
'		Wait Seconds 0.05
'		If Slot(ZF1 + 7) = 0
'			Exit
'		EndIf
'	Next
'	' ----------------------------- zero to motor
'	Slot(ZF1 + 23) = 128  'align current position to 2048
'	Wait Seconds 0.05
'	Slot(ZF1 + 23) = 1    'restore normal function
'	Wait Seconds 0.05
'	'' ----------------------------- copy position to destination
'	Slot(ZF1 + 1) = Slot(ZF1 + 2)
'	Wait Seconds 0.05
'Return


' ╔══════════════════════════════════╗ ▀▀█▀▀ █▀▀█ █ █▀▀█ █
' ║ T R I A L S                      ║   █   █▄▄▀ █ █▄▄█ █
' ╚══════════════════════════════════╝   █   █ ▀▄ █ █  █ █▄▄


'BOOKMARKS 850 678 324 

'EDITEDLINES 242 5 21 1 24 2 25 3 28 4 227 5 230 6 229 7 228 8 -1 9 234 10 

'WATCH CR
