Andrew Reed / Mbed OS CITY1082-i2c_master_wifi_mqtt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MT_MultiTasking.c Source File

MT_MultiTasking.c

00001 /*********************************************************************
00002 *                SEGGER Microcontroller GmbH                         *
00003 *        Solutions for real time microcontroller applications        *
00004 **********************************************************************
00005 *                                                                    *
00006 *        (c) 1996 - 2018  SEGGER Microcontroller GmbH                *
00007 *                                                                    *
00008 *        Internet: www.segger.com    Support:  support@segger.com    *
00009 *                                                                    *
00010 **********************************************************************
00011 
00012 ** emWin V5.48 - Graphical user interface for embedded applications **
00013 All  Intellectual Property rights  in the Software belongs to  SEGGER.
00014 emWin is protected by  international copyright laws.  Knowledge of the
00015 source code may not be used to write a similar product.  This file may
00016 only be used in accordance with the following terms:
00017 
00018 The software  has been licensed to  Cypress Semiconductor Corporation,
00019 whose registered  office is situated  at 198 Champion Ct. San Jose, CA 
00020 95134 USA  solely for the  purposes of creating  libraries for Cypress
00021 PSoC3 and  PSoC5 processor-based devices,  sublicensed and distributed
00022 under  the  terms  and  conditions  of  the  Cypress  End User License
00023 Agreement.
00024 Full source code is available at: www.segger.com
00025 
00026 We appreciate your understanding and fairness.
00027 ----------------------------------------------------------------------
00028 Licensing information
00029 Licensor:                 SEGGER Microcontroller Systems LLC
00030 Licensed to:              Cypress Semiconductor Corp, 198 Champion Ct., San Jose, CA 95134, USA
00031 Licensed SEGGER software: emWin
00032 License number:           GUI-00319
00033 License model:            Services and License Agreement, signed June 10th, 2009
00034 Licensed platform:        Any Cypress platform (Initial targets are: PSoC3, PSoC5)
00035 ----------------------------------------------------------------------
00036 Support and Update Agreement (SUA)
00037 SUA period:               2009-06-12 - 2022-07-27
00038 Contact to extend SUA:    sales@segger.com
00039 ----------------------------------------------------------------------
00040 File        : MT_MultiTasking.c
00041 Purpose     : Example demonstrates MultiTasking capabilities of emWin
00042 Requirements: WindowManager - (x)
00043               MemoryDevices - (x)
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 
00049               Requires either a MS Windows environment or embOS.
00050               If not using embOS you have to change the hardware
00051               dependent macros to work with your OS.
00052 ----------------------------------------------------------------------
00053 */
00054 
00055 #ifndef SKIP_TEST
00056 #include <stddef.h>
00057 #include "GUI.h"
00058 #include "FRAMEWIN.h"
00059 
00060 #if GUI_OS == 0
00061   #error Multitasking sample requires task awareness (#define GUI_OS 1)
00062 #endif
00063 
00064 /*******************************************************************
00065 *
00066 *       Define how to create a task and start multitasking
00067 *
00068 ********************************************************************
00069 */
00070 #ifndef WIN32 
00071   #include "RTOS.h"    /* Definitions for embOS */
00072   #define CREATE_TASK(pTCB, pName, pFunc, Priority, pStack)  OS_CREATETASK(pTCB, pName, pFunc, Priority, pStack)
00073   #define START_MT()  OS_Terminate(0)
00074   #define Delay(t)    OS_Delay(t)
00075 #else
00076   #include "SIM.h"     /* Definitions for the Win32 simulation */
00077   #define CREATE_TASK(pTCB, pName, pFunc, Priority, pStack)   SIM_CreateTask(pName, pFunc)
00078   #define START_MT()  SIM_Start()
00079   #define Delay(t)    SIM_Delay(t)
00080 #endif
00081 
00082 //
00083 // Recommended memory to run the sample with adequate performance
00084 //
00085 #define RECOMMENDED_MEMORY (1024L * 20)
00086 
00087 /*******************************************************************
00088 *
00089 *       Static data
00090 *
00091 ********************************************************************
00092 */
00093 #ifndef WIN32
00094   //
00095   // Stacks
00096   //
00097   static OS_STACKPTR int Stack_0[600];
00098   static OS_STACKPTR int Stack_1[600];
00099   static OS_STACKPTR int Stack_2[600];
00100   static OS_TASK aTCB[3];               // Task control blocks
00101 #endif
00102 
00103 /*******************************************************************
00104 *
00105 *       Static code
00106 *
00107 ********************************************************************
00108 */
00109 /*******************************************************************
00110 *
00111 *       _cbCallbackT0
00112 */
00113 static int XPos;
00114 static const char aText[] = "Moving text...";
00115 
00116 static void _cbCallbackT0(WM_MESSAGE * pMsg) {
00117   switch (pMsg->MsgId) {
00118   case WM_PAINT:
00119     //
00120     // Handle the paint message
00121     //
00122     GUI_SetBkColor(GUI_MAKE_COLOR(0x4040FF));
00123     GUI_SetColor(GUI_BLACK);
00124     GUI_SetFont(&GUI_FontComic24B_ASCII);
00125     GUI_Clear();
00126     GUI_DispStringAt(aText, XPos, 0);
00127     break;
00128   default:
00129     WM_DefaultProc(pMsg);
00130   }
00131 }
00132 
00133 /*******************************************************************
00134 *
00135 *       _cbCallbackT1
00136 */
00137 static void _cbCallbackT1(WM_MESSAGE * pMsg) {
00138   WM_HWIN hWin = (FRAMEWIN_Handle)(pMsg->hWin);
00139   switch (pMsg->MsgId) {
00140   case WM_PAINT:
00141     //
00142     // Handle the paint message
00143     //
00144     GUI_SetBkColor(GUI_MAKE_COLOR(0xA02020));
00145     GUI_SetColor(GUI_WHITE);
00146     GUI_SetFont(&GUI_FontComic24B_ASCII);
00147     GUI_SetTextAlign(GUI_TA_HCENTER | GUI_TA_VCENTER);
00148     GUI_Clear();
00149     GUI_DispStringHCenterAt("Moving window...", 
00150                             WM_GetWindowSizeX(hWin) / 2, 
00151                             WM_GetWindowSizeY(hWin) / 2);
00152     break;
00153   default:
00154     WM_DefaultProc(pMsg);
00155   }
00156 }
00157 
00158 /*******************************************************************
00159 *
00160 *       _cbBackgroundWin
00161 */
00162 static void _cbBackgroundWin(WM_MESSAGE* pMsg) {
00163   switch (pMsg->MsgId) {
00164   case WM_PAINT:
00165     //
00166     // Handle only the paint message
00167     //
00168     GUI_SetBkColor(GUI_MAKE_COLOR(0x00CC00));
00169     GUI_Clear();
00170     GUI_SetFont(&GUI_Font24_ASCII);
00171     GUI_DispStringHCenterAt("emWin - multitasking demo\n", 160, 5);
00172     GUI_SetFont(&GUI_Font13_1);
00173     GUI_DispStringAt("Scrolling text and moving windows without flickering", 5, 35);
00174   default:
00175     WM_DefaultProc(pMsg);
00176   }
00177 }
00178 
00179 /*******************************************************************
00180 *
00181 *       _Task_0
00182 */
00183 static void _Task_0(void) {
00184   FRAMEWIN_Handle hFrameWin;
00185   WM_HWIN         hChildWin;
00186   //
00187   // Create frame window
00188   //
00189   hFrameWin = FRAMEWIN_Create("Task 0",  NULL, WM_CF_SHOW | WM_CF_STAYONTOP,
00190                               0, 70, 200, 50);
00191   //
00192   // Create child window
00193   //
00194   hChildWin = WM_CreateWindowAsChild(0, 0, 0, 0, WM_GetClientWindow(hFrameWin),
00195                                      WM_CF_SHOW | WM_CF_MEMDEV,
00196                                      _cbCallbackT0, 0);
00197   FRAMEWIN_SetActive(hFrameWin, 0);
00198   //
00199   // Make sure the right window is active...
00200   //
00201   WM_SelectWindow(hChildWin);
00202   //
00203   // ...and the right font is selected
00204   //
00205   GUI_SetFont(&GUI_FontComic24B_ASCII);
00206   while(1) {
00207     GUI_RECT Rect;
00208     int XLen;
00209     
00210     XLen = GUI_GetStringDistX(aText);       // Get the length of the string
00211     WM_GetClientRect(&Rect);                // Get the size of the window
00212     //
00213     // Show moving text
00214     //
00215     for (XPos = 0; XPos < (Rect.x1 - Rect.x0) - XLen; XPos++) {
00216       WM_InvalidateWindow(hChildWin);
00217       Delay(50);
00218     }
00219     for (; XPos >= 0; XPos--) {
00220       WM_InvalidateWindow(hChildWin);
00221       Delay(100);
00222     }
00223   }
00224 }
00225 
00226 /*******************************************************************
00227 *
00228 *       _Task_1
00229 */
00230 static void _Task_1(void) {
00231   FRAMEWIN_Handle hFrameWin;
00232   WM_HWIN         hChildWin;
00233 
00234   //
00235   // Create frame window
00236   //
00237   hFrameWin = FRAMEWIN_Create("Task 1", NULL, WM_CF_SHOW | WM_CF_STAYONTOP,
00238                               20, 170, 200, 50);
00239   //
00240   // Create child window
00241   //
00242   hChildWin = WM_CreateWindowAsChild(0, 0, 0, 0, WM_GetClientWindow(hFrameWin),
00243                                      WM_CF_SHOW | WM_CF_MEMDEV,
00244                                      _cbCallbackT1, 0);
00245   FRAMEWIN_SetActive(hFrameWin, 0);
00246   while(1) {
00247     int i;
00248     int nx = 80;
00249     int ny = 90;
00250     //
00251     // Move window continously
00252     //
00253     for (i = 0; i < ny; i++) {
00254       WM_MoveWindow(hFrameWin, 0, -2);
00255       Delay(50);
00256     }
00257     for (i = 0; i < nx; i++) {
00258       WM_MoveWindow(hFrameWin, 2, 0);
00259       Delay(50);
00260     }
00261     for (i = 0; i < ny; i++) {
00262       WM_MoveWindow(hFrameWin, 0, 2);
00263       Delay(50);
00264     }
00265     for (i = 0; i < nx; i++) {
00266       WM_MoveWindow(hFrameWin, -2, 0);
00267       Delay(50);
00268     }
00269   }
00270 }
00271 
00272 /*******************************************************************
00273 *
00274 *       _GUI_Task
00275 *
00276 * This task does the background processing.
00277 * The MainTask job is to update invalid windows, but other things such as
00278 * evaluating mouse or touch input may also be done.
00279 */
00280 static void _GUI_Task(void) {
00281   while(1) {
00282     GUI_Exec();           // Do the background work ... Update windows etc.)
00283     GUI_X_ExecIdle();     // Nothing left to do for the moment ... Idle processing
00284   }
00285 }
00286 
00287 
00288 /*********************************************************************
00289 *
00290 *       Public code
00291 *
00292 **********************************************************************
00293 */
00294 /*********************************************************************
00295 *
00296 *       MainTask
00297 */
00298 void MainTask(void) {
00299   //
00300   // Init GUI
00301   //
00302   WM_SetCreateFlags(WM_CF_MEMDEV);              // Use memory devices on all windows to avoid flicker
00303   GUI_Init();
00304   //
00305   // Check if recommended memory for the sample is available
00306   //
00307   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00308     GUI_ErrorOut("Not enough memory available."); 
00309     return;
00310   }
00311   WM_SetCallback(WM_HBKWIN, _cbBackgroundWin);  // Set callback for background window
00312   //
00313   // Create tasks
00314   //
00315   CREATE_TASK(&aTCB[0], "Task_0",   _Task_0,   80, Stack_0);
00316   CREATE_TASK(&aTCB[1], "Task_1",   _Task_1,   60, Stack_1);
00317   CREATE_TASK(&aTCB[2], "GUI_TASK", _GUI_Task,  1, Stack_2);
00318   //
00319   // Start multitasking
00320   //
00321   START_MT();
00322 }
00323 
00324 #endif
00325 
00326 /*************************** End of file ****************************/