WM Timer (Sample)

From SEGGER Wiki
Jump to: navigation, search
WM_SimpleTimer.c
WM SetUserData.gif
File(s) required
  • WM_SimpleTimer.c
Runs in simulation Yes
Runs on target Yes
Download WM_SimpleTimer.c

This sample demonstrates the use of Window Manager timers.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin 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  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : WM_SimpleTimer.c
Purpose     : Sample that demonstrates how to use timers with the
              WindowManager in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdio.h>

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

#define ID_TIMER 0
#define PERIOD   500

/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  int              Id;
  char             acString[64];
  static WM_HTIMER hTimer; // Timer handle has to be static
  static int       Counter;

  switch (pMsg->MsgId) {
  case WM_CREATE:
    //
    // Create timer handle
    //
    //   Mode (unused, should be 0) ----------------------+
    //   Duration ---------------------------------+      |
    //   User-defined ID -----------------+        |      |
    //   Window to be informed -+         |        |      |
    //                          |         |        |      |
    hTimer = WM_CreateTimer(pMsg->hWin, ID_TIMER, PERIOD, 0);
    break;
  case WM_PAINT:
    //
    // This case is called everytime the window has to be redrawn
    //
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    GUI_SetColor(GUI_BLACK);
    sprintf(acString, "The timer has expired %d times.", Counter);
    GUI_DispStringAt(acString, 10, 10);
    //
    if (hTimer == 0) {
      GUI_DispStringAt("The timer has been deleted.", 10, 30);
    }
    break;
  case WM_TIMER:                      // When a timer expires, a WM_TIMER message is sent to the window
    Id = WM_GetTimerId(pMsg->Data.v); // The handle of the timer that expired is sent via the data value of the message.
    switch (Id) {
    case ID_TIMER:
      Counter++;
      if (Counter < 10) {
        //
        // Restart the timer
        //
        WM_RestartTimer(hTimer, PERIOD);
      }
      else {
        //
        // Delete the timer
        //
        WM_DeleteTimer(hTimer);
        hTimer = 0;
      }
      WM_InvalidateWindow(pMsg->hWin);
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  GUI_Init();

  //
  // Create a window
  //
  WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/