function interp(in1, out1, in2, out2, x)
	if x < in1 then
		return out1
	end
	if x > in2 then
		return out2
	end
	return out1 + (out2 - out1) * (x - in1) / (in2 - in1)
end

override1 = globalProperty("sim/flightmodel2/engines/prop_disc/override[0]")
disc_s_dim1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_s_dim[0]")
disc_t_dim1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_t_dim[0]")
disc_alpha_front1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_alpha_front[0]")
disc_alpha_side1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_alpha_side[0]")
disc_alpha_inside1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_alpha_inside[0]")
side_width1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_width[0]")
side_number_of_blades1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_number_of_blades[0]")
side_s_dim1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_s_dim[0]")
side_t_dim1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_t_dim[0]")
side_alpha_front1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_alpha_front[0]")
side_alpha_side1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_alpha_side[0]")
side_alpha_inside1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_alpha_inside[0]")
side_is_billboard1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_is_billboard[0]")


override2 = globalProperty("sim/flightmodel2/engines/prop_disc/override[1]")
disc_s_dim2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_s_dim[1]")
disc_t_dim2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_t_dim[1]")
disc_alpha_front2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_alpha_front[1]")
disc_alpha_side2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_alpha_side[1]")
disc_alpha_inside2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_alpha_inside[1]")
side_width2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_width[1]")
side_number_of_blades2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_number_of_blades[1]")
side_s_dim2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_s_dim[1]")
side_t_dim2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_t_dim[1]")
side_alpha_front2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_alpha_front[1]")
side_alpha_side2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_alpha_side[1]")
side_alpha_inside2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_alpha_inside[1]")
side_is_billboard2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_is_billboard[1]")

sim_speed = globalProperty("sim/time/sim_speed")
frame_rate_period = globalProperty("sim/operation/misc/frame_rate_period")

prop_rotation_speed_rad_sec1 = globalProperty("sim/flightmodel2/engines/prop_rotation_speed_rad_sec[0]")
prop_rotation_speed_rad_sec2 = globalProperty("sim/flightmodel2/engines/prop_rotation_speed_rad_sec[1]")

side_angle1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_angle[0]")
side_angle2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_angle[1]")

prop_rotation_angle_deg1 = globalProperty("sim/flightmodel2/engines/prop_rotation_angle_deg[0]")
prop_rotation_angle_deg2 = globalProperty("sim/flightmodel2/engines/prop_rotation_angle_deg[1]")

prop_is_disc1 = globalProperty("sim/flightmodel2/engines/prop_is_disc[0]")
disc_s1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_s[0]")
disc_t1 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_t[0]")
side_s1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_s[0]")
side_t1 = globalProperty("sim/flightmodel2/engines/prop_disc/side_t[0]")

prop_is_disc2 = globalProperty("sim/flightmodel2/engines/prop_is_disc[1]")
disc_s2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_s[1]")
disc_t2 = globalProperty("sim/flightmodel2/engines/prop_disc/disc_t[1]")
side_s2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_s[1]")
side_t2 = globalProperty("sim/flightmodel2/engines/prop_disc/side_t[1]")

lever_angle_1 = globalProperty("sim/cockpit2/engine/actuators/prop_rotation_speed_rad_sec[0]")
lever_angle_2 = globalProperty("sim/cockpit2/engine/actuators/prop_rotation_speed_rad_sec[1]")
rad_alt = globalProperty("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot")

local MIN_LEVER_ANGLE = 178.05 --168.3 still works, 178 in flight === 1700 rpm
local MAX_LEVER_ANGLE = 214.5 -- ~2050 RPM

set(override1, 1)
set(disc_s_dim1, 4)
set(disc_t_dim1, 2)
set(disc_alpha_front1, 1.0)
set(disc_alpha_side1, 0.1)
set(disc_alpha_inside1, 0.8)
set(side_width1 , 0.15)
set(side_number_of_blades1, 5)
set(side_s_dim1, 32)
set(side_t_dim1, 2)
set(side_alpha_front1, 0.0)
set(side_alpha_side1, 1.0)
set(side_alpha_inside1, 0.8)
set(side_is_billboard1,  0)

