os/kernelhwsrv/bsptemplate/asspandvariant/template_variant/specific/xyin.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// template\Template_Variant\Specific\xyin.cpp
sl@0
    15
// Implementation of a digitiser (touch-screen) driver. 
sl@0
    16
// This code assumes that an interrupt is generated on pen-down and pen-up events.
sl@0
    17
// This file is part of the Template Base port
sl@0
    18
// We use this driver to exemplify the usage of Resource Management for shared resources, Peripheral "Sleep"
sl@0
    19
// and detection and notification of Wakeup events
sl@0
    20
// 
sl@0
    21
//
sl@0
    22
sl@0
    23
sl@0
    24
#include <assp.h>
sl@0
    25
#include <template_assp.h>
sl@0
    26
#include <videodriver.h>
sl@0
    27
#include "mconf.h"
sl@0
    28
#include <drivers/xyin.h>
sl@0
    29
#include "template_power.h"
sl@0
    30
sl@0
    31
sl@0
    32
sl@0
    33
sl@0
    34
//
sl@0
    35
// TO DO: (mandatory)
sl@0
    36
//
sl@0
    37
// Define the following constants that describe the digitiser position & dimensions
sl@0
    38
// This is only example code... you need to modify it for your hardware
sl@0
    39
sl@0
    40
// digitiser origin & size in pixels
sl@0
    41
const TUint	KConfigXyOffsetX	= 0;		// digitiser origin - same as display area
sl@0
    42
const TUint	KConfigXyOffsetY	= 0;
sl@0
    43
const TUint	KConfigXyWidth		= 640;		// 640 pixels per line
sl@0
    44
const TUint	KConfigXyHeight		= 480;		// 480 lines per panel
sl@0
    45
sl@0
    46
// digitiser dimensions in digitiser co-ordinates
sl@0
    47
const TInt		KConfigXyBitsX			= 12;
sl@0
    48
const TInt		KConfigXyBitsY			= 12;
sl@0
    49
const TInt		KConfigXySpreadX		= 1 << KConfigXyBitsX;		// maximum valid X spread
sl@0
    50
const TInt		KConfigXySpreadY		= 1 << KConfigXyBitsY;		// maximum valid Y spread
sl@0
    51
const TInt		KConfigXyMinX			= 0;						// minimum valid X value
sl@0
    52
const TInt		KConfigXyMinY			= 0;						// minimum valid Y value
sl@0
    53
const TInt		KConfigXyMaxX			= KConfigXySpreadX - 1;		// maximum valid X value
sl@0
    54
const TInt		KConfigXyMaxY			= KConfigXySpreadY - 1;		// maximum valid Y value
sl@0
    55
sl@0
    56
sl@0
    57
// Define a 2x2 matrix and two constants Tx and Ty to convert digitiser co-ordinates 
sl@0
    58
// to pixels such that
sl@0
    59
//
sl@0
    60
// (X<<16 Y<<16)	=	(x y)	x	(R11 R12)	+	(Tx Ty)
sl@0
    61
//									(R21 R22)			
sl@0
    62
// or : 
sl@0
    63
//
sl@0
    64
// X = (x*R11 + y*R21 + TX) >> 16;
sl@0
    65
// Y = (x*R12 + y*R22 + TY) >> 16;
sl@0
    66
sl@0
    67
//
sl@0
    68
// where x,y are digitiser coordinates, Tx,Ty are constant offsets and X,Y are screen 
sl@0
    69
// coordinates. Left shifting by 16 bits is used so as not to lose precision.
sl@0
    70
//
sl@0
    71
// These are default values to be used before calibration has taken place
sl@0
    72
// These are best set by observation.
sl@0
    73
// The example values given below are for a digitiser whose origin is at bottom left
sl@0
    74
// (the screen origin is at top left)
sl@0
    75
const TInt		KConfigXyR11		= (KConfigXyWidth << 16) / KConfigXySpreadX;		// 10240
sl@0
    76
const TInt		KConfigXyR12		= 0;
sl@0
    77
const TInt		KConfigXyR21		= 0;
sl@0
    78
const TInt		KConfigXyR22		= - ((KConfigXyHeight << 16) / KConfigXySpreadY);	// -7680
sl@0
    79
const TInt		KConfigXyTx			= 0;
sl@0
    80
const TInt		KConfigXyTy			= (KConfigXyHeight << 16) / KConfigXySpreadY;
sl@0
    81
sl@0
    82
//
sl@0
    83
// TO DO: (optional)
sl@0
    84
//
sl@0
    85
// Define the following constants that describe the digitiser behaviour
sl@0
    86
// This is only example code... you need to modify it for your hardware
sl@0
    87
sl@0
    88
// After taking a sample, wait for the specified number of nano-kernel ticks (normally 1 ms) 
sl@0
    89
// before taking the next sample
sl@0
    90
const TInt		KInterSampleTime	= 1;	
sl@0
    91
sl@0
    92
// After a group of samples has been processed by the DDigitiser::ProcessRawSample() DFC,
sl@0
    93
// wait for the specified number of nano-kernel ticks before taking the next sample
sl@0
    94
const TInt		KInterGroupTime		= 1;
sl@0
    95
sl@0
    96
// After a pen-down interrupt, 
sl@0
    97
// wait for the specified number of nano-kernel ticks before taking the next sample
sl@0
    98
const TInt		KPenDownDelayTime	= 2;
sl@0
    99
sl@0
   100
// If powering up the device with the pen down, 
sl@0
   101
// wait for the specified number of nano-kernel ticks before taking the next sample
sl@0
   102
const TInt		KPenUpPollTime		= 30;
sl@0
   103
sl@0
   104
// After a pen-up interrupt, 
sl@0
   105
// wait for the specified number of nano-kernel ticks before calling PenUp()
sl@0
   106
const TInt		KPenUpDebounceTime	= 10;
sl@0
   107
sl@0
   108
// number of samples to discard on pen-down
sl@0
   109
const TInt		KConfigXyPenDownDiscard	= 1;		
sl@0
   110
sl@0
   111
// number of samples to discard on pen-up
sl@0
   112
const TInt		KConfigXyPenUpDiscard	= 1;		
sl@0
   113
sl@0
   114
// offset in pixels to cause movement in X direction
sl@0
   115
const TInt		KConfigXyAccThresholdX	= 12;		
sl@0
   116
sl@0
   117
// offset in pixels to cause movement in Y direction
sl@0
   118
const TInt		KConfigXyAccThresholdY	= 12;		
sl@0
   119
sl@0
   120
// number of samples to average - MUST be <= KMaxXYSamples
sl@0
   121
const TInt		KConfigXyNumXYSamples	= 2;		
sl@0
   122
sl@0
   123
// disregard extremal values in each 4-sample group
sl@0
   124
const TBool		KConfigXyDisregardMinMax= EFalse;	
sl@0
   125
sl@0
   126
sl@0
   127
sl@0
   128
// obsolete constants :
sl@0
   129
const TInt		KConfigXyDriveXRise		= 0;		
sl@0
   130
const TInt		KConfigXyDriveYRise		= 0;		
sl@0
   131
const TInt		KConfigXyMaxJumpX		= 0;		
sl@0
   132
const TInt		KConfigXyMaxJumpY		= 0;		
sl@0
   133
sl@0
   134
sl@0
   135
sl@0
   136
/******************************************************
sl@0
   137
 * Main Digitiser Class
sl@0
   138
 ******************************************************/
sl@0
   139
sl@0
   140
//
sl@0
   141
// TO DO: (optional)
sl@0
   142
//
sl@0
   143
// Add any private functions and data you require
sl@0
   144
//
sl@0
   145
NONSHARABLE_CLASS(DTemplateDigitiser) : public DDigitiser
sl@0
   146
	{
sl@0
   147
public:
sl@0
   148
	enum TState
sl@0
   149
		{
sl@0
   150
		E_HW_PowerUp,
sl@0
   151
		E_HW_PenUpDebounce,
sl@0
   152
		E_HW_CollectSample
sl@0
   153
		};
sl@0
   154
sl@0
   155
public:
sl@0
   156
	// from DDigitiser - initialisation
sl@0
   157
	DTemplateDigitiser();
sl@0
   158
	virtual TInt DoCreate();
sl@0
   159
	void SetDefaultConfig();
sl@0
   160
sl@0
   161
	// from DDigitiser - signals to hardware-dependent code
sl@0
   162
	virtual void WaitForPenDown();
sl@0
   163
	virtual void WaitForPenUp();
sl@0
   164
	virtual void WaitForPenUpDebounce();
sl@0
   165
	virtual void DigitiserOn();
sl@0
   166
	virtual void DigitiserOff();
sl@0
   167
	virtual void FilterPenMove(const TPoint& aPoint);
sl@0
   168
	virtual void ResetPenMoveFilter();
sl@0
   169
sl@0
   170
	// from DDigitiser - machine-configuration related things
sl@0
   171
	virtual TInt DigitiserToScreen(const TPoint& aDigitiserPoint, TPoint& aScreenPoint);
sl@0
   172
	virtual void ScreenToDigitiser(TInt& aX, TInt& aY);
sl@0
   173
	virtual TInt SetXYInputCalibration(const TDigitizerCalibration& aCalibration);
sl@0
   174
	virtual TInt CalibrationPoints(TDigitizerCalibration& aCalibration);
sl@0
   175
	virtual TInt SaveXYInputCalibration();
sl@0
   176
	virtual TInt RestoreXYInputCalibration(TDigitizerCalibrationType aType);
sl@0
   177
	virtual void DigitiserInfo(TDigitiserInfoV01& aInfo);
sl@0
   178
sl@0
   179
	// from DPowerHandler
sl@0
   180
	virtual void PowerDown(TPowerState);
sl@0
   181
	virtual void PowerUp();
sl@0
   182
sl@0
   183
public:
sl@0
   184
	// implementation
sl@0
   185
	void TakeSample();
sl@0
   186
	void PenInterrupt();
sl@0
   187
	void DigitiserPowerUp();
sl@0
   188
	void PowerUpDfc();
sl@0
   189
sl@0
   190
public:
sl@0
   191
	NTimer iTimer;
sl@0
   192
	NTimer iTimerInt;
sl@0
   193
	TDfc iTakeReadingDfc;
sl@0
   194
	TDfc iPowerDownDfc;
sl@0
   195
	TDfc iPowerUpDfc;
sl@0
   196
	TInt iSamplesCount;
sl@0
   197
	TState iState;
sl@0
   198
	TUint8 iPoweringDown;
sl@0
   199
sl@0
   200
	TSize iScreenSize;
sl@0
   201
	TActualMachineConfig& iMachineConfig;
sl@0
   202
	};
sl@0
   203
sl@0
   204
/******************************************************
sl@0
   205
 * Digitiser main code
sl@0
   206
 ******************************************************/
sl@0
   207
/**
sl@0
   208
Sample timer callback
sl@0
   209
Schedules a DFC to take a sample
sl@0
   210
sl@0
   211
@param aPtr	a pointer to DTemplateDigitiser
sl@0
   212
*/
sl@0
   213
LOCAL_C void timerExpired(TAny* aPtr)
sl@0
   214
	{
sl@0
   215
	DTemplateDigitiser* pD=(DTemplateDigitiser*)aPtr;
sl@0
   216
	__KTRACE_OPT(KHARDWARE,Kern::Printf("T"));
sl@0
   217
	pD->iTakeReadingDfc.Add();
sl@0
   218
	}
sl@0
   219
sl@0
   220
/**
sl@0
   221
Debounce timer callback 
sl@0
   222
schedules a DFC to process a pen-down interrupt
sl@0
   223
sl@0
   224
@param aPtr	a pointer to DTemplateDigitiser
sl@0
   225
*/
sl@0
   226
LOCAL_C void timerIntExpired(TAny* aPtr)
sl@0
   227
	{
sl@0
   228
	DTemplateDigitiser* pD=(DTemplateDigitiser*)aPtr;
sl@0
   229
	__KTRACE_OPT(KHARDWARE,Kern::Printf("TI"));
sl@0
   230
	// clear xy interrupt -> re-triggers the interrupt mechanism to catch the next IRQ
sl@0
   231
sl@0
   232
	// TO DO: (mandatory)
sl@0
   233
	// Write to the appropriate hardware register to clear the digitiser interrupt
sl@0
   234
	//
sl@0
   235
sl@0
   236
	pD->iTakeReadingDfc.Add();
sl@0
   237
	}
sl@0
   238
sl@0
   239
/**
sl@0
   240
Pen-up/down interrupt handler
sl@0
   241
sl@0
   242
@param aPtr	a pointer to DTemplateDigitiser
sl@0
   243
*/
sl@0
   244
LOCAL_C void penInterrupt(TAny* aPtr)
sl@0
   245
	{
sl@0
   246
	DTemplateDigitiser* pD=(DTemplateDigitiser*)aPtr;
sl@0
   247
	pD->PenInterrupt();
sl@0
   248
	}
sl@0
   249
sl@0
   250
/**
sl@0
   251
DFC for taking a sample
sl@0
   252
sl@0
   253
@param aPtr	a pointer to DTemplateDigitiser
sl@0
   254
*/
sl@0
   255
LOCAL_C void takeReading(TAny* aPtr)
sl@0
   256
	{
sl@0
   257
	DTemplateDigitiser* pD=(DTemplateDigitiser*)aPtr;
sl@0
   258
	pD->TakeSample();
sl@0
   259
	}
sl@0
   260
sl@0
   261
/**
sl@0
   262
DFC for powering down the device
sl@0
   263
sl@0
   264
@param aPtr	a pointer to DTemplateDigitiser
sl@0
   265
*/
sl@0
   266
LOCAL_C void powerDownDfc(TAny* aPtr)
sl@0
   267
	{
sl@0
   268
	DTemplateDigitiser* pD=(DTemplateDigitiser*)aPtr;
sl@0
   269
	pD->DigitiserOff();
sl@0
   270
	}
sl@0
   271
sl@0
   272
/**
sl@0
   273
DFC for powering up the device
sl@0
   274
sl@0
   275
@param aPtr	a pointer to DTemplateDigitiser
sl@0
   276
*/
sl@0
   277
LOCAL_C void powerUpDfc(TAny* aPtr)
sl@0
   278
	{
sl@0
   279
	DTemplateDigitiser* pD=(DTemplateDigitiser*)aPtr;
sl@0
   280
	pD->PowerUpDfc();
sl@0
   281
	}
sl@0
   282
sl@0
   283
/**
sl@0
   284
Creates a new instance of DDigitiser. 
sl@0
   285
Called by extension entry point (PIL) to create a DDigitiser-derived object.
sl@0
   286
sl@0
   287
@return	a pointer to a DTemplateDigitiser object
sl@0
   288
*/
sl@0
   289
DDigitiser* DDigitiser::New()
sl@0
   290
	{
sl@0
   291
	return new DTemplateDigitiser;
sl@0
   292
	}
sl@0
   293
sl@0
   294
/**
sl@0
   295
Default constructor
sl@0
   296
*/
sl@0
   297
DTemplateDigitiser::DTemplateDigitiser() :
sl@0
   298
		iTimer(timerExpired,this),
sl@0
   299
		iTimerInt(timerIntExpired,this),
sl@0
   300
		iTakeReadingDfc(takeReading,this,5),
sl@0
   301
		iPowerDownDfc(powerDownDfc,this,5),
sl@0
   302
		iPowerUpDfc(powerUpDfc,this,5),
sl@0
   303
		iMachineConfig(TheActualMachineConfig())
sl@0
   304
	{
sl@0
   305
	iDfcQ = Kern::DfcQue0();
sl@0
   306
	iTakeReadingDfc.SetDfcQ(iDfcQ);
sl@0
   307
	iPowerDownDfc.SetDfcQ(iDfcQ);
sl@0
   308
	iPowerUpDfc.SetDfcQ(iDfcQ);
sl@0
   309
	}
sl@0
   310
sl@0
   311
/**
sl@0
   312
Perform hardware-dependent initialisation
sl@0
   313
sl@0
   314
Called by platform independent layer
sl@0
   315
*/
sl@0
   316
TInt DTemplateDigitiser::DoCreate()
sl@0
   317
	{
sl@0
   318
	__KTRACE_OPT(KEXTENSION,Kern::Printf("DTemplateDigitiser::DoCreate"));
sl@0
   319
sl@0
   320
	if (Kern::ColdStart())
sl@0
   321
		{
sl@0
   322
		__KTRACE_OPT(KEXTENSION,Kern::Printf("Resetting digitiser calibration"));
sl@0
   323
		
sl@0
   324
		// Emergency digitiser calibration values
sl@0
   325
		iMachineConfig.iCalibration.iR11 = KConfigXyR11;
sl@0
   326
		iMachineConfig.iCalibration.iR12 = KConfigXyR12;
sl@0
   327
		iMachineConfig.iCalibration.iR21 = KConfigXyR21;
sl@0
   328
		iMachineConfig.iCalibration.iR22 = KConfigXyR22;
sl@0
   329
		iMachineConfig.iCalibration.iTx = KConfigXyTx;
sl@0
   330
		iMachineConfig.iCalibration.iTy = KConfigXyTy;
sl@0
   331
		}
sl@0
   332
sl@0
   333
	// register power handler
sl@0
   334
	Add();
sl@0
   335
	DigitiserPowerUp();
sl@0
   336
sl@0
   337
	// bind to the pen-up/down interrupt
sl@0
   338
	TInt r=Interrupt::Bind(KIntIdDigitiser, penInterrupt, this);
sl@0
   339
	if (r!=KErrNone)
sl@0
   340
		return r;
sl@0
   341
sl@0
   342
	// set up the default configuration
sl@0
   343
	SetDefaultConfig();
sl@0
   344
sl@0
   345
	return r;
sl@0
   346
	}
sl@0
   347
sl@0
   348
/**
sl@0
   349
Initialise the DDigitiser::iCfg structure
sl@0
   350
*/
sl@0
   351
void DTemplateDigitiser::SetDefaultConfig()
sl@0
   352
	{
sl@0
   353
	iCfg.iPenDownDiscard = KConfigXyPenDownDiscard;		// number of samples to discard on pen-down
sl@0
   354
	iCfg.iPenUpDiscard = KConfigXyPenUpDiscard;			// number of samples to discard on pen-up
sl@0
   355
	iCfg.iDriveXRise = KConfigXyDriveXRise;				// number of milliseconds to wait when driving horizontal edges
sl@0
   356
	iCfg.iDriveYRise = KConfigXyDriveYRise;				// number of milliseconds to wait when driving vertical edges
sl@0
   357
	iCfg.iMinX = KConfigXyMinX;							// minimum valid X value
sl@0
   358
	iCfg.iMaxX = KConfigXyMaxX;							// maximum valid X value
sl@0
   359
	iCfg.iSpreadX = KConfigXySpreadX;					// maximum valid X spread
sl@0
   360
	iCfg.iMinY = KConfigXyMinY;							// minimum valid Y value
sl@0
   361
	iCfg.iMaxY = KConfigXyMaxY;							// maximum valid Y value
sl@0
   362
	iCfg.iSpreadY = KConfigXySpreadY;					// maximum valid Y spread
sl@0
   363
	iCfg.iMaxJumpX = KConfigXyMaxJumpX;					// maximum X movement per sample (pixels)
sl@0
   364
	iCfg.iMaxJumpY = KConfigXyMaxJumpY;					// maximum Y movement per sample (pixels)
sl@0
   365
	iCfg.iAccThresholdX = KConfigXyAccThresholdX;		// offset in pixels to cause movement in X direction
sl@0
   366
	iCfg.iAccThresholdY = KConfigXyAccThresholdY;		// offset in pixels to cause movement in Y direction
sl@0
   367
	iCfg.iNumXYSamples = KConfigXyNumXYSamples;			// number of samples to average
sl@0
   368
	iCfg.iDisregardMinMax = KConfigXyDisregardMinMax;	// disregard extremal values in each 4-sample group
sl@0
   369
	}
sl@0
   370
sl@0
   371
/**
sl@0
   372
Takes a sample from the digitiser.
sl@0
   373
Called in the context of a DFC thread.
sl@0
   374
*/
sl@0
   375
void DTemplateDigitiser::TakeSample()
sl@0
   376
	{
sl@0
   377
	TTemplatePowerController::WakeupEvent();	// notify of pendown (wakeup event) and let the power controller sort
sl@0
   378
												// out if it needs propagation
sl@0
   379
sl@0
   380
	TBool penDown = EFalse;
sl@0
   381
sl@0
   382
	// TO DO: (mandatory)
sl@0
   383
	// Read from appropriate hardware register to determine whether digitiser panel is being touched
sl@0
   384
	// Set penDown to ETrue if touched or EFalse if not touched.
sl@0
   385
	//
sl@0
   386
sl@0
   387
	__KTRACE_OPT(KHARDWARE,Kern::Printf("TS: S%d PD%d Sp%d", (TInt)iState, penDown?1:0, iSamplesCount));
sl@0
   388
sl@0
   389
	if (iState==E_HW_PowerUp)
sl@0
   390
		{
sl@0
   391
		// waiting for pen to go up after switch on due to pen down or through the HAL
sl@0
   392
		// coverity[dead_error_condition]
sl@0
   393
		// The next line should be reachable when this template file is edited for use
sl@0
   394
		if (!penDown)							// pen has gone up -> transition to new state
sl@0
   395
			{
sl@0
   396
			iState=E_HW_CollectSample;
sl@0
   397
			iSamplesCount=0;						// reset sample buffer
sl@0
   398
sl@0
   399
			// TO DO: (mandatory)
sl@0
   400
			// Write to the appropriate hardware register to clear the digitiser interrupt
sl@0
   401
			//
sl@0
   402
sl@0
   403
			// TO DO: (mandatory)
sl@0
   404
			// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   405
			// to detect when the digitizer panel is touched
sl@0
   406
			//
sl@0
   407
sl@0
   408
			Interrupt::Enable(KIntIdDigitiser);		// enable pen-down interrupt
sl@0
   409
sl@0
   410
 			// TO DO: (mandatory)
sl@0
   411
			// Write to the appropriate hardware register to enable the digitiser interrupt
sl@0
   412
			//
sl@0
   413
sl@0
   414
			}
sl@0
   415
		else									// pen is still down, wait a bit longer in this state
sl@0
   416
			{
sl@0
   417
			iTimer.OneShot(KPenUpPollTime);
sl@0
   418
			}
sl@0
   419
		return;
sl@0
   420
		}
sl@0
   421
sl@0
   422
	if (!penDown)
sl@0
   423
		{
sl@0
   424
		if (iState==E_HW_PenUpDebounce)
sl@0
   425
			{
sl@0
   426
			iState=E_HW_CollectSample;	// back to initial state, no samples collected
sl@0
   427
			iSamplesCount=0;			// reset sample buffer
sl@0
   428
			// Need to lock the kernel to simulate ISR context and thus defer preemption, 
sl@0
   429
			// since PenUp() expects an ISR context and calls TDfc::Add().
sl@0
   430
			NKern::Lock();
sl@0
   431
			PenUp();					// adds DFC
sl@0
   432
			NKern::Unlock();
sl@0
   433
			}
sl@0
   434
		else							// iState=E_HW_CollectSample
sl@0
   435
			{
sl@0
   436
			iState=E_HW_PenUpDebounce;
sl@0
   437
			iTimer.OneShot(KPenUpDebounceTime);		// wait a bit to make sure pen still up
sl@0
   438
			}
sl@0
   439
		return;
sl@0
   440
		}
sl@0
   441
	else if (iState==E_HW_PenUpDebounce)	// pen down
sl@0
   442
		{
sl@0
   443
		// false alarm - pen is down again
sl@0
   444
		iState=E_HW_CollectSample;		// take a new set of samples
sl@0
   445
		iSamplesCount=0;				// reset sample buffer
sl@0
   446
		}
sl@0
   447
	// default: pen down and iState=E_HW_CollectSample
sl@0
   448
sl@0
   449
	// TO DO: (mandatory)
sl@0
   450
	// Read from appropriate hardware register to get the current digitiser coordinates 
sl@0
   451
	// of the point that is being touched. Set aResults accordingly.
sl@0
   452
	// This is only example code... you need to modify it for your hardware
sl@0
   453
	//
sl@0
   454
	TPoint aResults;
sl@0
   455
sl@0
   456
	// X axis
sl@0
   457
	iX[iSamplesCount] = aResults.iX;
sl@0
   458
	// Y axis
sl@0
   459
	iY[iSamplesCount] = aResults.iY;
sl@0
   460
sl@0
   461
	__KTRACE_OPT(KHARDWARE,Kern::Printf("Raw: X=%d Y=%d",iX[iSamplesCount],iY[iSamplesCount]));
sl@0
   462
sl@0
   463
	// Put the hardware back into pen-detect mode
sl@0
   464
sl@0
   465
	// TO DO: (mandatory)
sl@0
   466
	// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   467
	// to detect when the digitizer panel is touched
sl@0
   468
	//
sl@0
   469
sl@0
   470
	// count samples collected - if it's less than minimum,
sl@0
   471
	// schedule the reading of another sample
sl@0
   472
	if (++iSamplesCount < iCfg.iNumXYSamples)	// iX[] and iY[] are 4 levels deep in xyin.h...
sl@0
   473
		{
sl@0
   474
		if(KInterSampleTime > 0)				// need to check this config param as it might be zero!
sl@0
   475
			iTimer.OneShot(KInterSampleTime);	// haven't got a complete group yet, so queue timer to sample again
sl@0
   476
		else
sl@0
   477
			iTakeReadingDfc.Enque();
sl@0
   478
		return;
sl@0
   479
		}
sl@0
   480
sl@0
   481
	// Have a complete group of samples so pass up to processing layer (PIL)
sl@0
   482
sl@0
   483
	// Need to lock the kernel to simulate ISR context and thus defer preemption, 
sl@0
   484
	// since RawSampleValid() expects an ISR context and calls TDfc::Add().
sl@0
   485
	NKern::Lock();			
sl@0
   486
	RawSampleValid();		// adds DFC
sl@0
   487
	NKern::Unlock();
sl@0
   488
	}
sl@0
   489
sl@0
   490
/**
sl@0
   491
Request for an interrupt to be generated when the pen is next down
sl@0
   492
Called by PIL at startup or when pen leaves digitiser after pen-up event issued
sl@0
   493
*/
sl@0
   494
void DTemplateDigitiser::WaitForPenDown()
sl@0
   495
	{
sl@0
   496
	// Called at startup or when pen leaves digitiser after pen-up event issued
sl@0
   497
	__KTRACE_OPT(KHARDWARE,Kern::Printf("WD: PowerDownMask %x",iPoweringDown));
sl@0
   498
	if (iPoweringDown)
sl@0
   499
		{
sl@0
   500
		// powering down
sl@0
   501
sl@0
   502
		// TO DO: (mandatory)
sl@0
   503
		// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   504
		// to detect when the digitizer panel is touched and wakes up the system if in standby
sl@0
   505
		//
sl@0
   506
sl@0
   507
		//
sl@0
   508
		// TO DO: (optional)
sl@0
   509
		//
sl@0
   510
		// Relinquish request on power resources
sl@0
   511
		// This will place the peripheral hardware in a low power "Sleep" mode which is Interrupt detection capable
sl@0
   512
		// EXAMPLE ONLY
sl@0
   513
		//
sl@0
   514
		TemplateResourceManager* aManager = TTemplatePowerController::ResourceManager();
sl@0
   515
		aManager -> ModifyToLevel(TemplateResourceManager::AsynchMlResourceUsedByXOnly, 50 /* a percentage */);
sl@0
   516
		aManager -> SharedBResource1() -> Release();
sl@0
   517
sl@0
   518
		iPoweringDown = EFalse;
sl@0
   519
		PowerDownDone();
sl@0
   520
		}
sl@0
   521
	else
sl@0
   522
		{
sl@0
   523
sl@0
   524
		// TO DO: (mandatory)
sl@0
   525
		// Write to the appropriate hardware register to clear the digitiser interrupt
sl@0
   526
		//
sl@0
   527
sl@0
   528
		// TO DO: (mandatory)
sl@0
   529
		// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   530
		// to detect when the digitizer panel is touched
sl@0
   531
		//
sl@0
   532
sl@0
   533
		if ((iTimer.iState == NTimer::EIdle) && (iTimerInt.iState == NTimer::EIdle))
sl@0
   534
			Interrupt::Enable(KIntIdDigitiser);		// enable pen-down interrupt
sl@0
   535
		}
sl@0
   536
	}
sl@0
   537
sl@0
   538
/**
sl@0
   539
Called by PIL after it has processed a group of raw samples while pen is down.
sl@0
   540
Used to indicate that the iX, iY buffers may be re-used
sl@0
   541
*/
sl@0
   542
void DTemplateDigitiser::WaitForPenUp()
sl@0
   543
	{
sl@0
   544
	__KTRACE_OPT(KHARDWARE,Kern::Printf("WU"));
sl@0
   545
	iState = E_HW_CollectSample;
sl@0
   546
	iSamplesCount = 0;					// reset sample buffer
sl@0
   547
	if(KInterGroupTime > 0)				// need to check this config param as it might be zero!
sl@0
   548
		iTimer.OneShot(KInterGroupTime);
sl@0
   549
	else
sl@0
   550
		iTakeReadingDfc.Enque();
sl@0
   551
	}
sl@0
   552
sl@0
   553
/**
sl@0
   554
Called by PIL if the group of samples collected is not good enough
sl@0
   555
Used to indicate that the iX, iY buffers may be re-used
sl@0
   556
*/
sl@0
   557
void DTemplateDigitiser::WaitForPenUpDebounce()
sl@0
   558
	{
sl@0
   559
	__KTRACE_OPT(KHARDWARE,Kern::Printf("WUDB"));
sl@0
   560
	iState = E_HW_CollectSample;
sl@0
   561
	iSamplesCount = 0;			// reset sample buffer
sl@0
   562
	if(KInterGroupTime > 0)					// need to check this config param as it might be zero!
sl@0
   563
		iTimer.OneShot(KInterGroupTime);
sl@0
   564
	else
sl@0
   565
		iTakeReadingDfc.Enque();
sl@0
   566
	}
sl@0
   567
sl@0
   568
/**
sl@0
   569
Pen up/down interrupt service routine (ISR)
sl@0
   570
*/
sl@0
   571
void DTemplateDigitiser::PenInterrupt()
sl@0
   572
    {
sl@0
   573
	__KTRACE_OPT(KHARDWARE,Kern::Printf("I"));
sl@0
   574
sl@0
   575
	
sl@0
   576
	Interrupt::Clear(KIntIdDigitiser);	// should already have been cleared
sl@0
   577
sl@0
   578
	// TO DO: (mandatory)
sl@0
   579
	// Read from appropriate hardware register to determine whether digitiser panel is being touched
sl@0
   580
	// Set penDown to ETrue if touched or EFalse if not touched.
sl@0
   581
	// This is only example code... you need to modify it for your hardware
sl@0
   582
	TBool penDown = EFalse;
sl@0
   583
sl@0
   584
	// coverity[dead_error_condition]
sl@0
   585
	if(!penDown)					// pen up
sl@0
   586
		{
sl@0
   587
sl@0
   588
		// TO DO: (mandatory)
sl@0
   589
		// Write to the appropriate hardware register to clear the digitiser interrupt
sl@0
   590
		//
sl@0
   591
sl@0
   592
		// TO DO: (mandatory)
sl@0
   593
		// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   594
		// to detect when the digitizer panel is touched
sl@0
   595
		//
sl@0
   596
sl@0
   597
		return;						// ... and exit!
sl@0
   598
		}
sl@0
   599
sl@0
   600
	Interrupt::Disable(KIntIdDigitiser);		// do NOT disable the capability to generate interrupts at the source
sl@0
   601
sl@0
   602
	if (KPenDownDelayTime>0)					// need to check this config param as it might be zero!
sl@0
   603
		iTimerInt.OneShot(KPenDownDelayTime);	// start a debounce timer which will queue a DFC to process the interrupt
sl@0
   604
	else
sl@0
   605
		{
sl@0
   606
sl@0
   607
		// TO DO: (mandatory)
sl@0
   608
		// Write to the appropriate hardware register to clear the digitiser interrupt
sl@0
   609
		// This will re-trigger the interrupt mechanism to catch the next interrupt...
sl@0
   610
		//
sl@0
   611
sl@0
   612
		iTakeReadingDfc.Add();
sl@0
   613
		}
sl@0
   614
	}
sl@0
   615
sl@0
   616
/**
sl@0
   617
DPowerHandler pure virtual
sl@0
   618
*/
sl@0
   619
void DTemplateDigitiser::PowerUp()
sl@0
   620
	{
sl@0
   621
	iPowerUpDfc.Enque();			// queue a DFC in this driver's context
sl@0
   622
	}
sl@0
   623
sl@0
   624
/**
sl@0
   625
Called by power up DFC
sl@0
   626
*/
sl@0
   627
void DTemplateDigitiser::PowerUpDfc()
sl@0
   628
	{
sl@0
   629
	__KTRACE_OPT(KPOWER, Kern::Printf("DTemplateDigitiser::PowerUpDfc()"));
sl@0
   630
	DigitiserOn();
sl@0
   631
	PowerUpDone();			// must be called from a different thread than PowerUp()
sl@0
   632
	}
sl@0
   633
sl@0
   634
/**
sl@0
   635
Turn the digitiser on
sl@0
   636
May be called as a result of a power transition or from the HAL
sl@0
   637
If called from HAL, then the digitiser may be already be on (iPointerOn == ETrue)
sl@0
   638
*/
sl@0
   639
void DTemplateDigitiser::DigitiserOn()
sl@0
   640
	{
sl@0
   641
	__KTRACE_OPT(KPOWER,Kern::Printf("DTemplateDigitiser::DigitiserOn() iPointerOn=%d", iPointerOn));
sl@0
   642
sl@0
   643
	if (!iPointerOn)				// may have been powered up already
sl@0
   644
		DigitiserPowerUp();
sl@0
   645
	}
sl@0
   646
sl@0
   647
/**
sl@0
   648
Power-up the digitiser. Assumes digitiser is off.
sl@0
   649
*/
sl@0
   650
void DTemplateDigitiser::DigitiserPowerUp()
sl@0
   651
	{
sl@0
   652
	__KTRACE_OPT(KPOWER, Kern::Printf("DigitiserPowerUp"));
sl@0
   653
	iPointerOn = ETrue;		// now turned on
sl@0
   654
sl@0
   655
	// TO DO: (mandatory)
sl@0
   656
	// Write to the appropriate hardware register to clear the digitiser interrupt
sl@0
   657
	//
sl@0
   658
sl@0
   659
	//
sl@0
   660
	// TO DO: (optional)
sl@0
   661
	//
sl@0
   662
	// Reassert request on power resources
sl@0
   663
	// This will move the peripheral hardware out of low power "Sleep" mode back to fully operational
sl@0
   664
	// EXAMPLE ONLY
sl@0
   665
	//
sl@0
   666
	TemplateResourceManager* aManager = TTemplatePowerController::ResourceManager();
sl@0
   667
	aManager -> ModifyToLevel(TemplateResourceManager::AsynchMlResourceUsedByXOnly, 100 /* a percentage */);
sl@0
   668
	aManager -> SharedBResource1() -> Use();
sl@0
   669
sl@0
   670
	// TO DO: (mandatory)
sl@0
   671
	// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   672
	// to detect when the digitizer panel is touched
sl@0
   673
	//
sl@0
   674
sl@0
   675
	iState = E_HW_PowerUp;	// so we wait for pen up if necessary
sl@0
   676
	iTakeReadingDfc.Enque();
sl@0
   677
	}
sl@0
   678
sl@0
   679
/**
sl@0
   680
DPowerHandler pure virtual
sl@0
   681
sl@0
   682
@param aPowerState the current power state
sl@0
   683
*/
sl@0
   684
void DTemplateDigitiser::PowerDown(TPowerState /*aPowerState*/)
sl@0
   685
	{
sl@0
   686
	iPoweringDown = ETrue;
sl@0
   687
	iPowerDownDfc.Enque();						// queue a DFC in this driver's context
sl@0
   688
	}
sl@0
   689
sl@0
   690
/**
sl@0
   691
Turn the digitiser off
sl@0
   692
May be called as a result of a power transition or from the HAL
sl@0
   693
If called from Power Manager, then the digitiser may be already be off (iPointerOn == EFalse)
sl@0
   694
if the platform is in silent running mode
sl@0
   695
*/
sl@0
   696
void DTemplateDigitiser::DigitiserOff()
sl@0
   697
	{
sl@0
   698
	__KTRACE_OPT(KPOWER,Kern::Printf("DTemplateDigitiser::DigitiserOff() iPointerOn=%d", iPointerOn));
sl@0
   699
	if (iPointerOn)		// can have been powered down from HAL
sl@0
   700
		{
sl@0
   701
		iPointerOn = EFalse;
sl@0
   702
		Interrupt::Disable(KIntIdDigitiser);
sl@0
   703
sl@0
   704
		// TO DO: (mandatory)
sl@0
   705
		// Write to the appropriate hardware register to disable the digitiser interrupt
sl@0
   706
		//
sl@0
   707
sl@0
   708
		iTimer.Cancel();
sl@0
   709
		iTimerInt.Cancel();
sl@0
   710
		iTakeReadingDfc.Cancel();
sl@0
   711
		if (iState != E_HW_CollectSample)
sl@0
   712
			{
sl@0
   713
			// Need to lock the kernel to simulate ISR context and thus defer preemption, 
sl@0
   714
			// since PenUp() expects an ISR context and calls TDfc::Add().
sl@0
   715
			NKern::Lock();
sl@0
   716
			PenUp();		// adds DFC (will call WaitForPenDown)
sl@0
   717
			NKern::Unlock();
sl@0
   718
			}
sl@0
   719
		else
sl@0
   720
			{
sl@0
   721
sl@0
   722
			// TO DO: (mandatory)
sl@0
   723
			// Write to the appropriate hardware register(s) to allow the hardware 
sl@0
   724
			// to detect when the digitizer panel is touched and wakes up the system if in standby
sl@0
   725
			//
sl@0
   726
sl@0
   727
			//
sl@0
   728
			// TO DO: (optional)
sl@0
   729
			//
sl@0
   730
			// Relinquish request on power resources as we are being powered down
sl@0
   731
			// This will place the peripheral hardware in a low power "Sleep" mode which is Interrupt detection capable
sl@0
   732
			// EXAMPLE ONLY
sl@0
   733
			//
sl@0
   734
			TemplateResourceManager* aManager = TTemplatePowerController::ResourceManager();
sl@0
   735
			aManager -> ModifyToLevel(TemplateResourceManager::AsynchMlResourceUsedByXOnly, 0 /* a percentage */);
sl@0
   736
			aManager -> SharedBResource1() -> Release();
sl@0
   737
sl@0
   738
			if (iPoweringDown)			// came here through PowerDown
sl@0
   739
				{
sl@0
   740
				iPoweringDown = EFalse;
sl@0
   741
				PowerDownDone();
sl@0
   742
				}
sl@0
   743
			}
sl@0
   744
		}
sl@0
   745
	else	// already powered down (by HAL)
sl@0
   746
		{
sl@0
   747
			if (iPoweringDown)			// came here through PowerDown
sl@0
   748
				{
sl@0
   749
				iPoweringDown = EFalse;
sl@0
   750
				PowerDownDone();
sl@0
   751
				}
sl@0
   752
		}
sl@0
   753
	}
sl@0
   754
sl@0
   755
sl@0
   756
sl@0
   757
sl@0
   758
/**
sl@0
   759
Convert digitiser coordinates to screen coordinates
sl@0
   760
sl@0
   761
@param aDigitiserPoint the digitiser coordinates
sl@0
   762
@param aScreenPoint A TPoint supplied by the caller. 
sl@0
   763
					On return, set to the converted screen coordinates in pixels.
sl@0
   764
sl@0
   765
@return KErrNone if successful
sl@0
   766
*/
sl@0
   767
TInt DTemplateDigitiser::DigitiserToScreen(const TPoint& aDigitiserPoint, TPoint& aScreenPoint)
sl@0
   768
	{
sl@0
   769
	NKern::LockSystem();
sl@0
   770
	TInt R11 = iMachineConfig.iCalibration.iR11;
sl@0
   771
	TInt R12 = iMachineConfig.iCalibration.iR12;
sl@0
   772
	TInt R21 = iMachineConfig.iCalibration.iR21;
sl@0
   773
	TInt R22 = iMachineConfig.iCalibration.iR22;
sl@0
   774
	TInt TX = iMachineConfig.iCalibration.iTx;
sl@0
   775
	TInt TY = iMachineConfig.iCalibration.iTy;
sl@0
   776
	NKern::UnlockSystem();
sl@0
   777
	TInt X = aDigitiserPoint.iX;
sl@0
   778
	TInt Y = aDigitiserPoint.iY;
sl@0
   779
sl@0
   780
	aScreenPoint.iX = (X*R11 + Y*R21 + TX) >> 16;
sl@0
   781
	aScreenPoint.iY = (X*R12 + Y*R22 + TY) >> 16;
sl@0
   782
sl@0
   783
	__KTRACE_OPT(KHARDWARE,Kern::Printf("DtS: Dp.x %d, Dp.y %d, Sp.x %d, Sp.y %d", X,Y,aScreenPoint.iX,aScreenPoint.iY));
sl@0
   784
sl@0
   785
	return KErrNone;
sl@0
   786
	}
sl@0
   787
sl@0
   788
/**
sl@0
   789
Convert screen coordinates back into digitiser coordinates
sl@0
   790
using the current constants from the superpage
sl@0
   791
sl@0
   792
@param aX The screen X coordinate in pixels; On return, set to the digitiser X coordinate.
sl@0
   793
@param aY The screen Y coordinate in pixels; On return, set to the digitiser Y coordinate.
sl@0
   794
*/
sl@0
   795
void DTemplateDigitiser::ScreenToDigitiser(TInt& aX, TInt& aY)
sl@0
   796
	{
sl@0
   797
	NKern::LockSystem();
sl@0
   798
	Int64 R11 = iMachineConfig.iCalibration.iR11;
sl@0
   799
	Int64 R12 = iMachineConfig.iCalibration.iR12;
sl@0
   800
	Int64 R21 = iMachineConfig.iCalibration.iR21;
sl@0
   801
	Int64 R22 = iMachineConfig.iCalibration.iR22;
sl@0
   802
	Int64 TX = iMachineConfig.iCalibration.iTx;
sl@0
   803
	Int64 TY = iMachineConfig.iCalibration.iTy;
sl@0
   804
	NKern::UnlockSystem();
sl@0
   805
	Int64 X = aX;
sl@0
   806
	Int64 Y = aY;
sl@0
   807
	//
sl@0
   808
	// Xd=(Xs<<16)*R22-(Ys<<16)*R21-(TX*R22)+(TY*R21)
sl@0
   809
	//	  -------------------------------------------
sl@0
   810
	//				   (R22*R11)-(R21*R12)
sl@0
   811
	//
sl@0
   812
	//
sl@0
   813
	// Yd=(Xs<<16)*R12-(Ys<<16)*R11-(TX*R12)+(TY*R11)
sl@0
   814
	//	  -------------------------------------------
sl@0
   815
	//				   (R21*R12)-(R22*R11)
sl@0
   816
	//
sl@0
   817
	// where Xd and Yd are digitiser coordinates
sl@0
   818
	//		 Xs and Ys are supplied screen coordinates
sl@0
   819
	//
sl@0
   820
	X<<=16;
sl@0
   821
	Y<<=16;
sl@0
   822
sl@0
   823
	Int64 d=Int64(R21)*Int64(R12)-Int64(R22)*Int64(R11);
sl@0
   824
sl@0
   825
	Int64 r=(X*R12)-(Y*R11)-(TX*R12)+(TY*R11);
sl@0
   826
sl@0
   827
	r=r/d;
sl@0
   828
sl@0
   829
	aY=(TInt)r;
sl@0
   830
sl@0
   831
	r=(X*R22)-(Y*R21)-(TX*R22)+(TY*R21);
sl@0
   832
sl@0
   833
	r=r/(-d);
sl@0
   834
sl@0
   835
	aX=(TInt)r;
sl@0
   836
	}
sl@0
   837
sl@0
   838
/**
sl@0
   839
Calculate values for R11, R12, R21, R22, TX and TY
sl@0
   840
sl@0
   841
@param aCalibration the screen coordinates of points touched
sl@0
   842
@return KErrNone if successful
sl@0
   843
*/
sl@0
   844
TInt DTemplateDigitiser::SetXYInputCalibration(const TDigitizerCalibration& aCalibration)
sl@0
   845
	{
sl@0
   846
	TInt R11,R12,R21,R22,TX,TY;
sl@0
   847
	//
sl@0
   848
	// Get coords of expected points
sl@0
   849
	//
sl@0
   850
	TDigitizerCalibration cal;
sl@0
   851
	TInt ret=CalibrationPoints(cal);
sl@0
   852
	if (ret!=KErrNone)
sl@0
   853
		return ret;
sl@0
   854
sl@0
   855
	TInt Xp1=cal.iTl.iX;
sl@0
   856
	TInt Yp1=cal.iTl.iY;
sl@0
   857
	TInt Xp2=cal.iBl.iX;
sl@0
   858
	TInt Yp2=cal.iBl.iY;
sl@0
   859
	TInt Xp3=cal.iBr.iX;
sl@0
   860
	TInt Yp3=cal.iBr.iY;
sl@0
   861
	//
sl@0
   862
	// Get coords of points touched in screen coordinates
sl@0
   863
	//
sl@0
   864
	TInt X1=aCalibration.iTl.iX;
sl@0
   865
	TInt Y1=aCalibration.iTl.iY;
sl@0
   866
	TInt X2=aCalibration.iBl.iX;
sl@0
   867
	TInt Y2=aCalibration.iBl.iY;
sl@0
   868
	TInt X3=aCalibration.iBr.iX;
sl@0
   869
	TInt Y3=aCalibration.iBr.iY;
sl@0
   870
	//
sl@0
   871
	// Convert back to raw digitiser coordinates
sl@0
   872
	//
sl@0
   873
	ScreenToDigitiser(X1,Y1);
sl@0
   874
	ScreenToDigitiser(X2,Y2);
sl@0
   875
	ScreenToDigitiser(X3,Y3);
sl@0
   876
	//
sl@0
   877
	// (Y1-Y2)(Xp1-Xp3) - (Y1-Y3)(Xp1-Xp2)
sl@0
   878
	// ----------------------------------- = R11
sl@0
   879
	// (Y1-Y2)(X1-X3)	- (Y1-Y3)(X1-X2)
sl@0
   880
	//
sl@0
   881
	Int64 r=((Int64(Y1-Y2)*Int64(Xp1-Xp3))-(Int64(Y1-Y3)*Int64(Xp1-Xp2)));
sl@0
   882
	r<<=16;
sl@0
   883
	r/=(Int64(Y1-Y2)*Int64(X1-X3)-Int64(Y1-Y3)*Int64(X1-X2));
sl@0
   884
	R11=(TInt)r;
sl@0
   885
	//
sl@0
   886
	// (Y1-Y2)(Yp1-Yp3) - (Y1-Y3)(Yp1-Yp2)
sl@0
   887
	// ----------------------------------- = R12
sl@0
   888
	// (Y1-Y2)(X1-X3)	- (Y1-Y3)(X1-X2)
sl@0
   889
	//
sl@0
   890
	r=((Int64(Y1-Y2)*Int64(Yp1-Yp3))-(Int64(Y1-Y3)*Int64(Yp1-Yp2)));
sl@0
   891
	r<<=16;
sl@0
   892
	r/=(Int64(Y1-Y2)*Int64(X1-X3)-Int64(Y1-Y3)*Int64(X1-X2));
sl@0
   893
	R12=(TInt)r;
sl@0
   894
	//
sl@0
   895
	// (X1-X3)(Xp2-Xp3) - (X2-X3)(Xp1-Xp3)
sl@0
   896
	// ----------------------------------- = R21
sl@0
   897
	// (Y2-Y3)(X1-X3)	- (Y1-Y3)(X2-X3)
sl@0
   898
	//
sl@0
   899
	r=(((X1-X3)*(Xp2-Xp3))-((X2-X3)*(Xp1-Xp3)));
sl@0
   900
	r<<=16;
sl@0
   901
	r/=(Int64(Y2-Y3)*Int64(X1-X3)-Int64(Y1-Y3)*Int64(X2-X3));
sl@0
   902
	R21=(TInt)r;
sl@0
   903
	//	
sl@0
   904
	// (X1-X3)(Yp2-Yp3) - (X2-X3)(Yp1-Yp3)
sl@0
   905
	// ----------------------------------- = R22
sl@0
   906
	// (Y2-Y3)(X1-X3)	- (Y1-Y3)(X2-X3)
sl@0
   907
	//
sl@0
   908
	r=((Int64(X1-X3)*Int64(Yp2-Yp3))-(Int64(X2-X3)*Int64(Yp1-Yp3)));
sl@0
   909
	r<<=16;
sl@0
   910
	r/=(Int64(Y2-Y3)*Int64(X1-X3)-Int64(Y1-Y3)*Int64(X2-X3));
sl@0
   911
	R22=(TInt)r;
sl@0
   912
	//
sl@0
   913
	// TX = Xp1 - X1*R11 - Y1*R21
sl@0
   914
	//
sl@0
   915
   TX=(Xp1<<16)-(X1*R11)-(Y1*R21);
sl@0
   916
	//
sl@0
   917
	// TY = Yp1 - X1*R12 - Y1*R22
sl@0
   918
	//
sl@0
   919
	TY=(Yp1<<16)-(X1*R12)-(Y1*R22);
sl@0
   920
	
sl@0
   921
	//
sl@0
   922
	// Write new values into the superpage
sl@0
   923
	// 
sl@0
   924
	NKern::LockSystem();
sl@0
   925
	iMachineConfig.iCalibration.iR11 = R11;
sl@0
   926
	iMachineConfig.iCalibration.iR12 = R12;
sl@0
   927
	iMachineConfig.iCalibration.iR21 = R21;
sl@0
   928
	iMachineConfig.iCalibration.iR22 = R22;
sl@0
   929
	iMachineConfig.iCalibration.iTx = TX;
sl@0
   930
	iMachineConfig.iCalibration.iTy = TY;
sl@0
   931
	NKern::UnlockSystem();
sl@0
   932
sl@0
   933
	return(KErrNone);
sl@0
   934
	}
sl@0
   935
sl@0
   936
/**
sl@0
   937
Informs the user-side calibration application where to draw 
sl@0
   938
the cross-hairs on the screen
sl@0
   939
sl@0
   940
@param aCalibration On return contains the for points on the screen (in screen coordinates)
sl@0
   941
					where the cross-hairs should be drawn
sl@0
   942
@return KErrNone if succcessful
sl@0
   943
*/
sl@0
   944
TInt DTemplateDigitiser::CalibrationPoints(TDigitizerCalibration& aCalibration)
sl@0
   945
	{
sl@0
   946
	TVideoInfoV01Buf buf;
sl@0
   947
	TVideoInfoV01& vidinfo=buf();
sl@0
   948
	TInt r = Kern::HalFunction(EHalGroupDisplay, EDisplayHalCurrentModeInfo, (TAny*)&buf, NULL);
sl@0
   949
	if (r!=KErrNone)
sl@0
   950
		return r;
sl@0
   951
	iScreenSize=vidinfo.iSizeInPixels;
sl@0
   952
sl@0
   953
    aCalibration.iBl.iX = aCalibration.iTl.iX = iScreenSize.iWidth/10;
sl@0
   954
    aCalibration.iTr.iY = aCalibration.iTl.iY = iScreenSize.iHeight/10;
sl@0
   955
    aCalibration.iBr.iY = aCalibration.iBl.iY = iScreenSize.iHeight-iScreenSize.iHeight/10;
sl@0
   956
    aCalibration.iTr.iX = aCalibration.iBr.iX = iScreenSize.iWidth-iScreenSize.iWidth/10;
sl@0
   957
    return r;
sl@0
   958
	}
sl@0
   959
/**
sl@0
   960
Saves the digitiser calibration to the persistent machine configuration area
sl@0
   961
so that it can be restored after a power-down/up
sl@0
   962
sl@0
   963
@return KErrNone if succcessful
sl@0
   964
*/
sl@0
   965
TInt DTemplateDigitiser::SaveXYInputCalibration()
sl@0
   966
	{
sl@0
   967
	NKern::LockSystem();
sl@0
   968
	iMachineConfig.iCalibrationSaved = iMachineConfig.iCalibration;
sl@0
   969
	NKern::UnlockSystem();
sl@0
   970
	return(KErrNone);
sl@0
   971
	}
sl@0
   972
sl@0
   973
/**
sl@0
   974
Restores the digitiser calibration from the persistent machine configuration area
sl@0
   975
following a power-up
sl@0
   976
sl@0
   977
@param aType indicates whether to restore factory or saved settings
sl@0
   978
@return KErrNone if succcessful
sl@0
   979
*/
sl@0
   980
TInt DTemplateDigitiser::RestoreXYInputCalibration(TDigitizerCalibrationType aType)
sl@0
   981
	{
sl@0
   982
	TInt r=KErrNone;
sl@0
   983
	NKern::LockSystem();
sl@0
   984
	switch (aType)
sl@0
   985
		{
sl@0
   986
		case EFactory:
sl@0
   987
			iMachineConfig.iCalibration=iMachineConfig.iCalibrationFactory;
sl@0
   988
			break;
sl@0
   989
		case ESaved:
sl@0
   990
			iMachineConfig.iCalibration=iMachineConfig.iCalibrationSaved;
sl@0
   991
			break;
sl@0
   992
		default:
sl@0
   993
			r=KErrNotSupported;
sl@0
   994
			break;
sl@0
   995
		}
sl@0
   996
	NKern::UnlockSystem();
sl@0
   997
	return r;
sl@0
   998
	}
sl@0
   999
sl@0
  1000
/**
sl@0
  1001
Gets the digitiser configuration information
sl@0
  1002
sl@0
  1003
@param aInfo On return, contains information about the digitiser's dimensions etc.
sl@0
  1004
*/
sl@0
  1005
void DTemplateDigitiser::DigitiserInfo(TDigitiserInfoV01& aInfo)
sl@0
  1006
	{
sl@0
  1007
	__KTRACE_OPT(KEXTENSION,Kern::Printf("DTemplateDigitiser::DigitiserInfo"));
sl@0
  1008
	aInfo.iDigitiserSize.iWidth=KConfigXyWidth;
sl@0
  1009
	aInfo.iDigitiserSize.iHeight=KConfigXyHeight;
sl@0
  1010
	aInfo.iOffsetToDisplay.iX=KConfigXyOffsetX;
sl@0
  1011
	aInfo.iOffsetToDisplay.iY=KConfigXyOffsetY;
sl@0
  1012
	}
sl@0
  1013
sl@0
  1014
/**
sl@0
  1015
Issues a pen move event if the distance from the last point is greater than the threshold
sl@0
  1016
sl@0
  1017
@param aPoint the pen position in screen coordinates
sl@0
  1018
*/
sl@0
  1019
void DTemplateDigitiser::FilterPenMove(const TPoint& aPoint)
sl@0
  1020
	{
sl@0
  1021
	TPoint offset=aPoint;
sl@0
  1022
	offset.iX-=iLastPos.iX;
sl@0
  1023
	offset.iY-=iLastPos.iY;
sl@0
  1024
	if (Abs(offset.iX)>=iCfg.iAccThresholdX || Abs(offset.iY)>=iCfg.iAccThresholdY)
sl@0
  1025
		{
sl@0
  1026
		iLastPos=aPoint;
sl@0
  1027
		IssuePenMoveEvent(aPoint);
sl@0
  1028
		}
sl@0
  1029
	}
sl@0
  1030
sl@0
  1031
/**
sl@0
  1032
Reset the pen move filter
sl@0
  1033
*/
sl@0
  1034
void DTemplateDigitiser::ResetPenMoveFilter()
sl@0
  1035
	{
sl@0
  1036
	}
sl@0
  1037