📄 vibra.cpp
字号:
/********************************************************************
created: 2008/04/10
created: 10:4:2008 10:26
filename: e:\qWork\2.0\Vibra\Vibra\src\Vibra.cpp
file path: e:\qWork\2.0\Vibra\Vibra\src
file base: Vibra
file ext: cpp
author: ioernt
purpose: 振动提示相关操作的一个封装的实现
*********************************************************************/
#include "Vibra.h"
#include <f32file.h> // efsrv.lib
#include <s32file.h> // efsrv.lib
CVibra::CVibra()
:CTimer(0)
{
iVibraCtrl = NULL;
iStrength = 0;
iMaxStrength = 0;
iStepInterval = 0;
iStepCount = 0;
iIsVibraEnabled = EFalse;
}
CVibra::~CVibra()
{
delete iVibraCtrl;
iVibraCtrl = NULL;
Cancel();
}
void CVibra::ConstructL()
{
iVibraCtrl = VibraFactory::NewL(this);
iIsVibraEnabled = (iVibraCtrl->VibraSettings() == CVibraControl::EVibraModeON)? ETrue : EFalse;
CTimer::ConstructL();
CActiveScheduler::Add(this);
}
CVibra* CVibra::NewLC()
{
CVibra* self = new (ELeave)CVibra;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CVibra* CVibra::NewL()
{
CVibra* self = NewLC();
CleanupStack::Pop();
return self;
}
CVibraControl::TVibraModeState CVibra::VibraSettings()
{
return iVibraCtrl->VibraSettings();
}
void CVibra::VibraGraduallyUp(TInt aMaxIn100 /* = 80 */, TInt aStepInterval /* = KStepInterval */, TInt aStepCount /* = KSetpCount */)
{
if (!iIsVibraEnabled)
{
//iStepCount = 0;
//iStepInterval = 0;
//iStrength = 0;
iMaxStrength = 0;
Cancel();
return ;
}
if (aMaxIn100 < 20)
{
aMaxIn100 = 20;
}
if (aStepInterval < 50)
{
aStepInterval = 50;
}
if (aStepCount < 2)
{
aStepCount = 2;
}
if (aStepCount*aStepInterval > 6*1000)
{
aStepCount = 6*1000/aStepInterval;
}
iMaxStrength = aMaxIn100;
iStepCount = aStepCount;
iStepInterval = aStepInterval;
iStrength = iMaxStrength/iStepCount;
if (iStrength < 30)
{
iStrength = 30;
}
#ifdef _VIBRA_DEBUG_
TBuf<128> buf;
buf.Format(_L("iMax:%d, iCount:%d, iInterval:%d, iStrength:%d"), iMaxStrength, iStepCount, iStepInterval, iStrength);
Log(buf);
#endif
iVibraCtrl->StartVibraL(iStepInterval, iStrength);
CTimer::After(iStepInterval*1000);
}
void CVibra::StartVibraL(TUint16 aDuration, TInt aIntensity)
{
//iStrength = 0;
//iStepCount = 0;
//iStepInterval = 0;
iMaxStrength = 0;
if (!iIsVibraEnabled)
{
return ;
}
if (aIntensity < 30)
{
aIntensity = 30;
}
Cancel();
iVibraCtrl->StartVibraL(aDuration, aIntensity);
}
void CVibra::StopVibraL()
{
//iStrength = 0;
//iStepCount = 0;
//iStepInterval = 0;
iMaxStrength = 0;
if (!iIsVibraEnabled)
{
return ;
}
Cancel();
iVibraCtrl->StopVibraL();
}
void CVibra::VibraModeStatus(CVibraControl::TVibraModeState aStatus)
{
switch(aStatus)
{
case CVibraControl::EVibraModeON:
iIsVibraEnabled = ETrue;
break;
//case CVibraControl::TVibraModeState::EVibraModeOFF:
// iIsVibraEnabled = EFalse;
// break;
//case CVibraControl::TVibraModeState::EVibraModeUnknown:
// iIsVibraEnabled = EFalse;
// break;
default:
iIsVibraEnabled = EFalse;
break;
}
}
void CVibra::RunL()
{
if (KErrNone != iStatus.Int())
{
// some error
return ;
}
if ((0 != iMaxStrength) && (iStrength < iMaxStrength))
{
#ifdef _VIBRA_DEBUG_
TBuf<128> buf;
#endif
iStrength += iMaxStrength/iStepCount;
if (iStrength > 100)
{
iStrength = 100;
iMaxStrength = 0;
#ifdef _VIBRA_DEBUG_
buf.Format(_L("iMax:%d, iCount:%d, iInterval:%d, iStrength:%d"), iMaxStrength, iStepCount, iStepInterval, iStrength);
Log(buf);
#endif
iVibraCtrl->StartVibraL(iStepInterval, iStrength);
return ;
}
#ifdef _VIBRA_DEBUG_
buf.Format(_L("iMax:%d, iCount:%d, iInterval:%d, iStrength:%d"), iMaxStrength, iStepCount, iStepInterval, iStrength);
Log(buf);
#endif
iVibraCtrl->StartVibraL(iStepInterval, iStrength);
CTimer::After(iStepInterval*1000);
}
}
void CVibra::DoCancel()
{
CTimer::DoCancel();
iMaxStrength = 0;
}
void CVibra::VibraRequestStatus(CVibraControl::TVibraRequestStatus aStatus)
{
switch(aStatus)
{
case CVibraControl::EVibraRequestStopped:
break;
case CVibraControl::EVibraRequestUnableToStop:
break;
case CVibraControl::EVibraRequestOK:
break;
default:
break;
}
}
#ifdef _VIBRA_DEBUG_
TInt CVibra::Log(const TDesC& aText)
{
Log( TPtrC8( (TUint8*)aText.Ptr(), aText.Length()*2 ) );
}
TInt CVibra::Log(const TDesC8& aText)
{
TInt err = KErrNone;
RFs fsSession;
RFile logFile;
fsSession.Connect();
_LIT(KLoggerPath, "C:\\Nokia\\Others\\Vibra.txt");
//Create the file if it does not exist and open for writing
err = logFile.Open(fsSession, KLoggerPath, EFileShareExclusive|EFileStreamText|EFileWrite);
if(err!= KErrNone)
{
if (logFile.Create(fsSession, KLoggerPath, EFileShareExclusive|EFileStreamText|EFileWrite) != KErrNone)
{
err = logFile.Open(fsSession, KLoggerPath, EFileShareExclusive|EFileStreamText|EFileWrite);
}
}
TInt pos = 0;
logFile.Size(pos);
if (pos > 1024*32)
{
logFile.SetSize(0);
}
pos = 0;
err = logFile.Seek(ESeekEnd, pos);
logFile.Write(aText, aText.Length());
logFile.Flush();
logFile.Close();
fsSession.Close();
return err;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -