
-- module defines Hystersis class, that handles below functionality
--  
--                             threshold
--                                 |                                ^
--                                 V                                |
--                                    --------- <- valueAfter       |
--                                                                  |
--                                                                  |
--                               --*-- <- deadValue                 |
--                                                                  | outputValue
--                                                                  |
--                                                                  |
-- valueBefore -> ---------------                                   |
--                              ^     ^                             |
--                              |     |                             |
--                              |width|                             |
--
-- ----------------------------------------------------------------->
--                                inputValue


-- class definition
DeadZone = {};

DeadZone.__index = DeadZone;

-- constructor
function DeadZone.create(threshold, valueBefore, valueAfter, width, deadValue)

  local dz = {};                  -- our new object
  setmetatable(dz, DeadZone);    -- make DeadZone handle lookup
  
  -- initialize our object
  dz.threshold = threshold;
  dz.valueBefore = valueBefore;
  dz.valueAfter = valueAfter;
  dz.width = width;
  dz.deadValue = deadValue;
  
  return dz;
 
  end  

  
-- function updates parameters
-- should be called if below parameters can change
function DeadZone:updateParameters(threshold, valueBefore, valueAfter, width, deadValue)

  self.threshold = threshold;
  self.valueBefore = valueBefore;
  self.valueAfter = valueAfter;
  self.width = width;
  self.deadValue = deadValue;

end

--returns output values on the basis of hysteresis parameters and input value
function DeadZone:getOutputValue(inputValue)

  if inputValue >= (self.threshold + (self.width / 2)) then
    return self.valueAfter;
  elseif inputValue <= (self.threshold - (self.width / 2)) then
    return self.valueBefore;
  else
    return self.deadValue;
  end

end


