Merge branch 'InfiniTimeOrg:develop' into workflow-ignore-md

This commit is contained in:
mashuptwice 2022-08-11 01:43:19 +02:00 committed by GitHub
commit a86668b6b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 165 deletions

View file

@ -11,13 +11,36 @@ namespace {
constexpr int16_t POS_X_DAY = -72; constexpr int16_t POS_X_DAY = -72;
constexpr int16_t POS_X_MONTH = 0; constexpr int16_t POS_X_MONTH = 0;
constexpr int16_t POS_X_YEAR = 72; constexpr int16_t POS_X_YEAR = 72;
constexpr int16_t POS_Y_PLUS = -50;
constexpr int16_t POS_Y_TEXT = -6; constexpr int16_t POS_Y_TEXT = -6;
constexpr int16_t POS_Y_MINUS = 40;
void event_handler(lv_obj_t* obj, lv_event_t event) { void event_handler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<SettingSetDate*>(obj->user_data); auto* screen = static_cast<SettingSetDate*>(obj->user_data);
screen->HandleButtonPress(obj, event); if (event == LV_EVENT_CLICKED) {
screen->HandleButtonPress();
}
}
void ValueChangedHandler(void* userData) {
auto* screen = static_cast<SettingSetDate*>(userData);
screen->CheckDay();
}
int MaximumDayOfMonth(uint8_t month, uint16_t year) {
switch (month) {
case 2: {
if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) {
return 29;
}
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
} }
} }
@ -35,128 +58,44 @@ SettingSetDate::SettingSetDate(Pinetime::Applications::DisplayApp* app, Pinetime
lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER); lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER);
lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0); lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0);
dayValue = static_cast<int>(dateTimeController.Day()); dayCounter.SetValueChangedEventCallback(this, ValueChangedHandler);
lblDay = lv_label_create(lv_scr_act(), nullptr); dayCounter.Create();
lv_label_set_text_fmt(lblDay, "%d", dayValue); dayCounter.SetValue(dateTimeController.Day());
lv_label_set_align(lblDay, LV_LABEL_ALIGN_CENTER); lv_obj_align(dayCounter.GetObject(), nullptr, LV_ALIGN_CENTER, POS_X_DAY, POS_Y_TEXT);
lv_obj_align(lblDay, lv_scr_act(), LV_ALIGN_CENTER, POS_X_DAY, POS_Y_TEXT);
lv_obj_set_auto_realign(lblDay, true);
monthValue = static_cast<int>(dateTimeController.Month()); monthCounter.EnableMonthMode();
lblMonth = lv_label_create(lv_scr_act(), nullptr); monthCounter.SetValueChangedEventCallback(this, ValueChangedHandler);
UpdateMonthLabel(); monthCounter.Create();
lv_label_set_align(lblMonth, LV_LABEL_ALIGN_CENTER); monthCounter.SetValue(static_cast<int>(dateTimeController.Month()));
lv_obj_align(lblMonth, lv_scr_act(), LV_ALIGN_CENTER, POS_X_MONTH, POS_Y_TEXT); lv_obj_align(monthCounter.GetObject(), nullptr, LV_ALIGN_CENTER, POS_X_MONTH, POS_Y_TEXT);
lv_obj_set_auto_realign(lblMonth, true);
yearValue = static_cast<int>(dateTimeController.Year()); yearCounter.SetValueChangedEventCallback(this, ValueChangedHandler);
if (yearValue < 2021) yearCounter.Create();
yearValue = 2021; yearCounter.SetValue(dateTimeController.Year());
lblYear = lv_label_create(lv_scr_act(), nullptr); lv_obj_align(yearCounter.GetObject(), nullptr, LV_ALIGN_CENTER, POS_X_YEAR, POS_Y_TEXT);
lv_label_set_text_fmt(lblYear, "%d", yearValue);
lv_label_set_align(lblYear, LV_LABEL_ALIGN_CENTER);
lv_obj_align(lblYear, lv_scr_act(), LV_ALIGN_CENTER, POS_X_YEAR, POS_Y_TEXT);
lv_obj_set_auto_realign(lblYear, true);
btnDayPlus = lv_btn_create(lv_scr_act(), nullptr);
btnDayPlus->user_data = this;
lv_obj_set_size(btnDayPlus, 50, 40);
lv_obj_align(btnDayPlus, lv_scr_act(), LV_ALIGN_CENTER, POS_X_DAY, POS_Y_PLUS);
lv_obj_set_style_local_value_str(btnDayPlus, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "+");
lv_obj_set_event_cb(btnDayPlus, event_handler);
btnDayMinus = lv_btn_create(lv_scr_act(), nullptr);
btnDayMinus->user_data = this;
lv_obj_set_size(btnDayMinus, 50, 40);
lv_obj_align(btnDayMinus, lv_scr_act(), LV_ALIGN_CENTER, POS_X_DAY, POS_Y_MINUS);
lv_obj_set_style_local_value_str(btnDayMinus, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "-");
lv_obj_set_event_cb(btnDayMinus, event_handler);
btnMonthPlus = lv_btn_create(lv_scr_act(), nullptr);
btnMonthPlus->user_data = this;
lv_obj_set_size(btnMonthPlus, 50, 40);
lv_obj_align(btnMonthPlus, lv_scr_act(), LV_ALIGN_CENTER, POS_X_MONTH, POS_Y_PLUS);
lv_obj_set_style_local_value_str(btnMonthPlus, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "+");
lv_obj_set_event_cb(btnMonthPlus, event_handler);
btnMonthMinus = lv_btn_create(lv_scr_act(), nullptr);
btnMonthMinus->user_data = this;
lv_obj_set_size(btnMonthMinus, 50, 40);
lv_obj_align(btnMonthMinus, lv_scr_act(), LV_ALIGN_CENTER, POS_X_MONTH, POS_Y_MINUS);
lv_obj_set_style_local_value_str(btnMonthMinus, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "-");
lv_obj_set_event_cb(btnMonthMinus, event_handler);
btnYearPlus = lv_btn_create(lv_scr_act(), nullptr);
btnYearPlus->user_data = this;
lv_obj_set_size(btnYearPlus, 50, 40);
lv_obj_align(btnYearPlus, lv_scr_act(), LV_ALIGN_CENTER, POS_X_YEAR, POS_Y_PLUS);
lv_obj_set_style_local_value_str(btnYearPlus, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "+");
lv_obj_set_event_cb(btnYearPlus, event_handler);
btnYearMinus = lv_btn_create(lv_scr_act(), nullptr);
btnYearMinus->user_data = this;
lv_obj_set_size(btnYearMinus, 50, 40);
lv_obj_align(btnYearMinus, lv_scr_act(), LV_ALIGN_CENTER, POS_X_YEAR, POS_Y_MINUS);
lv_obj_set_style_local_value_str(btnYearMinus, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "-");
lv_obj_set_event_cb(btnYearMinus, event_handler);
btnSetTime = lv_btn_create(lv_scr_act(), nullptr); btnSetTime = lv_btn_create(lv_scr_act(), nullptr);
btnSetTime->user_data = this; btnSetTime->user_data = this;
lv_obj_set_size(btnSetTime, 120, 48); lv_obj_set_size(btnSetTime, 120, 48);
lv_obj_align(btnSetTime, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); lv_obj_align(btnSetTime, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0);
lv_obj_set_style_local_bg_color(btnSetTime, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
lv_obj_set_style_local_value_str(btnSetTime, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "Set"); lv_obj_set_style_local_value_str(btnSetTime, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "Set");
lv_obj_set_event_cb(btnSetTime, event_handler); lv_obj_set_event_cb(btnSetTime, event_handler);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_DISABLED);
} }
SettingSetDate::~SettingSetDate() { SettingSetDate::~SettingSetDate() {
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
} }
void SettingSetDate::HandleButtonPress(lv_obj_t* object, lv_event_t event) { void SettingSetDate::HandleButtonPress() {
if (event != LV_EVENT_CLICKED) const uint16_t yearValue = yearCounter.GetValue();
return; const uint8_t monthValue = monthCounter.GetValue();
const uint8_t dayValue = dayCounter.GetValue();
if (object == btnDayPlus) {
dayValue++;
if (dayValue > MaximumDayOfMonth())
dayValue = 1;
lv_label_set_text_fmt(lblDay, "%d", dayValue);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
} else if (object == btnDayMinus) {
dayValue--;
if (dayValue < 1)
dayValue = MaximumDayOfMonth();
lv_label_set_text_fmt(lblDay, "%d", dayValue);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
} else if (object == btnMonthPlus) {
monthValue++;
if (monthValue > 12)
monthValue = 1;
UpdateMonthLabel();
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
CheckDay();
} else if (object == btnMonthMinus) {
monthValue--;
if (monthValue < 1)
monthValue = 12;
UpdateMonthLabel();
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
CheckDay();
} else if (object == btnYearPlus) {
yearValue++;
lv_label_set_text_fmt(lblYear, "%d", yearValue);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
CheckDay();
} else if (object == btnYearMinus) {
yearValue--;
lv_label_set_text_fmt(lblYear, "%d", yearValue);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
CheckDay();
} else if (object == btnSetTime) {
NRF_LOG_INFO("Setting date (manually) to %04d-%02d-%02d", yearValue, monthValue, dayValue); NRF_LOG_INFO("Setting date (manually) to %04d-%02d-%02d", yearValue, monthValue, dayValue);
dateTimeController.SetTime(static_cast<uint16_t>(yearValue), dateTimeController.SetTime(yearValue,
static_cast<uint8_t>(monthValue), monthValue,
static_cast<uint8_t>(dayValue), dayValue,
0, 0,
dateTimeController.Hours(), dateTimeController.Hours(),
dateTimeController.Minutes(), dateTimeController.Minutes(),
@ -164,35 +103,9 @@ void SettingSetDate::HandleButtonPress(lv_obj_t* object, lv_event_t event) {
nrf_rtc_counter_get(portNRF_RTC_REG)); nrf_rtc_counter_get(portNRF_RTC_REG));
lv_btn_set_state(btnSetTime, LV_BTN_STATE_DISABLED); lv_btn_set_state(btnSetTime, LV_BTN_STATE_DISABLED);
} }
}
int SettingSetDate::MaximumDayOfMonth() const {
switch (monthValue) {
case 2:
if ((((yearValue % 4) == 0) && ((yearValue % 100) != 0)) || ((yearValue % 400) == 0))
return 29;
return 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
void SettingSetDate::CheckDay() { void SettingSetDate::CheckDay() {
int maxDay = MaximumDayOfMonth(); const int maxDay = MaximumDayOfMonth(monthCounter.GetValue(), yearCounter.GetValue());
if (dayValue > maxDay) { dayCounter.SetMax(maxDay);
dayValue = maxDay; lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
lv_label_set_text_fmt(lblDay, "%d", dayValue);
lv_obj_align(lblDay, lv_scr_act(), LV_ALIGN_CENTER, POS_X_DAY, POS_Y_TEXT);
}
}
void SettingSetDate::UpdateMonthLabel() {
lv_label_set_text_static(
lblMonth,
Pinetime::Controllers::DateTime::MonthShortToStringLow(static_cast<Pinetime::Controllers::DateTime::Months>(monthValue)));
} }

View file

@ -4,6 +4,7 @@
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
#include "components/datetime/DateTimeController.h" #include "components/datetime/DateTimeController.h"
#include "displayapp/screens/Screen.h" #include "displayapp/screens/Screen.h"
#include "displayapp/widgets/Counter.h"
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
@ -13,28 +14,17 @@ namespace Pinetime {
SettingSetDate(DisplayApp* app, Pinetime::Controllers::DateTime& dateTimeController); SettingSetDate(DisplayApp* app, Pinetime::Controllers::DateTime& dateTimeController);
~SettingSetDate() override; ~SettingSetDate() override;
void HandleButtonPress(lv_obj_t* object, lv_event_t event); void HandleButtonPress();
void CheckDay();
private: private:
Controllers::DateTime& dateTimeController; Controllers::DateTime& dateTimeController;
int dayValue;
int monthValue;
int yearValue;
lv_obj_t* lblDay;
lv_obj_t* lblMonth;
lv_obj_t* lblYear;
lv_obj_t* btnDayPlus;
lv_obj_t* btnDayMinus;
lv_obj_t* btnMonthPlus;
lv_obj_t* btnMonthMinus;
lv_obj_t* btnYearPlus;
lv_obj_t* btnYearMinus;
lv_obj_t* btnSetTime; lv_obj_t* btnSetTime;
int MaximumDayOfMonth() const; Widgets::Counter dayCounter = Widgets::Counter(1, 31, jetbrains_mono_bold_20);
void CheckDay(); Widgets::Counter monthCounter = Widgets::Counter(1, 12, jetbrains_mono_bold_20);
void UpdateMonthLabel(); Widgets::Counter yearCounter = Widgets::Counter(1970, 9999, jetbrains_mono_bold_20);
}; };
} }
} }

