gui_touch_driveranalog.c

来自「一个在44b0上移植成功的ucgui」· C语言 代码 · 共 333 行

C
333
字号
/***********************************************************************************************************                                                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.CPurpose     : Touch screen manager----------------------------------------------------------------------This module handles the touch screen. It is configured in the fileGUITouch.conf.h (Should be located in the Config\ directory).----------------------------------------------------------------------*/#include	"UCOS-II\includes.h"      /* uC/OS interface */#include "44b.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include "LCD_Private.H"      /* private modul definitions & config */#include "GUI_Protected.h"#include "GUI.H"/* 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 0x0a#endif#ifndef GUI_TOUCH_AD_RIGHT      /* min value returned by AD-converter */  #define GUI_TOUCH_AD_RIGHT 0xd0   #endif#ifndef GUI_TOUCH_AD_TOP       /* max value returned by AD-converter */  #define GUI_TOUCH_AD_TOP 0x1e#endif#ifndef GUI_TOUCH_AD_BOTTOM      /* min value returned by AD-converter */  #define GUI_TOUCH_AD_BOTTOM  0xe4#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  ////(1)触摸屏的最小值在左下角时,(0) 触摸屏的最小值在左上角时#endif#ifndef GUI_TOUCH_YSIZE  #define GUI_TOUCH_YSIZE LCD_YSIZE#endif#ifndef GUI_TOUCH_XSIZE  #define GUI_TOUCH_XSIZE LCD_XSIZE#endif/******************************************************************       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 WIN32static int xMin;static int xMax;static int yMin;static int yMax;#endif/**********************************************************************    AD值转换为像素坐标值。*       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);    }#if !GUI_TOUCH_MIRROR_Yint 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);    }#elseint AD2Y(int ady) {  I32 r =xyMinMax[GUI_COORD_Y].Max - ady;   //   触摸屏的最小值在左下角时      r *= GUI_TOUCH_YSIZE - 1;  return r/(xyMinMax[GUI_COORD_Y].Max - xyMinMax[GUI_COORD_Y].Min);    }#endif/**********************************************************************功能读出X,Y.*        Diagnostic routines*/int  GUI_TOUCH_GetxPhys(void) {  return ad_run(0x0dD); //GUI_TOUCH_xPhys;}int  GUI_TOUCH_GetyPhys(void) {  return ad_run(0x09D); //GUI_TOUCH_yPhys;}void TOUCH_X_ActivateX(void) {}void TOUCH_X_ActivateY(void) {}void TOUCH_X_Disable(void) {}int  TOUCH_X_MeasureX(void) {}int  TOUCH_X_MeasureY(void) {}/**********************************************************************  设置没有校准前的X轴和Y轴的最大值和最小值。GUI_TOUCH_AD_LEFT...等是前面(42)define的常数。*              SetDefault Calibration*/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*   校准X轴和Y轴的最大值和最小值。*/static int Log2Phys(int l, I32 l0, I32 l1, I32 p0, I32 p1) {  return p0+ ((p1-p0) * (l-l0)) / (l1-l0);}//Coord是常量GUI_COORD_X或GUI_COORD_Y,即0或1. //见例子Sample\GUIDemo\GUIDEMO_Touch.c(55):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 */}void TOUCH_task(void *p){  static int x,y;
  //static int  countk;
  int  p_en=0;   for(;;)   {     OS_ENTER_CRITICAL();     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;	 }	 OS_EXIT_CRITICAL();     /* Execute the state machine which reads the touch */        x = ad_run(0x0dD);        y = ad_run(0x09D);     OS_ENTER_CRITICAL();  
	 if(x <xMin)
	 {p_en = 0;}     if ((x <xMin) | (x>xMax)  | (y <yMin) | (y>yMax)|(x>>3 != ad_run(0x0dD)>>3) | (y>>3 != ad_run(0x09D)>>3) |p_en==1)      {       GUI_TOUCH_StoreState(-1,-1);	 }	 else if(p_en == 0)	 {      x = AD2X(x);      y = AD2Y(y);      GUI_TOUCH_StoreState(x,y);    //存储像素坐标值存到全局变量_State里
	  p_en = 1;     }    OS_EXIT_CRITICAL();   }}U8 ad_run(U8 AD_CH){   U32  AD_DAT;   U32  k,i,TM;     OS_ENTER_CRITICAL();     rPDATF = ~( (~rPDATF) | 1<<8 );             //  CLK =0     for(i=0; i<8; i++)	 {         if(AD_CH>>7 == 1)		 {rPDATF = rPDATF | 1<<5;}               // DIN =1 		 else{rPDATF = ~( (~rPDATF) | 1<<5 );}   // DIN =0        rPDATF = ~( (~rPDATF) | 1<<8 );           //  CLK =0       for (k=0; k<5; k++){}       rPDATF = rPDATF | 1<<8;                   // CLK =1 	   AD_CH = AD_CH <<1;	 }     rPDATF = ~( (~rPDATF) | 1<<5 );            // DIN =0      OS_EXIT_CRITICAL();      OSTimeDly(1);                       //延时     OS_ENTER_CRITICAL();	 AD_DAT = 0;     rPDATF = ~( (~rPDATF) | 1<<8 );            //  CLK =0     for(i=0; i<16; i++)	 {       rPDATF = rPDATF | 1<<8;                   // CLK =1         for (k=0; k<5; k++){}	   AD_DAT = AD_DAT & 0x0ffff00;       rPDATF = ~( (~rPDATF) | 1<<8 );           //  CLK =0	   TM = rPDATF;	   AD_DAT = AD_DAT | (TM & 0x0ff);	   AD_DAT = AD_DAT <<1;	 }     OS_EXIT_CRITICAL(); 	 return  (U8)(AD_DAT>>16);	 //for (k=0; k<40000; k++){}}#elsevoid GUI_TOUCH_DriverAnalog_C(void) {}#endif    /* defined(GUI_SUPPORT_TOUCH) && GUI_SUPPORT_TOUCH */

⌨️ 快捷键说明

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