不是我小气,框架代码在项目中,单独提取出来也不容易:
模拟android的栈式界面切换
1,栈
界面结构体:
[mw_shl_code=c,true]//保存一个页面信息
typedef struct Activity_Tag
{
char * pageName; //activity名字,指向一个唯一地址
MyState_TypeDef (* pageCreate)(void * thizactivity, Intent * pram); //配置每个页面具体的函数,申请页面内存
void (* pageStart)(void); //显示页面,初始化页面数据
void (* pageInput)(unsigned char *pbuf , unsigned short len); //获取页面输入
void (* pageFresh)(void); //页面刷新
void (* pageHide)(void); //页面隐藏,切换到子页面
void (* pageResume)(void); //从隐藏状态返回,重新显示
void (* pageDestroy)(void); //页面销毁,显示上一个页面
Intent * pram; //上一个页面传进来的参数地址,读取完需要释放
}Activity;[/mw_shl_code]
界面控制结构体,即栈
[mw_shl_code=c,true]typedef struct _stacknode_
{
Activity * activity; //当前节点的页面信息
struct _stacknode_ * lastStackNode; //上一节点地址
}StackNode;
typedef struct
{
StackNode * top; //栈顶节点
}LinkStack;[/mw_shl_code]
入栈:
[mw_shl_code=c,true]MyState_TypeDef StackPush(LinkStack * linkStack, Activity * activity)
{
StackNode * stackNode = NULL;
//不允许入栈空值
if(NULL == activity)
return My_Fail;
stackNode = MyMalloc(sizeof(StackNode));
if(stackNode)
{
//入栈,先隐藏当前页面
if((linkStack->top) && (linkStack->top->activity) && (linkStack->top->activity->pageHide))
linkStack->top->activity->pageHide();
stackNode->activity = activity;
stackNode->lastStackNode = linkStack->top;
linkStack->top = stackNode;
return My_Pass;
}
else
return My_Fail;
}[/mw_shl_code]
出栈:
[mw_shl_code=c,true]/***************************************************************************************************
*FunctionName: StackPop
*Description: 出栈操作
*Input:
*Output:
*Return:
*Author: xsx
*Date: 2016年12月20日08:50:28
***************************************************************************************************/
MyState_TypeDef StackPop(LinkStack * linkStack, MyBool isResume)
{
StackNode * stackNode = NULL;
//栈空,出栈失败
if(linkStack->top == NULL)
return My_Fail;
//不空
else
{
//销毁activity页面数据
if((linkStack->top->activity) && (linkStack->top->activity->pageDestroy))
linkStack->top->activity->pageDestroy();
//销毁activity
MyFree(linkStack->top->activity);
stackNode = linkStack->top;
//恢复新节点
linkStack->top = linkStack->top->lastStackNode;
if((linkStack->top) && (linkStack->top->activity) && (linkStack->top->activity->pageResume) && (isResume))
linkStack->top->activity->pageResume();
//销毁出栈节点
MyFree(stackNode);
return My_Pass;
}
}[/mw_shl_code]
获取栈顶界面:
[mw_shl_code=c,true]MyState_TypeDef StackTop(LinkStack * linkStack, Activity ** activity)
{
//栈空,出栈失败
if(linkStack->top == NULL)
return My_Fail;
//不空
else
{
if(activity)
*activity = linkStack->top->activity;
return My_Pass;
}
}[/mw_shl_code]
2,定义一个界面
以下为重写的函数(这些函数都不需要手动调用)
static void activityStart(void);
static void activityInput(unsigned char *pbuf , unsigned short len);
static void activityFresh(void);
static void activityHide(void);
static void activityResume(void);
static void activityDestroy(void);
static MyState_TypeDef activityBufferMalloc(void);
static void activityBufferFree(void);
以下为需要手动调用函数
[mw_shl_code=c,true]MyState_TypeDef createAboutUsActivity(Activity * thizActivity, Intent * pram)
{
if(NULL == thizActivity)
return My_Fail;
if(My_Pass == activityBufferMalloc())
{
InitActivity(thizActivity, "AboutUsActivity", activityStart, activityInput, activityFresh, activityHide, activityResume, activityDestroy);
return My_Pass;
}
return My_Fail;
}[/mw_shl_code]
3,需要两个任务
任务1:接收屏幕输入数据
[mw_shl_code=c,true]/***************************************************************************************************
*FunctionName:GetLCDInputData
*Description:获取LCD按键输入数据
*Input:None
*Output:None
*Author:xsx,565170595@qq.com
*Data:2016年4月28日17:43:24
***************************************************************************************************/
void GetLCDInputData(void)
{
rxcount = 0;
ReceiveDataFromQueue(GetUsart6RXQueue(), NULL, buf, 100, &rxcount, 1, 10 / portTICK_RATE_MS, 10 / portTICK_RATE_MS);
if(rxcount > 0)
AnalysisCode(buf, rxcount);
}
/***************************************************************************************************
*FunctionName:AnalysisCode
*Description:处理接收的数据
*Input:None
*Output:None
*Author:xsx,565170595@qq.com
*Data:2016年4月29日10:08:39
***************************************************************************************************/
static void AnalysisCode(void * pbuf, unsigned short len)
{
unsigned char * p = (unsigned char *)pbuf;
unsigned short * crc = (unsigned short *)(p+len-2);
unsigned short *tempcrc = (unsigned short *)(p+len);
*tempcrc = CalModbusCRC16Fun1(p+3, len-2-3);
if((p[0] == LCD_Head_1)&&(p[1] == LCD_Head_2)&&(len == (p[2]+3))&&(*crc == *tempcrc))
{
activityInputFunction(pbuf, len);
}
}
void activityInputFunction(unsigned char *pbuf , unsigned short len)
{
Activity * activity = NULL;
if(My_Pass == StackTop(&GB_ActivityLinkStack, &activity))
{
if( (activity) && (activity->pageInput))
activity->pageInput(pbuf, len);
}
}
[/mw_shl_code]
任务2:处理界面刷新
[mw_shl_code=c,true]
static void vSystemUITask( void *pvParameters )
{
static unsigned int count = 0;
while(1)
{
activityFreshFunction();
vTaskDelay(100 / portTICK_RATE_MS);
}
}
void activityFreshFunction(void)
{
Activity * activity = NULL;
if(My_Pass == StackTop(&GB_ActivityLinkStack, &activity))
{
if( (activity) && (activity->pageFresh))
activity->pageFresh();
}
}[/mw_shl_code]
4,启动一个界面:
[mw_shl_code=c,true]/***************************************************************************************************
*FunctionName: startActivity
*Description: 启动一个新的activity
*Input: pageCreate -- activity的创建函数
* pram -- 传入的参数
*Output:
*Return: MyState_TypeDef -- 启动成功与否
*Author: xsx
*Date: 2016年12月20日11:16:59
***************************************************************************************************/
MyState_TypeDef startActivity(MyState_TypeDef (* pageCreate)(Activity * thizactivity, Intent * pram), Intent * pram)
{
Activity * activity = NULL;
if(NULL == pageCreate)
return My_Fail;
//为新页面申请内存
activity = MyMalloc(sizeof(Activity));
if(activity)
{
memset(activity, 0, sizeof(Activity));
activity->pageCreate = (MyState_TypeDef (*)(void * thizactivity, Intent * pram))pageCreate;
//新页面入栈
if(My_Pass == StackPush(&GB_ActivityLinkStack, activity))
{
//执行页面创建,创建成功则显示
if(My_Pass == activity->pageCreate(activity, pram))
{
//新页面显示
if(activity->pageStart)
activity->pageStart();
return My_Pass;
}
//创建失败,则出栈此页面,并销毁
else
StackPop(&GB_ActivityLinkStack, TRUE);
}
//入栈失败,则销毁
MyFree(activity);
}
return My_Fail;
}[/mw_shl_code] |
|