InfiniTime.git

ref: fc5424cb72e477c5f1bbfaeddb5c50b851a965ae

src/displayapp/screens/Navigation.cpp


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*  Copyright (C) 2021  Adam Pigg

    This file is part of InfiniTime.

    InfiniTime is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    InfiniTime is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "displayapp/screens/Navigation.h"
#include <cstdint>
#include "displayapp/DisplayApp.h"
#include "components/ble/NavigationService.h"
#include "displayapp/InfiniTimeTheme.h"

using namespace Pinetime::Applications::Screens;

/* Notes about the navigation icons :
 *  - Icons are generated from a TTF font converted in PNG images. Those images are all appended
 *    vertically into a single PNG images. Since LVGL support images width and height up to
 *    2048 px, the icons needs to be split into 2 separate PNG pictures. More info in
 *    src/displayapp/fonts/README.md
 *  - To make the handling of those icons easier, they must all have the same width and height
 *  - Those PNG are then converted into BINARY format using the classical image generator
 *    (in src/resources/generate-img.py)
 *  - The array `iconMap` maps each icon with an index. This index corresponds to the position of
 *    the icon in the file. All index lower than 25 (`maxIconsPerFile`) represent icons located
 *    in the first file (navigation0.bin). All the other icons are located in the second file
 *    (navigation1.bin). Since all icons have the same height, this index must be multiplied by
 *    80px (`iconHeight`) to get the actual position (in pixels) of the icon in the image.
 * - This is how the images are laid out in the PNG files :
 *  *---------------*
 *  | ICON 0        |
 *  | FILE 0        |
 *  | INDEX = 0     |
 *  | PIXEL# = 0    |
 *  *---------------*
 *  | ICON 1        |
 *  | FILE 0        |
 *  | INDEX = 1     |
 *  | PIXEL# = -80  |
 *  *---------------*
 *  | ICON 2        |
 *  | FILE 0        |
 *  | INDEX = 2     |
 *  | PIXEL# = -160 |
 *  *---------------*
 *  |     ...       |
 *  *---------------*
 *  | ICON 25       |
 *  | FILE 1        |
 *  | INDEX = 25    |
 *  | PIXEL# = 0    |
 *  *---------------*
 *  | ICON 26       |
 *  | FILE 1        |
 *  | INDEX = 26    |
 *  | PIXEL# = -80  |
 *  *---------------*
 *   - The source images are located in `src/resources/navigation0.png` and `src/resources/navigation1.png`
 */

namespace {
  struct Icon {
    const char* fileName;
    int16_t offset;
  };

  constexpr uint16_t iconHeight = -80;
  constexpr uint8_t flagIndex = 18;
  constexpr uint8_t maxIconsPerFile = 25;
  const char* iconsFile0 = "F:/images/navigation0.bin";
  const char* iconsFile1 = "F:/images/navigation1.bin";

  constexpr std::array<std::pair<const char*, uint8_t>, 86> iconMap = {{
    {"arrive-left", 1},
    {"arrive-right", 2},
    {"arrive-straight", 0},
    {"arrive", 0},
    {"close", 3},
    {"continue-left", 5},
    {"continue-right", 6},
    {"continue-slight-left", 7},
    {"continue-slight-right", 8},
    {"continue-straight", 4},
    {"continue-uturn", 9},
    {"continue", 4},
    {"depart-left", 11},
    {"depart-right", 12},
    {"depart-straight", 10},
    {"end-of-road-left", 13},
    {"end-of-road-right", 14},
    {"ferry", 15},
    {"flag", 16},
    {"fork-left", 18},
    {"fork-right", 19},
    {"fork-slight-left", 20},
    {"fork-slight-right", 21},
    {"fork-straight", 22},
    {"invalid", 4},
    {"invalid-left", 5},
    {"invalid-right", 6},
    {"invalid-slight-left", 7},
    {"invalid-slight-right", 8},
    {"invalid-straight", 4},
    {"invalid-uturn", 9},
    {"merge-left", 23},
    {"merge-right", 24},
    {"merge-slight-left", 25},
    {"merge-slight-right", 26},
    {"merge-straight", 4},
    {"new-name-left", 5},
    {"new-name-right", 6},
    {"new-name-sharp-left", 27},
    {"new-name-sharp-right", 28},
    {"new-name-slight-left", 7},
    {"new-name-slight-right", 8},
    {"new-name-straight", 4},
    {"notification-left", 5},
    {"notification-right", 6},
    {"notification-sharp-left", 27},
    {"notification-sharp-right", 37},
    {"notification-slight-left", 7},
    {"notification-slight-right", 8},
    {"notification-straight", 4},
    {"off-ramp-left", 29},
    {"off-ramp-right", 30},
    {"off-ramp-slight-left", 31},
    {"off-ramp-slight-right", 32},
    {"on-ramp-left", 5},
    {"on-ramp-right", 6},
    {"on-ramp-sharp-left", 27},
    {"on-ramp-sharp-right", 37},
    {"on-ramp-slight-left", 7},
    {"on-ramp-slight-right", 8},
    {"on-ramp-straight", 4},
    {"rotary", 33},
    {"rotary-left", 34},
    {"rotary-right", 35},
    {"rotary-sharp-left", 36},
    {"rotary-sharp-right", 37},
    {"rotary-slight-left", 38},
    {"rotary-slight-right", 39},
    {"rotary-straight", 40},
    {"roundabout", 33},
    {"roundabout-left", 34},
    {"roundabout-right", 35},
    {"roundabout-sharp-left", 36},
    {"roundabout-sharp-right", 37},
    {"roundabout-slight-left", 38},
    {"roundabout-slight-right", 39},
    {"roundabout-straight", 40},
    {"turn-left", 5},
    {"turn-right", 6},
    {"turn-sharp-left", 27},
    {"turn-sharp-right", 37},
    {"turn-slight-left", 7},
    {"turn-slight-right", 8},
    {"turn-straight", 4},
    {"updown", 41},
    {"uturn", 9},
  }};

