classdef AnalogInputSim < handle
    properties
        mux;
        channel;
        rawValue;
        lastValue;
        lastStored=0;
        rawMax,rawMin,euMax,euMin;
        history=zeros(1, 100, 'double');
        badReads=0;
        
    end
    
    properties(Access=private)
        m,b;
    end
    
    methods
        function obj = AnalogInputSim(mux, channel, rawMin, rawMax, euMin, euMax)
            obj.mux = mux;
            obj.channel = channel;
            obj.rawMin = double(rawMin);
            obj.rawMax = double(rawMax);
            obj.euMin = double(euMin);
            obj.euMax = double(euMax);
            
            obj.m = (obj.euMax - obj.euMin) / (obj.rawMax - obj.rawMin);
            obj.b = obj.euMax - obj.m * (obj.rawMax);
        end
        
        function value = setValue(obj, inValue)
            obj.rawValue = inValue;
            value = obj.m * inValue + obj.b;
            obj.lastValue = value;
            obj.lastStored = now;
            obj.history = [value, obj.history(1:end-1)];
        end
        
        function value = read(obj, simRaw)
            value  = obj.m * simRaw + obj.b;
            obj.lastValue = value;
            obj.lastStored = now;
            
            % Store value in history array
            histTemp = obj.history(1:end-1);
            obj.history(2:end) = histTemp;
            obj.history(1) = value;
        end
    end
end