⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointer.c

📁 FreeRTOS is a portable, open source, mini Real Time Kernel - a free to download and royalty free RTO
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************* (C) COPYRIGHT 2007 RAISONANCE S.A.S. *******************/
/**
*
* @file     pointer.c
* @brief    Various utilities for the pointer management for STM32-primer.
* @author   FL
* @date     07/2007
* @version  1.1
* @date     10/2007
* @version  1.5 various corrections reported by Ron Miller to suppress jittery
*
*
**/
/******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "circle.h"

/// @cond Internal

/* Private define ------------------------------------------------------------*/
#define POS_MIN                     0
#define POS_MAX                     (SCREEN_WIDTH - POINTER_WIDTH - 1)
#define POINTER_DIVIDER             50
#define POINTER_DEFAULT_COLOR       RGB_BLUE

// defines for pointer move
#define ANGLEPAUSE                  500 
#define DEFAULT_ANGLESTART          25
#define MIN_ANGLE_FOR_SHIFT_UP      (ANGLEPAUSE+CurrentAngleStart)
#define MIN_ANGLE_FOR_SHIFT_DOWN    (ANGLEPAUSE-CurrentAngleStart)
#define MIN_ANGLE_FOR_SHIFT_RIGHT   (signed)(0+CurrentAngleStart)
#define MIN_ANGLE_FOR_SHIFT_LEFT    (signed)(0-CurrentAngleStart)
#define DEFAULT_SPEED_ON_ANGLE      60

/* Private variables ---------------------------------------------------------*/
static int           divider                          = 0;

unsigned char        BallPointerBmp[ POINTER_WIDTH ]  = { 0x38, 0x7C, 0xFF, 0xFF, 0xFF, 0x7C, 0x38 } ;
unsigned char        locbuf[ POINTER_WIDTH ];
unsigned char        DefaultAreaStore[ 2 * POINTER_WIDTH * POINTER_WIDTH ];

// Variables for pointer.
u8*                  CurrentPointerBmp                = 0;
u8                   CurrentPointerWidth              = 0;
u8                   CurrentPointerHeight             = 0;
u16                  CurrentSpeedOnAngle              = DEFAULT_SPEED_ON_ANGLE;
u32                  CurrentAngleStart                = DEFAULT_ANGLESTART  ;
unsigned char*       ptrAreaStore                     = DefaultAreaStore;
u16                  CurrentPointerColor              = POINTER_DEFAULT_COLOR;
enum POINTER_mode    Pointer_Mode                     = POINTER_UNDEF;
enum POINTER_state   Pointer_State                    = POINTER_S_UNDEF;

s16                  OUT_X;
s16                  OUT_Y;

// Init pointer Info Structure (structure definition in circle.h)
tPointer_Info POINTER_Info = {
         SCREEN_WIDTH - POINTER_WIDTH / 2,
         SCREEN_WIDTH - POINTER_WIDTH / 2,
         0,
         0} ;

