嵌入式系统上消息机制的实现

1引言
嵌入式系统,作为计算机两大分支之一,从不同的角度影响着人们的生活,尤其是在通讯、工控、电子等领域发挥着越来越重要的作用。嵌入式设备之所以为众多用户乐于接受,是因为嵌入式设备有着自然的人机交互界面,较小的尺寸、微功耗和低成本的特点。同时,这些特点也要求嵌入式产品设计者相应降低处理器的性能,限制内存容量和复用接口芯片,这就相应提高了对嵌入式软件设计技术要求。如,选用最佳的编程模型和不断改进算法。这也正是本文的目的,提出一个消息机制实现的模型,并具有良好的可扩展性和维护性。
本文对消息机制的定义为:从底层接收用户输入信息(如按键和鼠标操作等),经后台处理、记录后反映到前台显示的一个机制。
2 对MiniGUI中的消息机制分析
MiniGUI下的通讯是一种类似于 Win32的消息机制,运行在 MiniGUI-Threads模式下时,线程间的消息传递模型如下图所示,其中的 Desktop线程充当一个微服务器,所有的消息在 Event线程获取出来以后就会投递给 Desktop线程,然后再分发到目的应用程序主窗口上面。 
     MiniGUI-Threads 中每个线程创建的第一个主窗口,其托管窗口必须是桌面,即 HWND_DESKTOP,该线程的其他窗口,必须由属于同一线程的已有主窗口作为托管窗口。系统在托管窗口为 HWND_DESKTOP时创建新的消息队列,而在指定非桌面的窗口作为托管窗口时,使用该托管窗口的消息队列,也就是说,同一线程中的所有主窗口应该使用同一个消息队列。通过对 MiniGUI开源学习版本版本 1.6.2源代码的分析,其消息机制模型大致如下:
a)  Event线程中的 void* EventLoop (void* data)函数通过宏IAL_WaitEvent等待底层事件发生,IAL_WaitEvent实际上是调用当前系统所指定的 IAL引擎的 wait函数,收到具体的事件触发消息后,EventLoop再调用 ParseEvent函数来解析这个事件消息, ParseEvent解析后,再往 Desktop的消息队列中发送相应的消息, EventLoop从 ParseEvent返回后,单次循环结束,再次回到 IAL_WaitEvent,如此反复循环。
b)  Desktop线程在队列中收到消息后,根据消息种类处理分别处理,再将消息发往当前活动窗口(活动窗口句柄__mg_active_mainwnd)的消息队列。
c) 其它窗口线程中,一般会有如下格式的循环来处理消息
while (GetMessage (&Msg, hMainWnd)) {
TranslateMessage (&Msg);
DispatchMessage (&Msg);
} GetMessage 函数从句柄为hMainWnd的窗口的消息队列当中获得消息,然后调用 TranslateMessage函数将某些消息如 MSG_KEYDOWN 和 MSG_KEYUP 翻译成字符消息 MSG_CHAR ,最后调用 DispatchMessage 函数将消息发送到指定的窗口,或者理解为 DispatchMessage调用指定窗口的窗口过程,并传递消息及其参数。
由此可见,MiniGUI的消息机制是相当完整的,扩展性,健壮性都很好,很适合开发复杂的桌面系统,但是如果在一些资源相对有限的嵌入式场合并且窗口数目不多,菜单简单,不需要窗口重叠,窗口移动等应用情况下,没有必要使用专业 GUI中间件。在此情况下,有必要引入一个简单的消息机制,来维护系统底层事情、程序后台菜单和前台显示之间的关系。
3简单消息机制的实现
3.1 消息机制结构
     采用不同于MiniGUI的消息队列的通信方式,因为简单的应用的窗口数目不多,这里的
