You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 17:46.

CS000914 - Displaying sensor data

From Forum Nokia Wiki



ID CS000914 Creation date April 22, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95 8GB
Category Symbian C++ Subcategory Hardware, Sensor


Keywords (APIs, classes, methods, functions): MRRSensorDataListener, TRRSensorInfo, TRRSensorEvent, CFont, CWindowGc, MRRSensorDataListener::HandleDataEventL(), CGraphicsDevice::GetNearestFontInTwips(), CWindowGc::SetBrushColor(), CWindowGc::UseFont(), CWindowGc::DrawText()

Overview

This code snippet demonstrates how to display the data produced by the accelerometer and rotation sensors of the device on the screen.

Registering listeners for the sensors is not covered in this code snippet. See CS000888 - Listening for accelerometer sensor data events and CS000913 - Listening for rotation sensor data events for more information on registering listeners.

Note: In order to use the code, you need to install the sensor plug-in for your SDK.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  RRSensorApi.lib
LIBRARY gdi.lib

Header file

#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;
}

Source file

#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();
}

Postconditions

Data received from accelerometer and rotation sensors is constantly drawn onto the screen.

See also

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fS40E5fSensorsX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxS40E20SensorsE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZhardwareQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZjavaQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d06X qfnZuserE5ftagQSxhardwareX qfnZuserE5ftagQSxjavaX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