
Load Theremino_SlotViewer.exe
Load Theremino_VideoPlayer.exe
Load Theremino_Hal.exe

Label Loop
	ReadEncoder
	Print FinalValue
	Wait Seconds 0.01
Goto Loop


' ==========================================================
'  This function read an encoder channel
'  The "Multiplier" changes the variation speed
'  The "Final Value" is limited from Min to Max
' ==========================================================
Variable Numeric EncoderSlot = 1
Variable Numeric OutputSlot = 2
Variable Numeric Multiplier = 15 ' 22.3 or 0.12 are valid too
Variable Numeric MaxValue = 1000
Variable Numeric MinValue = 0

Variable Numeric FinalValue ' This could be changed while running
Variable Numeric NewValue
Variable Numeric OldValue = infinity
Variable Numeric Difference

Label ReadEncoder
	NewValue = Slot(EncoderSlot)
	If OldValue <> Infinity
		Difference = NewValue - OldValue
	EndIf
	OldValue = NewValue
	'
	If Difference > 32000
		Difference = Difference - 65536
	EndIf
	If Difference < -32000
		Difference = Difference + 65536
	EndIf
	'
	Difference = Difference * Multiplier
	FinalValue = FinalValue + Difference
	FinalValue = Limit(FinalValue, MinValue, MaxValue)
	'
	Slot OutputSlot = FinalValue
Return


