1
0
Fork 0

Use Counter widget in Alarm

This commit is contained in:
Riku Isokoski 2022-07-22 08:33:15 +03:00 committed by JF
parent 9f851f6321
commit 97048121b0
3 changed files with 61 additions and 117 deletions

View file

@ -368,7 +368,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
currentScreen = std::make_unique<Screens::Timer>(this, timerController); currentScreen = std::make_unique<Screens::Timer>(this, timerController);
break; break;
case Apps::Alarm: case Apps::Alarm:
currentScreen = std::make_unique<Screens::Alarm>(this, alarmController, settingsController, *systemTask); currentScreen = std::make_unique<Screens::Alarm>(this, alarmController, settingsController.GetClockType(), *systemTask);
break; break;
// Settings // Settings

View file

@ -22,6 +22,13 @@
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
using Pinetime::Controllers::AlarmController; using Pinetime::Controllers::AlarmController;
namespace {
void ValueChangedHandler(void* userData) {
auto* screen = static_cast<Alarm*>(userData);
screen->OnValueChanged();
}
}
static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<Alarm*>(obj->user_data); auto* screen = static_cast<Alarm*>(obj->user_data);
screen->OnButtonEvent(obj, event); screen->OnButtonEvent(obj, event);
@ -34,59 +41,34 @@ static void StopAlarmTaskCallback(lv_task_t* task) {
Alarm::Alarm(DisplayApp* app, Alarm::Alarm(DisplayApp* app,
Controllers::AlarmController& alarmController, Controllers::AlarmController& alarmController,
Pinetime::Controllers::Settings& settingsController, Controllers::Settings::ClockType clockType,
System::SystemTask& systemTask) System::SystemTask& systemTask)
: Screen(app), alarmController {alarmController}, settingsController {settingsController}, systemTask {systemTask} { : Screen(app), alarmController {alarmController}, clockType {clockType}, systemTask {systemTask} {
time = lv_label_create(lv_scr_act(), nullptr); hourCounter.Create();
lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); lv_obj_align(hourCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); if (clockType == Controllers::Settings::ClockType::H12) {
hourCounter.EnableTwelveHourMode();
}
hourCounter.SetValue(alarmController.Hours());
hourCounter.SetValueChangedEventCallback(this, ValueChangedHandler);
alarmHours = alarmController.Hours(); minuteCounter.Create();
alarmMinutes = alarmController.Minutes(); lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
lv_label_set_text_fmt(time, "%02hhu:%02hhu", alarmHours, alarmMinutes); minuteCounter.SetValue(alarmController.Minutes());
minuteCounter.SetValueChangedEventCallback(this, ValueChangedHandler);
lv_obj_align(time, lv_scr_act(), LV_ALIGN_CENTER, 0, -25); lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(colonLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
lv_label_set_text_static(colonLabel, ":");
lv_obj_align(colonLabel, lv_scr_act(), LV_ALIGN_CENTER, 0, -29);
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_obj_set_style_local_text_color(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
lv_label_set_text_static(lblampm, " "); 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, 0, 30); lv_obj_align(lblampm, lv_scr_act(), LV_ALIGN_CENTER, 0, 30);
btnHoursUp = lv_btn_create(lv_scr_act(), nullptr);
btnHoursUp->user_data = this;
lv_obj_set_event_cb(btnHoursUp, btnEventHandler);
lv_obj_set_size(btnHoursUp, 60, 40);
lv_obj_align(btnHoursUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -85);
txtHrUp = lv_label_create(btnHoursUp, nullptr);
lv_label_set_text_static(txtHrUp, "+");
btnHoursDown = lv_btn_create(lv_scr_act(), nullptr);
btnHoursDown->user_data = this;
lv_obj_set_event_cb(btnHoursDown, btnEventHandler);
lv_obj_set_size(btnHoursDown, 60, 40);
lv_obj_align(btnHoursDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, 35);
txtHrDown = lv_label_create(btnHoursDown, nullptr);
lv_label_set_text_static(txtHrDown, "-");
btnMinutesUp = lv_btn_create(lv_scr_act(), nullptr);
btnMinutesUp->user_data = this;
lv_obj_set_event_cb(btnMinutesUp, btnEventHandler);
lv_obj_set_size(btnMinutesUp, 60, 40);
lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, -85);
txtMinUp = lv_label_create(btnMinutesUp, nullptr);
lv_label_set_text_static(txtMinUp, "+");
btnMinutesDown = lv_btn_create(lv_scr_act(), nullptr);
btnMinutesDown->user_data = this;
lv_obj_set_event_cb(btnMinutesDown, btnEventHandler);
lv_obj_set_size(btnMinutesDown, 60, 40);
lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, 35);
txtMinDown = lv_label_create(btnMinutesDown, nullptr);
lv_label_set_text_static(txtMinDown, "-");
btnStop = lv_btn_create(lv_scr_act(), nullptr); btnStop = lv_btn_create(lv_scr_act(), nullptr);
btnStop->user_data = this; btnStop->user_data = this;
lv_obj_set_event_cb(btnStop, btnEventHandler); lv_obj_set_event_cb(btnStop, btnEventHandler);
@ -104,14 +86,15 @@ Alarm::Alarm(DisplayApp* app,
lv_obj_align(btnRecur, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0); lv_obj_align(btnRecur, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
txtRecur = lv_label_create(btnRecur, nullptr); txtRecur = lv_label_create(btnRecur, nullptr);
SetRecurButtonState(); SetRecurButtonState();
lv_obj_set_style_local_bg_color(btnRecur, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
// Invisible button above the time
btnInfo = lv_btn_create(lv_scr_act(), nullptr); btnInfo = lv_btn_create(lv_scr_act(), nullptr);
btnInfo->user_data = this; btnInfo->user_data = this;
lv_obj_set_event_cb(btnInfo, btnEventHandler); lv_obj_set_event_cb(btnInfo, btnEventHandler);
lv_obj_set_size(btnInfo, 50, 40); lv_obj_set_size(btnInfo, LV_HOR_RES, 80);
lv_obj_align(btnInfo, lv_scr_act(), LV_ALIGN_CENTER, 0, -85); lv_obj_align(btnInfo, lv_scr_act(), LV_ALIGN_CENTER, 0, -29);
txtInfo = lv_label_create(btnInfo, nullptr); lv_obj_set_style_local_bg_opa(btnInfo, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_label_set_text_static(txtInfo, "i");
enableSwitch = lv_switch_create(lv_scr_act(), nullptr); enableSwitch = lv_switch_create(lv_scr_act(), nullptr);
enableSwitch->user_data = this; enableSwitch->user_data = this;
@ -119,6 +102,7 @@ Alarm::Alarm(DisplayApp* app,
lv_obj_set_size(enableSwitch, 100, 50); lv_obj_set_size(enableSwitch, 100, 50);
// Align to the center of 115px from edge // Align to the center of 115px from edge
lv_obj_align(enableSwitch, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 7, 0); lv_obj_align(enableSwitch, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 7, 0);
lv_obj_set_style_local_bg_color(enableSwitch, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
UpdateAlarmTime(); UpdateAlarmTime();
@ -136,8 +120,14 @@ Alarm::~Alarm() {
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
} }
void Alarm::DisableAlarm() {
if (alarmController.State() == AlarmController::AlarmState::Set) {
alarmController.DisableAlarm();
lv_switch_off(enableSwitch, LV_ANIM_ON);
}
}
void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
using Pinetime::Controllers::AlarmController;
if (event == LV_EVENT_CLICKED) { if (event == LV_EVENT_CLICKED) {
if (obj == btnStop) { if (obj == btnStop) {
StopAlerting(); StopAlerting();
@ -159,49 +149,8 @@ void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
} }
return; return;
} }
// If any other button was pressed, disable the alarm
// this is to make it clear that the alarm won't be set until it is turned back on
if (alarmController.State() == AlarmController::AlarmState::Set) {
alarmController.DisableAlarm();
lv_switch_off(enableSwitch, LV_ANIM_ON);
}
if (obj == btnMinutesUp) {
if (alarmMinutes >= 59) {
alarmMinutes = 0;
} else {
alarmMinutes++;
}
UpdateAlarmTime();
return;
}
if (obj == btnMinutesDown) {
if (alarmMinutes == 0) {
alarmMinutes = 59;
} else {
alarmMinutes--;
}
UpdateAlarmTime();
return;
}
if (obj == btnHoursUp) {
if (alarmHours >= 23) {
alarmHours = 0;
} else {
alarmHours++;
}
UpdateAlarmTime();
return;
}
if (obj == btnHoursDown) {
if (alarmHours == 0) {
alarmHours = 23;
} else {
alarmHours--;
}
UpdateAlarmTime();
return;
}
if (obj == btnRecur) { if (obj == btnRecur) {
DisableAlarm();
ToggleRecurrence(); ToggleRecurrence();
} }
} }
@ -224,30 +173,20 @@ bool Alarm::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
return alarmController.State() == AlarmController::AlarmState::Alerting && event == TouchEvents::SwipeDown; return alarmController.State() == AlarmController::AlarmState::Alerting && event == TouchEvents::SwipeDown;
} }
void Alarm::OnValueChanged() {
DisableAlarm();
UpdateAlarmTime();
}
void Alarm::UpdateAlarmTime() { void Alarm::UpdateAlarmTime() {
if (settingsController.GetClockType() == Controllers::Settings::ClockType::H12) { if (clockType == Controllers::Settings::ClockType::H12) {
switch (alarmHours) { if (hourCounter.GetValue() >= 12) {
case 0:
lv_label_set_text_static(lblampm, "AM");
lv_label_set_text_fmt(time, "%02d:%02d", 12, alarmMinutes);
break;
case 1 ... 11:
lv_label_set_text_static(lblampm, "AM");
lv_label_set_text_fmt(time, "%02d:%02d", alarmHours, alarmMinutes);
break;
case 12:
lv_label_set_text_static(lblampm, "PM"); lv_label_set_text_static(lblampm, "PM");
lv_label_set_text_fmt(time, "%02d:%02d", 12, alarmMinutes);
break;
case 13 ... 23:
lv_label_set_text_static(lblampm, "PM");
lv_label_set_text_fmt(time, "%02d:%02d", alarmHours - 12, alarmMinutes);
break;
}
} else { } else {
lv_label_set_text_fmt(time, "%02d:%02d", alarmHours, alarmMinutes); lv_label_set_text_static(lblampm, "AM");
} }
alarmController.SetAlarmTime(alarmHours, alarmMinutes); }
alarmController.SetAlarmTime(hourCounter.GetValue(), minuteCounter.GetValue());
} }
void Alarm::SetAlerting() { void Alarm::SetAlerting() {
@ -283,6 +222,9 @@ void Alarm::SetSwitchState(lv_anim_enable_t anim) {
} }
void Alarm::ShowInfo() { void Alarm::ShowInfo() {
if (btnMessage != nullptr) {
return;
}
btnMessage = lv_btn_create(lv_scr_act(), nullptr); btnMessage = lv_btn_create(lv_scr_act(), nullptr);
btnMessage->user_data = this; btnMessage->user_data = this;
lv_obj_set_event_cb(btnMessage, btnEventHandler); lv_obj_set_event_cb(btnMessage, btnEventHandler);

View file

@ -21,6 +21,7 @@
#include "systemtask/SystemTask.h" #include "systemtask/SystemTask.h"
#include "displayapp/LittleVgl.h" #include "displayapp/LittleVgl.h"
#include "components/alarm/AlarmController.h" #include "components/alarm/AlarmController.h"
#include "displayapp/widgets/Counter.h"
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
@ -29,29 +30,28 @@ namespace Pinetime {
public: public:
Alarm(DisplayApp* app, Alarm(DisplayApp* app,
Controllers::AlarmController& alarmController, Controllers::AlarmController& alarmController,
Pinetime::Controllers::Settings& settingsController, Controllers::Settings::ClockType clockType,
System::SystemTask& systemTask); System::SystemTask& systemTask);
~Alarm() override; ~Alarm() override;
void SetAlerting(); void SetAlerting();
void OnButtonEvent(lv_obj_t* obj, lv_event_t event); void OnButtonEvent(lv_obj_t* obj, lv_event_t event);
bool OnButtonPushed() override; bool OnButtonPushed() override;
bool OnTouchEvent(TouchEvents event) override; bool OnTouchEvent(TouchEvents event) override;
void OnValueChanged();
void StopAlerting(); void StopAlerting();
private: private:
uint8_t alarmHours;
uint8_t alarmMinutes;
Controllers::AlarmController& alarmController; Controllers::AlarmController& alarmController;
Controllers::Settings& settingsController; const Controllers::Settings::ClockType clockType;
System::SystemTask& systemTask; System::SystemTask& systemTask;
lv_obj_t *time, *lblampm, *btnStop, *txtStop, *btnMinutesUp, *btnMinutesDown, *btnHoursUp, *btnHoursDown, *txtMinUp, *txtMinDown, lv_obj_t *lblampm, *btnStop, *txtStop, *btnRecur, *txtRecur, *btnInfo, *enableSwitch;
*txtHrUp, *txtHrDown, *btnRecur, *txtRecur, *btnInfo, *txtInfo, *enableSwitch;
lv_obj_t* txtMessage = nullptr; lv_obj_t* txtMessage = nullptr;
lv_obj_t* btnMessage = nullptr; lv_obj_t* btnMessage = nullptr;
lv_task_t* taskStopAlarm = nullptr; lv_task_t* taskStopAlarm = nullptr;
enum class EnableButtonState { On, Off, Alerting }; enum class EnableButtonState { On, Off, Alerting };
void DisableAlarm();
void SetRecurButtonState(); void SetRecurButtonState();
void SetSwitchState(lv_anim_enable_t anim); void SetSwitchState(lv_anim_enable_t anim);
void SetAlarm(); void SetAlarm();
@ -59,6 +59,8 @@ namespace Pinetime {
void HideInfo(); void HideInfo();
void ToggleRecurrence(); void ToggleRecurrence();
void UpdateAlarmTime(); void UpdateAlarmTime();
Widgets::Counter hourCounter = Widgets::Counter(0, 23, jetbrains_mono_76);
Widgets::Counter minuteCounter = Widgets::Counter(0, 59, jetbrains_mono_76);
}; };
}; };
}; };