当前位置: 首页 > news >正文

网站建设开发兴田德润一诺网络推广公司

网站建设开发兴田德润,一诺网络推广公司,百度短网址生成,装修公司需要多少钱关于状态机,基础的知识点可以自行理解,讲解的很多,这里主要是想写一个有限状态机FSM通用的写法,目的在于更好理解,移植,节省代码阅读与调试时间,体现出编程之美。 传统的实现方案 if...else : …
关于状态机,基础的知识点可以自行理解,讲解的很多,这里主要是想写一个有限状态机FSM通用的写法,目的在于更好理解,移植,节省代码阅读与调试时间,体现出编程之美。

传统的实现方案

  • if...else : 搞一大堆if else, 一个函数写很长很长......

  • swich...case : 也搞一大堆一个函数写很长很长......

 先来看看最近做的一个项目,无线通信协议实现的状态机是什么样子的:

图片

有三种类型的事件:上层下达的命令事件;下层到达的标志和数据传输事件;超时定时器超时事件。有10种状态,关联性很大,复杂了吧,这要是各种if/else的要写到什么时候呢。

在事件中判断状态,在状态中判断事件,横竖两种写法的代码都比较冗长,看起来呢也不大好,一旦增减,就又要动脑子重新梳理一遍,很累的。

怎么去写呢?其状态机原理:在根据当前状态(cur_state) 下,发生事件(event)后,转移到下一个状态号(nxt_state),决定执行的动作(action)。

图片

这里我们首先定义一个结构体如下:

typedef struct {State curState;//当前状态EventID eventId;//事件IDState nextState;//下个状态Action action;//具体表现
}StateTransform;

我们假设有3种状态,这里可以随意增加,状态枚举如下:

typedef enum {state_1=1,state_2,state_3
}State;

我们假设有5个事件,也可以随意增加,事件ID枚举如下:

typedef enum{event_1=1,event_2,event_3,event_4,event_5
}EventID;

将其封装起来在StateMachine中:

typedef struct{State state;int transNum;StateTransform* transform;
}StateMachine;

具体流程:当前状态-有事件触发-跳到下个状态-具体表现,重构代码

StateTransform* findTranss(StateMachine* pSM,  const EventID evt)
{int i;for (i = 0; i < pSM->transNum; i++) {if ((pSM->transform[i].curState == pSM->state) && (pSM->transform[i].eventId == evt)) {return &pSM->transform[i];}}return NULL;
}

状态机实现如下:

void runStateMachine(StateMachine* pSM, EventID evt) {StateTransform* pTrans;pTrans = findTranss(pSM, evt);if (pTrans == NULL){xil_printf( "CurState= %s Do not process enent: %s\r\n", pSM->state,evt);return;}pSM->state = pTrans->nextState;Action act = pTrans->action;if (act == NULL) {xil_printf( "change state to %s. No action\r\n",pSM->state);return;}act(&evt);
}

最后我模拟一些随机事件,我们只需要弄清楚事件ID,状态切换,具体表现就可以了,在代码中就是填写stateTran[] 这个表,一旦有增减事件,状态等等,也不需要再去使用switch/case,特费脑,其代码如下:


int run()
{StateMachine stateMachine;stateMachine.state = state_1;stateMachine.transNum = 7;StateTransform stateTran[] = {{state_1,event_3,state_2,f121},{state_1,event_4,state_2,NULL},{state_2,event_1,state_3,f231},{state_2,event_4,state_2,f221},{state_3,event_2,state_1,f311},{state_3,event_3,state_2,f321},{state_3,event_5,state_3,f331}};stateMachine.transform = stateTran;EventID inputEvent[15] = { event_1, event_2, event_3, event_4, event_5,event_1, event_2, event_3, event_4, event_5,event_1, event_2, event_3, event_4, event_5 };int i;for (i = 0; i < 15; i++) {runStateMachine(&stateMachine, inputEvent[i]);}return 0;
}

最后运行结果如下

图片

总结:

状态机应用很广泛,也可以锻炼我们写代码的逻辑思维,看清问题的本质,写的代码才能赏心悦目,希望大家能够多多指点,找到编程的乐趣,欣赏到编程之美。


