Consolidated time conversion logic

Consolidated 24 hour to 12 hour time conversion logic into function, addressed formatting issues, cleaned up code.
This commit is contained in:
Eli Weiss 2022-01-09 10:39:04 -05:00 committed by JF
parent 1813399959
commit 5be2f57a78
2 changed files with 94 additions and 87 deletions

View file

@ -2,6 +2,7 @@
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
#include <hal/nrf_rtc.h> #include <hal/nrf_rtc.h>
#include <nrf_log.h> #include <nrf_log.h>
#include <string.h>
#include "displayapp/DisplayApp.h" #include "displayapp/DisplayApp.h"
#include "displayapp/screens/Symbols.h" #include "displayapp/screens/Symbols.h"
#include "components/settings/Settings.h" #include "components/settings/Settings.h"
@ -17,25 +18,29 @@ namespace {
constexpr int16_t POS_Y_MINUS = 40; constexpr int16_t POS_Y_MINUS = 40;
constexpr int16_t OFS_Y_COLON = -2; constexpr int16_t OFS_Y_COLON = -2;
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<SettingSetTime *>(obj->user_data); auto* screen = static_cast<SettingSetTime*>(obj->user_data);
screen->HandleButtonPress(obj, event); screen->HandleButtonPress(obj, event);
} }
} }
SettingSetTime::SettingSetTime( struct Time12H {
Pinetime::Applications::DisplayApp *app, std::string ampm;
Pinetime::Controllers::DateTime& dateTimeController, int hours;
Pinetime::Controllers::Settings& settingsController) };
: Screen(app),
dateTimeController {dateTimeController}, Time12H timeConvert(int time24H);
settingsController {settingsController} {
lv_obj_t * title = lv_label_create(lv_scr_act(), nullptr); SettingSetTime::SettingSetTime(Pinetime::Applications::DisplayApp* app,
Pinetime::Controllers::DateTime& dateTimeController,
Pinetime::Controllers::Settings& settingsController)
: Screen(app), dateTimeController {dateTimeController}, settingsController {settingsController} {
lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(title, "Set current time"); lv_label_set_text_static(title, "Set current time");
lv_label_set_align(title, LV_LABEL_ALIGN_CENTER); lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15); lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15);
lv_obj_t * icon = lv_label_create(lv_scr_act(), nullptr); lv_obj_t* icon = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE); lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE);
lv_label_set_text_static(icon, Symbols::clock); lv_label_set_text_static(icon, Symbols::clock);
@ -50,7 +55,7 @@ SettingSetTime::SettingSetTime(
lv_obj_align(lblHours, lv_scr_act(), LV_ALIGN_CENTER, POS_X_HOURS, POS_Y_TEXT); lv_obj_align(lblHours, lv_scr_act(), LV_ALIGN_CENTER, POS_X_HOURS, POS_Y_TEXT);
lv_obj_set_auto_realign(lblHours, true); lv_obj_set_auto_realign(lblHours, true);
lv_obj_t * lblColon1 = lv_label_create(lv_scr_act(), nullptr); lv_obj_t* lblColon1 = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(lblColon1, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); lv_obj_set_style_local_text_font(lblColon1, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_label_set_text_static(lblColon1, ":"); lv_label_set_text_static(lblColon1, ":");
lv_label_set_align(lblColon1, LV_LABEL_ALIGN_CENTER); lv_label_set_align(lblColon1, LV_LABEL_ALIGN_CENTER);
@ -64,13 +69,13 @@ SettingSetTime::SettingSetTime(
lv_obj_align(lblMinutes, lv_scr_act(), LV_ALIGN_CENTER, POS_X_MINUTES, POS_Y_TEXT); lv_obj_align(lblMinutes, lv_scr_act(), LV_ALIGN_CENTER, POS_X_MINUTES, POS_Y_TEXT);
lv_obj_set_auto_realign(lblMinutes, true); lv_obj_set_auto_realign(lblMinutes, true);
lv_obj_t * lblColon2 = lv_label_create(lv_scr_act(), nullptr); lv_obj_t* lblColon2 = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(lblColon2, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); lv_obj_set_style_local_text_font(lblColon2, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_label_set_text_static(lblColon2, ":"); lv_label_set_text_static(lblColon2, ":");
lv_label_set_align(lblColon2, LV_LABEL_ALIGN_CENTER); lv_label_set_align(lblColon2, LV_LABEL_ALIGN_CENTER);
lv_obj_align(lblColon2, lv_scr_act(), LV_ALIGN_CENTER, (POS_X_MINUTES + POS_X_SECONDS) / 2, POS_Y_TEXT + OFS_Y_COLON); lv_obj_align(lblColon2, lv_scr_act(), LV_ALIGN_CENTER, (POS_X_MINUTES + POS_X_SECONDS) / 2, POS_Y_TEXT + OFS_Y_COLON);
lv_obj_t * lblSeconds = lv_label_create(lv_scr_act(), nullptr); lv_obj_t* lblSeconds = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(lblSeconds, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); lv_obj_set_style_local_text_font(lblSeconds, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_label_set_text_static(lblSeconds, "00"); lv_label_set_text_static(lblSeconds, "00");
lv_label_set_align(lblSeconds, LV_LABEL_ALIGN_CENTER); lv_label_set_align(lblSeconds, LV_LABEL_ALIGN_CENTER);
@ -78,21 +83,9 @@ SettingSetTime::SettingSetTime(
lblampm = lv_label_create(lv_scr_act(), nullptr); lblampm = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20); lv_obj_set_style_local_text_font(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20);
lv_label_set_text_static(lblampm, " ");
lv_label_set_align(lblampm, LV_LABEL_ALIGN_CENTER); lv_label_set_align(lblampm, LV_LABEL_ALIGN_CENTER);
lv_obj_align(lblampm, lv_scr_act(), LV_ALIGN_CENTER, POS_X_SECONDS, POS_Y_TEXT - 40); lv_obj_align(lblampm, lv_scr_act(), LV_ALIGN_CENTER, POS_X_SECONDS, POS_Y_PLUS);
lv_label_set_text_fmt(lblampm, "%02c", ampmValue);
if (settingsController.GetClockType() == Controllers::Settings::ClockType::H24)
lv_label_set_text(lblampm, " ");
else {
if (hoursValue > 11)
lv_label_set_text(lblampm, "PM");
else
lv_label_set_text(lblampm, "AM");
if (hoursValue > 12)
lv_label_set_text_fmt(lblHours, "%02d", hoursValue-12);
else if (hoursValue == 0)
lv_label_set_text_fmt(lblHours, "%02d", hoursValue+1);
}
btnHoursPlus = lv_btn_create(lv_scr_act(), nullptr); btnHoursPlus = lv_btn_create(lv_scr_act(), nullptr);
btnHoursPlus->user_data = this; btnHoursPlus->user_data = this;
@ -128,74 +121,90 @@ SettingSetTime::SettingSetTime(
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_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);
if (settingsController.GetClockType() == Controllers::Settings::ClockType::H12) {
struct Time12H convertedTime = timeConvert(hoursValue);
if (hoursValue < 12) {
lv_label_set_text_static(lblampm, "AM");
} else {
lv_label_set_text_static(lblampm, "PM");
}
lv_label_set_text_fmt(lblHours, "%02d", convertedTime.hours);
}
} }
SettingSetTime::~SettingSetTime() { SettingSetTime::~SettingSetTime() {
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
} }
int timeConvert(int time24H) { Time12H timeConvert(int time24H) {
switch (time24H) { struct Time12H time12H;
case 0: switch (time24H) {
return 12; case 0:
break; time12H.hours = 12;
case 1 ... 12: time12H.ampm = "AM";
return time24H; break;
break; case 1 ... 11:
case 13 ... 23: time12H.hours = time24H;
return time24H - 12; time12H.ampm = "AM";
break; break;
default: case 12:
return 99; time12H.hours = 12;
break; time12H.ampm = "PM";
} break;
case 13 ... 23:
time12H.hours = time24H - 12;
time12H.ampm = "PM";
break;
}
return time12H;
} }
void SettingSetTime::HandleButtonPress(lv_obj_t *object, lv_event_t event) { void SettingSetTime::HandleButtonPress(lv_obj_t* object, lv_event_t event) {
bool is24H;
if (event != LV_EVENT_CLICKED) if (event != LV_EVENT_CLICKED)
return; return;
if (settingsController.GetClockType() == Controllers::Settings::ClockType::H24) is24H = (settingsController.GetClockType() == Controllers::Settings::ClockType::H24);
is24H = true;
else
is24H = false;
if (object == btnHoursPlus) { if (object == btnHoursPlus) {
hoursValue++; hoursValue++;
if (hoursValue > 23) if (hoursValue > 23) {
hoursValue = 0; hoursValue = 0;
}
struct Time12H convertedTime = timeConvert(hoursValue);
if (!is24H) { if (!is24H) {
if (hoursValue < 12) lv_label_set_text_static(lblampm, convertedTime.ampm.c_str());
lv_label_set_text(lblampm, "AM"); lv_label_set_text_fmt(lblHours, "%02d", convertedTime.hours);
else } else {
lv_label_set_text(lblampm, "PM");
lv_label_set_text_fmt(lblHours, "%02d", timeConvert(hoursValue));
} else
lv_label_set_text_fmt(lblHours, "%02d", hoursValue); lv_label_set_text_fmt(lblHours, "%02d", hoursValue);
}
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED); lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
} else if (object == btnHoursMinus) { } else if (object == btnHoursMinus) {
hoursValue--; hoursValue--;
if (hoursValue < 0) if (hoursValue < 0) {
hoursValue = 23; hoursValue = 23;
}
struct Time12H convertedTime = timeConvert(hoursValue);
if (!is24H) { if (!is24H) {
if (hoursValue < 12) lv_label_set_text_static(lblampm, convertedTime.ampm.c_str());
lv_label_set_text(lblampm, "AM"); lv_label_set_text_fmt(lblHours, "%02d", convertedTime.hours);
else } else {
lv_label_set_text(lblampm, "PM");
lv_label_set_text_fmt(lblHours, "%02d", timeConvert(hoursValue));
} else
lv_label_set_text_fmt(lblHours, "%02d", hoursValue); lv_label_set_text_fmt(lblHours, "%02d", hoursValue);
}
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED); lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
} else if (object == btnMinutesPlus) { } else if (object == btnMinutesPlus) {
minutesValue++; minutesValue++;
if (minutesValue > 59) if (minutesValue > 59) {
minutesValue = 0; minutesValue = 0;
}
lv_label_set_text_fmt(lblMinutes, "%02d", minutesValue); lv_label_set_text_fmt(lblMinutes, "%02d", minutesValue);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED); lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
} else if (object == btnMinutesMinus) { } else if (object == btnMinutesMinus) {
minutesValue--; minutesValue--;
if (minutesValue < 0) if (minutesValue < 0) {
minutesValue = 59; minutesValue = 59;
}
lv_label_set_text_fmt(lblMinutes, "%02d", minutesValue); lv_label_set_text_fmt(lblMinutes, "%02d", minutesValue);
lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED); lv_btn_set_state(btnSetTime, LV_BTN_STATE_RELEASED);
} else if (object == btnSetTime) { } else if (object == btnSetTime) {

View file

@ -9,31 +9,29 @@
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
namespace Screens { namespace Screens {
class SettingSetTime : public Screen{ class SettingSetTime : public Screen {
public: public:
SettingSetTime(DisplayApp* app, SettingSetTime(DisplayApp* app,
Pinetime::Controllers::DateTime& dateTimeController, Pinetime::Controllers::DateTime& dateTimeController,
Pinetime::Controllers::Settings& settingsController); Pinetime::Controllers::Settings& settingsController);
~SettingSetTime() override; ~SettingSetTime() override;
void HandleButtonPress(lv_obj_t *object, lv_event_t event); void HandleButtonPress(lv_obj_t* object, lv_event_t event);
private: private:
Controllers::DateTime& dateTimeController; Controllers::DateTime& dateTimeController;
Controllers::Settings& settingsController; Controllers::Settings& settingsController;
int hoursValue; int hoursValue;
int minutesValue; int minutesValue;
char ampmValue[3]; lv_obj_t* lblHours;
bool is24H; lv_obj_t* lblMinutes;
lv_obj_t * lblHours; lv_obj_t* lblampm;
lv_obj_t * lblMinutes; lv_obj_t* btnHoursPlus;
lv_obj_t * lblampm; lv_obj_t* btnHoursMinus;
lv_obj_t * btnHoursPlus; lv_obj_t* btnMinutesPlus;
lv_obj_t * btnHoursMinus; lv_obj_t* btnMinutesMinus;
lv_obj_t * btnMinutesPlus; lv_obj_t* btnSetTime;
lv_obj_t * btnMinutesMinus;
lv_obj_t * btnSetTime;
}; };
} }
} }