View file

@ -1,4 +1,5 @@
#include "displayapp/widgets/Counter.h" #include "displayapp/widgets/Counter.h"
#include "components/datetime/DateTimeController.h"
using namespace Pinetime::Applications::Widgets; using namespace Pinetime::Applications::Widgets;
@ -18,7 +19,7 @@ namespace {
} }
} }
Counter::Counter(int min, int max, lv_font_t& font) : min {min}, max {max}, font {font} { Counter::Counter(int min, int max, lv_font_t& font) : min {min}, max {max}, value {min}, font {font} {
} }
void Counter::UpBtnPressed() { void Counter::UpBtnPressed() {
@ -74,6 +75,8 @@ void Counter::UpdateLabel() {
} else { } else {
lv_label_set_text_fmt(number, "%.2i", value - 12); lv_label_set_text_fmt(number, "%.2i", value - 12);
} }
} else if (monthMode) {
lv_label_set_text(number, Controllers::DateTime::MonthShortToStringLow(static_cast<Controllers::DateTime::Months>(value)));
} else { } else {
lv_label_set_text_fmt(number, "%.2i", value); lv_label_set_text_fmt(number, "%.2i", value);
} }
@ -85,6 +88,20 @@ void Counter::EnableTwelveHourMode() {
twelveHourMode = true; twelveHourMode = true;
} }
// Value is kept between 1 and 12, but the displayed value is the corresponding month
// Make sure to set the max and min values to 1 and 12. Otherwise behaviour is undefined
void Counter::EnableMonthMode() {
monthMode = true;
}
void Counter::SetMax(int newMax) {
max = newMax;
if (value > max) {
value = max;
UpdateLabel();
}
}
void Counter::SetValueChangedEventCallback(void* userData, void (*handler)(void* userData)) { void Counter::SetValueChangedEventCallback(void* userData, void (*handler)(void* userData)) {
this->userData = userData; this->userData = userData;
this->ValueChangedHandler = handler; this->ValueChangedHandler = handler;
@ -100,10 +117,14 @@ void Counter::Create() {
lv_obj_set_style_local_text_font(number, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &font); lv_obj_set_style_local_text_font(number, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &font);
lv_obj_align(number, nullptr, LV_ALIGN_CENTER, 0, 0); lv_obj_align(number, nullptr, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_auto_realign(number, true); lv_obj_set_auto_realign(number, true);
lv_label_set_text_static(number, "00"); if (monthMode) {
lv_label_set_text_static(number, "Jan");
} else {
lv_label_set_text_fmt(number, "%d", max);
}
static constexpr uint8_t padding = 5; static constexpr uint8_t padding = 5;
const uint8_t width = lv_obj_get_width(number) + padding * 2; const uint8_t width = std::max(lv_obj_get_width(number) + padding * 2, 58);
static constexpr uint8_t btnHeight = 50; static constexpr uint8_t btnHeight = 50;
const uint8_t containerHeight = btnHeight * 2 + lv_obj_get_height(number) + padding * 2; const uint8_t containerHeight = btnHeight * 2 + lv_obj_get_height(number) + padding * 2;

View file

@ -15,6 +15,8 @@ namespace Pinetime {
void HideControls(); void HideControls();
void ShowControls(); void ShowControls();
void EnableTwelveHourMode(); void EnableTwelveHourMode();
void EnableMonthMode();
void SetMax(int newMax);
void SetValueChangedEventCallback(void* userData, void (*handler)(void* userData)); void SetValueChangedEventCallback(void* userData, void (*handler)(void* userData));
int GetValue() const { int GetValue() const {
@ -36,10 +38,11 @@ namespace Pinetime {
lv_obj_t* upperLine; lv_obj_t* upperLine;
lv_obj_t* lowerLine; lv_obj_t* lowerLine;
lv_point_t linePoints[2]; lv_point_t linePoints[2];
int value = 0;
int min; int min;
int max; int max;
int value;
bool twelveHourMode = false; bool twelveHourMode = false;
bool monthMode = false;
lv_font_t& font; lv_font_t& font;
void* userData = nullptr; void* userData = nullptr;