Working and studying, a recent speech module, the boss arranged a task to do voice wake achieved. After lovemaking session myself silly not refuse to use the simplest way to achieve the vulgar, the boss looked at, you can use a state machine model to try to do, and then under the leadership of the boss, the state machine learning rewarding .
First, what is the status mode.
The official explanation: allows an object to alter its behavior when its internal state changes. The object will appear to change its class
own understanding: that is, when an external object A method of calling a class, when the method of changing a certain state, then it will switch to this method in which a corresponding state in class B.
Second, look at the code to understand
2.1 All states need a management class.
wakeupEngineManager
wakeupEngineManager {
public:
wakeupEngineManager() : mCurrentStatus(std::make_shared<IdleStatus>(this)) { }
bool changeToNotWakeupStates( ){
if ( nullptr != mCurrentStatus) {
mCurrentStatus -> changeToNotWakeupStates();
}
}
bool changeToWakeuped() {
if ( nullptr != mCurrentStatus) {
mCurrentStatus -> changeToWakeuped();
}
}
bool changeToIdle() {
if ( nullptr != mCurrentStatus) {
mCurrentStatus -> changeToIdle();
}
}
void changeStatus(std::shared_ptr<WakeupStatusBase> currentStatus) {
mCurrentStatus = currentStatus;
}
private:
std::shared_ptr mCurrentStatus ;
}
2.2 WakeupStatusBase.h base class for all states.
class WakeupStatusBase {
public:
WakeupStatusBase(){};
virtual ~WakeupStatusBase(){};
virtual bool changeToNotWakeupStates = 0;
virtual bool changeToWakeuped() = 0;
virtual bool changeToIdle() = 0;
virtual int currentStatusClass() = 0;
};
2.3 One of the states on behalf of class NotWakeupStatus wake engine started, but has not yet awakened.
NotWakeupStatus, inherited from WakeupStatusBase
class NotWakeupStatus: public WakeupStatusBase {
public:
NotWakeupStatus(wakeupEngineManager * engine) {
mWakeupEngine = engine;
}
bool changeToNotWakeupStates() {
// 在这里方法里面做初始化唤醒引擎的动作。
return true;
}
bool changeToWakeuped(){
if (nullptr != mWakeupEngine) {
mWakeupEngine-> changeStatus(std::make_shared<WakeupedStatus> (mWakeupEngine));
}
return true;
}
bool changeToIdle() {
if (nullptr != mWakeupEngine) {
mWakeupEngine-> changeStatus(std::make_shared<IdleStatus> (mWakeupEngine));
}
return true;
}
private:
wakeupEngineManager * mWakeupEngine;
}
And the latter method wakeupedStatus idleStatus basic classes and NotWakeupStatus as just a little difference in the base class.
2.4 One class WakeupedStatus Representative awakened state.
WakeupedStatus, inherited from WakeupStatusBase
class WakeupedStatus : public WakeupStatusBase {
public:
WakeupedStatus (wakeupEngineManager * engine) {
mWakeupEngine = engine;
}
bool changeToNotWakeupStates() {
if (nullptr != mWakeupEngine) {
mWakeupEngine-> changeStatus(std::make_shared<NotWakeupStatus> (mWakeupEngine));
}
return true;
}
bool changeToWakeuped(){
// 在这里做唤醒之后的事情
}
bool changeToIdle() {
if (nullptr != mWakeupEngine) {
mWakeupEngine-> changeStatus(std::make_shared<IdleStatus> (mWakeupEngine));
}
return true;
}
private:
wakeupEngineManager * mWakeupEngine;
}
2.5 One representative of wake state class IdleStatus microphone module loses focus, or is the reason, can not be used.
IdleStatus, inherited from WakeupStatusBase
class IdleStatus : public WakeupStatusBase {
public:
WakeupedStatus (wakeupEngineManager * engine) {
mWakeupEngine = engine;
}
bool changeToNotWakeupStates() {
if (nullptr != mWakeupEngine) {
mWakeupEngine-> changeStatus(std::make_shared<NotWakeupStatus> (mWakeupEngine));
}
return true;
}
bool changeToWakeuped(){
// 不能够从idle直接到wakeuped。
return false;
}
bool changeToIdle() {
// 做切换到idle状态应该做的事情
return true;
}
private:
wakeupEngineManager * mWakeupEngine;
}
Third, the analysis of the code down to the basic state machine model is event-triggered event corresponding to migrate to the state.