gui_touch_driveranalog.c

来自「S3c44b0x下的ucgui」· C语言 代码 · 共 392 行

C
392
字号
/***********************************************************************************************************                                                uC/GUI*                        Universal graphic software for embedded applications**                       (c) Copyright 2002, Micrium Inc., Weston, FL*                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH**              礐/GUI is protected by international copyright laws. Knowledge of the*              source code may not be used to write a similar product. This file may*              only be used in accordance with a license and should not be redistributed*              in any way. We appreciate your understanding and fairness.*----------------------------------------------------------------------
File        : GUITOUCH.C
Purpose     : Touch screen manager
----------------------------------------------------------------------
This module handles the touch screen. It is configured in the file
GUITouch.conf.h (Should be located in the Config\ directory).
----------------------------------------------------------------------
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LCD_Private.H"      /* private modul definitions & config */
#include "GUI_Protected.h"
#include "def.h"#include "44b.h"#include "44blib.h"extern void irq_TspHandler(void);/* Generate code only if configuration says so ! */
#if GUI_SUPPORT_TOUCH

#include "GUITouchconf.h"    /* Located in GUIx, will include GUITouch.conf.h */

/*
**********************************************************************
*
*          Config defaults
*
**********************************************************************
*/
#ifndef GUI_TOUCH_AD_LEFT      /* max value returned by AD-converter */
  #define GUI_TOUCH_AD_LEFT 30   
#endif

#ifndef GUI_TOUCH_AD_RIGHT      /* min value returned by AD-converter */
  #define GUI_TOUCH_AD_RIGHT 220    
#endif

#ifndef GUI_TOUCH_AD_TOP       /* max value returned by AD-converter */
  #define GUI_TOUCH_AD_TOP 30
#endif

#ifndef GUI_TOUCH_AD_BOTTOM      /* min value returned by AD-converter */
  #define GUI_TOUCH_AD_BOTTOM 220
#endif

#ifndef GUI_TOUCH_SWAP_XY    /* Is XY of touch swapped ? */
  #define GUI_TOUCH_SWAP_XY 0
#endif

#ifndef GUI_TOUCH_MIRROR_X
  #define GUI_TOUCH_MIRROR_X 0
#endif

#ifndef GUI_TOUCH_MIRROR_Y
  #define GUI_TOUCH_MIRROR_Y 0
#endif

#ifndef GUI_TOUCH_YSIZE
  #define GUI_TOUCH_YSIZE LCD_YSIZE
#endif

#ifndef GUI_TOUCH_XSIZE
  #define GUI_TOUCH_XSIZE LCD_XSIZE
#endif

/*
**********************************************************************
*
*          Config check
*
**********************************************************************
*/

/*
  *****************************************************************
  *                                                               *
  *              Global data                                      *
  *                                                               *
  *****************************************************************

The global data below is for debugging purposes only. A "clean"
application should not use these values for any other purpose.
NEVER write into these values !
*/


//int GUI_TOUCH_yPhys, GUI_TOUCH_xPhys;

/****************************************************************
*
*       Static data
*
*****************************************************************
*/

typedef struct {int Min; int Max; } tMinMax;

static tMinMax xyMinMax[2] = {
#if ((GUI_TOUCH_SWAP_XY==0) && (GUI_TOUCH_MIRROR_X==0)) || ((GUI_TOUCH_SWAP_XY) && (GUI_TOUCH_MIRROR_Y==0))
  { GUI_TOUCH_AD_LEFT, GUI_TOUCH_AD_RIGHT },
#else
  { GUI_TOUCH_AD_RIGHT, GUI_TOUCH_AD_LEFT },
#endif
#if ((GUI_TOUCH_SWAP_XY==0) && (GUI_TOUCH_MIRROR_Y==0)) || ((GUI_TOUCH_SWAP_XY) && (GUI_TOUCH_MIRROR_X==0))
  { GUI_TOUCH_AD_TOP,  GUI_TOUCH_AD_BOTTOM }
#else
  { GUI_TOUCH_AD_BOTTOM,  GUI_TOUCH_AD_TOP }
#endif
};

#ifndef WIN32
static int xMin;
static int xMax;
static int yMin;
static int yMax;
#endif

