touchscreen.c

来自「本代码包为该GUI LIB在PC上的测试移植. PIC_Graphics 为P」· C语言 代码 · 共 730 行 · 第 1/2 页

C
730
字号
*
* Note: none
*
********************************************************************/
void TouchGetMsg(GOL_MSG* pMsg)
{
	static SHORT prevX = -1;
	static SHORT prevY = -1;

	SHORT x,y;
		
    pMsg->type    = TYPE_TOUCHSCREEN;
    pMsg->uiEvent = EVENT_INVALID;

    x = TouchGetX();
    if( x == -1 )
    {
        y = -1;
    }
    else
    {
        y = TouchGetY();
        if( y == -1 )
            x = -1;
    }

    if( (prevX == x) && (prevY == y) )
        return;

	x = pMsg->param1;
	y = pMsg->param2;
	
    if( (prevX != -1) && (prevY != -1) )
    {
        if( (x != -1) && (y != -1) )
        {
            // Move
            pMsg->uiEvent = EVENT_MOVE;
        }
        else
        {
            // Released
            pMsg->uiEvent = EVENT_RELEASE;
            pMsg->param1  = prevX;
            pMsg->param2  = prevY;
            prevX = x;
            prevY = y;
            return;
        }
    }
    else
    {
        if( (x != -1) && (y != -1) )
        {
            // Pressed
            pMsg->uiEvent = EVENT_PRESS;
        }
        else
        {
            // No message
            pMsg->uiEvent = EVENT_INVALID;
        }
    }

    pMsg->param1 = x;
    pMsg->param2 = y;
    prevX = x;
    prevY = y;
}

#else

void TouchGetMsg(GOL_MSG* pMsg)
{
	
}    

SHORT TouchGetX()
{
	return 0;
}

SHORT TouchGetY()
{
	return 0;
}     
#endif



/*********************************************************************
* Function: void TouchStoreCalibration(void)
*
* PreCondition: EEPROMInit() must be called before
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: stores calibration parameters into EEPROM
*
* Note: none
*
********************************************************************/
void TouchStoreCalibration(void)
{
//    EEPROMWriteWord(_calXMin, EEPROM_XMIN);
//    EEPROMWriteWord(_calXMax, EEPROM_XMAX);
//    EEPROMWriteWord(_calYMin, EEPROM_YMIN);
//    EEPROMWriteWord(_calYMax, EEPROM_YMAX);
}

/*********************************************************************
* Function: void TouchLoadCalibration(void)
*
* PreCondition: EEPROMInit() must be called before
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: loads calibration parameters from EEPROM
*
* Note: none
*
********************************************************************/
void TouchLoadCalibration(void)
{
//    _calXMin = EEPROMReadWord(EEPROM_XMIN);
//    _calXMax = EEPROMReadWord(EEPROM_XMAX);
//    _calYMin = EEPROMReadWord(EEPROM_YMIN);
//    _calYMax = EEPROMReadWord(EEPROM_YMAX);
}

