Ensemble Graphics Toolkit: Timers
Introduction
In this training, you will learn how to construct a Periodic and One-Shot Timer. As a bonus, you will also learn how to display the CPU usage on the WVGA Display.
Steps
- Create a New Source File
- Periodic Timer
- One-Shot Timer
Prerequisites
You have prepared the Host PC with all the development software tools and Ensemble Graphics Toolkit source code as explained in:
You have installed and prepared the Eclipse IDE for C/C++ Developers as explained in:
Create a New Source File
In this section, you will be creating a new timer.cpp source file that you will build in this and later training, demonstrating the following:
In this training, you will use EgtProject and the settings you entered in the Ensemble Graphics Toolkit -- First Application using Eclipse IDE training.
Start the Eclipse IDE.
Eclipse will ask you to select a directory for your workspace. Accept the default directory and click on the Launch button.
Eclipse will launch the previous workspace you configured.
You will be adding a new source file with the name: timer.cpp. If you have a *.cpp source file from a previous Ensemble Graphics Toolkit training, perform the following steps:
The Exclude from build window will open:
The selected *.cpp will be excluded from the build.
Right-click on EgtProject and select New > Source File.
The New Source File window will open:
Enter "timer.cpp" into the Source file: text box. Click on the Finish button.
A new source file tab timers.cpp is created within the EgtProject.
Enter the following source code to the timers.cpp window pane:
#include <iostream>
using namespace std;
using namespace egt;
int main(int argc, const char ** argv)
{
Application app;
TopWindow window;
Button button(window, "Press Me");
center(button);
window.show();
return app.run();
}
Periodic Timer
In this section, you will modify and build the timer.cpp source code that you entered in the previous section to construct a Periodic Timer and display the CPU usage.
Modify the timer.cpp source code by adding the following lines of code as shown:
#include <iostream>
using namespace std;
using namespace egt;
int main(int argc, const char ** argv)
{
Application app;
TopWindow window;
Button button(window, "Press Me");
center(button);
//periodic timer
Label CPUlabel("CPU:---", Rect(0, 0, 100, 50));
window.add(bottom(CPUlabel));
egt::experimental::CPUMonitorUsage tools;
PeriodicTimer cputimer(std::chrono::seconds(1));
cputimer.on_timeout([ & CPUlabel, & tools]() {
cout << "periodic timer fired!" << endl;
tools.update();
ostringstream ss;
ss << "CPU: " << static_cast < int > (tools.usage()) << "%";
CPUlabel.text(ss.str());
});
cputimer.start();
window.show();
return app.run();
}
The Console window (bottom pane) will display the build progress.
In the left-hand pane, select EgtProject Debug (under C/C++ Remote Application).
In the Run Configurations window, enter the following in the Remote Absolute File Path for C/C++ Application: text box:
/root/timer
This is the location the timer.cpp executable will be loaded and run on the target.
Observe the WVGA Display on the target:
The timer.cpp executable is running remotely on the target.
Observe the Press Me button in the center of the WVGA Display and observe the status “periodic timer fire!” every three seconds on the Console.
Also, observe the CPU usage in the bottom left corner of the WVGA Display.
periodic timer fired!
periodic timer fired!
periodic timer fired!
One-Shot Timer
In this section, you will modify and build the timer.cpp source code to construct a One-Shot Timer that will fire after three seconds.
Modify the timer.cpp source code by adding the following lines of code as shown:
#include <iostream>
using namespace std;
using namespace egt;
int main(int argc, const char ** argv)
{
Application app;
TopWindow window;
Button button(window, "Press Me");
center(button);
//One-shot timer
Timer timer1(std::chrono::seconds(3));
timer1.on_timeout([]()
{
cout << "One shot timer fired!" << endl;
});
timer1.start();
//periodic timer
Label CPUlabel("CPU:---", Rect(0, 0, 100, 50));
window.add(bottom(CPUlabel));
egt::experimental::CPUMonitorUsage tools;
PeriodicTimer cputimer(std::chrono::seconds(1));
cputimer.on_timeout([ & CPUlabel, & tools]() {
cout << "periodic timer fired!" << endl;
tools.update();
ostringstream ss;
ss << "CPU: " << static_cast < int > (tools.usage()) << "%";
CPUlabel.text(ss.str());
});
cputimer.start();
window.show();
return app.run();
}
The Console window (bottom pane) will display the build progress.
Observe the WVGA Display on the target:
The timer.cpp executable is running remotely on the target.
Observe the target Console for the status “One shot timer fired” after a period of three seconds.
# /root/timer; exitOne shot timer fired
Summary
In this training, you learned how to construct a Periodic and One-Shot Timer. As a bonus, you also learned how to display the CPU usage on the WVGA Display.
What’s Next?
There’s plenty more to learn. Here are some additional Ensemble Graphics Toolkit training resources to help you gain more knowledge and skills:
- Widget Positioning
- Label and Font Classes
- Static Grid
- BoxSizer
- Themes
- Events
- Dialog
- Timers (this training)
- Animation Part 1
- Animation Part 2
- Resource Files