rt-thread移植emwin

GUI_X_RTTHREAD.C

#include "GUI.h"

/*
*********************************************************************************************************
*                                         GLOBAL VARIABLES
*********************************************************************************************************
*/


//static int		KeyPressed;
//static char		KeyIsInited;

static rt_sem_t DispSem;    	//显示的信号量
static rt_sem_t EventSem;
static rt_sem_t KeySem;     //按键信号量
static int		KeyPressed;
static char		KeyIsInited;
/*
*********************************************************************************************************
*                                        TIMING FUNCTIONS
*
* Notes: Some timing dependent routines of uC/GUI require a GetTime and delay funtion. 
*        Default time unit (tick), normally is 1 ms.
*********************************************************************************************************
*/
int GUI_X_GetTime(void) { 
  //获取系统时间,此处时间单元为1ms
	return rt_tick_get();
}

//GUI延时函数
void GUI_X_Delay(int period) { 
	
	rt_thread_delay(period);
}

/*
*********************************************************************************************************
*                                          GUI_X_ExecIdle()
*********************************************************************************************************
*/
void GUI_X_ExecIdle(void)
{
	
	GUI_X_Delay(1);
	
}

/*
*********************************************************************************************************
*                                    MULTITASKING INTERFACE FUNCTIONS
*
* Note(1): 1) The following routines are required only if uC/GUI is used in a true multi task environment, 
*             which means you have more than one thread using the uC/GUI API.  In this case the #define 
*             GUI_OS 1   needs to be in GUIConf.h
*********************************************************************************************************
*/
void GUI_X_InitOS(void)
{
	//创建初始值为1的信号量,用于共享资源	
	DispSem = rt_sem_create("Disp_SEM",1,RT_IPC_FLAG_FIFO);
	//创建初始值为1的信号量,用于事件触发
	EventSem = rt_sem_create("Event_SEM",0,RT_IPC_FLAG_FIFO);
}

//等待信号量
void GUI_X_Lock(void)
{
	//请求信号量
	rt_sem_take(DispSem,0);
}

void GUI_X_Unlock(void)
{
	//释放信号量
	rt_sem_release(DispSem);
}
//放回任务ID,此处返回的是任务优先级,
//轮转调度,因此如果使用了时间片轮转调度功能的话有可能会出错!
U32 GUI_X_GetTaskId(void)
{
	//获取任务优先级,也就是任务ID
	return (U32)rt_thread_self();
}

/*
*********************************************************************************************************
*                                        GUI_X_WaitEvent()
*                                        GUI_X_SignalEvent()
*********************************************************************************************************
*/

void GUI_X_WaitEvent(void)
{
	//请求信号量
	rt_sem_take(EventSem,0);
}

void GUI_X_SignalEvent(void)
{
	//释放信号量
	rt_sem_release(EventSem);
}
/*
*********************************************************************************************************
*                                      KEYBOARD INTERFACE FUNCTIONS
*
* Purpose: The keyboard routines are required only by some widgets.
*          If widgets are not used, they may be eliminated.
*
* Note(s): If uC/OS-II is used, characters typed into the log window will be placed	in the keyboard buffer. 
*          This is a neat feature which allows you to operate your target system without having to use or 
*          even to have a keyboard connected to it. (useful for demos !)
*********************************************************************************************************
*/

static void CheckInit(void)
{
	if(KeyIsInited == (0u)){
		KeyIsInited = (1u);
		GUI_X_Init();
	}
}

void GUI_X_Init(void)
{
	KeySem = rt_sem_create("Key_SEM",0,RT_IPC_FLAG_FIFO);
}

int GUI_X_GetKey(void) 
{
	int r;
	
	r = KeyPressed;
	CheckInit();
	KeyPressed = 0;
	return (r);
}

int GUI_X_WaitKey(void)
{
	int r;
	
	CheckInit();
	if(KeyPressed == 0)
	{
		rt_sem_take(KeySem,0); 	//请求信号量
	}
	r = KeyPressed;
	KeyPressed = 0;
	return (r);
}

void GUI_X_StoreKey(int k)
{
	KeyPressed = k;
	rt_sem_release(KeySem);			//发送信号量
}

/*********************************************************************
*
*      Logging: OS dependent

Note:
  Logging is used in higher debug levels only. The typical target
  build does not use logging and does therefor not require any of
  the logging routines below. For a release build without logging
  the routines below may be eliminated to save some space.
  (If the linker is not function aware and eliminates unreferenced
  functions automatically)

*/

void GUI_X_Log     (const char *s) { GUI_USE_PARA(s); }
void GUI_X_Warn    (const char *s) { GUI_USE_PARA(s); }
void GUI_X_ErrorOut(const char *s) { GUI_USE_PARA(s); }

你可能感兴趣的:(STM32,rtthread)