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


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


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


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

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


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


/**
 * @brief The OTouch class provides translation of raw touch values to
 *        pixel coordinates via claibration.
 *
 *        In the constructor the raw values of the calibration points have to
 *        be specified. The calibration points are on the corner of the display
 *        distanced to the border by getCalibrationBorder().
 */
class OTouch
{

private:

   class OAxle
   {
      friend class OTouch;

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

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

      public:
         constexpr OAxle(int totalDistance, int low, int high)
            :m_totalDistance( totalDistance )
            ,m_calLow(low)
            ,m_calHigh(high)
            ,m_rawPerHektoPixel( ( ( m_calHigh - m_calLow ) * HEKTO )
                                      / ( m_totalDistance - ( 2 * CALIBRATION_BORDER ) ) )
         {
         }
         int getPixel(int raw) const
         {
            return ( CALIBRATION_BORDER + ( ( ( raw - m_calLow ) * HEKTO )
                     /  m_rawPerHektoPixel ) );
         }
   };

   OAxle m_x, m_y;

public:
   constexpr OTouch(int width, int height, int calXLeft, int calXRight, int calYTop, int calYBottom)
      :m_x(width, calXLeft, calXRight )
      ,m_y(height, calYTop, calYBottom )
   {
   }
   int getX(int xRaw) const
   {
      return( m_x.getPixel(xRaw) );
   }
   int getY(int yRaw) const
   {
      return( m_y.getPixel(yRaw) );
   }
   int getCalibrationBorder() const
   {
      return( OAxle::CALIBRATION_BORDER );
   }
};


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