--[[
{***************************************************************************}
{ S4_logic01                                                11/10/2012      }
{                                                                           }
{	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.                                                         }
{***************************************************************************}
--]]


-- ***************************
-- variables
-- ***************************
out_or = {0, 0, 0, 0, 0, 0, 0};
sendMsg = {0, 0, 0, 0, 0, 0, 0};
sendMsgOr = 0;

heatCool = 0;
idP = 0;
tmpVal = 0;

-- ***************************
-- 
-- ***************************
function myLogic(x)
-- 
--  OR condition
--
  if ( x >= 237 and x <= 250 ) then
  
-- get mode
    heatCool = knx.get_integer(230);

--
   if ((heatCool == 0) and ((x % 2) ~= 0)) or
     ((heatCool == 1) and ((x % 2) == 0)) then
-- get/evaluate byte value    
		tmpVal = knx.get_integer(x);
		if (tmpVal > 0) then
		  tmpVal = 1;
		else
		  tmpVal = 0;
		end
	
-- set condition
		  for i=0,6 do
			idP = 237+(i*2)+heatCool;
			if (x == idP) then
			  out_or[i+1] = tmpVal;
			--  knx.set_integer(222+i,0,tmpVal);
			  sendMsg[i+1] = 1;
			end  -- if
		  end  -- for
	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

-- ***************************
-- call back for timeout
-- ***************************
function timeout(x)  
  sendMsgOr = 0;
  
	 for i=1,7 do
	   if (sendMsg[i] == 1) and (sendMsgOr == 0) then
	     sendMsg[i] = 0;
		 sendMsgOr = 1;
		 
	     knx.set_integer(222+i-1,0,out_or[i]);
	   end -- if
	  end -- for

--
-- check OR 
--  
  if (sendMsgOr == 1) then
    sendMsgOr = 0;
	  if ( out_or[1] == 1 or out_or[2] == 1 or out_or[3] == 1 
		  or out_or[4] == 1 or out_or[5] == 1 or out_or[6] == 1 or out_or[7] == 1) then
			knx.set_integer(229,0,1);
		else
		  knx.set_integer(229,0,0);		
	  end 
  end

  return 0 ;
end

-- ***************************
-- init
-- ***************************
sys.timeout(500)

--[[
  EOF 
--]]