/* Private function prototypes -----------------------------------------------*/
static int   POINTER_Move ( void );

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
*
*                                Pointer_Move
*
*******************************************************************************/
/**
*  Moves LCD pointer according to Mems indications.
*
*  @retval  0  The pointer resides in the screen
*  @retval  -1 The pointer touche the screen edges.
*
**/
/******************************************************************************/
static int POINTER_Move( void )
   {
   int res                    = 0;
   s16 oldPointer_xPos        = POINTER_Info.xPos;
   s16 oldPointer_yPos        = POINTER_Info.yPos;
   s16 unmodied_shift_PosX;
   s16 unmodied_shift_PosY;
   signed outx                = MEMS_Info.OutX_F16 >> 4;
   signed outy                = MEMS_Info.OutY_F16 >> 4;

   POINTER_Info.shift_PosX  =  POINTER_Info.shift_PosY  = 0;

   // The move depends on the screen orientation
   switch( LCD_GetScreenOrientation() )
      {
      // north
      case V12 :
         MEMS_Info.RELATIVE_X = outx;
         MEMS_Info.RELATIVE_Y = outy;

         if( outx > MIN_ANGLE_FOR_SHIFT_RIGHT )
            {
            POINTER_Info.shift_PosX = ( outx - MIN_ANGLE_FOR_SHIFT_RIGHT );
            }
         else if( outx < MIN_ANGLE_FOR_SHIFT_LEFT )
            {
            POINTER_Info.shift_PosX  = ( outx - MIN_ANGLE_FOR_SHIFT_LEFT );
            }               

         if( outy < -MIN_ANGLE_FOR_SHIFT_UP )
            {
            POINTER_Info.shift_PosY = ( outy + MIN_ANGLE_FOR_SHIFT_UP  );
            }
         else if( outy > -MIN_ANGLE_FOR_SHIFT_DOWN )
            {
            POINTER_Info.shift_PosY = ( outy + MIN_ANGLE_FOR_SHIFT_DOWN );
            }
         break;

      // West
      case V9 :   
         MEMS_Info.RELATIVE_X = -( outy );
         MEMS_Info.RELATIVE_Y = outx;

         if( outy > MIN_ANGLE_FOR_SHIFT_RIGHT )
            {
            POINTER_Info.shift_PosX = -( outy - MIN_ANGLE_FOR_SHIFT_RIGHT );
            }
         else if( outy < MIN_ANGLE_FOR_SHIFT_LEFT )
            {
            POINTER_Info.shift_PosX = -( outy - MIN_ANGLE_FOR_SHIFT_LEFT );
            }

         if( outx < -MIN_ANGLE_FOR_SHIFT_UP )
            {
            POINTER_Info.shift_PosY = ( outx + MIN_ANGLE_FOR_SHIFT_UP );
            }
         else if( outx > -MIN_ANGLE_FOR_SHIFT_DOWN )
            {
            POINTER_Info.shift_PosY = ( outx + MIN_ANGLE_FOR_SHIFT_DOWN );
            }
         break;

      // South
      case V6 :   
         MEMS_Info.RELATIVE_X = -( outx );
         MEMS_Info.RELATIVE_Y = -( outy );

         if( outx > MIN_ANGLE_FOR_SHIFT_RIGHT )
            {
            POINTER_Info.shift_PosX = ( MIN_ANGLE_FOR_SHIFT_RIGHT - outx );
            }
         else if( outx < MIN_ANGLE_FOR_SHIFT_LEFT )
            {
            POINTER_Info.shift_PosX = ( MIN_ANGLE_FOR_SHIFT_LEFT - outx );
            }

         if( outy > MIN_ANGLE_FOR_SHIFT_UP )
            {
            POINTER_Info.shift_PosY = -( outy - MIN_ANGLE_FOR_SHIFT_UP );
            }
         else if( outy < MIN_ANGLE_FOR_SHIFT_DOWN )
            {
            POINTER_Info.shift_PosY = +( MIN_ANGLE_FOR_SHIFT_DOWN - outy );
            }
         break;

      // East
      case V3 :   
         MEMS_Info.RELATIVE_X = outy;
         MEMS_Info.RELATIVE_Y = -( outx );

         if( outy > MIN_ANGLE_FOR_SHIFT_RIGHT )
            {
            POINTER_Info.shift_PosX = ( outy - MIN_ANGLE_FOR_SHIFT_RIGHT );
            }
         else if( outy < MIN_ANGLE_FOR_SHIFT_LEFT )
            {
            POINTER_Info.shift_PosX = ( outy - MIN_ANGLE_FOR_SHIFT_LEFT );
            }

         if( outx > MIN_ANGLE_FOR_SHIFT_UP )
            {
            POINTER_Info.shift_PosY = ( MIN_ANGLE_FOR_SHIFT_UP - outx);
            }
         else if( outx < MIN_ANGLE_FOR_SHIFT_DOWN )
            {
            POINTER_Info.shift_PosY = ( MIN_ANGLE_FOR_SHIFT_DOWN - outx );
            }

      default :
         break;
      }

   unmodied_shift_PosX = POINTER_Info.shift_PosX;
   unmodied_shift_PosY = POINTER_Info.shift_PosY;

   POINTER_Info.shift_PosX /= CurrentSpeedOnAngle;
   POINTER_Info.shift_PosY /= CurrentSpeedOnAngle;

   if( Pointer_Mode == POINTER_APPLICATION )
      {
      if ( Application_Pointer_Mgr )
         {
         Application_Pointer_Mgr( POINTER_Info.shift_PosX, POINTER_Info.shift_PosY );
         }

      return 0;
      }

   POINTER_Info.xPos += POINTER_Info.shift_PosX;
   POINTER_Info.yPos += POINTER_Info.shift_PosY;

   if( POINTER_Info.xPos < POINTER_Info.X_PosMin )
      {
      POINTER_Info.xPos = POINTER_Info.X_PosMin;
      }

   if( POINTER_Info.xPos > POINTER_Info.X_PosMax )
      {
      POINTER_Info.xPos = POINTER_Info.X_PosMax;
      }

   if( POINTER_Info.yPos < POINTER_Info.Y_PosMin )
      {
      POINTER_Info.yPos = POINTER_Info.Y_PosMin;
      }

   if( POINTER_Info.yPos > POINTER_Info.Y_PosMax )
      {
      POINTER_Info.yPos = POINTER_Info.Y_PosMax;
      }

   if( ( Pointer_Mode != POINTER_MENU ) && ( Pointer_Mode != POINTER_RESTORE_LESS ) &&
       ( ( oldPointer_xPos != POINTER_Info.xPos ) || ( oldPointer_yPos != POINTER_Info.yPos ) )  )
      {
      // Use default area.
      POINTER_SetCurrentAreaStore( 0 );

      // Restore previously drawn area.
      POINTER_Restore( oldPointer_xPos, oldPointer_yPos, POINTER_WIDTH, POINTER_WIDTH );

      // Save new area and draw pointer
      POINTER_Save( POINTER_Info.xPos, POINTER_Info.yPos, POINTER_WIDTH, POINTER_WIDTH );
      POINTER_Draw( POINTER_Info.xPos, POINTER_Info.yPos, POINTER_WIDTH, POINTER_WIDTH, CurrentPointerBmp );
      }

   if( ( Pointer_Mode == POINTER_RESTORE_LESS ) &&
       ( ( oldPointer_xPos != POINTER_Info.xPos ) || ( oldPointer_yPos != POINTER_Info.yPos ) )  )
      {
      // Use default area.
      POINTER_SetCurrentAreaStore( 0 );

      // Restore previously drawn area.
      POINTER_Restore( oldPointer_xPos, oldPointer_yPos, CurrentPointerWidth, CurrentPointerHeight );

      // Save new area and draw pointer
      POINTER_Save( POINTER_Info.xPos, POINTER_Info.yPos, CurrentPointerWidth, CurrentPointerHeight );
      POINTER_Draw( POINTER_Info.xPos, POINTER_Info.yPos, CurrentPointerWidth, CurrentPointerHeight, CurrentPointerBmp );
      }

   // Is the pointer touching one edge of the screen ?
   if( ( POINTER_Info.xPos == POS_MIN ) || ( POINTER_Info.yPos == POS_MIN ) ||
       ( POINTER_Info.xPos == POS_MAX ) || ( POINTER_Info.yPos == POS_MAX ) )
      {
      res = -1;
      }

   return res;
   }

