Author:家有仙妻谢掌柜
Date:2021/2/8
总结一下用单片机做项目时代码的架构,写在这里记录自己的成长历程!
1.做智能手表的框架,暂时不完全,有时间会更新该文章
event.h文件
#define AppID_System 0 //系统准备
#define AppID_Timer 1 //GIF定时器
#define AppID_Main1 2 //主界面1
#define AppID_Main2 3 //主界面2
#define AppID_Main3 4 //主界面3
#define AppID_Main4 5 //主界面4
#define AppID_Max 6 //AppID最大值
#define UiID_None 0
#define UiID_Main1 1 //主界面1
#define UiID_Main2 2 //主界面2
#define UiID_Main3 3 //主界面3
#define UiID_Main4 4 //主界面4
#define MaxEventAmount 10 //表示同时可以最多执行10个事件
#define SystemEventStart(id) ((id) << 8)
#define SystemEventMask(event) (((event) >> 8)&0xffffffff)
enum{
System_NoneEvent = SystemEventStart(AppID_System),
System_InitEvent, //先发这个事件
System_MaxEvent
};
enum{
Main1_NoneEvent = SystemEventStart(AppID_Main1),
Main1_ShowEvent,
Main1_ShowGifEvent,
Main1_MaxEvent
};
enum{
Main2_NoneEvent = SystemEventStart(AppID_Main2),
Main2_ShowEvent,
Main2_ShowGifEvent,
Main2_MaxEvent
};
enum{
Main3_NoneEvent = SystemEventStart(AppID_Main3),
Main3_ShowEvent,
Main3_ShowGifEvent,
Main3_MaxEvent
};
enum{
Main4_NoneEvent = SystemEventStart(AppID_Main4),
Main4_ShowEvent,
Main4_ShowGifEvent,
Main4_MaxEvent
};
enum{
Timer_NoneEvent = SystemEventStart(AppID_Timer),
Timer_GifEvent,
Timer_MaxEvent
};
event.c文件
static uint32_t AppEventArray[MaxEventAmount] = {
System_NoneEvent,System_NoneEvent,System_NoneEvent,System_NoneEvent,System_NoneEvent};
uint32_t AppIDChoose;
uint16_t MenuChoose = UiID_None;
static int readEventIndex = 0;
static int writeEventIndex= 0;
static uint32_t APP_GetFirstEvent(void)
{
uint32_t event = AppEventArray[readEventIndex];
if(event==System_NoneEvent)
{
readEventIndex++;
readEventIndex%=MaxEventAmount;
}
return event;
}
void APP_SetEvent(uint32_t event)
{
//AppEventArray|=event;
AppEventArray[writeEventIndex]=event;
writeEventIndex++;
writeEventIndex%=MaxEventAmount;
}
void APP_ClearEvent(uint32_t event)
{
int index;
if(AppEventArray[readEventIndex]==event)
{
AppEventArray[readEventIndex] = System_NoneEvent;
readEventIndex++;
readEventIndex%=MaxEventAmount;
}
else
{
for(index=0;index<MaxEventAmount;index++)
{
if(AppEventArray[index]==event)
AppEventArray[index] = System_NoneEvent;
}
}
//AppEventArray &= (~event);
}
前面已经将框架的准备工作做好了,接下来就是搭建框架!
uint32_t AppEventSystem(uint32_t event)
{
uint32_t RetainEvent=0;
if(event==System_InitEvent)
{
APP_SetEvent(Main1_ShowEvent);
RetainEvent=event;
MenuChoose=UiID_None;
}
return RetainEvent;
}
uint32_t AppEventMain1(uint32_t event)
{
uint32_t RetainEvent=0;
if(event==Main1_ShowEvent)
{
StopAppGifTimer();
StartAppGifTimer(AppIDChoose,t_time_GIF,Main1_ShowGifEvent,1);
RetainEvent=event;
ClearGifCnt();
MenuChoose=UiID_Main1;
}
if(event==Main1_ShowGifEvent)
{
RetainEvent=event;
MenuChoose=UiID_Main1;
}
return RetainEvent;
}
uint32_t AppEventMain2(uint32_t event)
{
uint32_t RetainEvent=0;
if(event==Main2_ShowEvent)
{
StopAppGifTimer();
StartAppGifTimer(AppIDChoose,t_time_GIF,Main2_ShowGifEvent,1);
RetainEvent=event;
ClearGifCnt();
MenuChoose=UiID_Main2;
}
if(event==Main2_ShowGifEvent)
{
RetainEvent=event;
MenuChoose=UiID_Main2;
}
return RetainEvent;
}
uint32_t AppEventMain3(uint32_t event)
{
uint32_t RetainEvent=0;
if(event==Main3_ShowEvent)
{
StopAppGifTimer();
StartAppGifTimer(AppIDChoose,t_time_GIF,Main3_ShowGifEvent,1);
RetainEvent=event;
ClearGifCnt();
MenuChoose=UiID_Main3;
}
if(event==Main3_ShowGifEvent)
{
RetainEvent=event;
MenuChoose=UiID_Main3;
}
return RetainEvent;
}
uint32_t AppEventMain4(uint32_t event)
{
uint32_t RetainEvent=0;
if(event==Main4_ShowEvent)
{
StopAppGifTimer();
StartAppGifTimer(AppIDChoose,t_time_GIF,Main4_ShowGifEvent,1);
RetainEvent=event;
ClearGifCnt();
MenuChoose=UiID_Main4;
}
if(event==Main4_ShowGifEvent)
{
RetainEvent=event;
MenuChoose=UiID_Main4;
}
return RetainEvent;
}
uint32_t AppEventTimer(uint32_t event)
{
uint32_t RetainEvent=0;
if(event==Timer_GifEvent)
{
app_gif_timer_evt();
RetainEvent=event;
}
return RetainEvent;
}
void AppEventMainLoop(void)
{
uint32_t RetainEvent=0;
uint32_t GetEvent = APP_GetFirstEvent();
if(GetEvent!=0)
{
AppIDChoose = SystemEventMask(GetEvent);
if(AppID_System == AppIDChoose){ //System
RetainEvent = AppEventSystem(GetEvent);
}else if(AppID_Main1 == AppIDChoose){ //Main1
RetainEvent = AppEventMain1(GetEvent);
}else if(AppID_Main2 == AppIDChoose){ //Main2
RetainEvent = AppEventMain2(GetEvent);
}else if(AppID_Main3 == AppIDChoose){ //Main3
RetainEvent = AppEventMain3(GetEvent);
}else if(AppID_Main4 == AppIDChoose){ //Main4
RetainEvent = AppEventMain4(GetEvent);
}else if(AppID_Timer == AppIDChoose){ //GIF_timer
RetainEvent = AppEventTimer(GetEvent);
}
APP_ClearEvent(RetainEvent);
}
}
void AppKeyMianLoop(void)
{
if(KeyPressFlag)//中断里面,按键按下就置一
{
if(SystemFlag)//系统运行标志位
{
APP_SetEvent(System_InitEvent);
AppKeyClearEvent(KeyPressFlag);
if(UiID_Main1==MenuChoose)
APP_SetEvent(Main2_ShowEvent);
else if(UiID_Main2==MenuChoose)
APP_SetEvent(Main3_ShowEvent);
else if(UiID_Main3==MenuChoose)
APP_SetEvent(Main4_ShowEvent);
else if(UiID_Main4==MenuChoose)
APP_SetEvent(Main1_ShowEvent);
}
AppKeyClearEvent(KeyPressFlag);
}
}