--[[
{***************************************************************************}
{ S3_logic02                                                11/07/2013      }
{                                                                           }
{	Eelectron S.p.A.                                                        }
{   Via Magenta 77/22                                                       }
{   I-20017 Rho(MI)                                                         }
{   Web: www.eelectron.com                                                  }
{                                                                           }
{ The source code is given as is, WITHOUT WARRANTY OF ANY KIND.             }
{ The author is not responsible for any possible damage done due to the     }
{ use of this code.                                                         }
{***************************************************************************}
--]]

-- ***************************
-- Touch database v2.2
-- ***************************

-- ***************************
-- parameters
-- ***************************
settings={
	 {name="Incremento Setpoint";min=1;max=10;val=5;dc=0};
	 {name="Velocita Fancoil";min=10;max=100;val=80;dc=0};
        };

-- ***************************
-- init variables
-- ***************************
outFCCspeed = 0;
isAlarm = 0;
FCC_MAN = 1;
FCC_AUTO = 0;



function settings_set(x)
 sys.write_settings(x)
-- knx_value_changed(252) --dummy change
end;

-- ***************************
-- 
-- ***************************
function myLogic(x)
-- 
-- get received SP, increment by offset and trasmit 
--

  if (( x >= 240 ) and ( x <= 244 )) then
-- get input setpoint
     inSPval = knx.get_integer(x);
	 
-- increment setpoint bay parameter
   inSPval = inSPval + (settings[1].val * 100);

-- write new value to bus
   knx.set_integer(x+5,2, inSPval);

  end;	
 
 --
 -- alarm status
 --
   if (( x >= 222 ) and ( x <= 226 )) then
-- read alarm status
    isAlarm = knx.get_integer(x);
	
	if (isAlarm == 1) then
-- set FC to man
	  knx.set_integer(x+5, 0, FCC_MAN);
	  
-- set FCC speed
      outFCCspeed = 2.55 * settings[2].val;
	  knx.set_integer(x+10, 1, outFCCspeed);      
	end; 
	
	if (isAlarm == 0) then
-- set FC to auto
	  knx.set_integer(x+5, 0, FCC_AUTO);
	end;
  end;
  
end;


-- ***************************
-- call back for value changed
-- ***************************
function knx_value_changed(x)
  myLogic(x);
end;

-- ***************************
-- call back for value update
-- ***************************
function knx_value_update(x)
  myLogic(x);
end;



-- ***************************
-- init
-- ***************************
sys.read_settings("settings");

--[[
  EOF 
--]]
