
' ----------------------------------- Restored by LoadVars
Numeric DutyCycle1
Numeric DutyCycle2
Numeric Linearity = 0.5
Numeric Smoothing = 0.3
Numeric TrimFreq = 800

' ----------------------------------- Load Vars
Load Vars

' ----------------------------------- Not affected by LoadVars
Numeric CommandsTextSlot = 100
Numeric Slot_Value1 = 110
Numeric Slot_Value2 = 111
Numeric Slot_Duty1 = 120
Numeric Slot_Duty2 = 121
Linearity = 0.7
Smoothing = 0.1
TrimFreq = 800

' ----------------------------------- Buttons
Button 2 Label TrimCapacitor1
Button 2 Image0S Button1.png
Button 2 Text "Trim cap. 1"

Button 3 Label TrimCapacitor2
Button 3 Image0S Button1.png
Button 3 Text "Trim cap. 2"

Button 5 Label CalibrateZero
Button 5 Image0S Button1.png
Button 5 Text "Calibrate zero"

Controls SetBackColor 250 255 220
' ----------------------------------- Load Apps
Load Theremino_Theremin.exe
Load Theremino_SlotViewer.exe
'Load Theremino_SignalScope.exe

' ----------------------------------- Start
OpenComPort
SendOptions
EnableSerialData
Wait Seconds 0.5
CalibrateZero
Goto Loop
Stop

Label EventStop
	DisableSerialData
	Save Vars
	SendOptions
	COM Close
	Slot Slot_Value1 = 0
	Slot Slot_Value2 = 0
End


' ----------------------------------- Button execution

' ----------------------------------- Send Commands
Label EnablePwm
	COM WriteString "ENABLE_PWM" + CRLF
Return

Label DisablePwm
	COM WriteString "DISABLE_PWM" + CRLF
Return

Label DisableSerialData
	COM WriteString "DISABLE_SERIAL_DATA" + CRLF
Return

Label EnableSerialData
	COM WriteString "ENABLE_SERIAL_DATA" + CRLF
Return

Label TrimCapacitor1
	COM WriteString "TRIM_CAP1" + CRLF
Return

Label TrimCapacitor2
	COM WriteString "TRIM_CAP2" + CRLF
Return

Label CalibrateZero
	COM WriteString "CALIBRATE" + CRLF
Return


' ----------------------------------- Send Options
Label SendOptions
	'PrintOptions
	SendOptionsToDriverModule
Return

Label SendOptionsToDriverModule
	String Cmd = DutyCycle1 + " " + DutyCycle2
	Cmd = Cmd + " " + Linearity + " " + Smoothing + " " + TrimFreq
	COM WriteString "OPTIONS " + Cmd  + CRLF
Return

Label PrintOptions
	Cmd = ""
	Cmd = Cmd + CRLF
	Cmd = Cmd + PadRight(" Duty1=" + DutyCycle1, 10)
	Cmd = Cmd + PadRight(" Duty2=" + DutyCycle2, 10)
	Cmd = Cmd + PadRight(" Linearity=" + Linearity, 10)
	Cmd = Cmd + PadRight(" Smoothing=" + Smoothing, 10)
	Print Cmd
Return


' =============================================================
'  SEND COMMAND (default timeout = 0.5 Sec)
' =============================================================
Label SendCommand
	Variable String SC1           '<-- Parameter Command-String
	SlotText CommandsTextSlot = SC1
	Variable Numeric SC2
	For SC2 = 0.1 To 0.5 Step 0.1 ' <-- 0.5 seconds timeout
		If SlotText(CommandsTextSlot) = ""
			Exit
		EndIf
		Wait Seconds 0.1
	Next
Return


' -----------------------------------------------
'  OPEN COM
' -----------------------------------------------
Label OpenComPort
	String  DeviceID = "CapDetector1"
	Numeric Bauds    = 1000000
	String  ComPort
	Numeric ComIndex
	Numeric ComRetry1
	Numeric ComRetry2
	For ComRetry1 = 1 To 10
		For ComIndex = 0 To GetSeparatedStringCount(COM_PortNames) - 1
			ComPort = GetSeparatedString(COM_PortNames, ComIndex)
			COM Open ComPort Bauds
			If COM_Status = "OPENED"
				For ComRetry2 = 1 To 3
					COM DiscardOutBuffer
					COM DiscardInBuffer
					COM WriteString "GetID" + CRLF
					Wait Seconds 0.01
					If COM_ReceiveUntil(CRLF) = DeviceID + CRLF
						ComIndex = -1
						Beep "440-1,0-80,880-1"
						Return
					EndIf
				Next
			EndIf
		Next
	Next
	If ComIndex >= 0
		Print "COM PORT NOT FOUND"
		TTS SelectVoice "ENGF"
		TTS Speak "Com port not found."
		'TTS Speak "Please test connections and retry."
		Wait Not TTSspeaking
		End
	EndIf
Return

' =====================================================
'  LOOP
' =====================================================
Numeric Decoded
String ComStr
String DecodeStr
String DecodeStrOld
Numeric Repetitions

Label Loop
	If DecodeCommandSlot(0) = "Calibrate"
		CalibrateZero
	EndIf
	' ------------------------------------------- Receive
	For Repetitions = 1 To Infinity
		ComStr = COM_ReceiveUntil(CRLF)
		If ComStr <> ""
			DecodeStr = ComStr
		Else
			Exit
		EndIf
		If Repetitions > 5
			COM Open ComPort Bauds ' Clear COM buffers
		EndIf
	Next
	If DecodeStr <> DecodeStrOld
		' ------------------------------------------- Decode 1
		Decoded = GetSeparatedString(DecodeStr, 0)
		Slot Slot_Value1 = Decoded
		' ------------------------------------------- Decode 2
		Decoded = GetSeparatedString(DecodeStr, 1)
		Slot Slot_Value2 = Decoded
		' ------------------------------------------- Decode 3
		Decoded = GetSeparatedString(DecodeStr, 4)
		If Decoded <> DutyCycle1
			DutyCycle1 = Decoded
			Slot Slot_Duty1 = Decoded
		EndIf
		' ------------------------------------------- Decode 4
		Decoded = GetSeparatedString(DecodeStr, 5)
		If Decoded <> DutyCycle2
			DutyCycle2 = Decoded
			Slot Slot_Duty2 = Decoded
		EndIf
		' ------------------------------------------- Print
		Print DecodeStr
		' -------------------------------------------
		DecodeStrOld = DecodeStr
	EndIf
	' ------------------------------------------- Loop time
	Wait Seconds 0.001
	' ------------------------------------------- Debugs
	DutyCycleFromSlot
    'PrintLoopTime
Goto Loop

' =====================================================
'  DEBUG
' =====================================================
Label PrintLoopTime
	Numeric newlooptime = ElapsedTime
	Numeric looptime = newlooptime - oldlooptime
	Numeric oldlooptime = newlooptime
	Print Format(looptime * 1000, "0.0")
Return

Label DutyCycleFromSlot
	If Slot(Slot_Duty1) <> DutyCycle1
		EnablePwm
		DutyCycle1 = Slot(Slot_Duty1)
		SendOptions
	EndIf
	If Slot(Slot_Duty2) <> DutyCycle2
		EnablePwm
		DutyCycle2 = Slot(Slot_Duty2)
		SendOptions
	EndIf
Return


'BOOKMARKS 

'EDITEDLINES 246 7 38 8 185 10 175 10 

'EXEC DestX = 12
