summaryrefslogtreecommitdiff
path: root/src/displayapp/screens/WatchFaceDigital.cpp
blob: 1436a79beca3b4a912a763c7d1acd4a46306cc9f (plain)
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
#include "displayapp/screens/WatchFaceDigital.h"

#include <date/date.h>
#include <lvgl/lvgl.h>
#include <cstdio>
#include "displayapp/screens/BatteryIcon.h"
#include "displayapp/screens/BleIcon.h"
#include "displayapp/screens/NotificationIcon.h"
#include "displayapp/screens/Symbols.h"
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "components/ble/NotificationManager.h"
#include "components/motion/MotionController.h"
#include "components/settings/Settings.h"
#include "displayapp/fonts/neofont.h"
using namespace Pinetime::Applications::Screens;

WatchFaceDigital::WatchFaceDigital(DisplayApp* app,
                                   Controllers::DateTime& dateTimeController,
                                   Controllers::Battery& batteryController,
                                   Controllers::Ble& bleController,
                                   Controllers::NotificationManager& notificatioManager,
                                   Controllers::Settings& settingsController,
                                   Controllers::MotionController& motionController,
                                   System::SystemTask& systemTask)
  : Screen(app),
    systemTask {systemTask},
    currentDateTime {{}},
    dateTimeController {dateTimeController},
    batteryController {batteryController},
    bleController {bleController},
    notificatioManager {notificatioManager},
    settingsController {settingsController},
    motionController {motionController} {

  /*Create a line meter */
  lmeter = lv_linemeter_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_pad_top(lmeter,   LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, 0);
  lv_obj_set_style_local_pad_bottom(lmeter,   LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, 0);
  lv_obj_set_style_local_pad_left(lmeter,   LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, 0);
  lv_obj_set_style_local_pad_right(lmeter,   LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, 0);
  lv_obj_set_width_margin(lmeter, 0);
  lv_obj_set_height_margin(lmeter, 0);
  lv_obj_set_size(lmeter, 240, 240);
  lv_obj_align(lmeter, nullptr, LV_ALIGN_CENTER, 0, 0);
  lv_linemeter_set_range(lmeter, 0, 60); /*Set the range*/
  lv_linemeter_set_value(lmeter, 30); /*Set the current value*/
  lv_linemeter_set_angle_offset(lmeter, 180);
  lv_linemeter_set_scale(lmeter, 360, 97); /*Set the angle and number of lines*/

  lv_obj_set_style_local_scale_end_color(lmeter, LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, lv_color_make(255, 0, 0));
  lv_obj_set_style_local_scale_grad_color(lmeter, LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, lv_color_make(160, 0, 0));
  lv_obj_set_style_local_scale_end_line_width(lmeter, LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, 2);
  lv_obj_set_style_local_line_width(lmeter, LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, 2);
  lv_obj_set_style_local_line_color(lmeter, LV_LINEMETER_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_SILVER);

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

  batteryIcon = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_text_fmt(batteryIcon, "B##%%");
  lv_obj_set_style_local_text_font(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont1);
  lv_obj_align(batteryIcon, lv_scr_act(), LV_ALIGN_IN_TOP_RIGHT, 0, 0);

  batteryPlug = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(batteryPlug, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFF00));
  lv_label_set_text_static(batteryPlug, "");
  lv_obj_align(batteryPlug, batteryIcon, LV_ALIGN_OUT_LEFT_MID, -5, 0);

  bleIcon = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(bleIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x0082FC));
  lv_label_set_text_static(bleIcon, Symbols::bluetooth);
  lv_obj_align(bleIcon, batteryPlug, LV_ALIGN_OUT_LEFT_MID, -5, 0);

  notificationIcon = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(notificationIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x00FF00));
  lv_label_set_text_static(notificationIcon, NotificationIcon::GetIcon(false));
  lv_obj_align(notificationIcon, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);

  label_temp = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(  label_temp, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x222222));
  lv_obj_set_style_local_text_font(   label_temp, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont15);
  lv_label_set_text_fmt(label_temp, "??°C");
  lv_obj_align(label_temp, lv_scr_act(), LV_ALIGN_CENTER, 75, -75);

  label_time = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_font(label_time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont3);
  lv_label_set_text_fmt(label_time, hhmm_label_text);
  lv_obj_align(label_time, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
  lv_label_set_long_mode(label_time, LV_LABEL_LONG_CROP);
  lv_label_set_align(label_time, LV_LABEL_ALIGN_RIGHT);

#if 0
  label_time_sec = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(label_time_sec, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
  lv_obj_set_style_local_text_font(label_time_sec, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont2);
  lv_label_set_text_fmt(label_time_sec, ":??");
  lv_obj_align(label_time_sec, label_time, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 0);
#else
  label_time_sec = lv_label_create(lv_scr_act(), nullptr);
  // lv_obj_set_style_local_text_color(label_time_sec, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
  lv_obj_set_style_local_text_font(label_time_sec, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont15);
  lv_label_set_text_fmt(label_time_sec, ":??");
  lv_obj_align(label_time_sec, label_time, LV_ALIGN_OUT_RIGHT_BOTTOM, 0, 0); // -2
#endif

  label_time_pm = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_text_static(label_time_pm, "PM");
  lv_obj_set_style_local_text_font(label_time_pm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont1);
  lv_obj_align(label_time_pm, label_time, LV_ALIGN_OUT_LEFT_TOP, 0, 0);
  lv_obj_set_hidden(label_time_pm, 1);

  label_date_dd = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(label_date_dd, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x999999));
  lv_label_set_text_fmt(label_date_dd, "DD");
  lv_obj_set_style_local_text_font(label_date_dd, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont2);
  lv_obj_align(label_date_dd, label_time, LV_ALIGN_OUT_TOP_MID, 0, 0);
  lv_label_set_long_mode(label_date_dd, LV_LABEL_LONG_CROP);
  lv_label_set_align(label_date_dd, LV_LABEL_ALIGN_CENTER);

  label_date_dow = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(label_date_dow, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x999999));
  lv_label_set_text_fmt(label_date_dow, "DOW ");
  lv_obj_set_style_local_text_font(label_date_dow, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont2);
  lv_obj_align(label_date_dow, label_date_dd, LV_ALIGN_OUT_LEFT_MID, 0, 0);

  label_date_mmm = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(label_date_mmm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x999999));
  lv_label_set_text_fmt(label_date_mmm, "MMM");
  lv_obj_set_style_local_text_font(label_date_mmm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont15);
  lv_obj_align(label_date_mmm, label_date_dd, LV_ALIGN_OUT_TOP_MID, 0, 0);

  label_date_yyyy = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_style_local_text_color(label_date_yyyy, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x999999));
  lv_label_set_text_fmt(label_date_yyyy, "YYYY");
  lv_obj_set_style_local_text_font(label_date_yyyy, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont1);
  lv_obj_align(label_date_yyyy, label_date_mmm, LV_ALIGN_OUT_TOP_MID, 0, 0);
  lv_label_set_long_mode(label_date_yyyy, LV_LABEL_LONG_CROP);
  lv_label_set_align(label_date_yyyy, LV_LABEL_ALIGN_CENTER);

  backgroundLabel = lv_label_create(lv_scr_act(), nullptr);
  lv_obj_set_click(backgroundLabel, true);
  lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP);
  lv_obj_set_size(backgroundLabel, 240, 240);
  lv_obj_set_pos(backgroundLabel, 0, 0);
  lv_label_set_text_static(backgroundLabel, "");

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

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

void WatchFaceDigital::Refresh() {
  powerPresent = batteryController.IsPowerPresent();
  if (powerPresent.IsUpdated()) {
    lv_label_set_text_static(batteryPlug, (powerPresent.Get() ? "PWR" : ""));
  }

  batteryPercentRemaining = batteryController.PercentRemaining();
  if (batteryPercentRemaining.IsUpdated()) {
    auto batteryPercent = batteryPercentRemaining.Get();
    if (batteryPercent == 100) {
      lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
    } else {
      lv_obj_set_style_local_text_color(batteryIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    }
    lv_label_set_text_fmt(batteryIcon, "B%d%%", batteryPercent);
  }

  bleState = bleController.IsConnected();
  if (bleState.IsUpdated()) {
    lv_label_set_text(bleIcon, BleIcon::GetIcon(bleState.Get()));
  }
  lv_obj_realign(batteryIcon);
  lv_obj_realign(batteryPlug);
  lv_obj_realign(bleIcon);

  notificationState = notificatioManager.AreNewNotificationsAvailable();
  if (notificationState.IsUpdated()) {
    lv_label_set_text_static(notificationIcon, NotificationIcon::GetIcon(notificationState.Get()));
  }

#ifndef INFINISIM
  temperature = systemTask.motionSensor.getTemperature();
  if (temperature.IsUpdated()) {
    lv_obj_set_style_local_text_color(  label_temp, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x999999));
    lv_label_set_text_fmt(label_temp, "%d°C");
#if 0
    lv_label_set_text_fmt(label_temp, "T%d [%d,%d]",
      ((int)(systemTask.motionSensor.temperature_last_read_value+23)),
      ((int)(systemTask.motionSensor.temperature_last_result)),
      ((int)(systemTask.motionSensor.temperature_read_counter))
    );
#endif
  }
#endif

  currentDateTime = dateTimeController.CurrentDateTime();

  if (currentDateTime.IsUpdated()) {
    auto newDateTime = currentDateTime.Get();

    auto dp = date::floor<date::days>(newDateTime);
    auto time = date::make_time(newDateTime - dp);
    auto yearMonthDay = date::year_month_day(dp);

    auto year = static_cast<int>(yearMonthDay.year());
    auto month = static_cast<Pinetime::Controllers::DateTime::Months>(static_cast<unsigned>(yearMonthDay.month()));
    auto day = static_cast<unsigned>(yearMonthDay.day());
    auto dayOfWeek = static_cast<Pinetime::Controllers::DateTime::Days>(date::weekday(yearMonthDay).iso_encoding());

    uint8_t hour = time.hours().count();
    uint8_t minute = time.minutes().count();
    uint8_t second = time.seconds().count();

    if (displayedHour != hour || displayedMinute != minute) {
      displayedHour = hour;
      displayedMinute = minute;
      bool hide_pm = true;
      uint8_t h0;
      if (settingsController.GetClockType() == Controllers::Settings::ClockType::H12) {
        if (hour == 0) {
          hour = 12;
        } else if (hour == 12) {
          hide_pm = false;
        } else if (hour > 12) {
          hour -= 12;
          hide_pm = false;
        }
        h0 = hour < 10 ? ' ' : '0' + (hour-10);
      } else {
        h0 = '0' + (hour / 10);
      }
      hhmm_label_text[0] = h0;
      hhmm_label_text[1] = '0' + (hour%10);
      hhmm_label_text[3] = '0' + (minute / 10);
      hhmm_label_text[4] = '0' + (minute % 10);
      lv_label_set_text_static(label_time, hhmm_label_text);
      lv_obj_set_hidden(label_time_pm, hide_pm);
    }

    if (displayedSecond != second) {
      displayedSecond = second;
      lv_label_set_text_fmt(label_time_sec, ":%02d", second);
    }

    if ((year != currentYear) || (month != currentMonth) || (dayOfWeek != currentDayOfWeek) || (day != currentDay)) {
      // if (settingsController.GetClockType() == Controllers::Settings::ClockType::H24) {
        // lv_label_set_text_fmt(
        //     label_date, "%s %d %s %d", dateTimeController.DayOfWeekShortToString(), day, dateTimeController.MonthShortToString(), year);
      // } else {
        // lv_label_set_text_fmt(
        //     label_date, "%s %s %d %d", dateTimeController.DayOfWeekShortToString(), dateTimeController.MonthShortToString(), day, year);
      // }
      // lv_obj_realign(label_date);
      // lv_label_set_text_static(label_date, "");
      lv_label_set_text_fmt(label_date_dd, "%d", day);
      // should rather extract info from the timestamp
      lv_label_set_text_static(label_date_dow, dateTimeController.DayOfWeekShortToString());
      lv_label_set_text_static(label_date_mmm, dateTimeController.MonthShortToString());
      lv_label_set_text_fmt(label_date_yyyy, "%d", year);

      currentYear = year;
      currentMonth = month;
      currentDayOfWeek = dayOfWeek;
      currentDay = day;
    }
  }

}