How to use Timers in EmWin ?

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • How to use Timers in EmWin ?

    I want to use Timer but I don't know how to restart it in case of Button click ?

    Source Code

    1. switch (pMsg->MsgId)
    2. {
    3. case WM_INIT_DIALOG:
    4. WM_CreateTimer(hWin, 0, 100, 0);
    5. break;
    6. case WM_TIMER:
    7. if(restart_timer == 1)
    8. WM_RestartTimer(pMsg->Data.v, 100); //TIMER IS NOT ALWAYS RESTARTED IN WM_TIMER CASE
    9. break;
    10. case WM_NOTIFY_PARENT:
    11. Id = WM_GetId(pMsg->hWinSrc);
    12. NCode = pMsg->Data.v;
    13. switch(Id)
    14. {
    15. case ID_BUTTON_0: // Notifications sent by '-'
    16. switch(NCode)
    17. {
    18. case WM_NOTIFICATION_CLICKED:
    19. WM_RestartTimer(pMsg->Data.v, 400); //HOW TO ACCESS / RESTART TIMER IN THIS CASE ???? pMsg->Data.v does not work
    20. break;
    21. }
    22. }
    23. break;
    24. }
    Display All
  • Hello ToneL,

    You need to create the timer using the handle of the client window. When creating a callback function for a dialog, it is actually the callback function of the client window. But pMsg->hWin still contains the handle to the Dialog itself, so it is required to use the function WM_GetClientWindow(). I will check the emWin documentation if this is made clear enough. Here is my code:

    C Source Code

    1. #include "DIALOG.h"
    2. /*********************************************************************
    3. *
    4. * Static data
    5. *
    6. **********************************************************************
    7. */
    8. static GUI_WIDGET_CREATE_INFO _aDialog[] = {
    9. { FRAMEWIN_CreateIndirect, NULL, 0, 10, 10, 100, 80, 0, 0, 0 },
    10. { BUTTON_CreateIndirect, NULL, GUI_ID_BUTTON0, 15, 15, 60, 40, 0, 0, 0 }
    11. };
    12. /*********************************************************************
    13. *
    14. * _cbDialog
    15. */
    16. static void _cbDialog(WM_MESSAGE * pMsg) {
    17. WM_HWIN hWin;
    18. int NCode;
    19. int Id;
    20. hWin = pMsg->hWin;
    21. switch (pMsg->MsgId) {
    22. case WM_INIT_DIALOG:
    23. WM_CreateTimer(WM_GetClientWindow(hWin), 0, 100, 0);
    24. break;
    25. case WM_TIMER:
    26. WM_RestartTimer((WM_HMEM)(pMsg->Data.v), 100);
    27. break;
    28. case WM_NOTIFY_PARENT:
    29. Id = WM_GetId(pMsg->hWinSrc);
    30. NCode = pMsg->Data.v;
    31. switch (Id) {
    32. case GUI_ID_BUTTON0:
    33. switch (NCode) {
    34. case WM_NOTIFICATION_CLICKED:
    35. WM_RestartTimer((WM_HMEM)(pMsg->Data.v), 100);
    36. break;
    37. }
    38. }
    39. break;
    40. default:
    41. WM_DefaultProc(pMsg);
    42. }
    43. }
    44. /*********************************************************************
    45. *
    46. * MainTask
    47. */
    48. void MainTask(void) {
    49. GUI_Init();
    50. GUI_CreateDialogBox(_aDialog, 2, _cbDialog, WM_HBKWIN, 10, 10);
    51. while (1) {
    52. GUI_Delay(100);
    53. }
    54. }
    Display All


    Could you please let me know if this is a help for you?

    Best regards,
    Adrian
  • Thanks Adrian, but your example does not work the way I want.

    Attached is working example, if you comment first line timers does not work any more (copy from your example).

    For timer I use following code now:

    C Source Code

    1. static WM_HTIMER hTimer;
    2. hTimer = WM_CreateTimer(hWin, 0, 100, 0);
    3. WM_RestartTimer(hTimer, 400);


    Code now works but I just want to know if this is correct way or not.

    My second problem:
    Why cursor in edit moves one position to the right first time after value increment.

    C Source Code

    1. #define GUI_TIMER_MODE2 //IF THIS LINE IS UNDER COMMENT TIMER DOES NOT WORK
    2. #include "DIALOG.h"
    3. /*********************************************************************
    4. *
    5. * Defines
    6. *
    7. **********************************************************************
    8. */
    9. #define ID_WINDOW_0 (GUI_ID_USER + 0x06)
    10. #define ID_EDIT_0 (GUI_ID_USER + 0x07)
    11. #define ID_BUTTON_0 (GUI_ID_USER + 0x08)
    12. /*********************************************************************
    13. *
    14. * _aDialogCreate
    15. */
    16. /*********************************************************************
    17. *
    18. * Static data
    19. *
    20. **********************************************************************
    21. */
    22. static GUI_WIDGET_CREATE_INFO _aDialog[] = {
    23. { WINDOW_CreateIndirect, "EditTest", ID_WINDOW_0, 0, 0, 320, 240, 0, 0, 0 },
    24. { EDIT_CreateIndirect, "Edit", ID_EDIT_0, 90, 20, 130, 40, 0, 5, 0 },
    25. { BUTTON_CreateIndirect, "+", ID_BUTTON_0, 180, 180, 80, 40, 0, 0, 0 }
    26. // { BUTTON_CreateIndirect, NULL, GUI_ID_BUTTON0, 100, 100, 60, 40, 0, 0, 0 }
    27. };
    28. /*********************************************************************
    29. *
    30. * _cbDialog
    31. */
    32. static void _cbDialog(WM_MESSAGE * pMsg) {
    33. WM_HWIN hWin;
    34. WM_HWIN hItem;
    35. int NCode;
    36. int Id;
    37. #ifdef GUI_TIMER_MODE2
    38. static WM_HTIMER hTimer;
    39. #endif
    40. hWin = pMsg->hWin;
    41. switch (pMsg->MsgId) {
    42. case WM_INIT_DIALOG:
    43. hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_0);
    44. EDIT_SetText(hItem, "1000");
    45. EDIT_SetTextAlign(hItem, GUI_TA_RIGHT | GUI_TA_VCENTER);
    46. EDIT_SetFont(hItem, GUI_FONT_D24X32);
    47. EDIT_SetFloatMode(hItem, 0 , 0 , 99.99, 2, 2);
    48. EDIT_SetCursorAtChar(hItem , 3);
    49. hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0);
    50. BUTTON_SetFont(hItem, GUI_FONT_24B_ASCII);
    51. BUTTON_SetFocussable(hItem,0);
    52. #ifdef GUI_TIMER_MODE2
    53. hTimer = WM_CreateTimer(hWin, 0, 100, 0);
    54. #else
    55. WM_CreateTimer(WM_GetClientWindow(hWin), 0, 100, 0);
    56. #endif
    57. break;
    58. case WM_TIMER:
    59. hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0);
    60. if(BUTTON_IsPressed(hItem))
    61. {
    62. GUI_SendKeyMsg(GUI_KEY_UP, 1); // Send a key message to the focussed window
    63. #ifdef GUI_TIMER_MODE2
    64. WM_RestartTimer(hTimer, 50);
    65. #else
    66. WM_RestartTimer((WM_HMEM)(pMsg->Data.v), 50);
    67. #endif
    68. }
    69. break;
    70. case WM_NOTIFY_PARENT:
    71. Id = WM_GetId(pMsg->hWinSrc);
    72. NCode = pMsg->Data.v;
    73. switch (Id) {
    74. case ID_BUTTON_0:
    75. switch (NCode) {
    76. case WM_NOTIFICATION_CLICKED:
    77. #ifdef GUI_TIMER_MODE2
    78. WM_RestartTimer(hTimer, 400);
    79. #else
    80. WM_RestartTimer((WM_HMEM)(pMsg->Data.v), 400);
    81. #endif
    82. GUI_SendKeyMsg(GUI_KEY_UP, 1); // Send a key message to the focussed window
    83. break;
    84. }
    85. }
    86. break;
    87. default:
    88. WM_DefaultProc(pMsg);
    89. }
    90. }
    91. /*********************************************************************
    92. *
    93. * MainTask
    94. */
    95. void MainTask(void) {
    96. GUI_Init();
    97. GUI_CreateDialogBox(_aDialog, 3, _cbDialog, WM_HBKWIN, 10, 10);
    98. while (1) {
    99. GUI_Exec();
    100. }
    101. }
    Display All
  • Hello Tone L,

    Yes, this works correct. pMsg->Data.v contains a handle the expired timer only if the message WM_TIMER is currently processed. So, if a timer needs to be restarted from within a click event (pMsg->MsgId == WM_NOTIFY_PARENT) a static variable should be used to store the timer handle at creation.

    Thank you for the hint regarding the change of cursor position in the EDIT widget. Moving the cursor should only occur if signs are displayed. This is intended to have the cursor remain on the same position when 0 is incremented/decremented or when 0 is reached. Since you do not use the EDIT widget to display signed values, this should not occur. If you purchased a source code license directly from SEGGER, may I ask you to write an e-mail to support@segger.com? I will then update you with an updated version of the EDIT widget. Otherwise I will have to ask you to wait until the next emWin object code version is released.

    As a work around you can just update the position of the cursor manually, of course.

    If you have any further questions, please let me know.

    Best regards,
    Adrian