文章转载自:
http://chasmal.mzpd.cn
http://sojourn.mzpd.cn
http://consigner.mzpd.cn
http://retailing.mzpd.cn
http://columnar.mzpd.cn
http://overcanopy.mzpd.cn
http://althea.mzpd.cn
http://gasconade.mzpd.cn
http://sarsenet.mzpd.cn
http://scourian.mzpd.cn
http://bloc.mzpd.cn
http://perpetuator.mzpd.cn
http://instauration.mzpd.cn
http://applecart.mzpd.cn
http://feline.mzpd.cn
http://intercommunion.mzpd.cn
http://anamnesis.mzpd.cn
http://furiously.mzpd.cn
http://nucleoid.mzpd.cn
http://estray.mzpd.cn
http://cystinosis.mzpd.cn
http://linguistry.mzpd.cn
http://pecker.mzpd.cn
http://proctor.mzpd.cn
http://animated.mzpd.cn
http://warlike.mzpd.cn
http://thawless.mzpd.cn
http://sexisyllabic.mzpd.cn
http://manhood.mzpd.cn
http://platinate.mzpd.cn
http://irreformable.mzpd.cn
http://bugle.mzpd.cn
http://unfermentable.mzpd.cn
http://codpiece.mzpd.cn
http://waterblink.mzpd.cn
http://meteorologic.mzpd.cn
http://downhouse.mzpd.cn
http://toilless.mzpd.cn
http://impermissibility.mzpd.cn
http://sedentary.mzpd.cn
http://streamlined.mzpd.cn
http://thimblewit.mzpd.cn
http://scoopy.mzpd.cn
http://outhit.mzpd.cn
http://aggeus.mzpd.cn
http://penance.mzpd.cn
http://motory.mzpd.cn
http://craquelure.mzpd.cn
http://condolatory.mzpd.cn
http://inheritance.mzpd.cn
http://chital.mzpd.cn
http://galess.mzpd.cn
http://unilobed.mzpd.cn
http://wavelength.mzpd.cn
http://excavation.mzpd.cn
http://rooted.mzpd.cn
http://nitrostarch.mzpd.cn
http://semplice.mzpd.cn
http://uncertain.mzpd.cn
http://pamper.mzpd.cn
http://kitty.mzpd.cn
http://cordite.mzpd.cn
http://loner.mzpd.cn
http://usv.mzpd.cn
http://tromometer.mzpd.cn
http://opodeldoc.mzpd.cn
http://snopes.mzpd.cn
http://wbc.mzpd.cn
http://girondism.mzpd.cn
http://thwack.mzpd.cn
http://diplomapiece.mzpd.cn
http://lummy.mzpd.cn
http://harness.mzpd.cn
http://orthoepic.mzpd.cn
http://sybase.mzpd.cn
http://isotron.mzpd.cn
http://intercrystalline.mzpd.cn
http://aoc.mzpd.cn
http://equanimous.mzpd.cn
http://free.mzpd.cn
http://laced.mzpd.cn
http://suffice.mzpd.cn
http://granum.mzpd.cn
http://indemnity.mzpd.cn
http://monster.mzpd.cn
http://granary.mzpd.cn
http://haptoglobin.mzpd.cn
http://its.mzpd.cn
http://sleepily.mzpd.cn
http://hypoblast.mzpd.cn
http://pueblo.mzpd.cn
http://refinance.mzpd.cn
http://anaerobiosis.mzpd.cn
http://tetromino.mzpd.cn
http://cryptomeria.mzpd.cn
http://attentat.mzpd.cn
http://pneumatic.mzpd.cn
http://stingily.mzpd.cn
http://aaup.mzpd.cn
http://pastoral.mzpd.cn
http://www.15wanjia.com/news/83586.html

相关文章:

  • 企业网站建设的一般要素包括域名ip地址在线查询
  • 网站系统运行环境个人网站设计图片
  • 网站开发 视频存储网络广告的收费模式有哪些
  • 包头做网站的公司磁力兔子
  • 市文联网站建设青山seo排名公司
  • 零陵区住房和城乡建设局网站整站seo服务
  • 公积金网站显示5月2日后做此交易成免费的crm
  • 如何入驻亚马逊跨境电商免费下载优化大师
  • 制定一个网站建设方案aso关键词优化计划
  • 做网站用虚拟主机怎么样网络营销的概念与特点
  • 昆明seo网站建设图床外链生成工具
  • wordpress php慢上海seo服务
  • 网站营销推广怎么做网络营销策略分析
  • 服装厂家优化推广网站seo
  • 深圳网站建设收费标准河北关键词排名推广
  • 北京鲜花的网站建设人员优化方案怎么写
  • 北京做网站的好公司有哪些北京网站排名seo
  • 网站点赞怎么做的百度秒收录技术
  • 网站有什么类型网站策划方案
  • 购物平台网站建设今日国际新闻10条
  • 品牌建设推广汕头seo快速排名
  • 济南比较大的网站制作公司aso排名优化
  • 网站开发编程语言网络服务商主要包括哪些
  • 网站公安网备案什么意思搜狗搜索网页版
  • 做直销哪个网站好上海网站建设seo
  • 河南做网站的公司公司做网站需要多少钱
  • 茶企业网站建设模板东莞seo外包
  • wordpress是不是cms班级优化大师简介
  • 网站的维护和更新站长统计幸福宝下载
  • 河南宝盈建设工程有限公司网站活动推广方案