×
Namespaces

Variants
Actions

显示感应器的数据

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 27 Jun 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000948


  • 开发伙伴平台:

S60 3rd Edition, MR


  • 详细描述

代码片段演示了如何显示从手机屏幕加速和旋转感应器生成数据。本代码中没有包含注册监听感应器的部分,这个可以参考CS000888 - Listening for accelerometer sensor data eventsCS000913 - Listening for rotation sensor data events,在这里可以获得更多注册监听的信息。


注意:为了使用这些代码,你需要安装sensor plug-in到您的SDK中。


该代码片段可以通过自签名执行


MMP

需要下列链接库:

LIBRARY  RRSensorApi.lib
LIBRARY gdi.lib


头文件

#include <coecntrl.h>   // CCoeControl
#include <gdi.h> // CFont
#include <RRSensorApi.h>// Accelerometer sensor UID
const TInt KAccelerometerSensorUID = 0x10273024;
// Rotation sensor UID
const TInt KRotationSensorUID = 0x303E;class CMyControl : public CCoeControl, public MRRSensorDataListener
{
// ...
 
private: // Functions from base classes
/**
* From CCoeControl.
* Draws this control to the screen.
* @param aRect the rectangle of this view that needs updating
*/
void Draw(const TRect& aRect) const;
 
/**
* From MRRSensorDataListener.
* Callback function for receiving sensor data events.
*
* @param aSensor identifies the sensor that created the event.
* @param aEvent contains data about created event.
*/
void HandleDataEventL(TRRSensorInfo aSensor, TRRSensorEvent aEvent);
 
private: // Data
CRRSensorApi* iAccelerometerSensor;
TInt iAccSensorDataX;
TInt iAccSensorDataY;
TInt iAccSensorDataZ;
CRRSensorApi* iRotationSensor;
TInt iRotSensorDataX;
TInt iRotSensorDataY;
TInt iRotSensorDataZ;
CFont* iFont;
}


源文件

#include <EIKENV.H> // CEikonEnv
#include <gdi.h> // TFontSpec, CGraphicsDevice
#include <RRSensorApi.h>void CMyControl::ConstructL(const TRect& aRect)
{
// ...
 
// Reset sensor data
iAccSensorDataX = 0;
iAccSensorDataY = 0;
iAccSensorDataZ = 0;
iRotSensorDataX = 0;
iRotSensorDataY = 0;
iRotSensorDataZ = 0;
 
// Initialize the font used in drawing onto the screen
_LIT(KFontName, "Arial");
const TInt KFontSize = 120;
TFontSpec fontSpec(KFontName, KFontSize);
CGraphicsDevice* screenDevice = CEikonEnv::Static()->ScreenDevice();
screenDevice->GetNearestFontInTwips(iFont, fontSpec);
 
// ...
}/**
* Draws the display.
*/
void CMyControl::Draw(const TRect& /*aRect*/) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
 
// Get the control's extent
TRect drawRect(Rect());
 
// Clear the screen with white color
gc.SetBrushColor(KRgbWhite);
gc.Clear(drawRect);
 
// Set the font
gc.UseFont(iFont);
// The text is drawn in black
gc.SetBrushColor(KRgbBlack);
 
TBuf<255> buffer;
 
// Draw accelerometer sensor data
_LIT(KAccTxt, "Accel. sensor: X: %d, Y: %d, Z: %d");
buffer.Format(KAccTxt, iAccSensorDataX, iAccSensorDataY, iAccSensorDataZ);
gc.DrawText(buffer, TPoint(0, 20));
 
// Draw rotation sensor data
_LIT(KRotTxt, "Rotation sensor: X: %d, Y: %d, Z: %d");
buffer.Format(KRotTxt, iRotSensorDataX, iRotSensorDataY, iRotSensorDataZ);
gc.DrawText(buffer, TPoint(0, 40));
}/**
* Gets called whenever a data event occurs.
*/
void CMyControl::HandleDataEventL(TRRSensorInfo aSensor, TRRSensorEvent aEvent)
{
// A data event occurred. Store new values from the sensor in question.
switch (aSensor.iSensorId)
{
case KAccelerometerSensorUID:
{
iAccSensorDataX = aEvent.iSensorData1;
iAccSensorDataY = aEvent.iSensorData2;
iAccSensorDataZ = aEvent.iSensorData3;
}
break;
case KRotationSensorUID:
{
iRotSensorDataX = aEvent.iSensorData1;
iRotSensorDataY = aEvent.iSensorData2;
iRotSensorDataZ = aEvent.iSensorData3;
}
default:
break;
}
// Update the display
DrawNow();
}
This page was last modified on 15 December 2011, at 08:43.
128 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×