/*********************************************************************
* Function:  void TouchCalibration()
*
* PreCondition: InitGraph() must be called before
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: calibrates touch screen
*
* Note: none
*
********************************************************************/
void TouchCalibration()
{
SHORT counter;
SHORT x,y;
WORD  ax[3],ay[3];

SHORT textHeight;

    // Set touch screen timing for calibration
    calibration = 1;

    SetFont((void*)&GOLFontDefault);
    textHeight = GetTextHeight((void*)&GOLFontDefault);

    SetColor(WHITE);
    ClearDevice();

    SetColor(BRIGHTRED);
    OutTextXY(0,0*textHeight,"IMPORTANT.");
    SetColor(BLACK);
    OutTextXY(0,1*textHeight,"Now touch screen calibration");
    OutTextXY(0,2*textHeight,"will be perfomed.Touch points");
    OutTextXY(0,3*textHeight,"EXACTLY at the positions");
    OutTextXY(0,4*textHeight,"shown by arrows.");
    SetColor(BRIGHTRED);
    OutTextXY(0,6*textHeight,"Touch screen to continue.");

    // Wait for touch
    do{
        x=ADCGetX(); y=ADCGetY();
    }while((y==-1)||(x==-1));

    Beep();

    DelayMs(500);

    SetColor(WHITE);
    ClearDevice();

    SetColor(BRIGHTRED);

    Line(5,5,5,15);
    Line(4,5,4,15);
    Line(6,5,6,15);

    Line(5,5,15,5);
    Line(5,4,15,4);
    Line(5,6,15,6);

    Line(5,6,15,16);
    Line(5,4,15,14);
    Line(5,5,15,15);

    TouchGetCalPoints(ax, ay);

    // Choose max X and min Y
    _calXMax = 0;
    _calYMin = 0xFFFF;
    for(counter=0; counter<3; counter++){
        if(_calXMax < ax[counter])
            _calXMax = ax[counter];

        if(_calYMin > ay[counter])
            _calYMin = ay[counter];
    }

    SetColor(WHITE);
    ClearDevice();

    SetColor(BRIGHTRED);

    Line(5,GetMaxY()-5,5,GetMaxY()-15);
    Line(4,GetMaxY()-5,4,GetMaxY()-15);
    Line(6,GetMaxY()-5,6,GetMaxY()-15);

    Line(5,GetMaxY()-5,15,GetMaxY()-5);
    Line(5,GetMaxY()-4,15,GetMaxY()-4);
    Line(5,GetMaxY()-6,15,GetMaxY()-6);

    Line(5,GetMaxY()-6,15,GetMaxY()-16);
    Line(5,GetMaxY()-4,15,GetMaxY()-14);
    Line(5,GetMaxY()-5,15,GetMaxY()-15);

    TouchGetCalPoints(ax, ay);

    // Choose max Y
    _calYMax = 0;
    for(counter=0; counter<3; counter++){
        if(_calYMax < ay[counter])
            _calYMax = ay[counter];
    }

    SetColor(WHITE);
    ClearDevice();

    SetColor(BRIGHTRED);    

    Line(GetMaxX()-5,GetMaxY()/2-5,GetMaxX()-5,GetMaxY()/2-15);
    Line(GetMaxX()-4,GetMaxY()/2-5,GetMaxX()-4,GetMaxY()/2-15);
    Line(GetMaxX()-6,GetMaxY()/2-5,GetMaxX()-6,GetMaxY()/2-15);

    Line(GetMaxX()-5,GetMaxY()/2-5,GetMaxX()-15,GetMaxY()/2-5);
    Line(GetMaxX()-5,GetMaxY()/2-4,GetMaxX()-15,GetMaxY()/2-4);
    Line(GetMaxX()-5,GetMaxY()/2-6,GetMaxX()-15,GetMaxY()/2-6);

    Line(GetMaxX()-5,GetMaxY()/2-6,GetMaxX()-15,GetMaxY()/2-16);
    Line(GetMaxX()-5,GetMaxY()/2-4,GetMaxX()-15,GetMaxY()/2-14);
    Line(GetMaxX()-5,GetMaxY()/2-5,GetMaxX()-15,GetMaxY()/2-15);

    TouchGetCalPoints(ax, ay);

    // Choose min X
    _calXMin = 0xFFFF;
    for(counter=0; counter<3; counter++){
        if(_calXMin > ax[counter])
            _calXMin = ax[counter];
    }


    SetColor(WHITE);
    ClearDevice();

    SetColor(BLACK);
    OutTextXY(10,1*textHeight,"Hold S3 button and");
    OutTextXY(10,2*textHeight,"press MCLR reset(S1)");
    OutTextXY(10,3*textHeight,"to REPEAT the calibration");
    OutTextXY(10,4*textHeight,"procedure.");
    SetColor(BRIGHTRED);
    OutTextXY(10,6*textHeight,"Touch screen to continue.");

    // Wait for touch
    do{
        x=ADCGetX(); y=ADCGetY();
    }while((y==-1)||(x==-1));

    Beep();

    DelayMs(500);

    SetColor(BLACK);
    ClearDevice();

    // Set touch screen timing for work mode
    calibration = 0;
}

/*********************************************************************
* Function: void TouchGetCalPoints(WORD* ax, WORD* ay)
*
* PreCondition: InitGraph() must be called before
*
* Input: ax - pointer to array receiving 3 X touch positions
*        ay - pointer to array receiving 3 Y touch positions
*
* Output: none
*
* Side Effects: none
*
* Overview: gets values for 3 touches
*
* Note: none
*
********************************************************************/
void TouchGetCalPoints(WORD* ax, WORD* ay){
static const char calStr[] = "CALIBRATION";
char calTouchLeft[] = "3 touches left";
SHORT counter;
SHORT x,y;

    SetFont((void*)&GOLFontDefault);

    SetColor(BRIGHTRED);

    OutTextXY((GetMaxX()-GetTextWidth((char*)calStr,(void*)&GOLFontDefault))>>1,
              (GetMaxY()-GetTextHeight((void*)&GOLFontDefault))>>1,
              (char*)calStr);

    for(counter=0; counter<3; counter++){

        SetColor(BRIGHTRED);

        calTouchLeft[0] = '3' - counter;

        OutTextXY((GetMaxX()-GetTextWidth(calTouchLeft,(void*)&GOLFontDefault))>>1,
                  (GetMaxY()+GetTextHeight((void*)&GOLFontDefault))>>1,
                   calTouchLeft);

        // Wait for press
        do{
            x=ADCGetX(); y=ADCGetY();
        }while((y==-1)||(x==-1));

        Beep();

        *(ax+counter) = x; *(ay+counter) = y;
       
        // Wait for release
        do{
            x=ADCGetX(); y=ADCGetY();
        }while((y!=-1)&&(x!=-1));

        SetColor(WHITE);

        OutTextXY((GetMaxX()-GetTextWidth(calTouchLeft,(void*)&GOLFontDefault))>>1,
                  (GetMaxY()+GetTextHeight((void*)&GOLFontDefault))>>1,
                   calTouchLeft);

        DelayMs(500);
    }
}

⌨️ 快捷键说明

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