emWin on Arduino

From SEGGER Wiki
Jump to: navigation, search

emWin is available for Arduino and can be downloaded directly via the Arduino IDE. This Wiki page explains how to get started.

Requirements

To run emWin on Arduino, you will need the following components.

Hardware

  • Arduino GIGA R1
  • Arduino GIGA Display Shield

Software

  • Arduino IDE
  • Board packages for Arduino Mbed OS GIGA board

Follow Arduino's instructions to set up the hard- and software. Once everything is working, you can start integrating emWin.

Installing the emWin Library

To install the emWin library package, open the library manager in your Arduino IDE and type 'emWin' into the search box.

Select the most recent version of emWin and click on "Install".

The emWin library also requires the 'Arduino_GigaDisplayTouch' library to be installed.

There is also a repository available on GitHub.

Getting started

To get started with emWin, simply include "DIALOG.h" which makes any widget as well as the Window Manager and core functionality of emWin available.

After that, call GUI_Init() in setup() and add GUI_Exec() to the loop() function. The GUI_Exec() is required to keep your emWin application 'alive'. It will process any touch input and takes care of windows to be redrawn.

This might look like below:

#include <DIALOG.h>

void setup() {
  GUI_Init();
}

void loop() {
  // put your main code here, to run repeatedly:
  GUI_Exec();  // Keep emWin alive, handle touch and other stuff
}

To show something on the display, you can simply call the emWin API directly. For example, clear the entire screen with a color and display string on it.

Please note that it is mandatory to start and end multi buffering before the first and last drawing operation. This is only required if you are not using the Window Manager and/or Widgets. If you are using the Window manager, a call of WM_MULTIBUF_Enable(1) is sufficient.

Here are two examples:

Without the Window Manager:

#include <DIALOG.h>

void setup() {
  GUI_Init();
  GUI_MULTIBUF_Begin();
  GUI_SetBkColor(GUI_GREEN);
  GUI_Clear();
  GUI_SetColor(GUI_RED);
  GUI_DispString("Hello world");
  GUI_MULTIBUF_End();
}

void loop() {
  GUI_Exec();
}

And one example with the Window Manager:

#include "DIALOG.h"

static void _cbWin(WM_MESSAGE * pMsg) {
  switch (pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_GREEN);
    GUI_Clear();
    GUI_SetColor(GUI_RED);
    GUI_DispString("Hello world");
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

void setup() {
  GUI_Init();
  WM_MULTIBUF_Enable(1);
  WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);
}

void loop() {
  GUI_Exec();
}

Although, it might look more complex to use the Window Manager, it will help to keep your GUI application structured. This will come in handy, especially when your application grows and becomes more sophisticated.

Running emWin examples

emWin examples from Arduino IDE

The emWin library comes with a couple of ready to use examples which can be loaded directly via the Arduino IDE. These examples can be access through the menu by clicking 'File -> Examples -> emWin'.

emWin examples from our website

It is possible to port most emWin examples from our website to your Arduino GIGA R1 project. Please note, that some examples are simply not made for the screen dimensions of the Arduino GIGA Display Shield and might not work as expected.

To get hands on emWin examples, you might either refer to our examples website or take a look into our Wiki.

Most emWin examples are independent of the display dimensions and can be ported into your Arduino project.

emWin examples typically start in a function named MainTask(). In most cases, emWin and the application will be initialized in MainTask() and will end in an endless loop, which keeps emWin "alive".

To port the example, simply copy the content of MainTask() into the setup() function (except the while(1)-loop) and copy the content of the while(1)-loop into the loop() function. You might want to replace GUI_Delay() by GUI_Exec() which is basically the same but without performing a delay.

Of course, you have to copy the rest of the example content into your .ino file, too.

To get an idea on how this can be done, you can compare the "CreateWindow" example from the emWin Arduino library with the WM_CreateWindows example from our Wiki.