from buzz import *

fade = 128
max = 0x03E8
min = 0x0000

add = (max-min)/fade+1

trigger = 0
out_tick = 0
in_tick = 0

def OnStop():
	global trigger,out_tick,in_tick
	trigger = 0
	out_tick = 0
	in_tick = 0

def OnTick():
	global fade,max,min,add,trigger,out_tick,in_tick
	if(trigger>0):
		out_value = max-add*out_tick
		in_value = min+add*in_tick
		if(out_value>=min):
			SendPeerCtrlChangeNextTick(0,0,out_value)
			out_tick += 1
		if(in_value<=max):
			SendPeerCtrlChangeNextTick(1,0,in_value)
			in_tick += 1

def OnParameter(track,index,value):
	global trigger,out_tick,in_tick
	
	if(index==1):
		trigger = value
		out_tick = 0
		in_tick = 0

SetEventTarget("OnTick",OnTick)
SetEventTarget("OnParameter",OnParameter)
SetEventTarget("OnStop",OnStop)

SetPeerCtrlName(0,"Fade Out")
SetPeerCtrlName(1,"Fade In")
