examples/SFExamples/TiltMe/src/TiltMeAppView.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  Application view implementation
00028 */ 
00029 
00030 
00031 // INCLUDE FILES
00032 #include <coemain.h>
00033 #include "TiltMeAppView.h"
00034 #include <aknnotewrappers.h>
00035 
00036 
00037 // ============================ MEMBER FUNCTIONS ===============================
00038 
00039 // -----------------------------------------------------------------------------
00040 // CTiltMeAppView::NewL()
00041 // Two-phased constructor.
00042 // -----------------------------------------------------------------------------
00043 //
00044 CTiltMeAppView* CTiltMeAppView::NewL( const TRect& aRect )
00045         {
00046         CTiltMeAppView* self = CTiltMeAppView::NewLC( aRect );
00047         CleanupStack::Pop( self );
00048         return self;
00049         }
00050 
00051 // -----------------------------------------------------------------------------
00052 // CTiltMeAppView::NewLC()
00053 // Two-phased constructor.
00054 // -----------------------------------------------------------------------------
00055 //
00056 CTiltMeAppView* CTiltMeAppView::NewLC( const TRect& aRect )
00057         {
00058         CTiltMeAppView* self = new ( ELeave ) CTiltMeAppView;
00059         CleanupStack::PushL( self );
00060         self->ConstructL( aRect );
00061         return self;
00062         }
00063 
00064 // -----------------------------------------------------------------------------
00065 // CTiltMeAppView::ConstructL()
00066 // Symbian 2nd phase constructor can leave.
00067 // -----------------------------------------------------------------------------
00068 //
00069 void CTiltMeAppView::ConstructL( const TRect& aRect )
00070         {
00071         // Create a window for this application view
00072         CreateWindowL();
00073 
00074         // Set the windows size
00075         SetRect( aRect );
00076 
00077         // Activate the window, which makes it ready to be drawn
00078         ActivateL();
00079         
00080         // Construtig the time objects
00081         iFirst = TTime();
00082     iSecond = TTime();
00083     // Setting the initial time
00084     iFirst.HomeTime();
00085         }
00086 
00087 // -----------------------------------------------------------------------------
00088 // CTiltMeAppView::CTiltMeAppView()
00089 // C++ default constructor can NOT contain any code, that might leave.
00090 // -----------------------------------------------------------------------------
00091 //
00092 CTiltMeAppView::CTiltMeAppView() : iSensorFound(EFalse)
00093         {
00094         // No implementation required
00095         }
00096 
00097 
00098 // -----------------------------------------------------------------------------
00099 // CTiltMeAppView::~CTiltMeAppView()
00100 // Destructor.
00101 // -----------------------------------------------------------------------------
00102 //
00103 CTiltMeAppView::~CTiltMeAppView()
00104         {
00105         StopSensor();
00106         delete iAccSensor;
00107         }
00108 
00109 
00110 // -----------------------------------------------------------------------------
00111 // CTiltMeAppView::Draw()
00112 // Draws the display.
00113 // -----------------------------------------------------------------------------
00114 //
00115 void CTiltMeAppView::Draw( const TRect& /*aRect*/ ) const
00116         {
00117         // Get the standard graphics context
00118         CWindowGc& gc = SystemGc();
00119         // Gets the control's extent
00120         TRect drawRect( Rect());
00121         // Clears the screen
00122         gc.Clear( drawRect );
00123         
00124         if( iSensorFound )
00125                 {
00126                 const CFont* fontUsed = CEikonEnv::Static()->LegendFont();
00127                 gc.UseFont(fontUsed);
00128                 gc.SetPenColor(KRgbBlack);
00129                 
00130                 // Draw the acceleration values in all directions
00131                 TBuf<35> values;
00132                 values.Format(_L("X:  %d  Y:  %d  Z:  %d"), iXAccl, iYAccl, iZAccl);            
00133                 gc.DrawText(values, TPoint(15,15));
00134 
00135                 //
00136                 // Draw the direction arrow
00137                 //
00138                 // Find the screen center point
00139                 TPoint screenCenter = Rect().iBr;
00140                 screenCenter.iX >>= 1;
00141                 screenCenter.iY >>= 1;
00142 
00143                 // Draw the poiting arrow
00144                 gc.SetPenSize(TSize(8,8));
00145                 gc.SetPenStyle(CGraphicsContext::ESolidPen);
00146                 gc.SetPenColor(KRgbRed);
00147                 gc.DrawLine( screenCenter, (screenCenter-TPoint( (TInt)(iYAccl*0.1), (TInt)(iXAccl*0.1) )) );
00148 
00149                 // Draw the center pivot point
00150                 gc.SetPenStyle(CGraphicsContext::ENullPen);
00151                 gc.SetBrushColor(KRgbDarkGreen);
00152                 gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00153                 gc.DrawEllipse( TRect( (screenCenter-TPoint(8,8)), (screenCenter+TPoint(10,10)) ) );
00154                 
00155                 gc.DiscardFont();
00156                 }
00157         }
00158 
00159 
00160 // -----------------------------------------------------------------------------
00161 // CTiltMeAppView::SizeChanged()
00162 // Called by framework when the view size is changed.
00163 // -----------------------------------------------------------------------------
00164 //
00165 void CTiltMeAppView::SizeChanged()
00166         {  
00167         DrawNow();
00168         }
00169 
00170 
00171 // -----------------------------------------------------------------------------
00172 // CTiltMeAppView::HandleDataEventL( TRRSensorInfo aSensor, 
00173 //                                                                                               TRRSensorEvent aEvent )
00174 //
00175 // -----------------------------------------------------------------------------
00176 //
00177 void CTiltMeAppView::HandleDataEventL( TRRSensorInfo /*aSensor*/, 
00178                                                                            TRRSensorEvent aEvent )
00179         {
00180     iSecond.HomeTime();
00181     // Find the interval from the second time to the first
00182         TTimeIntervalMicroSeconds interv = iSecond.MicroSecondsFrom(iFirst);
00183     TInt64 i = interv.Int64();
00184     if(i >= 100000)
00185         {
00186         iXAccl = aEvent.iSensorData1;
00187         iYAccl = aEvent.iSensorData2;
00188         iZAccl = aEvent.iSensorData3;
00189         // Reset the first time to the current one.     
00190         iFirst.HomeTime();
00191         // Update the screen
00192         DrawNow();
00193         }
00194         }
00195 
00196 
00197 
00198 // -----------------------------------------------------------------------------
00199 // CTiltMeAppView::StartSensor()
00200 // -----------------------------------------------------------------------------
00201 //
00202 void CTiltMeAppView::StartSensor()
00203         {
00204         // If the sensor is initially not found
00205         if( !iSensorFound )
00206                 {
00207                 // Get list of available sensors (if any)
00208                 RArray <TRRSensorInfo> sensorsList;
00209                 CRRSensorApi::FindSensorsL( sensorsList );
00210         
00211                 //Number of sensors available
00212                 TInt count = sensorsList.Count();
00213         
00214                 for( TInt i = 0 ; i != count ; ++i )
00215                     {
00216                     // If the ID matches Nokia 5500
00217                     if( sensorsList[i].iSensorId == 0x10273024 )        // 0x303E sensor ID for N95 
00218                         {
00219                         // Use the sensor found
00220                         iAccSensor = CRRSensorApi::NewL( sensorsList[i] );
00221                         // Register the sensor for our app
00222                         iAccSensor->AddDataListener( this );
00223                                 iSensorFound = ETrue;
00224                                 return;
00225                         }
00226                         }
00227                 
00228                 // If we reach here that means we could not find a sensor.
00229                 CAknInformationNote* note3 = new (ELeave) CAknInformationNote();
00230                 CleanupStack::PushL(note3);
00231                 note3->ExecuteLD(_L("No 3D sensor could be found!"));
00232                 CleanupStack::Pop();
00233                 }
00234         }
00235 
00236 
00237 // -----------------------------------------------------------------------------
00238 // CTiltMeAppView::StopSensor()
00239 // -----------------------------------------------------------------------------
00240 //
00241 void CTiltMeAppView::StopSensor()
00242         {
00243         // If the sensor has been registered
00244         if( iSensorFound )
00245                 {
00246                 iAccSensor->RemoveDataListener();
00247                 iSensorFound = EFalse;
00248                 // Clean up
00249                 delete iAccSensor;
00250                 iAccSensor = NULL;
00251                 }
00252         }
00253 
00254 
00255 // End of File

Generated by  doxygen 1.6.2