📄 mt_multitasking.c
字号:
/*
*********************************************************************************************************
* 礐/GUI
* Universal graphic software for embedded applications
*
* (c) Copyright 2002, Micrium Inc., Weston, FL
* (c) Copyright 2000, SEGGER Microcontroller Systeme GmbH
*
* 礐/GUI is protected by international copyright laws. Knowledge of the
* source code may not be used to write a similar product. This file may
* only be used in accordance with a license and should not be redistributed
* in any way. We appreciate your understanding and fairness.
*
* File : MT_MultiTasking.c
* Purpose : 礐/GUI sample code demonstrates MultiTasking capabilities
*********************************************************************************************************
*/
#include "GUI.h"
#include "FrameWin.h"
#include <stddef.h>
/*******************************************************************
*
* Define how to create a task and start multitasking
*
********************************************************************
If not using embOS you have to change the hardware
dependent macros to work with your OS
*/
#ifndef WIN32
/* Definitions for using embOS */
#include "RTOS.h"
#define CREATE_TASK(pTCB, pName, pFunc, Priority, pStack) \
OS_CREATETASK(pTCB, pName, pFunc, Priority, pStack)
#define START_MT() \
OS_Start()
#else
/* Definitions for using the simulation */
#include "SIM.h"
#define CREATE_TASK(pTCB, pName, pFunc, Priority, pStack) \
SIM_CreateTask(pName, pFunc, Priority)
#define START_MT() \
SIM_Start()
#endif
/*******************************************************************
*
* Callback routine of child window from task 0
*
********************************************************************
*/
static int XPos;
static const char aText[] = {"Moving text..."};
static WM_RESULT cbCallbackT0(WM_MESSAGE * pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT:
/* Handle the paint message */
GUI_SetBkColor(GUI_RED);
GUI_SetColor(GUI_BLACK);
GUI_SetFont(&GUI_FontComic24B_ASCII);
GUI_Clear();
GUI_DispStringAt(aText, XPos, 0);
break;
}
}
/*******************************************************************
*
* Task 0: Moves the text in the client window
*
********************************************************************
*/
static void Task_0(void) {
/* Create frame window */
FRAMEWIN_Handle hFrameWin = FRAMEWIN_Create("Task 0",
NULL, WM_CF_SHOW | WM_CF_STAYONTOP,
0, 40, 200, 40);
/* Create child window */
WM_HWIN hChildWin = WM_CreateWindowAsChild(0, 0, 0, 0, hFrameWin,
WM_CF_SHOW | WM_CF_STAYONTOP | WM_CF_MEMDEV,
cbCallbackT0, 0);
FRAMEWIN_SetActive(hFrameWin, 0);
/* Keep sure the right window is active... */
WM_SelectWindow(hChildWin);
/* ...and the right font is selected */
GUI_SetFont(&GUI_FontComic24B_ASCII);
while(1) {
GUI_RECT Rect;
/* Get the length of the string */
int XLen = GUI_GetStringDistX(aText);
/* Get the size of the window */
WM_GetClientRect(&Rect);
/* Show moving text */
for (XPos = 0; XPos < (Rect.x1 - Rect.x0) - XLen; XPos++) {
WM_InvalidateWindow(hChildWin);
GUI_Delay(40);
}
for (; XPos >= 0; XPos--) {
WM_InvalidateWindow(hChildWin);
GUI_Delay(40);
}
}
}
/*******************************************************************
*
* Callback routine of child window from task 1
*
********************************************************************
*/
static WM_RESULT cbCallbackT1(WM_MESSAGE * pMsg) {
WM_HWIN hWin = (FRAMEWIN_Handle)(pMsg->hWin);
switch (pMsg->MsgId) {
case WM_PAINT:
/* Handle the paint message */
GUI_SetBkColor(GUI_BLUE);
GUI_SetColor(GUI_WHITE);
GUI_SetFont(&GUI_FontComic24B_ASCII);
GUI_SetTextAlign(GUI_TA_HCENTER | GUI_TA_VCENTER);
GUI_Clear();
GUI_DispStringHCenterAt("Moving window...",
WM_GetWindowSizeX(hWin) / 2,
WM_GetWindowSizeY(hWin) / 2);
break;
}
}
/*******************************************************************
*
* Task 1: Moves frame- and client window continously
*
********************************************************************
*/
static void Task_1(void) {
/* Create frame window */
FRAMEWIN_Handle hFrameWin = FRAMEWIN_Create("Task 1",
NULL, WM_CF_SHOW | WM_CF_STAYONTOP,
20, 170, 200, 40);
/* Create child window */
WM_HWIN hChildWin = WM_CreateWindowAsChild(0, 0, 0, 0, hFrameWin,
WM_CF_SHOW | WM_CF_STAYONTOP,
cbCallbackT1, 0);
FRAMEWIN_SetActive(hFrameWin, 0);
while(1) {
int i;
int nx = 80;
int ny = 160;
/* Move window continously */
for (i = 0; i < ny; i++) {
WM_MoveWindow(hFrameWin, 0, -1);
WM_MoveWindow(hChildWin, 0, -1);
GUI_Delay(10);
}
for (i = 0; i < nx; i++) {
WM_MoveWindow(hFrameWin, 1, 0);
WM_MoveWindow(hChildWin, 1, 0);
GUI_Delay(10);
}
for (i = 0; i < ny; i++) {
WM_MoveWindow(hFrameWin, 0, 1);
WM_MoveWindow(hChildWin, 0, 1);
GUI_Delay(10);
}
for (i = 0; i < nx; i++) {
WM_MoveWindow(hFrameWin, -1, 0);
WM_MoveWindow(hChildWin, -1, 0);
GUI_Delay(10);
}
}
}
/*******************************************************************
*
* Callback routine of background window
*
********************************************************************
*/
static WM_RESULT cbBackgroundWin(WM_MESSAGE* pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT:
/* Handle only the paint message */
GUI_Clear();
GUI_SetFont(&GUI_Font13HB_1);
GUI_DispStringHCenterAt("礐/GUI - multitasking demo\n", 160, 0);
GUI_SetFont(&GUI_Font13_1);
GUI_DispStringHCenterAt("Scrolling text and moving windows without flickering",
160, GUI_GetDispPosY());
}
}
/*******************************************************************
*
* main
*
********************************************************************
*/
#ifndef WIN32
/* Stacks */
static OS_STACKPTR int Stack_0[600];
static OS_STACKPTR int Stack_1[600];
/* Task controll blocks */
static OS_TASK TCB_0;
static OS_TASK TCB_1;
#endif
void main(void) {
#ifndef WIN32
/* Init hardware */
OS_InitKern();
OS_InitHW();
#endif
/* Init GUI */
GUI_Init();
/* Call GUI_MEMDEV_Load */
GUI_MEMDEV_Load();
/* Set callback for background window */
WM_SetCallback(WM_HBKWIN, cbBackgroundWin);
/* Invalidate background window */
WM_InvalidateWindow(WM_HBKWIN);
/* Create tasks */
CREATE_TASK(&TCB_0, "Task_0", Task_0, 100, Stack_0);
CREATE_TASK(&TCB_1, "Task_1", Task_1, 50, Stack_1);
/* Start multitasking */
START_MT();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -