×
Namespaces

Variants
Actions

Monitoring RAM using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: User:PushL (29 Jun 2007)
Last edited: hamishwillee (26 Jul 2012)

This simple control allows you to monitor available RAM in your mobile. To use it, just instantiate the control and set a timer to periodically invoke the plot function:

// The view owns the RAM control
void CSystemView::CmdStartTimers()
{
iRAMControl->Reset();
DrawDeferred();
 
const TInt KUpdateInterval = 1000000; // 1 sec
iUpdateTimer->Start(KUpdateInterval, KUpdateInterval, TCallBack(DoUpdateL, this));
}
 
TInt CSystemView::DoUpdateL(TAny* aPtr)
{
CSystemView* self = static_cast<CSystemView*>(aPtr);
self->iRAMControl->PlotL();
 
return 0;
}

The RAM monitor control class:

#include <coecntrl.h>   // CCoeControl
 
class CRAMControl : public CCoeControl
{
public:
void ConstructL(const TRect& aRect, const CCoeControl* aParent);
~CRAMControl();
 
void Reset();
void PlotL();
 
private:
void Draw(const TRect& aRect) const;
void SizeChanged();
 
private:
CArrayFix<TInt>* iFreeRAMList;
TInt iFreeRAM;
TReal32 iTotalRAM;
 
CArrayFix<TPoint>* iPointList;
TRect iPaintRect;
 
HBufC* iFreeRAMText;
};
#include "RAMControl.h"
#include <YourApp.rsg> // Define r_text_free_ram, etc
 
#include <coemain.h> // CCoeEnv
#include <eikenv.h> // CEikonEnv
#include <hal.h>
 
void CRAMControl::ConstructL(const TRect& aRect, const CCoeControl* aParent)
{
if (aParent == 0)
CreateWindowL();
else
SetContainerWindowL(*aParent);
 
iFreeRAMList = new(ELeave) CArrayFixSeg<TInt>(5);
iPointList = new(ELeave) CArrayFixFlat<TPoint>(5);
 
iFreeRAMText = iCoeEnv->AllocReadResourceL(R_TEXT_FREE_RAM);
 
SetRect(aRect);
ActivateL();
}
 
CRAMControl::~CRAMControl()
{
delete iFreeRAMText;
 
delete iPointList;
delete iFreeRAMList;
}
 
void CRAMControl::Reset()
{
iFreeRAMList->Reset();
iTotalRAM = 0;
 
iPointList->Reset();
}
 
void CRAMControl::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
 
gc.Clear(iPaintRect);
gc.SetClippingRect(iPaintRect);
gc.SetPenColor(KRgbRed);
gc.DrawPolyLine(iPointList);
 
gc.SetPenColor(KRgbBlack);
gc.DrawRect(iPaintRect);
 
const CFont* font = iEikonEnv->LegendFont();
gc.UseFont(font);
 
TInt freeRam = 0;
HAL::Get(HAL::EMemoryRAMFree, freeRam);
TBuf<64> freeRAMText;
freeRAMText.Format(*iFreeRAMText, freeRam >> 10);
 
gc.DrawText(freeRAMText, iPaintRect, iPaintRect.Height() -
font->DescentInPixels(), CGraphicsContext::ERight);
gc.DiscardFont();
}
 
void CRAMControl::SizeChanged()
{
iPaintRect = Rect();
iPaintRect.Shrink(5, 5);
}
 
void CRAMControl::PlotL()
{
HAL::Get(HAL::EMemoryRAMFree, iFreeRAM);
 
// In case there's a new maximum of free memory, resize graphic
TInt possibleMax = static_cast<TInt> (iFreeRAM * 1.2);
if (possibleMax > iTotalRAM)
iTotalRAM = possibleMax;
 
if (iFreeRAMList->Count() == iPaintRect.Width() - 2)
iFreeRAMList->Delete(0);
iFreeRAMList->AppendL(iFreeRAM);
 
iPointList->Reset();
for (TInt i = 0; i < iFreeRAMList->Count(); ++i)
{
TInt x = iPaintRect.iBr.iX - 1 - iFreeRAMList->Count() + i;
TInt y = iPaintRect.iBr.iY - 1 -
static_cast<TInt> (iPaintRect.Height() * (iFreeRAMList->At(i) / iTotalRAM));
iPointList->AppendL(TPoint(x, y));
}
 
DrawDeferred();
}

You can check TaskSpy source code to see how this works.

This page was last modified on 26 July 2012, at 02:04.
41 page views in the last 30 days.
×