// Script to use pad Leds to show tempo on a Arturia Keylab mkII 
// 		this script use midi sysEx message instead of midiNoteOn
//		It is more versatile and does not depend on the keyboard setting (Analog Lab, DAW, or USER mode)
//		It also use the concept of function so it is more reusable
// Arguments:  
//		$arg1 : 1 to always run, none or 0 to stop with song
//
// Author: LoopToGo
//
// Description  : 	Will flash one led at each tick and 2 of them at an exact bar. (LED1 and LED5 are always flash together)
//					Two other Leds at the other ticks	
//					To be used with ScriptMarker or to load at beginning of song (or LoopToGo) with arg1 = 1

loadScript ..\aliases.txt

setScriptName "Tempo Led"
setScriptStopMode 1

loadScript ..\libs\arturiaKeylabMKIIUtilities.ltgl // For setPadColor

 
// Here are some examples of what the sysEx message looks like to turn on Arturia Keylab mkII leds
//sendMidiRawBytes PadLight "F0 00 20 6B 7F 42 02 00 16 70 7F 00 7F F7"	// Pad 0 set to Purple
//sendMidiRawBytes PadLight "F0 00 20 6B 7F 42 02 00 16 72 00 00 7F F7"	// Pad 2 set to Blue
//sendMidiRawBytes PadLight "F0 00 20 6B 7F 42 02 00 16 75 7F 00 00 F7"	// Pad 5 set to Red
//sendMidiRawBytes PadLight "F0 00 20 6B 7F 42 02 00 16 78 00 7F 00 F7"	// Pad 8 set to Green
//sendMidiRawBytes PadLight "F0 00 20 6B 7F 42 02 00 16 7F 7F 7F 7F F7"	// Pad 15 set to White


//callFunction setPadColor PadLight 1 127 0 0
//callFunction setPadColor PadLight 2 0 127 0
//callFunction setPadColor PadLight 3 0 0 127
//callFunction setPadColor PadLight 4 127 127 127
//endScript

while $arg1 + $isPlaying + $isRecording

	variable bar $songBar 
	variable barQ $bar 1	// Quantization to the bar

	variable remaining	$bar - $barQ
		
	for n 1 $topNumber		
									
		if $remaining > $n/$topNumber - 1/$topNumber
							
			if $remaining < $n/$topNumber - .1
				
					variable ledState&$n ON
					
			else

					variable ledState&$n OFF

			endIf
			
		else
					variable ledState&$n OFF

		endIf
			

	endFor	
	
	for n 1 $topNumber
	
		variable state	ledState&$n
		variable lastState	lastLedState&$n
	
		ifNot $$state = $$lastState
						
			variable end $n + $topNumber
			
			if $$state = ON
										
				if $n = 1
					callFunction ledContour 0 127 0   
				else			
					variable rollColor $n - 1
					while $rollColor > 3
						variable rollColor $rollColor - 3
					endWhile
					
					if $rollColor = 1
						callFunction ledCenter 127 0 0
					else
						if $rollColor = 2
							callFunction ledCenter 0 127 0 
						else	
							callFunction ledCenter 0 0 127
						endIf						
					endIf
					
				endIf
			else
				if $n = 1
					callFunction ledContour  0 0 0  
				else
					callFunction ledCenter 0 0 0
				endIf
			endIf
					
			variable $lastState $$state
		endIf
	endFor
	
	//print In tempo script
	
	waitDelta .01 // Very important to have at least 1 wait in the while so the script is non blocking!
endWhile

// Turn off the leds 
for n 1 16					
	callFunction setPadColor PadLight $n 0 0 0
endFor
		
function ledContour // r [in] g [in] g [in]

	if $nbArgs = 3
		variable r $arg1
		variable g $arg2
		variable b $arg3
		
		callFunction setPadColor PadLight 1 $r $g $b 
		callFunction setPadColor PadLight 2 $r $g $b 
		callFunction setPadColor PadLight 3 $r $g $b 
		callFunction setPadColor PadLight 4 $r $g $b  
		
		callFunction setPadColor PadLight 5 $r $g $b  
		callFunction setPadColor PadLight 8 $r $g $b  
		callFunction setPadColor PadLight 9 $r $g $b  
		callFunction setPadColor PadLight 12 $r $g $b  
		
		callFunction setPadColor PadLight 13 $r $g $b  
		callFunction setPadColor PadLight 14 $r $g $b  
		callFunction setPadColor PadLight 15 $r $g $b  
		callFunction setPadColor PadLight 16 $r $g $b				
		
	else
		print "ledContour: wrong number of args!"
	endIf

endFunction

function ledCenter// r [in] g [in] g [in]

	if $nbArgs = 3
		variable r $arg1
		variable g $arg2
		variable b $arg3
		
		callFunction setPadColor PadLight 6 $r $g $b 
		callFunction setPadColor PadLight 7 $r $g $b  
		callFunction setPadColor PadLight 10 $r $g $b  
		callFunction setPadColor PadLight 11 $r $g $b
		
	else
		print "ledCenter: wrong number of args!"
	endIf

endFunction