// Script to use pad Leds to show tempo on a LaunchPad Mini MkIII 
// 		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 the contour leds at exact bar.
//					Inside Leds at the other ticks	
//					To be used with ScriptMarker or to load at beginning of song (or LoopToGo) with arg1 = 1
//					Also, it is used by manageLaunchPad
//					When the song is not playing or recording, display a Green T

alias aDeviceOut LaunchPadMini

loadScript ..\aliases.txt

setScriptName "Tempo Led"
setScriptStopMode 1

loadScript ..\libs\Novation\novationLaunchPadMiniUtilities.ltgl

variable playRecState $isPlaying + $isRecording
variable lastPlayRecState $playRecState + 1


while $arg1 + $playRecState

	
	variable playRecState $isPlaying + $isRecording


	if $playRecState
		ifNot $lastPlayRecState = $playRecState			
			callFunction clearPads aDeviceOut 0		
		endIf
		callFunction managetempoLed
	else	
		ifNot $lastPlayRecState = $playRecState
			
			callFunction clearPads aDeviceOut 0
			callFunction showBigT  aColorPaletteGreen
		
		endIf
	
	endIf
	
	//print In tempo script
	variable lastPlayRecState $playRecState
	
	waitDelta .03 // Very important to have at least 1 wait in the while so the script is non blocking!
endWhile

// Turn off the leds 
callFunction clearPads aDeviceOut 
		
function ledContour // color [in]

	if $nbArgs = 1
		
		variable color $arg1
		
		for pad 1 8
			callFunction setPadColor aDeviceOut $pad $color 
			callFunction setPadColor aDeviceOut $pad + 63 $color 
		endFor
		
		for p 1 6
			variable pad 1 + 9*$p
			
			callFunction setPadColor aDeviceOut $pad $color 
			callFunction setPadColor aDeviceOut $pad + 7 $color
			
		endFor
												
	else
		print "ledContour: wrong number of args!"
	endIf

endFunction

function ledCenter// color [in] 

	if $nbArgs = 1
		variable color $arg1
		
		for pad 1 6
			callFunction setPadColor aDeviceOut 10 + $pad $color 
			callFunction setPadColor aDeviceOut 55 + $pad $color 
		endFor
		
		for p 1 4
			variable pad 11 + 9*$p
			callFunction setPadColor aDeviceOut $pad $color 
			callFunction setPadColor aDeviceOut $pad + 5 $color 
		endFor
		
	else
		print "ledCenter: wrong number of args!"
	endIf

endFunction

function managetempoLed

	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 - .0001
							
			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 aColorPaletteGreen   
				else			
					variable rollColor $n - 1
					while $rollColor > 3
						variable rollColor $rollColor - 3
					endWhile
					
					if $rollColor = 1
						callFunction ledCenter aColorPaletteRed
					else
						if $rollColor = 2
							callFunction ledCenter aColorPalettePurple 
						else	
							callFunction ledCenter aColorPaletteBlue
						endIf						
					endIf
					
				endIf
			else
				if $n = 1
					callFunction ledContour aColorPaletteBlack  
				else
					callFunction ledCenter aColorPaletteBlack
				endIf
			endIf
					
			globalVariable $lastState $$state
		endIf
	endFor

endFunction

function showBigT // color [in]
	
	
	for i 0 4 
		
		callFunction setPadColor aDeviceOut $i*9 + 4 $arg1
		callFunction setPadColor aDeviceOut $i*9 + 5 $arg1
			
	endFor
	
	for i 2 7
		
		callFunction setPadColor aDeviceOut 45 + $i $arg1
		callFunction setPadColor aDeviceOut 54 + $i $arg1
			
	endFor
	
endFunction

