Timers (Sample)

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

This sample demonstrates how to use timers without the use of the WindowManager in emWin.

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        : GUI_Timers.c
Purpose     : Sample that demonstrates how to use timers without the
              use of the WindowManager in emWin.
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

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

/*********************************************************************
*
*       Types
*
**********************************************************************
*/
//
// Define struct to send a pointer with the timer.
// This is optional, since any type of pointer can be sent via the timer.
//
typedef struct {
  char Message[16];
} CUSTOM_TIMER_DATA;

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

#define TIME 3000

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

/*********************************************************************
*
*       _cbTimer
*/
static void _cbTimer(GUI_TIMER_MESSAGE * pTM) {
  WM_HWIN             hWin;
  CUSTOM_TIMER_DATA * pContext;
  char                acBuffer[128];

  //
  // Retrieve user context
  //
  pContext = (CUSTOM_TIMER_DATA *)pTM->Context;
  //
  // Do something when timer is expired...
  // pTM->Time contains the time value when the timer expired
  //
  sprintf(acBuffer, "The timer has expired at %dms.\nThe message contains: %s", pTM->Time, pContext->Message);
  GUI_DispString(acBuffer);
  //
  // The timer can also be restarted using these routines:
  // --> GUI_TIMER_SetPeriod(pTM->hTimer, TIME);
  // --> GUI_TIMER_Restart(pTM->hTimer);
  //

  //
  // Delete timer handle
  //
  GUI_TIMER_Delete(pTM->hTimer);
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  GUI_TIMER_HANDLE         hTimer;
  static CUSTOM_TIMER_DATA Data;

  //
  // Init GUI.
  //
  GUI_Init();
  //
  // Init user context
  //
  memcpy(Data.Message, "Hello!", 16);
  //
  // Create timer handle
  //
  //   - Mode (unused, should be 0) ------------------------------------------+
  //   - User context (can be used for numbers or any pointer addresses) -+   |
  //   - System time when the timer should expire ---+                    |   |
  //   - Callback that will                          |                    |   |
  //     be called when the timer expires ---+       |                    |   |
  //                          +--------------+       |                    |   |
  //                          |         +------------+                    |   |
  //                          |         |    +----------------------------+   |
  //                          |         |    |     +--------------------------+
  //                          |         |    |     |
  hTimer = GUI_TIMER_Create(_cbTimer, TIME, &Data, 0);
  GUI_DispString("Timer started...\n");

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

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