using System;
using Microsoft.SPOT;

namespace CPF
{
    class Calibrator
    {
        private double m_Gain;
        private double m_Offset;
        private double m_LowExpected;
        private double m_LowActual;
        private double m_HighExpected;
        private double m_HighActual;
        private double m_SensorSpan;

        public Calibrator(double lowExpected, double lowObserved, 
                   double highExpected, double highObserved,
                   double span)
        {
            m_LowExpected = lowExpected;
            m_LowActual = lowObserved;
            m_HighExpected = highExpected;
            m_HighActual = highObserved;
            m_SensorSpan = span;
            m_Offset = m_LowExpected - m_LowActual;
            m_Gain = (m_HighExpected - m_LowExpected) / (m_HighActual - m_LowExpected);
        }

        public double Calibrate(double uncalibrated)
        {
            return (uncalibrated * m_Gain) + m_Offset;
        }

        public double Scale(double data)
        {
            return (data - m_LowExpected) / (m_HighExpected - m_LowExpected) * m_SensorSpan;
        }
    }
}
