Skip to content

Commit f3d4f04

Browse files
committed
Application selection at build time : update documentation
Update documentation about the apps (Apps.md) : fix obsolete information, add doc about user/system apps and update the part about the implementation of a new app.
1 parent 473d9c4 commit f3d4f04

File tree

1 file changed

+88
-30
lines changed

1 file changed

+88
-30
lines changed

doc/code/Apps.md

+88-30
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,95 @@ This page will teach you:
99

1010
The user interface of InfiniTime is made up of **screens**.
1111
Screens that are opened from the app launcher are considered **apps**.
12-
Every app in InfiniTime is it's own class.
12+
Every app in InfiniTime is its own class.
1313
An instance of the class is created when the app is launched, and destroyed when the user exits the app.
14-
Apps run inside the "displayapp" task (briefly discussed [here](./Intro.md)).
14+
Apps run inside the `DisplayApp` task (briefly discussed [here](./Intro.md)).
1515
Apps are responsible for everything drawn on the screen when they are running.
16-
By default, apps only do something (as in a function is executed) when they are created or when a touch event is detected.
16+
Apps can be refreshed periodically and reacts to external events (touch or button).
1717

1818
## Interface
1919

20-
Every app class has to be inside the namespace `Pinetime::Applications::Screens` and inherit from `Screen`.
21-
The constructor should have at least one parameter `DisplayApp* app`, which it needs for the constructor of its parent class Screen.
22-
Other parameters should be references to controllers that the app needs.
23-
A destructor is needed to clean up LVGL and restore any changes (for example re-enable sleeping).
24-
App classes can override `bool OnButtonPushed()`, `bool OnTouchEvent(TouchEvents event)` and `bool OnTouchEvent(uint16_t x, uint16_t y)` to implement their own functionality for those events.
25-
If an app only needs to display some text and do something upon a touch screen button press,
26-
it does not need to override any of these functions, as LVGL can also handle touch events for you.
27-
If you have any doubts, you can always look at how the other apps function for reference.
20+
Every app class is declared inside the namespace `Pinetime::Applications::Screens`
21+
and inherits
22+
from [`Pinetime::Applications::Screens::Screen`](https://github.com/InfiniTimeOrg/InfiniTime/blob/main/src/displayapp/screens/Screen.h).
2823

29-
### Continuous updating
24+
Each app defines its own constructor.
25+
The constructors mostly take references to InfiniTime `Controllers` (ex: Alarm, DateTime, BLE services, Settings,...)
26+
the app needs for its operations. The constructor is responsible for initializing the UI of the app.
3027

31-
If your app needs to be updated continuously, you can do so by overriding the `Refresh()` function in your class
32-
and calling `lv_task_create` inside the constructor.
28+
The **destructor** cleans up LVGL and restores any changes (for example re-enable sleeping).
3329

34-
An example call could look like this:
30+
App classes can override `bool OnButtonPushed()`, `bool OnTouchEvent(TouchEvents event)`
31+
and `bool OnTouchEvent(uint16_t x, uint16_t y)` to implement their own functionality for those events.
3532

36-
```cpp
37-
taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
33+
Apps that need to be refreshed periodically create an `lv_task` (using `lv_task_create()`)
34+
that will call the method `Refresh()` periodically.
35+
36+
## App types
37+
38+
There are basically 2 types of applications : **system** apps and **user** apps.
39+
40+
**System** applications are always built into InfiniTime, and InfiniTime cannot work properly without those apps.
41+
The watchfaces, settings, notifications and the application launcher are examples of such system applications.
42+
43+
**User** applications are optionally built into the firmware. They extend the functionalities of the system.
44+
45+
The distinction between **system** and **user** applications allows for more flexibility and customization.
46+
This allows to easily select which user applications must be built into the firmware
47+
without overflowing the system memory.
48+
49+
## Apps initialization
50+
51+
Apps are created by `DisplayApp` in `DisplayApp::LoadScreen()`.
52+
This method simply call the creates an instance of the class that corresponds to the app specified in parameters.
53+
54+
The constructor of **system** apps is called directly. If the application is a **user** app,
55+
the corresponding `AppDescription` is first retrieved from `userApps`
56+
and then the function `create` is called to create an instance of the app.
57+
58+
## User application selection at build time
59+
60+
The list of user applications is generated at build time by the `consteval` function `CreateAppDescriptions()`
61+
in `UserApps.h`. This method takes the list of applications that must be built into the firmware image.
62+
This list of applications is defined as a list `Apps` enum values named `UserAppTypes` in `Apps.h`.
63+
For each application listed in `UserAppTypes`, an entry of type `AppDescription` is added to the array `userApps`.
64+
This entry is created by using the information provided by a template `AppTraits`
65+
that is customized for every user application.
66+
67+
Here is an example of an AppTraits customized for the Alarm application.
68+
It defines the type of application, its icon and a function that returns an instance of the application.
69+
70+
```c++
71+
template <>
72+
struct AppTraits<Apps::Alarm> {
73+
static constexpr Apps app = Apps::Alarm;
74+
static constexpr const char* icon = Screens::Symbols::clock;
75+
76+
static Screens::Screen* Create(AppControllers& controllers) {
77+
return new Screens::Alarm(controllers.alarmController,
78+
controllers.settingsController.GetClockType(),
79+
*controllers.systemTask,
80+
controllers.motorController);
81+
};
82+
};
3883
```
3984
40-
With `taskRefresh` being a member variable of your class and of type `lv_task_t*`.
41-
Remember to delete the task again using `lv_task_del`.
42-
The function `RefreshTaskCallback` is inherited from `Screen` and just calls your `Refresh` function.
85+
This array `userApps` is used by `DisplayApp` to create the applications and the `AppLauncher`
86+
to list all available applications.
4387
4488
## Creating your own app
4589
46-
A minimal app could look like this:
90+
A minimal user app could look like this:
4791
4892
MyApp.h:
4993
5094
```cpp
5195
#pragma once
5296
97+
#include "displayapp/Apps.h"
5398
#include "displayapp/screens/Screen.h"
54-
#include <lvgl/lvgl.h>
99+
#include "displayapp/Controllers.h"
100+
#include "Symbols.h"
55101
56102
namespace Pinetime {
57103
namespace Applications {
@@ -62,6 +108,15 @@ namespace Pinetime {
62108
~MyApp() override;
63109
};
64110
}
111+
112+
template <>
113+
struct AppTraits<Apps:MyApp> {
114+
static constexpr Apps app = Apps::MyApp;
115+
static constexpr const char* icon = Screens::Symbol::myApp;
116+
static Screens::Screens* Create(AppController& controllers) {
117+
return new Screens::MyApp();
118+
}
119+
};
65120
}
66121
}
67122
```
@@ -70,7 +125,6 @@ MyApp.cpp:
70125

71126
```cpp
72127
#include "displayapp/screens/MyApp.h"
73-
#include "displayapp/DisplayApp.h"
74128

75129
using namespace Pinetime::Applications::Screens;
76130

@@ -86,20 +140,24 @@ MyApp::~MyApp() {
86140
}
87141
```
88142
89-
Both of these files should be in [displayapp/screens/](/src/displayapp/screens/)
90-
or [displayapp/screens/settings/](/src/displayapp/screens/settings/) if it's a setting app.
143+
Both of these files should be in [displayapp/screens/](/src/displayapp/screens/).
91144
92145
Now we have our very own app, but InfiniTime does not know about it yet.
93-
The first step is to include your MyApp.cpp (or any new cpp files for that matter)
146+
The first step is to include your `MyApp.cpp` (or any new cpp files for that matter)
94147
in the compilation by adding it to [CMakeLists.txt](/CMakeLists.txt).
95-
The next step to making it launchable is to give your app an id.
148+
The next step to making it launch-able is to give your app an id.
96149
To do this, add an entry in the enum class `Pinetime::Applications::Apps` ([displayapp/Apps.h](/src/displayapp/Apps.h)).
97-
Name this entry after your app. Add `#include "displayapp/screens/MyApp.h"` to the file [displayapp/DisplayApp.cpp](/src/displayapp/DisplayApp.cpp).
98-
Now, go to the function `DisplayApp::LoadScreen` and add another case to the switch statement.
150+
Name this entry after your app. Add `#include "displayapp/screens/MyApp.h"`
151+
to the file [displayapp/DisplayApp.cpp](/src/displayapp/DisplayApp.cpp).
152+
153+
If your application is a **system** application, go to the function `DisplayApp::LoadScreen`
154+
and add another case to the switch statement.
99155
The case will be the id you gave your app earlier.
100156
If your app needs any additional arguments, this is the place to pass them.
101157
102-
If you want to add your app in the app launcher, add your app in [displayapp/screens/ApplicationList.h](/src/displayapp/screens/ApplicationList.h) to the array containing the applications and their corresponding symbol. If your app is a setting, do the same procedure in [displayapp/screens/settings/Settings.h](/src/displayapp/screens/settings/Settings.h).
158+
If your application is a **user** application, you don't need to add anything in DisplayApp,
159+
everything will be automatically generated for you.
160+
The user application will also be automatically be added to the app launcher menu.
103161
104162
You should now be able to [build](../buildAndProgram.md) the firmware
105163
and flash it to your PineTime. Yay!

0 commit comments

Comments
 (0)