窗口数目的含义是每一个在目标机上可能出现的系统菜单逐级显示界面。这个消息机制的系统结构图,如图1所,结合图说明这个简单消息机制的实现过程:
后续再判断按键是不是但前菜单需要的按键,如果不是,则此函数不会调用任何处理函数,直接返回,反之,则调用相应按键的处理子程序。
c) 经判定后,如果要处理该按键,就进入了相应的过程函数,每个过程函数,都有一段 refreshRoutine,这个函数的作用就是根据具体的按键(按键可能触发转到新的菜单页面),Current_win_ID,pre_win_ID,3个要素来更新 Current_win_ID,同时把原来的 Current_win_ID保存到Pre_win_ID中,这样新的按键有效后,前台显示和后台菜单的位置就同步了,之后调用相应的应用程序,根据应用程序返回的参数,再决定是否刷新 screenDC,即调用 GDI进行相应区域的重绘工作。
3.2 利用2个数组来记录所有菜单
     OBJECT* WIN_OBJECT[]、MENU WIN_MENU[]这2个数组与Menu之间的关系如图 2所示。WIN_OBJECT是一个结构体指针数组,数组的每一项,存放一个指向 OBJECT类型的结构体数组的指针(如mainMenu[3]),这个结构体数组相当于一个菜单的作用,数组的长度表示该菜单下,菜单项的个数,同时,ID标签也指向这个菜单,如图 2中 mainMenu_ID,WIN_OBJECT[mainMenu_ID]中就存放了一个指向mainMenu[]数组的指针。 mainMenu[]中的每一项指向一个具体的OBJECT结构体,可以抽象理解为指向一个按钮。 WIN_MENU[]数组的作用是指示当前菜单下按钮的个数,以及当前按钮的索引和默认按钮的索引,如WIN_MENU[mainMenu_ID]={3,0,0};表示mainMenu_ID菜单下,有3个按钮,默认和当前按钮都是button0。在OBJECT结构体中,还存放了该 OBJECT的事件处理函数指针。

     可以看出,采用这种模型能把前台显示的菜单系统很直观的表现出来,这样,极大的方 便了后台的维护,有着相对可视化的优点,并且具有良好的移植性能,在更换平台时,只要考虑GDI函数的重写以及底层按键与结构体 EVENT_DESCRIPTOR注册关系。适合于轻量级的嵌入式系统应用,不能应用于复杂的界面开发,如需要窗口重叠,剪贴等,也恰恰印证了嵌入式系统都有着自己特殊的应用范围这一特殊性。
本文介绍的消息机制实现灵活,占用额外 ROM空间少,可以用于单环或多线程模式,执行效率高,同样也有着相对完整的结构组织。虽然不适用于大型的界面开发,但是非常适合一些简单应用场合,在当前GUI功能越来越复杂,占用 ROM&RAM空间越来越多的情况下,这种简单的实现方法为一些简单的界面开发提供了一种消息机制,能有效的降低成本并保持较高的实时性。此方法已经在基于 UC/OSII的操作系统上实现了多媒体播放器的功能。

转载于:https://www.cnblogs.com/arm9/archive/2010/03/22/1691408.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include "vxWorks.h" #include "msgQLib.h" #include "taskLib.h" /*#include "memPartLib.h"*/ #include "memLib.h" /*宏定义*/ #define MAX_MSGS (10) /* the length of msg*/ #define MAX_MSG_LEN sizeof(MESSAGE) /*the length of message*/ #define STACK_SIZE 20000 /*the stack size of task*/ #define DELAY_TICKS 50 /*the time of sending message*/ #define MAX_point 5 /*用户从系统内存池中获得内存的最大次数*/ #define size_1 30 /*用户分区的分配的大小*/ #define size_2 40 /*全局变量*/ int tidtask1; int tidtask2; int tidtask3; SEM_ID syncSemId; SEM_ID waitSemId; MSG_Q_ID myMsgQId1; MSG_Q_ID myMsgQId2; MSG_Q_ID myMsgQId3; typedef struct _MESSAGE { int mSendId; /*发送任务 ID*/ int mRecvId; /*接收任务 ID*/ int mData; /*消息中传递的数据*/ char Data[14]; } MESSAGE; /*内存管理*/ char* usermem1; char* usermem2; MESSAGE *point1[MAX_point]; MESSAGE *point2[MAX_point]; MESSAGE *point3[MAX_point]; int point1_index=0; int point2_index=0; int point3_index=0; PART_ID partid1; PART_ID partid2; #define MID_MESSAGE(id) (id) /*函数声明*/ int start(void); int task1(void); int task2(void); int task3(void); template T* mymalloc(unsigned nBytes); void myfree(void); void bye(void); /***************************************[progStart]*******************************************/ /*启动程序,创建息队例,任务*/ int start(void) { tidtask1=taskSpawn("tTask1", 220, 0, STACK_SIZE, (FUNCPTR)task1,0,0,0,0,0,0,0,0,0,0); usermem1=malloc(200); partid1=memPartCreate(usermem1,200); usermem2=malloc(400); partid2=memPartCreate(usermem2,400); return; } /**************************************[test_end]********************************************/ /*是否相等,相等返回1*/ int test_end(char *end,char *target) { int ret; if(!strcmp(end,target)) ret=1; else ret=0; return ret; } /****************************************[task1]***********************************************/ /*管理Task。负责系统启动时同步系统中其他Task的启动同步,利用信号量的semFlush()完成。同时接收各*/ /*Task的告警信息,告警信息需编号以logmsg方式输出。本task负责系统结束时的Task删除处理*/ int task1(void) { int singal; int message; MESSAGE *rxMsg=mymalloc(26); /*define messages,and alloc memory*/ memset(rxMsg,0,26); syncSemId=semBCreate(SEM_Q_FIFO,SEM_EMPTY); /*creat semaphore*/ waitSemId=semBCreate(SEM_Q_PRIORITY,SEM_EMPTY); myMsgQId1=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY); /*create msgQ*/ myMsgQId2=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY); myMsgQId3=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY); tidtask2=taskSpawn("tTask2", 200, 0, STACK_SIZE, (FUNCPTR)task2,0,0,0,0,0,0,0,0,0,0); /*create task*/ tidtask3=taskSpawn("tTask3", 210, 0, STACK_SIZE, (FUNCPTR)task3,0,0,0,0,0,0,0,0,0,0); printf("Please input one of the following commands:add,sub,multiply,divide,testcommand\n"); /*the command we should put into the console*/ semFlush(syncSemId); /*release semaphore*/ semGive(waitSemId); while(1) { singal=1; msgQReceive(myMsgQId1,(char*)&rxMsg,sizeof(rxMsg),WAIT_FOREVER); if(rxMsg->mRecvId==MID_MESSAGE(3)) /*receive MsgQ from task3*/ { singal=test_end(rxMsg->Data,"wrong length")-1; logMsg("task3 receiveing a:%s\n",rxMsg->Data); /*put the warn from task3*/ logMsg("Please reput the other command!\n"); msgQReceive(myMsgQId1,(char*)&rxMsg,MAX_MSG_LEN,WAIT_FOREVER); /*recive MsgQ from task3*/ } if(rxMsg->mRecvId==MID_MESSAGE(2)) /*receive MsgQ from task2*/ { message=test_end(rxMsg->Data,"sysend"); if(message) { /*if the message from task2 is "sysend" and did not receive the warn from task3, close the system*/ if(singal) { bye(); } } else {/*if the message from task2 is "sysend" and receive the warn from task3, reput the command*/ if(singal) logMsg("task2 receiveing a %s\n",rxMsg->Data); logMsg("please reput the correct command!\n"); } } } return; } /********************************************************************************************/ int change_buf(char *command) { int ret; if(!strcmp(command,"add")) ret=1; else if(!strcmp(command,"sub")) ret=2; else if(!strcmp(command,"multiply")) ret=3; else if(!strcmp(command,"divide")) ret=4; else if(!strcmp(command,"testcommand")) ret=5; else ret=0; return ret; } /****************************************[task2]*********************************************/ /*console 命令行接收Task。接收并分析console发来的命令行及参数。自行设置5种以上命令,并根据命*/ /*令的内容向Task3发送激励消息。同时实现系统退出命令,使系统采用适当方式安全退出。收到非法命令*/ /*向Task1告警*/ int task2(void) { char buf[100]; int command; char *str=mymalloc(35); MESSAGE *txMsg=mymalloc(26); memset(str,0,35); memset(txMsg,0,26); txMsg->mSendId=MID_MESSAGE(2); txMsg->mRecvId=MID_MESSAGE(2); FOREVER { semTake(syncSemId,WAIT_FOREVER); semTake(waitSemId,WAIT_FOREVER); gets(buf); command=change_buf(buf);/*change the commands into numbers*/ switch(command) { case 0:/*receive uncorrect command*/ txMsg->mData=0; strcpy(txMsg->Data,"wrong command");/*send warn to task1*/ msgQSend(myMsgQId1,(char*)&txMsg,sizeof(txMsg),WAIT_FOREVER,MSG_PRI_NORMAL); break; case 1:/*receive add command*/ strcpy(str,"This an add caculate!\0"); txMsg->mData=1; break; case 2:/*receive sub command*/ strcpy(str,"This a sub caculate!\0"); txMsg->mData=2; break; case 3:/*receive multiply command*/ strcpy(str,"This a multiply caculate!\0"); txMsg->mData=3; break; case 4:/*receive divide command*/ strcpy(str,"This a divide caculate!\0"); txMsg->mData=4; break; case 5:/*receive testcommand,send a long string to task3*/ strcpy(str,"This a testcommand to warn task1!\0"); txMsg->mData=5; break; default: break; } if(txMsg->mData!=0) {/*send along string to task3,and send a message to taks3*/ msgQSend(myMsgQId3,(char*)&str,sizeof(str),WAIT_FOREVER,MSG_PRI_NORMAL); msgQSend(myMsgQId3,(char*)&txMsg,sizeof(txMsg),WAIT_FOREVER,MSG_PRI_NORMAL); } semGive(waitSemId); semGive(syncSemId); taskDelay(DELAY_TICKS); if(txMsg->mData!=0) {/*send sysend to task1 to let task1 close system*/ strcpy(txMsg->Data,"sysend"); msgQSend(myMsgQId1,(char*)&txMsg,sizeof(txMsg),WAIT_FOREVER,MSG_PRI_NORMAL); } } return; } /****************************************[task3]********************************************/ /*console输出Task。接收需打印输出的字串消息(命令),输出到console。收到长度为0或超常字串向*/ /*Task1告警*/ int task3(void) { int firstData=100; int secondData=10; MESSAGE *rxMsg=mymalloc(26); MESSAGE *txMsg=mymalloc(26); char *rstr=mymalloc(35); memset(txMsg,0,26); memset(txMsg,0,26); memset(rstr,0,35); txMsg->mSendId=MID_MESSAGE(3); txMsg->mRecvId=MID_MESSAGE(3); while(1) { semTake(syncSemId,WAIT_FOREVER); msgQReceive(myMsgQId3,(char*)&rstr,sizeof(rstr),WAIT_FOREVER); if(strlen(rstr)=26) {/*make sure whether the string is too long or short*/ strcpy(txMsg->Data,"wrong length"); msgQSend(myMsgQId1,(char*)&txMsg,sizeof(txMsg),WAIT_FOREVER,MSG_PRI_NORMAL); /*msgQReceive(myMsgQId3,(char*)&rxMsg,sizeof(rxMsg),WAIT_FOREVER);*/ } semTake(waitSemId,WAIT_FOREVER); msgQReceive(myMsgQId3,(char*)&rxMsg,sizeof(rxMsg),WAIT_FOREVER); if(rxMsg->mData!=5) {/*when it is not testcommand,printf these*/ printf("%s\n",rstr); printf("there are two datas!\n"); printf("firstData:100\n"); printf("secondData:10\n"); } switch(rxMsg->mData) { case 1:/*printf add caculate*/ printf("The result is:%d\n",firstData+secondData); break; case 2:/*printf sub caculate*/ printf("The result is:%d\n",firstData-secondData); break; case 3:/*printf multiply caculate*/ printf("The result is:%d\n",firstData*secondData); break; case 4:/*printf divide caculate*/ printf("The result is:%d\n",firstData/secondData); break; case 5: break; default: break; } semGive(waitSemId); semGive(syncSemId); taskDelay(DELAY_TICKS); } return; } template T* mymalloc(unsigned nBytes) { T* point; int i=0; /*用户分区一是否能分配的标志位*/ int j=0; /*用户分区二是否能分配的标志位*/ if(nBytes=size_1 && nBytes=size_2) && point3_index<MAX_point) /*若用户分区二不能分配,由系统内存池来分配,且只能从系统内存池中分配MAX_point次*/ { point=malloc(nBytes); point3[point3_index]=point; printf("the number of the point3_index is:%d\n",point3_index); point3_index++; } return point; } void myfree(void) { int i=0; for (i=0;i<point1_index;i++) { memPartFree(partid1,point1[i]); } for (i=0;i<point2_index;i++) { memPartFree(partid2,point2[i]); } for (i=0;i<point3_index;i++) { free(point3[i]); } free(usermem1); free(usermem2); printf("The memory have freed!\n"); } void bye(void) { myfree(); logMsg("Bye-bye\n"); taskDelete(tidtask2); taskDelete(tidtask3); msgQDelete(myMsgQId1); msgQDelete(myMsgQId2); msgQDelete(myMsgQId3); semDelete(syncSemId); taskDelete(tidtask1); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值