WM custom message (Sample)

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

This sample shows how a custom message can be sent from a window 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        : WM_CustomMessage.c
Purpose     : Sample that demonstrates how to send custom messages
              between windows.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

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

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

#define MSG_CUSTOM  (WM_USER + 0x01)

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

/*********************************************************************
*
*       _cbWindowReceive
*/
static void _cbWindowReceive(WM_MESSAGE * pMsg) {
  char       acBuffer[64];
  int        Message;
  static int MessageFlag;
  static int Value;

  switch (pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();

    if (MessageFlag == 0) {
      sprintf(acBuffer, "Message received with data value %d", Value);
      GUI_DispString(acBuffer);
    } else if (MessageFlag == 1) {
      GUI_DispString("Message received without parameters.");
    }
    break;
  case MSG_CUSTOM:
    if (pMsg->Data.v) {
      MessageFlag ^= 1;
      Value        = pMsg->Data.v;
    } else {
      MessageFlag ^= 1;
    }
    WM_InvalidateWindow(pMsg->hWin);
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  WM_MESSAGE     Message;
  static WM_HWIN hWinReceive;
  static int     Msg;

  GUI_Init();
  //
  // Create a window to received the message
  //
  hWinReceive = WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWindowReceive, 0);

  while (1) {
    if (Msg) {
      //
      // Fill message struct with data needed for the message
      //
      Message.hWin    = hWinReceive;   // Window to receive the message
      Message.hWinSrc = WM_HBKWIN;     // Source window sending the message
      Message.MsgId   = MSG_CUSTOM;    // Message ID
      Message.Data.v  = GUI_GetTime(); // Custom data value
      WM_SendMessage(hWinReceive, &Message);
      //
      // Toggle flag
      //
      Msg ^= 1;
    } else {
      WM_SendMessageNoPara(hWinReceive, MSG_CUSTOM);
      //
      // Toggle flag
      //
      Msg ^= 1;
    }

    GUI_Delay(1000);
  }
}

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