📄 mx1touch.cpp
字号:
0x81,0x09,0x09,0x08,0x00,0x80,0x0f,0x07,0x00,0x80,0x0f,0x08,0x00,0x81,0x09,0x09,
0x81,0x09,0x09,0x07,0x00,0x80,0x0f,0x09,0x00,0x80,0x0f,0x07,0x00,0x81,0x09,0x09,
0x81,0x09,0x09,0x06,0x00,0x80,0x0f,0x0b,0x00,0x80,0x0f,0x06,0x00,0x81,0x09,0x09,
0x81,0x09,0x09,0x05,0x00,0x80,0x0f,0x0d,0x00,0x80,0x0f,0x05,0x00,0x81,0x09,0x09,
0x81,0x09,0x09,0x04,0x00,0x80,0x0f,0x0f,0x00,0x80,0x0f,0x04,0x00,0x81,0x09,0x09,
0x81,0x09,0x09,0x03,0x00,0x80,0x0f,0x11,0x00,0x80,0x0f,0x03,0x00,0x81,0x09,0x09,
0x81,0x09,0x09,0x02,0x00,0x80,0x0f,0x13,0x00,0x80,0x0f,0x02,0x00,0x81,0x09,0x09,
0x84,0x09,0x09,0x00,0x00,0x0f,0x15,0x00,0x84,0x0f,0x00,0x00,0x09,0x09,0x83,0x09,
0x09,0x00,0x0f,0x17,0x00,0x83,0x0f,0x00,0x09,0x09,0x82,0x09,0x09,0x0f,0x19,0x00,
0x82,0x0f,0x09,0x09,0x1f,0x09,0x1f,0x09,};
PegBitmap gbTouchMeBitmap = { 0x01, 8, 32, 32, 0x000000ff,
(PEGUBYTE *) ucTouchMeBitmap};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class TouchMesgWin : public PegDialog
{
public:
TouchMesgWin(PEGINT iLeft, PEGINT iTop);
void SetText(char *pMesg);
private:
PegTextBox *mpTextBox;
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// Constructor
// Create top level object and add all children
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
TouchMesgWin::TouchMesgWin(PEGINT iLeft, PEGINT iTop) :
PegDialog(NULL)
{
PegRect ChildRect;
mReal.Set(iLeft, iTop, iLeft + 201, iTop + 120);
InitClient();
Add (new PegTitle("Calibrating Touch Screen", 0));
AddStatus(PSF_MOVEABLE);
ChildRect.Set(iLeft + 6, iTop + 36, iLeft + 193, iTop + 111);
mpTextBox = new PegTextBox(ChildRect, 0, FF_RECESSED|TT_COPY|TJ_CENTER|EF_WRAP, NULL);
mpTextBox->DataSet("Please touch the center of the box in the upper left corner of the screen.");
Add(mpTextBox);
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void TouchMesgWin::SetText(char *pMesg)
{
// We momentarily change the color of the text box so the user
// knows we received the touch:
mpTextBox->DataSet(pMesg);
mpTextBox->SetColor(PCI_NORMAL, LIGHTBLUE);
mpTextBox->Draw();
// we would normally start a PegTimer and change the color back when
// the timer expires. However, the calibration routine runs without
// returning to PEG, so no messages can be processed. Just do an
// inline delay:
TOUCH_DELAY(50);
mpTextBox->SetColor(PCI_NORMAL, WHITE);
mpTextBox->Invalidate();
mpTextBox->Draw();
}
/*--------------------------------------------------------------------------*/
/*
CalibrateTouchScreen-
This routine presents the user with little boxes to touch in each
corner of the screen. From this we obtain the raw ADC readings for
each screen corner. From this it is pretty simple to scale the touch
reading into pixel coordinates required by PEG.
*/
/*--------------------------------------------------------------------------*/
void CalibrateTouchScreen(void)
{
PegRect TargetPos;
PEGUSHORT RawSamples[8];
PegPresentationManager *pPresent = PegThing::Presentation();
TouchCal.ScreenWidth = pPresent->mReal.Width();
TouchCal.ScreenHeight = pPresent->mReal.Height();
// Wait for the screen NOT to be touched, in case something is on the
// screen or there is some sort of problem:
WaitForTouchState(FALSE);
// Put up a message box that tells the user what to do:
TouchMesgWin *pMesgBox = new TouchMesgWin(0, 0);
pPresent->Center(pMesgBox);
pPresent->Add(pMesgBox);
// Now put a target on the screen for the user to touch:
PegIcon *pTarget = new PegIcon(&gbTouchMeBitmap);
pPresent->Add(pTarget);
// Wait for the screen to be touched, this should be touch in upper-left
// corner:
while(1)
{
WaitForTouchState(TRUE);
// Get a raw reading:
if (TouchSample(RawSamples))
{
if (DetectTouch())
{
break;
}
}
}
// Change the message. This gives the user an indication that we got his
// touch:
pMesgBox->SetText("Now touch the box in the upper right corner of the screen");
// Wait for not touched:
WaitForTouchState(FALSE);
// Move the target to upper right corner:
pPresent->Remove(pTarget);
TargetPos = pTarget->mReal;
TargetPos.Shift(TouchCal.ScreenWidth - gbTouchMeBitmap.wWidth - 1, 0);
pTarget->Resize(TargetPos);
pPresent->Add(pTarget);
while(1)
{
// Wait for touched:
WaitForTouchState(TRUE);
// Get a raw reading:
if (TouchSample(&RawSamples[2]))
{
if (DetectTouch())
{
break;
}
}
}
// Change the message.
pMesgBox->SetText("Now touch the box in the lower right corner of the screen");
// Wait for not touched:
WaitForTouchState(FALSE);
// Move the target to lower right corner:
pPresent->Remove(pTarget);
TargetPos.Shift(0, TouchCal.ScreenHeight - gbTouchMeBitmap.wHeight - 1);
pTarget->Resize(TargetPos);
pPresent->Add(pTarget);
while(1)
{
// Wait for touched:
WaitForTouchState(TRUE);
// Get a raw reading:
if (TouchSample(&RawSamples[4]))
{
if (DetectTouch())
{
break;
}
}
}
// Change the message.
pMesgBox->SetText("Finally, touch the box in the lower left corner of the screen");
// Wait for not touched:
WaitForTouchState(FALSE);
// Move the target to lower left corner:
pPresent->Remove(pTarget);
TargetPos.Shift(-(TouchCal.ScreenWidth - gbTouchMeBitmap.wWidth - 1), 0);
pTarget->Resize(TargetPos);
pPresent->Add(pTarget);
while(1)
{
// Wait for touched:
WaitForTouchState(TRUE);
// Get a raw reading:
if (TouchSample(&RawSamples[6]))
{
if (DetectTouch())
{
break;
}
}
}
// Get rid of the message window and target:
pPresent->Destroy(pMesgBox);
pPresent->Destroy(pTarget);
// OK, we have all the raw sample data. We have to adjust the raw data
// to account for the width and height of the target box, because the user
// isn't really touching the very edges of the screen:
PEGINT ScreenSize = TouchCal.ScreenWidth;
PEGINT PixSpan = ScreenSize - gbTouchMeBitmap.wWidth;
PEGINT CountSpan = RawSamples[2] - RawSamples[0];
PEGINT FullCountSpan = CountSpan * ScreenSize / PixSpan;
PEGINT CountAdjust = (FullCountSpan - CountSpan) / 2;
TouchCal.RawXVals[TC_UL] = RawSamples[0] - CountAdjust;
TouchCal.RawXVals[TC_UR] = RawSamples[2] + CountAdjust;
CountSpan = RawSamples[4] - RawSamples[6];
FullCountSpan = CountSpan * ScreenSize / PixSpan;
CountAdjust = (FullCountSpan - CountSpan) / 2;
TouchCal.RawXVals[TC_LL] = RawSamples[6] - CountAdjust;
TouchCal.RawXVals[TC_LR] = RawSamples[4] + CountAdjust;
ScreenSize = TouchCal.ScreenHeight;
PixSpan = ScreenSize - gbTouchMeBitmap.wHeight;
CountSpan = RawSamples[7] - RawSamples[1];
FullCountSpan = CountSpan * ScreenSize / PixSpan;
CountAdjust = (FullCountSpan - CountSpan) / 2;
TouchCal.RawYVals[TC_UL] = RawSamples[1] - CountAdjust;
TouchCal.RawYVals[TC_LL] = RawSamples[7] + CountAdjust;
CountSpan = RawSamples[5] - RawSamples[3];
FullCountSpan = CountSpan * ScreenSize / PixSpan;
CountAdjust = (FullCountSpan - CountSpan) / 2;
TouchCal.RawYVals[TC_UR] = RawSamples[3] - CountAdjust;
TouchCal.RawYVals[TC_LR] = RawSamples[5] + CountAdjust;
TouchCal.CenterX = (TouchCal.RawXVals[TC_UL] + TouchCal.RawXVals[TC_LR]) / 2;
TouchCal.CenterY = (TouchCal.RawYVals[TC_UL] + TouchCal.RawYVals[TC_LR]) / 2;
TouchCal.IsCalibrated = TRUE;
}
#if defined(PEGSMX)
TCB_PTR TouchTask;
#else
#endif
extern "C" {
/************************ Touch Screen Update Task **************************/
/*--------------------------------------------------------------------------*/
// TouchTask- A continuous, very low priority task to update the touch
// position. This task sends mouse messages (PM_LBUTTONDOWN, PM_LBUTTONUP,
// and/or PM_POINTERMOVE) into PegMessageQueue, where PEG routes them
// to wherever they need to go.
//
// This task is started by the InitializeTouchScreen function if the
// calibration completes OK.
/*--------------------------------------------------------------------------*/
void PegTouchTask(void)
{
PegMessage TouchMesg;
PEGINT XPos, YPos, OldXPos, OldYPos;
PEGUINT wLastState;
PEGBOOL IsTouched;
TouchMesg.pTarget = NULL;
XPos = YPos = OldXPos = OldYPos = wLastState = 0;
// First configure the hardware registers:
TouchConfigureHardware();
// Next run the calibration sequence:
CalibrateTouchScreen();
// Now continuously read and post touch screen messages:
while(1)
{
TOUCH_DELAY(TOUCH_POLLING_PERIOD);
IsTouched = GetScaledTouchPos(&XPos, &YPos);
if (IsTouched)
{
if (wLastState & TS_TOUCH_SENT) // already sent a touch message?
{
// check for a move message:
if (OldXPos != XPos || OldYPos != YPos)
{
// The screen is still touched, but the position
// changed. Send a PointerMove message to PEG
TouchMesg.wType = PM_POINTER_MOVE;
TouchMesg.Point.x = XPos;
TouchMesg.Point.y = YPos;
PegThing::MessageQueue()->Fold(&TouchMesg);
OldXPos = XPos;
OldYPos = YPos;
}
}
else
{
// This is a new touch down. Send a LBUTTONDOWN message
// to PEG:
TouchMesg.wType = PM_LBUTTONDOWN;
TouchMesg.Point.x = XPos;
TouchMesg.Point.y = YPos;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -