Skip to content

Commit 44d1798

Browse files
authored
navigation: Move font to external memory (#1838)
The TTF font used by the navigation app is ~20KB and is stored in internal flash memory. To free this space, the TTF font is now converted in 2 "atlas pictures" (pictures that contain multiple concatenated images) stored in the external flash memory. The navigation app now accesses one of those 2 files and apply an offset to display the desired picture. The corresponding documentation has also been updated. Add comments about the layout of the pictures that contain the icon and about the indexing of those icons. In documentation (buildAndProgram.md), edit the section about the debug compilation mode. Remove the part about removing the Navigation app to free some memory (since it's not relevant anymore) and explain how to selectively build parts of the firmware in Debug mode.
1 parent 0f9f606 commit 44d1798

File tree

9 files changed

+209
-113
lines changed

9 files changed

+209
-113
lines changed

doc/buildAndProgram.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ CMake configures the project according to variables you specify the command line
4848
#### (\*) Note about **CMAKE_BUILD_TYPE**
4949
By default, this variable is set to *Release*. It compiles the code with size and speed optimizations. We use this value for all the binaries we publish when we [release](https://github.com/InfiniTimeOrg/InfiniTime/releases) new versions of InfiniTime.
5050
51-
The *Debug* mode disables all optimizations, which makes the code easier to debug. However, the binary size will likely be too big to fit in the internal flash memory. If you want to build and debug a *Debug* binary, you'll need to disable some parts of the code. For example, the icons for the **Navigation** app use a lot of memory space. You can comment the content of `m_iconMap` in the [Navigation](https://github.com/InfiniTimeOrg/InfiniTime/blob/main/src/displayapp/screens/Navigation.h#L148) application to free some memory.
51+
The *Debug* mode disables all optimizations, which makes the code easier to debug. However, the binary size will likely be too big to fit in the internal flash memory. If you want to build and debug a *Debug* binary, you can disable some parts of the code that are not needed for the test you want to achieve. You can also apply the *Debug* mode selectively on parts of the application by applying the `DEBUG_FLAGS` only for the part (CMake target) you want to debug. For example, let's say you want to debug code related to LittleFS, simply set the compilation options for the RELEASE configuration of the target to `DEBUG_FLAGS` (in `src/CMakeLists.txt`). This will force the compilation of that target in *Debug* mode while the rest of the project will be built in *Release* mode. Example:
52+
53+
```
54+
target_compile_options(littlefs PRIVATE
55+
${COMMON_FLAGS}
56+
$<$<CONFIG:DEBUG>: ${DEBUG_FLAGS}>
57+
$<$<CONFIG:RELEASE>: ${DEBUG_FLAGS}> # Change from RELEASE_FLAGS to DEBUG_FLAGS
58+
$<$<COMPILE_LANGUAGE:CXX>: ${CXX_FLAGS}>
59+
$<$<COMPILE_LANGUAGE:ASM>: ${ASM_FLAGS}>
60+
)
61+
```
5262

5363
#### (\*\*) Note about **BUILD_DFU**
5464
DFU files are the files you'll need to install your build of InfiniTime using OTA (over-the-air) mechanism. To generate the DFU file, the Python tool [adafruit-nrfutil](https://github.com/adafruit/Adafruit_nRF52_nrfutil) is needed on your system. Check that this tool is properly installed before enabling this option.

src/displayapp/fonts/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(FONTS jetbrains_mono_42 jetbrains_mono_76 jetbrains_mono_bold_20
2-
jetbrains_mono_extrabold_compressed lv_font_navi_80 lv_font_sys_48
2+
jetbrains_mono_extrabold_compressed lv_font_sys_48
33
open_sans_light fontawesome_weathericons)
44
find_program(LV_FONT_CONV "lv_font_conv" NO_CACHE REQUIRED
55
HINTS "${CMAKE_SOURCE_DIR}/node_modules/.bin")

src/displayapp/fonts/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ and for each font there is:
3333
### Navigation font
3434

3535
`navigtion.ttf` is created with the web app [icomoon](https://icomoon.io/app) by importing the svg files from `src/displayapp/icons/navigation/unique` and generating the font. `lv_font_navi_80.json` is a project file for the site, which you can import to add or remove icons.
36+
37+
To save space in the internal flash memory, the navigation icons are now moved into the external flash memory. To do this, the TTF font is converted into pictures (1 for each symbol). Those pictures are then concatenated into 2 big pictures (we need two files since LVGL supports maximum 2048px width/height). At runtime, a map is used to locate the desired icon in the corresponding file at a specific offset.
38+
39+
Here is the command to convert the TTF font in PNG picture:
40+
41+
```shell
42+
convert -background none -fill white -font navigation.ttf -pointsize 80 -gravity center label:"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" navigation0.png
43+
44+
convert -background none -fill white -font navigation.ttf -pointsize 80 -gravity center label:"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" navigation1.png
45+
```
46+
47+
*Please note that the characters after `label:` are UTF-8 characters and might not be displayed correctly in this document.*
48+
49+
The characters in the TTF font range from `0xEEA480` to `0xEEA4A9`. Characters from `0xEEA480` to `0xEEA498` are stored in `navigation0.png` and the others in `navigation1.png`. Each character is 80px height so displaying a specific character consists in multiplying its index in the file by -80 and use this value as the offset when calling `lv_img_set_offset_y()`.

src/displayapp/fonts/fonts.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@
6464
"bpp": 1,
6565
"size": 48
6666
},
67-
"lv_font_navi_80": {
68-
"sources": [
69-
{
70-
"file": "navigation.ttf",
71-
"range": "0xe900-0xe929"
72-
}
73-
],
74-
"bpp": 2,
75-
"size": 80,
76-
"compress": true
77-
},
7867
"fontawesome_weathericons": {
7968
"sources": [
8069
{

src/displayapp/screens/Navigation.cpp

Lines changed: 167 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -23,105 +23,166 @@
2323

2424
using namespace Pinetime::Applications::Screens;
2525

26-
LV_FONT_DECLARE(lv_font_navi_80)
26+
/* Notes about the navigation icons :
27+
* - Icons are generated from a TTF font converted in PNG images. Those images are all appended
28+
* vertically into a single PNG images. Since LVGL support images width and height up to
29+
* 2048 px, the icons needs to be split into 2 separate PNG pictures. More info in
30+
* src/displayapp/fonts/README.md
31+
* - To make the handling of those icons easier, they must all have the same width and height
32+
* - Those PNG are then converted into BINARY format using the classical image generator
33+
* (in src/resources/generate-img.py)
34+
* - The array `iconMap` maps each icon with an index. This index corresponds to the position of
35+
* the icon in the file. All index lower than 25 (`maxIconsPerFile`) represent icons located
36+
* in the first file (navigation0.bin). All the other icons are located in the second file
37+
* (navigation1.bin). Since all icons have the same height, this index must be multiplied by
38+
* 80px (`iconHeight`) to get the actual position (in pixels) of the icon in the image.
39+
* - This is how the images are laid out in the PNG files :
40+
* *---------------*
41+
* | ICON 0 |
42+
* | FILE 0 |
43+
* | INDEX = 0 |
44+
* | PIXEL# = 0 |
45+
* *---------------*
46+
* | ICON 1 |
47+
* | FILE 0 |
48+
* | INDEX = 1 |
49+
* | PIXEL# = -80 |
50+
* *---------------*
51+
* | ICON 2 |
52+
* | FILE 0 |
53+
* | INDEX = 2 |
54+
* | PIXEL# = -160 |
55+
* *---------------*
56+
* | ... |
57+
* *---------------*
58+
* | ICON 25 |
59+
* | FILE 1 |
60+
* | INDEX = 25 |
61+
* | PIXEL# = 0 |
62+
* *---------------*
63+
* | ICON 26 |
64+
* | FILE 1 |
65+
* | INDEX = 26 |
66+
* | PIXEL# = -80 |
67+
* *---------------*
68+
* - The source images are located in `src/resources/navigation0.png` and `src/resources/navigation1.png`
69+
*/
2770

2871
namespace {
29-
constexpr std::array<std::pair<const char*, const char*>, 86> m_iconMap = {{
30-
{"arrive-left", "\xEE\xA4\x81"},
31-
{"arrive-right", "\xEE\xA4\x82"},
32-
{"arrive-straight", "\xEE\xA4\x80"},
33-
{"arrive", "\xEE\xA4\x80"},
34-
{"close", "\xEE\xA4\x83"},
35-
{"continue-left", "\xEE\xA4\x85"},
36-
{"continue-right", "\xEE\xA4\x86"},
37-
{"continue-slight-left", "\xEE\xA4\x87"},
38-
{"continue-slight-right", "\xEE\xA4\x88"},
39-
{"continue-straight", "\xEE\xA4\x84"},
40-
{"continue-uturn", "\xEE\xA4\x89"},
41-
{"continue", "\xEE\xA4\x84"},
42-
{"depart-left", "\xEE\xA4\x8B"},
43-
{"depart-right", "\xEE\xA4\x8C"},
44-
{"depart-straight", "\xEE\xA4\x8A"},
45-
{"end-of-road-left", "\xEE\xA4\x8D"},
46-
{"end-of-road-right", "\xEE\xA4\x8E"},
47-
{"ferry", "\xEE\xA4\x8F"},
48-
{"flag", "\xEE\xA4\x90"},
49-
{"fork-left", "\xEE\xA4\x92"},
50-
{"fork-right", "\xEE\xA4\x93"},
51-
{"fork-slight-left", "\xEE\xA4\x94"},
52-
{"fork-slight-right", "\xEE\xA4\x95"},
53-
{"fork-straight", "\xEE\xA4\x96"},
54-
{"invalid", "\xEE\xA4\x84"},
55-
{"invalid-left", "\xEE\xA4\x85"},
56-
{"invalid-right", "\xEE\xA4\x86"},
57-
{"invalid-slight-left", "\xEE\xA4\x87"},
58-
{"invalid-slight-right", "\xEE\xA4\x88"},
59-
{"invalid-straight", "\xEE\xA4\x84"},
60-
{"invalid-uturn", "\xEE\xA4\x89"},
61-
{"merge-left", "\xEE\xA4\x97"},
62-
{"merge-right", "\xEE\xA4\x98"},
63-
{"merge-slight-left", "\xEE\xA4\x99"},
64-
{"merge-slight-right", "\xEE\xA4\x9A"},
65-
{"merge-straight", "\xEE\xA4\x84"},
66-
{"new-name-left", "\xEE\xA4\x85"},
67-
{"new-name-right", "\xEE\xA4\x86"},
68-
{"new-name-sharp-left", "\xEE\xA4\x9B"},
69-
{"new-name-sharp-right", "\xEE\xA4\x9C"},
70-
{"new-name-slight-left", "\xEE\xA4\x87"},
71-
{"new-name-slight-right", "\xEE\xA4\x88"},
72-
{"new-name-straight", "\xEE\xA4\x84"},
73-
{"notification-left", "\xEE\xA4\x85"},
74-
{"notification-right", "\xEE\xA4\x86"},
75-
{"notification-sharp-left", "\xEE\xA4\x9B"},
76-
{"notification-sharp-right", "\xEE\xA4\xA5"},
77-
{"notification-slight-left", "\xEE\xA4\x87"},
78-
{"notification-slight-right", "\xEE\xA4\x88"},
79-
{"notification-straight", "\xEE\xA4\x84"},
80-
{"off-ramp-left", "\xEE\xA4\x9D"},
81-
{"off-ramp-right", "\xEE\xA4\x9E"},
82-
{"off-ramp-slight-left", "\xEE\xA4\x9F"},
83-
{"off-ramp-slight-right", "\xEE\xA4\xA0"},
84-
{"on-ramp-left", "\xEE\xA4\x85"},
85-
{"on-ramp-right", "\xEE\xA4\x86"},
86-
{"on-ramp-sharp-left", "\xEE\xA4\x9B"},
87-
{"on-ramp-sharp-right", "\xEE\xA4\xA5"},
88-
{"on-ramp-slight-left", "\xEE\xA4\x87"},
89-
{"on-ramp-slight-right", "\xEE\xA4\x88"},
90-
{"on-ramp-straight", "\xEE\xA4\x84"},
91-
{"rotary", "\xEE\xA4\xA1"},
92-
{"rotary-left", "\xEE\xA4\xA2"},
93-
{"rotary-right", "\xEE\xA4\xA3"},
94-
{"rotary-sharp-left", "\xEE\xA4\xA4"},
95-
{"rotary-sharp-right", "\xEE\xA4\xA5"},
96-
{"rotary-slight-left", "\xEE\xA4\xA6"},
97-
{"rotary-slight-right", "\xEE\xA4\xA7"},
98-
{"rotary-straight", "\xEE\xA4\xA8"},
99-
{"roundabout", "\xEE\xA4\xA1"},
100-
{"roundabout-left", "\xEE\xA4\xA2"},
101-
{"roundabout-right", "\xEE\xA4\xA3"},
102-
{"roundabout-sharp-left", "\xEE\xA4\xA4"},
103-
{"roundabout-sharp-right", "\xEE\xA4\xA5"},
104-
{"roundabout-slight-left", "\xEE\xA4\xA6"},
105-
{"roundabout-slight-right", "\xEE\xA4\xA7"},
106-
{"roundabout-straight", "\xEE\xA4\xA8"},
107-
{"turn-left", "\xEE\xA4\x85"},
108-
{"turn-right", "\xEE\xA4\x86"},
109-
{"turn-sharp-left", "\xEE\xA4\x9B"},
110-
{"turn-sharp-right", "\xEE\xA4\xA5"},
111-
{"turn-slight-left", "\xEE\xA4\x87"},
112-
{"turn-slight-right", "\xEE\xA4\x88"},
113-
{"turn-straight", "\xEE\xA4\x84"},
114-
{"updown", "\xEE\xA4\xA9"},
115-
{"uturn", "\xEE\xA4\x89"},
72+
struct Icon {
73+
const char* fileName;
74+
int16_t offset;
75+
};
76+
77+
constexpr uint16_t iconHeight = -80;
78+
constexpr uint8_t flagIndex = 18;
79+
constexpr uint8_t maxIconsPerFile = 25;
80+
const char* iconsFile0 = "F:/images/navigation0.bin";
81+
const char* iconsFile1 = "F:/images/navigation1.bin";
82+
83+
constexpr std::array<std::pair<const char*, uint8_t>, 86> iconMap = {{
84+
{"arrive-left", 1},
85+
{"arrive-right", 2},
86+
{"arrive-straight", 0},
87+
{"arrive", 0},
88+
{"close", 3},
89+
{"continue-left", 5},
90+
{"continue-right", 6},
91+
{"continue-slight-left", 7},
92+
{"continue-slight-right", 8},
93+
{"continue-straight", 4},
94+
{"continue-uturn", 9},
95+
{"continue", 4},
96+
{"depart-left", 11},
97+
{"depart-right", 12},
98+
{"depart-straight", 10},
99+
{"end-of-road-left", 13},
100+
{"end-of-road-right", 14},
101+
{"ferry", 15},
102+
{"flag", 16},
103+
{"fork-left", 18},
104+
{"fork-right", 19},
105+
{"fork-slight-left", 20},
106+
{"fork-slight-right", 21},
107+
{"fork-straight", 22},
108+
{"invalid", 4},
109+
{"invalid-left", 5},
110+
{"invalid-right", 6},
111+
{"invalid-slight-left", 7},
112+
{"invalid-slight-right", 8},
113+
{"invalid-straight", 4},
114+
{"invalid-uturn", 9},
115+
{"merge-left", 23},
116+
{"merge-right", 24},
117+
{"merge-slight-left", 25},
118+
{"merge-slight-right", 26},
119+
{"merge-straight", 4},
120+
{"new-name-left", 5},
121+
{"new-name-right", 6},
122+
{"new-name-sharp-left", 27},
123+
{"new-name-sharp-right", 28},
124+
{"new-name-slight-left", 7},
125+
{"new-name-slight-right", 8},
126+
{"new-name-straight", 4},
127+
{"notification-left", 5},
128+
{"notification-right", 6},
129+
{"notification-sharp-left", 27},
130+
{"notification-sharp-right", 37},
131+
{"notification-slight-left", 7},
132+
{"notification-slight-right", 8},
133+
{"notification-straight", 4},
134+
{"off-ramp-left", 29},
135+
{"off-ramp-right", 30},
136+
{"off-ramp-slight-left", 31},
137+
{"off-ramp-slight-right", 32},
138+
{"on-ramp-left", 5},
139+
{"on-ramp-right", 6},
140+
{"on-ramp-sharp-left", 27},
141+
{"on-ramp-sharp-right", 37},
142+
{"on-ramp-slight-left", 7},
143+
{"on-ramp-slight-right", 8},
144+
{"on-ramp-straight", 4},
145+
{"rotary", 33},
146+
{"rotary-left", 34},
147+
{"rotary-right", 35},
148+
{"rotary-sharp-left", 36},
149+
{"rotary-sharp-right", 37},
150+
{"rotary-slight-left", 38},
151+
{"rotary-slight-right", 39},
152+
{"rotary-straight", 40},
153+
{"roundabout", 33},
154+
{"roundabout-left", 34},
155+
{"roundabout-right", 35},
156+
{"roundabout-sharp-left", 36},
157+
{"roundabout-sharp-right", 37},
158+
{"roundabout-slight-left", 38},
159+
{"roundabout-slight-right", 39},
160+
{"roundabout-straight", 40},
161+
{"turn-left", 5},
162+
{"turn-right", 6},
163+
{"turn-sharp-left", 27},
164+
{"turn-sharp-right", 37},
165+
{"turn-slight-left", 7},
166+
{"turn-slight-right", 8},
167+
{"turn-straight", 4},
168+
{"updown", 41},
169+
{"uturn", 9},
116170
}};
117171

118-
const char* iconForName(const std::string& icon) {
119-
for (auto iter : m_iconMap) {
172+
Icon GetIcon(uint8_t index) {
173+
if (index < maxIconsPerFile) {
174+
return {iconsFile0, static_cast<int16_t>(iconHeight * index)};
175+
}
176+
return {iconsFile1, static_cast<int16_t>(iconHeight * (index - maxIconsPerFile))};
177+
}
178+
179+
Icon GetIcon(const std::string& icon) {
180+
for (const auto& iter : iconMap) {
120181
if (iter.first == icon) {
121-
return iter.second;
182+
return GetIcon(iter.second);
122183
}
123184
}
124-
return "\xEE\xA4\x90";
185+
return GetIcon(flagIndex);
125186
}
126187
}
127188

@@ -130,11 +191,15 @@ namespace {
130191
*
131192
*/
132193
Navigation::Navigation(Pinetime::Controllers::NavigationService& nav) : navService(nav) {
133-
134-
imgFlag = lv_label_create(lv_scr_act(), nullptr);
135-
lv_obj_set_style_local_text_font(imgFlag, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &lv_font_navi_80);
136-
lv_obj_set_style_local_text_color(imgFlag, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
137-
lv_label_set_text_static(imgFlag, iconForName("flag"));
194+
const auto& image = GetIcon("flag");
195+
imgFlag = lv_img_create(lv_scr_act(), nullptr);
196+
lv_img_set_auto_size(imgFlag, false);
197+
lv_obj_set_size(imgFlag, 80, 80);
198+
lv_img_set_src(imgFlag, image.fileName);
199+
lv_img_set_offset_x(imgFlag, 0);
200+
lv_img_set_offset_y(imgFlag, image.offset);
201+
lv_obj_set_style_local_image_recolor_opa(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
202+
lv_obj_set_style_local_image_recolor(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
138203
lv_obj_align(imgFlag, nullptr, LV_ALIGN_CENTER, 0, -60);
139204

140205
txtNarrative = lv_label_create(lv_scr_act(), nullptr);
@@ -173,7 +238,11 @@ Navigation::~Navigation() {
173238
void Navigation::Refresh() {
174239
if (flag != navService.getFlag()) {
175240
flag = navService.getFlag();
176-
lv_label_set_text_static(imgFlag, iconForName(flag));
241+
const auto& image = GetIcon(flag);
242+
lv_img_set_src(imgFlag, image.fileName);
243+
lv_obj_set_style_local_image_recolor_opa(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
244+
lv_obj_set_style_local_image_recolor(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
245+
lv_img_set_offset_y(imgFlag, image.offset);
177246
}
178247

179248
if (narrative != navService.getNarrative()) {

src/displayapp/screens/Navigation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Pinetime {
3232
namespace Screens {
3333
class Navigation : public Screen {
3434
public:
35-
Navigation(Pinetime::Controllers::NavigationService& nav);
35+
explicit Navigation(Pinetime::Controllers::NavigationService& nav);
3636
~Navigation() override;
3737

3838
void Refresh() override;
@@ -48,7 +48,7 @@ namespace Pinetime {
4848
std::string flag;
4949
std::string narrative;
5050
std::string manDist;
51-
int progress;
51+
int progress = 0;
5252

5353
lv_task_t* taskRefresh;
5454
};

src/resources/images.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,19 @@
55
"output_format": "bin",
66
"binary_format": "ARGB8565_RBSWAP",
77
"target_path": "/images/"
8+
},
9+
"navigation0" : {
10+
"sources": "images/navigation0.png",
11+
"color_format": "CF_INDEXED_1_BIT",
12+
"output_format": "bin",
13+
"binary_format": "ARGB8565_RBSWAP",
14+
"target_path": "/images/"
15+
},
16+
"navigation1" : {
17+
"sources": "images/navigation1.png",
18+
"color_format": "CF_INDEXED_1_BIT",
19+
"output_format": "bin",
20+
"binary_format": "ARGB8565_RBSWAP",
21+
"target_path": "/images/"
822
}
923
}
22 KB
Loading
17.4 KB
Loading

0 commit comments

Comments
 (0)