/*********************************************************************
*
*       Convert physical value into (logical) coordinates
*/
int AD2X(int adx) {
  I32 r = adx - xyMinMax[GUI_COORD_X].Min;
  r *= GUI_TOUCH_XSIZE - 1;
  return r / (xyMinMax[GUI_COORD_X].Max - xyMinMax[GUI_COORD_X].Min);    
}

int AD2Y(int ady) {
  I32 r = ady - xyMinMax[GUI_COORD_Y].Min;
  r *= GUI_TOUCH_YSIZE - 1;
  return r/(xyMinMax[GUI_COORD_Y].Max - xyMinMax[GUI_COORD_Y].Min);    
}

/*********************************************************************
*
*        Diagnostic routines
*/
int  GUI_TOUCH_GetxPhys(void) {
  return 0; //GUI_TOUCH_xPhys;
}

int  GUI_TOUCH_GetyPhys(void) {
  return 0; //GUI_TOUCH_yPhys;
}



/*********************************************************************
*
*              SetDefaultCalibration
*/

void GUI_TOUCH_SetDefaultCalibration(void) {
  xyMinMax[0].Min = GUI_TOUCH_AD_LEFT;
  xyMinMax[0].Max = GUI_TOUCH_AD_RIGHT;
  xyMinMax[1].Min = GUI_TOUCH_AD_TOP;
  xyMinMax[1].Max = GUI_TOUCH_AD_BOTTOM;
}

/*********************************************************************
*
*              Calibration
*/


static int Log2Phys(int l, I32 l0, I32 l1, I32 p0, I32 p1) {
  return p0+ ((p1-p0) * (l-l0)) / (l1-l0);
}

int GUI_TOUCH_Calibrate(int Coord, int Log0, int Log1, int Phys0, int Phys1) {
  int l0 = 0;
  int l1 = (Coord==GUI_COORD_X) ? LCD_XSIZE-1 : LCD_YSIZE-1;
  if (labs(Phys0-Phys1) < 20)
    return 1;
  if (labs(Log0-Log1) < 20)
    return 1;
  xyMinMax[Coord].Min = Log2Phys(l0, Log0, Log1, Phys0, Phys1);
  xyMinMax[Coord].Max = Log2Phys(l1, Log0, Log1, Phys0, Phys1);
  return 0;
}


/*********************************************************************
*
*              GUI_TOUCH_Exec
*/

void GUI_TOUCH_Exec(void) {
  #ifndef WIN32
  static U8 ReadState;
  static int xPhys, yPhys;
  int x,y;
  /* calculate Min / Max values */
  if (xyMinMax[GUI_COORD_X].Min < xyMinMax[GUI_COORD_X].Max) {
    xMin = xyMinMax[GUI_COORD_X].Min;
    xMax = xyMinMax[GUI_COORD_X].Max;
  } else {
    xMax = xyMinMax[GUI_COORD_X].Min;
    xMin = xyMinMax[GUI_COORD_X].Max;
  }
  if (xyMinMax[GUI_COORD_Y].Min < xyMinMax[GUI_COORD_Y].Max) {
    yMin = xyMinMax[GUI_COORD_Y].Min;
    yMax = xyMinMax[GUI_COORD_Y].Max;
  } else {
    yMax = xyMinMax[GUI_COORD_Y].Min;
    yMin = xyMinMax[GUI_COORD_Y].Max;
  }
  /* Execute the state machine which reads the touch */
  switch (ReadState) {
  case 0:
    yPhys = TOUCH_X_MeasureY();
    TOUCH_X_ActivateY();  /* Prepare X- measurement */
    ReadState++;
    break;
  default:
    xPhys = TOUCH_X_MeasureX();
    TOUCH_X_ActivateX();  /* Prepare Y- measurement */
    /* Convert values into logical values */
      #if !GUI_TOUCH_SWAP_XY   /* Is X/Y swapped ? */
        x = xPhys;
        y = yPhys;
      #else
        x = yPhys;
        y = xPhys;
      #endif

    if ((x <xMin) | (x>xMax)  | (y <yMin) | (y>yMax)) {
      GUI_TOUCH_StoreState(-1,-1);
    } else {
      x = AD2X(x);
      y = AD2Y(y);
      GUI_TOUCH_StoreState(x,y);
    }
    /* Reset state machine */
    ReadState=0;
    break;
  }
  #endif /* WIN32 */
}


