aboutsummaryrefslogtreecommitdiffstats
path: root/src/DisplayApp/Screens/Clock.cpp
blob: a413476a0af4c1a1d9ab953a1ab49968bb327cbf (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
#include <cstdio>
#include <libs/date/includes/date/date.h>
#include <Components/DateTime/DateTimeController.h>
#include <Version.h>
#include <libs/lvgl/lvgl.h>
#include "Clock.h"

using namespace Pinetime::Applications::Screens;
extern lv_font_t jetbrains_mono_extrabold_compressedextrabold_compressed;
extern lv_font_t jetbrains_mono_bold_20;

Clock::Clock(Pinetime::Components::Gfx &gfx) : Screen(gfx), currentDateTime{{}}, version {{}} {
  label_battery = lv_label_create(lv_scr_act(), NULL);
  lv_obj_align(label_battery, lv_scr_act(), LV_ALIGN_IN_TOP_RIGHT, -80, 0);

  labelStyle = const_cast<lv_style_t *>(lv_label_get_style(label_battery, LV_LABEL_STYLE_MAIN));
  labelStyle->text.font = &jetbrains_mono_bold_20;

  lv_style_copy(&labelBigStyle, labelStyle);
  labelBigStyle.text.font = &jetbrains_mono_extrabold_compressedextrabold_compressed;

  lv_label_set_style(label_battery, LV_LABEL_STYLE_MAIN, labelStyle);

  label_ble = lv_label_create(lv_scr_act(), NULL);
  lv_label_set_style(label_ble, LV_LABEL_STYLE_MAIN, labelStyle);
  lv_obj_align(label_ble, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 10, 0);

  label_time = lv_label_create(lv_scr_act(), NULL);
  lv_label_set_style(label_time, LV_LABEL_STYLE_MAIN, &labelBigStyle);
  lv_obj_align(label_time, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 0, 0);


  label_date = lv_label_create(lv_scr_act(), NULL);
  lv_label_set_style(label_date, LV_LABEL_STYLE_MAIN, labelStyle);
  lv_obj_align(label_date, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 0, 80);

  label_version = lv_label_create(lv_scr_act(), NULL);
  lv_label_set_style(label_version, LV_LABEL_STYLE_MAIN, labelStyle);
  lv_obj_align(label_version, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 0, 100);

}

void Clock::Refresh(bool fullRefresh) {
  if(fullRefresh) {
    auto dummy = currentDateTime.Get();
  }

  if (fullRefresh || batteryPercentRemaining.IsUpdated()) {
    char batteryChar[11];
    auto newBatteryValue = batteryPercentRemaining.Get();
    newBatteryValue = (newBatteryValue > 100) ? 100 : newBatteryValue;
    newBatteryValue = (newBatteryValue < 0) ? 0 : newBatteryValue;

    sprintf(batteryChar, "BAT: %d%%", newBatteryValue);
    lv_label_set_text(label_battery, batteryChar);
  }

  if (fullRefresh || bleState.IsUpdated()) {
    uint16_t color = (bleState.Get() == BleConnectionStates::Connected) ? 0xffff : 0x0000;
    gfx.DrawString(10, 0, color, "BLE", &smallFont, false);
    lv_label_set_text(label_ble, "BLE");
    // TODO color
  }

  if(fullRefresh || 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 = (int)yearMonthDay.year();
    auto month = static_cast<Pinetime::Controllers::DateTime::Months>((unsigned)yearMonthDay.month());
    auto day = (unsigned)yearMonthDay.day();
    auto dayOfWeek = static_cast<Pinetime::Controllers::DateTime::Days>(date::weekday(yearMonthDay).iso_encoding());

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

    char minutesChar[3];
    sprintf(minutesChar, "%02d", minute);

    char hoursChar[3];
    sprintf(hoursChar, "%02d", hour);

    char timeStr[6];
    sprintf(timeStr, "%c%c:%c%c", hoursChar[0],hoursChar[1],minutesChar[0], minutesChar[1]);
    lv_label_set_text(label_time, timeStr);

    if ((year != currentYear) || (month != currentMonth) || (dayOfWeek != currentDayOfWeek) || (day != currentDay)) {
      gfx.FillRectangle(0,180, 240, 15, 0x0000);
      char dateStr[22];
      sprintf(dateStr, "%s %d %s %d", DayOfWeekToString(dayOfWeek), day, MonthToString(month), year);
      lv_label_set_text(label_date, dateStr);


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

  if(fullRefresh || version.IsUpdated()) {
    char version[20];
    sprintf(version, "VERSION: %d.%d.%d", Version::Major(), Version::Minor(), Version::Patch());
    lv_label_set_text(label_version, version);
  }

}

const char *Clock::MonthToString(Pinetime::Controllers::DateTime::Months month) {
  return Clock::MonthsString[static_cast<uint8_t>(month)];
}

const char *Clock::DayOfWeekToString(Pinetime::Controllers::DateTime::Days dayOfWeek) {
  return Clock::DaysString[static_cast<uint8_t>(dayOfWeek)];
}

char const *Clock::DaysString[] = {
        "",
        "MONDAY",
        "TUESDAY",
        "WEDNESDAY",
        "THURSDAY",
        "FRIDAY",
        "SATURDAY",
        "SUNDAY"
};

char const *Clock::MonthsString[] = {
        "",
        "JAN",
        "FEB",
        "MAR",
        "APR",
        "MAY",
        "JUN",
        "JUL",
        "AUG",
        "SEP",
        "OCT",
        "NOV",
        "DEC"
};