set(override2, 1)
set(disc_s_dim2, 4)
set(disc_t_dim2, 2)
set(disc_alpha_front2, 1.0)
set(disc_alpha_side2, 0.1)
set(disc_alpha_inside2, 0.8)
set(side_width2, 0.15)
set(side_number_of_blades2, 5)
set(side_s_dim2, 32)
set(side_t_dim2, 2)
set(side_alpha_front2, 0.0)
set(side_alpha_side2, 1.0)
set(side_alpha_inside2, 0.8)
set(side_is_billboard2,  0)


function onAvionicsDone()
	set(override1, 0)
	set(override2, 0)
end

	
function update()
	local prop_speed_now1
	local prop_speed_now2
	local prop_angle_now1
	local prop_angle_now2
	local side_angle_now1
	local side_angle_now2

	local _s = get(sim_speed)
	_s = _s * get(frame_rate_period)
	
	prop_speed_now1 = get(prop_rotation_speed_rad_sec1)
	prop_speed_now2 = get(prop_rotation_speed_rad_sec2)
	
	side_angle_now1 = get(side_angle1)
	side_angle_now2 = get(side_angle2)
	
	prop_angle_now1 = get(prop_rotation_angle_deg1)
	prop_angle_now2 = get(prop_rotation_angle_deg2)

	prop_angle_now1 = prop_angle_now1 + prop_speed_now1 * _s * 60.0
	prop_angle_now2 = prop_angle_now2 + prop_speed_now2 * _s * 60.0
	side_angle_now1 = side_angle_now1 + prop_speed_now1 * _s * 60.0
	side_angle_now2 = side_angle_now2 + prop_speed_now2 * _s * 60.0
	
	set(side_angle1, side_angle_now1)
	set(side_angle2, side_angle_now2)

	set(prop_rotation_angle_deg1, prop_angle_now1)
	set(prop_rotation_angle_deg2, prop_angle_now2)

	if prop_speed_now1 > 18.0 then
		if get(prop_is_disc1) ~= 1 then
			set(prop_is_disc1, 1)
		end
	else
		if get(prop_is_disc1) ~= 0 then
			set(prop_is_disc1, 0)
		end
	end

	if prop_speed_now2 > 18.0 then
		if get(prop_is_disc2) ~= 1 then
			set(prop_is_disc2, 1)
		end
	else
		if get(prop_is_disc2) ~= 0 then
			set(prop_is_disc2, 0)
		end
	end

	set(disc_s1, interp(15,0,100,2,prop_speed_now1))
	set(disc_s2, interp(15,0,100,2,prop_speed_now2))
	
	-- TODO Do we need to update here or is it enough outside?
	if get(disc_t1) ~= 0 then
		set(disc_t1,0)
	end
	if get(disc_t2) ~= 1 then
		set(disc_t2,1)
	end

	set(side_s1, interp(15,26,100,24,prop_speed_now1))
	set(side_s2, interp(15,26,100,24,prop_speed_now2))

	if get(side_t1) ~= 0 then
		set(side_t1,0)
	end
	if get(side_t2) ~= 1 then
		set(side_t2,1)
	end

	--Avoid engine stall due to excessive propeller angle
	if get(lever_angle_1) < MIN_LEVER_ANGLE then
		set(lever_angle_1, MIN_LEVER_ANGLE)
	end
	if get(lever_angle_2) < MIN_LEVER_ANGLE then
		set(lever_angle_2, MIN_LEVER_ANGLE)
	end
	--Avoid engine not producing proper TRQ in higher altitude airports & warmer weather
	if get(lever_angle_1) > MAX_LEVER_ANGLE then
		set(lever_angle_1, MAX_LEVER_ANGLE)
	end
	if get(lever_angle_2) > MAX_LEVER_ANGLE then
		set(lever_angle_2, MAX_LEVER_ANGLE)
	end

	
end




-- if get(SimVersion) == 1 then 
-- set(SimV, 1)
-- --set(Retract, 0)
-- else
-- set(SimV, 0)
-- end
