//----------------------------------------------------------------------------
///
/// \file   touch.hpp
///
/// \brief  class for touch screens
///
/// \date   20220619
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

//--- Own ----------------------------

#include <biwak/touch.h>


//---Declaration---------------------------------------------------------------


class CTouchCalibration
{

private:

   class CAxle
   {
      friend class CTouchCalibration;

      private:
         int m_totalDistance;
         int m_calLow;
         int m_calHigh;
         int m_rawPerHektoPixel;

      protected:
         static int constexpr CALIBRATION_BORDER=25;
         static int constexpr HEKTO=100;

      public:
         CAxle(int totalDistance, int low, int high)
            :m_totalDistance( totalDistance )
            ,m_calLow(low)
            ,m_calHigh(high)
         {
            calculate();
         }
         void setCalibration(int totalDistance, int low, int high)
         {
            m_totalDistance = totalDistance;
            m_calLow = low;
            m_calHigh = high;
            calculate();
         }

         void calculate()
         {
            m_rawPerHektoPixel = ( ( ( m_calHigh - m_calLow ) * HEKTO )
                                             / ( m_totalDistance - ( 2 * CALIBRATION_BORDER ) ) );
            if( !m_rawPerHektoPixel )
            {
               // Avoid division by zero when calibration is zero-data
               m_rawPerHektoPixel=1;
            }
         }
         int getPixel(int raw)
         {
            return ( CALIBRATION_BORDER + ( ( ( raw - m_calLow ) * HEKTO )
                     /  m_rawPerHektoPixel ) );

         }
   };

   CAxle m_x, m_y;

public:
   CTouchCalibration(int width, int height, int calXLeft, int calXRight, int calYTop, int calYBottom)
      :m_x(width, calXLeft, calXRight )
      ,m_y(height, calYTop, calYBottom )
   {
   }
   CTouchCalibration()
      :m_x( 0, 0, 0 )
      ,m_y( 0, 0, 0 )
   {
   }
   void setCalibration(int width, int height, int calXLeft, int calXRight, int calYTop, int calYBottom)
   {
      m_x.setCalibration( width, calXLeft, calXRight );
      m_y.setCalibration( height, calYTop, calYBottom );
   }
   int getX(int xRaw)
   {
      return( m_x.getPixel(xRaw) );
   }
   int getY(int yRaw)
   {
      return( m_y.getPixel(yRaw) );
   }
   static int getCalibrationBorder()
   {
      return( CAxle::CALIBRATION_BORDER );
   }
};


//---fin-----------------------------------------------------------------------