/* Public functions for CircleOS ---------------------------------------------*/

/*******************************************************************************
*
*                                POINTER_Init
*
*******************************************************************************/
/**
*  Initialize pointer. Called at CircleOS startup. Set default pointer at the
*  middle of the screen and allows it to move into the whole screen.
*
*  @attention  This function must <b>NOT</b> be called by the user.
*
**/
/******************************************************************************/
void POINTER_Init( void )
   {
   // Increase pointer sensibility.
   POINTER_SetCurrentSpeedOnAngle( DEFAULT_SPEED_ON_ANGLE );
   POINTER_SetCurrentAngleStart( DEFAULT_ANGLESTART );
   POINTER_SetCurrentPointer( POINTER_WIDTH, POINTER_WIDTH, BallPointerBmp );
   POINTER_SetMode( POINTER_ON );
   POINTER_SetPos( 56, 56 );
   POINTER_SetRectScreen();

   CurrentPointerColor = POINTER_DEFAULT_COLOR;
   }

/*******************************************************************************
*
*                                POINTER_Handler
*
*******************************************************************************/
/**
*
*  Called by the CircleOS scheduler to manage the pointer.
*
*  @attention  This function must <b>NOT</b> be called by the user.
*
**/
/******************************************************************************/
void POINTER_Handler( void )
   {
   switch( Pointer_Mode )
      {
      // Nothing to do!
      case POINTER_OFF  :
      case POINTER_UNDEF:
         return;
      }

   // Where is the MEMS ?
   MEMS_GetPosition( &OUT_X, &OUT_Y );

   POINTER_Move();
   }

/// @endcond

/* Public functions ----------------------------------------------------------*/

/*******************************************************************************
*
*                                POINTER_SetCurrentPointer
*
*******************************************************************************/
/**
*
*  Set the dimension and the bitmap of the pointer.
*  @note The bitmap is a monochrome one!
*
*  @param[in]  width    width of the pointer  (u8)
*  @param[in]  height   height of the pointer (u8)
*  @param[in]  bmp      pointer to an array of width * height bits.
*
**/
/********************************************************************************/
void POINTER_SetCurrentPointer( u8 width, u8 height, u8* bmp )
   {
   if( !bmp )
      {
      bmp = BallPointerBmp;
      }

   CurrentPointerWidth  = width;
   CurrentPointerHeight = height;
   CurrentPointerBmp    = bmp;
   }

/*******************************************************************************
*
*                                POINTER_GetCurrentAngleStart
*
*******************************************************************************/
/**
*
*  Get the current minimal angle to move pointer
*
*  @return  current minimal angle.
*
**/
/******************************************************************************/
u16 POINTER_GetCurrentAngleStart( void )
   {
   return CurrentAngleStart;
   }

/*******************************************************************************
*
*                                POINTER_SetCurrentAngleStart
*
*******************************************************************************/
/**
*
*  Set the current minimal angle to move pointer
*
*  @param[in]  newangle The new minimal angle to move pointer.
*
**/
/******************************************************************************/
void POINTER_SetCurrentAngleStart( u16 newangle )
   {
   CurrentAngleStart = newangle;
   }

/*******************************************************************************
*
*                                POINTER_GetCurrentSpeedOnAngle
*
*******************************************************************************/
/**
*
*  Return the current speed/angle ratio.
*
*  @return  current ratio.
*
**/
/******************************************************************************/
u16 POINTER_GetCurrentSpeedOnAngle( void )
   {
   return CurrentSpeedOnAngle;
   }

/*******************************************************************************
*
*                                POINTER_SetCurrentSpeedOnAngle
*
*******************************************************************************/
/**
*
*  Set the current speed/angle ratio.
*
*  @param[in]  newspeed New speed/angle ratio.
*
**/
/******************************************************************************/
void POINTER_SetCurrentSpeedOnAngle( u16 newspeed )
   {
   CurrentSpeedOnAngle = newspeed;
   }

/*******************************************************************************
*
*                                POINTER_SetCurrentAreaStore
*
*******************************************************************************/
/**
*
*  Change the current storage area. If the provided value is NULL, the default
*  storage area will be used.
*
*  @param[in]  ptr New storage area (may be null).
*
*  @warning    Memory space pointed by the provided pointer must be large enough
*              to store a color bitmap corresponding to the pointer area.
*              In other words, space must be width * height * 2 large.
*
**/
/******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -