Update contrib.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\drivers\xyin\d_xyin.cpp
15 // Generic digitiser driver
20 #include <drivers/xyin.h>
21 #include <kernel/kern_priv.h>
23 _LIT(KLitDigitiser,"Digitiser");
25 LOCAL_C void sampleDfc(TAny* aPtr)
27 ((DDigitiser*)aPtr)->ProcessRawSample();
30 LOCAL_C void penUpDfc(TAny* aPtr)
32 ((DDigitiser*)aPtr)->ProcessPenUp();
35 LOCAL_C TInt halFunction(TAny* aPtr, TInt aFunction, TAny* a1, TAny* a2)
37 DDigitiser* pH=(DDigitiser*)aPtr;
38 return pH->HalFunction(aFunction,a1,a2);
41 LOCAL_C void rxMsg(TAny* aPtr)
43 DDigitiser& h=*(DDigitiser*)aPtr;
44 TMessageBase* pM=h.iMsgQ.iMessage;
49 DDigitiser::DDigitiser()
50 : DPowerHandler(KLitDigitiser),
51 iMsgQ(rxMsg,this,NULL,1),
52 iSampleDfc(sampleDfc,this,5),
53 iPenUpDfc(penUpDfc,this,5)
56 // iLastPos=TPoint(0,0);
62 TInt DDigitiser::Create()
64 TInt r=DoCreate(); // do hardware-dependent initialisation
69 __ASSERT_DEBUG(iDfcQ, Kern::Fault("DDigitiser::Create iDfcQ not set", __LINE__));
71 iSampleDfc.SetDfcQ(iDfcQ);
72 iPenUpDfc.SetDfcQ(iDfcQ);
74 TInt n=iCfg.iPenUpDiscard; // number of samples to delay
75 iBuffer=(TPoint*)Kern::Alloc(n*sizeof(TPoint));
79 // install the HAL function
80 r=Kern::AddHalEntry(EHalGroupDigitiser,halFunction,this);
92 void DDigitiser::RawSampleValid()
94 // Called by hardware-dependent code when a raw sample is available
100 void DDigitiser::PenUp()
102 // Called by hardware-dependent code when the pen goes up
108 void DDigitiser::ProcessRawSample()
110 // DFC to process a raw sample
115 TBool ok=SamplesToPoint(p);
118 // wait for pen to stabilise
119 __KTRACE_XY2(Kern::Printf("BS"));
120 WaitForPenUpDebounce();
123 __KTRACE_XY2(Kern::Printf("GS (%d,%d) %d",p.iX,p.iY,iState));
127 // pen has just gone down
128 iCount=iCfg.iPenDownDiscard;
129 iState=EDiscardOnPenDown;
131 case EDiscardOnPenDown:
138 iState=EBufferFilling;
140 iCount=iCfg.iPenUpDiscard;
145 // buffer still filling
147 iBuffer[iBufferIndex++]=p;
148 if (iBufferIndex==iCfg.iPenUpDiscard)
155 r=DelayAndConvertSample(p,iLastPos);
157 break; // off the screen, so don't issue Pen Down Event
159 ResetPenMoveFilter();
163 r=DelayAndConvertSample(p,p);
166 iState=EIdle; // off the screen, so treat as pen-up
173 WaitForPenUp(); // request another sample from the hardware
176 void DDigitiser::ProcessPenUp()
178 // DFC to process pen-up events
181 __KTRACE_XY2(Kern::Printf("up %d",iState));
185 case EDiscardOnPenDown:
195 WaitForPenDown(); // tell the hardware to watch for another pen-down
198 TBool DDigitiser::SamplesToPoint(TPoint& aPoint)
200 // Average and validate the raw samples from the hardware
203 #if defined(__DIGITISER_DEBUG2__)
213 TInt n=iCfg.iNumXYSamples;
228 // __KTRACE_XY2(buf.AppendFormat(_L("(%d,%d) "),x,y));
229 __KTRACE_XY2(Kern::Printf("(%d,%d) ",x,y));
231 // __KTRACE_XY2(Kern::Printf("%S", buf));
233 TInt spreadx=maxx-minx;
234 TInt spready=maxy-miny;
235 if (iCfg.iDisregardMinMax)
237 sumx-=minx; // disregard extremal values in average
243 sumx/=n; // average the values
245 if (spreadx<iCfg.iSpreadX && spready<iCfg.iSpreadY && sumx>=iCfg.iMinX && sumx<=iCfg.iMaxX && sumy>=iCfg.iMinY && sumy<=iCfg.iMaxY)
256 TInt DDigitiser::DelayAndConvertSample(const TPoint& aSample, TPoint& aScreenPoint)
258 // Pass a sample through the delay line and convert to screen coordinates
261 if (iCfg.iPenUpDiscard != 0)
263 TPoint p=iBuffer[iBufferIndex]; // sample leaving delay line
264 iBuffer[iBufferIndex++]=aSample; // sample entering delay line
265 if (iBufferIndex==iCfg.iPenUpDiscard)
267 return DigitiserToScreen(p,aScreenPoint);
269 return DigitiserToScreen(aSample,aScreenPoint);
272 void DDigitiser::IssuePenDownEvent()
275 e.Set(TRawEvent::EButton1Down,iLastPos.iX,iLastPos.iY);
277 __KTRACE_XY2(Kern::Printf("D %d,%d",e.Pos().iX,e.Pos().iY));
280 void DDigitiser::IssuePenUpEvent()
283 e.Set(TRawEvent::EButton1Up,iLastPos.iX,iLastPos.iY);
285 __KTRACE_XY2(Kern::Printf("U %d,%d",e.Pos().iX,e.Pos().iY));
288 void DDigitiser::IssuePenMoveEvent(const TPoint& aPoint)
291 e.Set(TRawEvent::EPointerMove,aPoint.iX,aPoint.iY);
293 __KTRACE_XY2(Kern::Printf("M %d,%d",e.Pos().iX,e.Pos().iY));
296 void DDigitiser::HandleMsg(TMessageBase* aMsg)
302 aMsg->Complete(KErrNone,ETrue);
305 TInt DDigitiser::HalFunction(TInt aFunction, TAny* a1, TAny* a2)
308 __KTRACE_OPT(KEXTENSION,Kern::Printf("HalFunction %d", aFunction));
311 case EDigitiserHalXYInfo:
313 TPckgBuf<TDigitiserInfoV01> vPckg;
314 DigitiserInfo(vPckg());
315 Kern::InfoCopy(*(TDes8*)a1,vPckg);
318 case EDigitiserHalSetXYInputCalibration:
320 if(!Kern::CurrentThreadHasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EDigitiserHalSetXYInputCalibration")))
321 return KErrPermissionDenied;
322 TDigitizerCalibration cal;
323 kumemget32(&cal,a1,sizeof(TDigitizerCalibration));
324 r=SetXYInputCalibration(cal);
327 case EDigitiserHalCalibrationPoints:
328 TDigitizerCalibration cal;
329 r=CalibrationPoints(cal);
330 kumemput32(a1,&cal,sizeof(TDigitizerCalibration));
332 case EDigitiserHalSaveXYInputCalibration:
333 r=SaveXYInputCalibration();
335 case EDigitiserHalRestoreXYInputCalibration:
336 if(!Kern::CurrentThreadHasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EDigitiserHalRestoreXYInputCalibration")))
337 return KErrPermissionDenied;
338 r=RestoreXYInputCalibration((TDigitizerCalibrationType)(TInt)a1);
340 case EDigitiserHalSetXYState:
342 if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EDigitiserHalSetXYState")))
343 return KErrPermissionDenied;
346 TThreadMessage& m=Kern::Message();
348 m.SendReceive(&iMsgQ);
352 TThreadMessage& m=Kern::Message();
354 m.SendReceive(&iMsgQ);
358 case EDigitiserHalXYState:
359 kumemput32(a1, (TBool*)&iPointerOn, sizeof(TBool));
368 DECLARE_STANDARD_EXTENSION()
370 __KTRACE_OPT(KEXTENSION,Kern::Printf("Starting digitiser driver"));
371 if (Kern::SuperPage().iCpuId & KCpuIdISS)
372 return KErrNone; // no digitiser on ARMULATOR
373 DDigitiser* pD=DDigitiser::New();
377 __KTRACE_OPT(KEXTENSION,Kern::Printf("Returning %d",r));
381 #ifdef __BUILD_DEVICE_DRIVER__
382 class DDigitiserPdd : public DPhysicalDevice
385 virtual TInt Install();
386 virtual void GetCaps(TDes8& aDes) const;
387 virtual TInt Create(DBase*& aChannel, TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
388 virtual TInt Validate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
391 _LIT(KPddName,"XYInput");
393 TInt DDigitiserPdd::Install()
395 return SetName(&KPddName);
398 void DDigitiserPdd::GetCaps(TDes8&) const
402 TInt DDigitiserPdd::Create(DBase*& aChannel, TInt, const TDesC8*, const TVersion&)
408 TInt DDigitiserPdd::Validate(TInt, const TDesC8*, const TVersion&)
410 return KErrNotSupported;
413 DECLARE_EXTENSION_PDD()
415 return new DDigitiserPdd;