  Icon GetIcon(uint8_t index) {
    if (index < maxIconsPerFile) {
      return {iconsFile0, static_cast<int16_t>(iconHeight * index)};
    }
    return {iconsFile1, static_cast<int16_t>(iconHeight * (index - maxIconsPerFile))};
  }

  Icon GetIcon(const std::string& icon) {
    for (const auto& iter : iconMap) {
      if (iter.first == icon) {
        return GetIcon(iter.second);
      }
    }
    return GetIcon(flagIndex);
  }
}

/**
 * Navigation watchapp
 *
 */
Navigation::Navigation(Pinetime::Controllers::NavigationService& nav) : navService(nav) {
  const auto& image = GetIcon("flag");
  imgFlag = lv_img_create(lv_scr_act(), nullptr);
  lv_img_set_auto_size(imgFlag, false);
  lv_obj_set_size(imgFlag, 80, 80);
  lv_img_set_src(imgFlag, image.fileName);
  lv_img_set_offset_x(imgFlag, 0);
  lv_img_set_offset_y(imgFlag, image.offset);
  lv_obj_set_style_local_image_recolor_opa(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
  lv_obj_set_style_local_image_recolor(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
  lv_obj_align(imgFlag, nullptr, LV_ALIGN_CENTER, 0, -60);

  txtNarrative = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_long_mode(txtNarrative, LV_LABEL_LONG_DOT);
  lv_obj_set_width(txtNarrative, LV_HOR_RES);
  lv_obj_set_height(txtNarrative, 80);
  lv_label_set_text_static(txtNarrative, "Navigation");
  lv_label_set_align(txtNarrative, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(txtNarrative, nullptr, LV_ALIGN_CENTER, 0, 30);

  txtManDist = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_long_mode(txtManDist, LV_LABEL_LONG_BREAK);
  lv_obj_set_style_local_text_color(txtManDist, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
  lv_obj_set_style_local_text_font(txtManDist, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
  lv_obj_set_width(txtManDist, LV_HOR_RES);
  lv_label_set_text_static(txtManDist, "--M");
  lv_label_set_align(txtManDist, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(txtManDist, nullptr, LV_ALIGN_CENTER, 0, 90);

  // Route Progress
  barProgress = lv_bar_create(lv_scr_act(), nullptr);
  lv_obj_set_size(barProgress, 200, 20);
  lv_obj_align(barProgress, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, -10);
  lv_obj_set_style_local_bg_color(barProgress, LV_BAR_PART_BG, LV_STATE_DEFAULT, lv_color_hex(0x222222));
  lv_obj_set_style_local_bg_color(barProgress, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_ORANGE);
  lv_bar_set_anim_time(barProgress, 500);
  lv_bar_set_range(barProgress, 0, 100);
  lv_bar_set_value(barProgress, 0, LV_ANIM_OFF);

  taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
}

Navigation::~Navigation() {
  lv_task_del(taskRefresh);
  lv_obj_clean(lv_scr_act());
}

void Navigation::Refresh() {
  if (flag != navService.getFlag()) {
    flag = navService.getFlag();
    const auto& image = GetIcon(flag);
    lv_img_set_src(imgFlag, image.fileName);
    lv_obj_set_style_local_image_recolor_opa(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_obj_set_style_local_image_recolor(imgFlag, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
    lv_img_set_offset_y(imgFlag, image.offset);
  }

  if (narrative != navService.getNarrative()) {
    narrative = navService.getNarrative();
    lv_label_set_text(txtNarrative, narrative.data());
  }

  if (manDist != navService.getManDist()) {
    manDist = navService.getManDist();
    lv_label_set_text(txtManDist, manDist.data());
  }

  if (progress != navService.getProgress()) {
    progress = navService.getProgress();
    lv_bar_set_value(barProgress, progress, LV_ANIM_OFF);
    if (progress > 90) {
      lv_obj_set_style_local_bg_color(barProgress, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
    } else {
      lv_obj_set_style_local_bg_color(barProgress, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, Colors::orange);
    }
  }
}

bool Navigation::IsAvailable(Pinetime::Controllers::FS& filesystem) {
  lfs_file file = {};

  if (filesystem.FileOpen(&file, "/images/navigation0.bin", LFS_O_RDONLY) < 0) {
    return false;
  }
  filesystem.FileClose(&file);

  if (filesystem.FileOpen(&file, "/images/navigation1.bin", LFS_O_RDONLY) < 0) {
    return false;
  }
  filesystem.FileClose(&file);

  return true;
}