#else
void GUI_TOUCH_DriverAnalog_C(void) {}

#endif    /* defined(GUI_SUPPORT_TOUCH) && GUI_SUPPORT_TOUCH */
/////////////////////////driver//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void TS_init(void){    	rINTMOD=0x0;	rINTCON=0x1;	rI_ISPC |= BIT_ZDMA0;            //clear pending    rI_ISPC |= BIT_EINT2;            // clear pending_bit	    //TSPX(GPE4) TSPY(GPE5) TSMY(GPE6) TSMX(GPE7)    //  0		1	 1          0    rPUPE  = 0x0;	                 //  Pull up    //rPCONE	= 0x20269;                  rPDATE = 0xb8;                   	    DelayTime(100);         rEXTINT |= 0x200;                // falling edge trigger    pISR_EINT2=(int)irq_TspHandler;        // set interrupt handler            rCLKCON = 0x7ff8;                // enable clock    rADCPSR =  0x10;//0x4;                   // A/D prescaler    //rADCPSR =  0x2f;//    rINTMSK =~(BIT_GLOBAL|BIT_EINT2|BIT_TIMER0);}void TOUCH_X_ActivateX(void)  {}void TOUCH_X_ActivateY(void) {}int  TOUCH_X_MeasureX(void){return 1;}int  TOUCH_X_MeasureY(void) {return 1;}void TOUCH_X_Disable(void)  {}float TP_ReadY()//读Y坐标{  float tmp[15];  float result;  int i;  //rPCONE = 0x24169;    //pe47 out,pe56 in  rPDATE = 0x98;                   //PE4=1,PE7=1  rADCCON = 0x0<<2;			       // AIN0  DelayTime(500);			       // delay to set up the next channel   for( i=0; i<15; i++ ){		rADCCON |= 0x1;			       // Start A/D conversion	    while( rADCCON & 0x1 );	       // Check if Enable_start is low    	while( !(rADCCON & 0x40) );    // Check ECFLG	    tmp[i] = (0x3ff&rADCDAT);   } result = 0;    for(i=0; i<15; i++){       result += tmp[i];    } result/= 15; return result;}float TP_ReadX()//读X坐标{ int tmp[15]; float result; int i;// rPCONE = 0x21469; rPDATE = 0x68; //pe5=1,pe6=1 rADCCON = 0x1<<2;               // AIN1 DelayTime(500);                // delay to set up the next channel for(i=0; i<15; i++){       rADCCON |= 0x1;             // Start A/D conversion	   while( rADCCON & 0x1 );     // Check if Enable_start is low	   while( !(rADCCON & 0x40) ); // Check ECFLG		 tmp[i] = (0x3ff&rADCDAT);     } result = 0; for(i=0; i<15; i++){    result += tmp[i]; } result /= 15; return result; }void TS(void){ float TS_X,TS_Y; char  fail = 0; float LOG_X,LOG_Y; TS_X=TP_ReadX(); if (TS_X < GUI_TOUCH_AD_LEFT || TS_X > GUI_TOUCH_AD_RIGHT ) {    fail = 1;    } else {     LOG_X = (320*(TS_X-GUI_TOUCH_AD_LEFT ))/(GUI_TOUCH_AD_RIGHT  - GUI_TOUCH_AD_LEFT);   // X - position    } TS_Y=TP_ReadY();  if (TS_Y < GUI_TOUCH_AD_BOTTOM || TS_Y > GUI_TOUCH_AD_TOP) {    fail = 1;   } else {    LOG_Y = (240*(GUI_TOUCH_AD_TOP - TS_Y))/( GUI_TOUCH_AD_TOP - GUI_TOUCH_AD_BOTTOM );  // Y - position      }    if (!fail) {      // GUI_SetFont(&GUI_Font24_1);    //GUI_GotoXY(70,120);   // GUI_DispFloat (TS_X,7);   // GUI_GotoXY(160,120);   // GUI_DispFloat ( TS_Y,7);   GUI_TOUCH_StoreState(LOG_X+1,LOG_Y+1);   GUI_TOUCH_StoreState(LOG_X,LOG_Y);   } rPDATE = 0xb8;     DelayTime(500);  }

⌨️ 快捷键说明

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