Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alarm: Display the time until alarm, even if it is off #698

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/components/alarm/AlarmController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "systemtask/SystemTask.h"
#include "app_timer.h"
#include "task.h"
#include <chrono>

using namespace Pinetime::Controllers;
using namespace std::chrono_literals;
Expand Down Expand Up @@ -48,13 +47,9 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
minutes = alarmMin;
}

void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the app_timer
app_timer_stop(alarmAppTimer);

std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> AlarmController::CalculateAlarmTimePoint() {
auto now = dateTimeController.CurrentDateTime();
alarmTime = now;
time_t ttAlarmTime = std::chrono::system_clock::to_time_t(alarmTime);
time_t ttAlarmTime = std::chrono::system_clock::to_time_t(now);
tm* tmAlarmTime = std::localtime(&ttAlarmTime);

// If the time being set has already passed today,the alarm should be set for tomorrow
Expand All @@ -79,15 +74,21 @@ void AlarmController::ScheduleAlarm() {
tmAlarmTime->tm_isdst = -1; // use system timezone setting to determine DST

// now can convert back to a time_point
alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime));
auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count();
return std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime));
}

void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the app_timer
app_timer_stop(alarmAppTimer);

auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count();
app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this);

state = AlarmState::Set;
}

uint32_t AlarmController::SecondsToAlarm() {
return std::chrono::duration_cast<std::chrono::seconds>(alarmTime - dateTimeController.CurrentDateTime()).count();
return std::chrono::duration_cast<std::chrono::seconds>(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count();
}

void AlarmController::DisableAlarm() {
Expand Down
4 changes: 3 additions & 1 deletion src/components/alarm/AlarmController.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cstdint>
#include "app_timer.h"
#include "components/datetime/DateTimeController.h"
#include <chrono>

namespace Pinetime {
namespace System {
Expand Down Expand Up @@ -56,11 +57,12 @@ namespace Pinetime {
}

private:
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> CalculateAlarmTimePoint();

Controllers::DateTime& dateTimeController;
System::SystemTask* systemTask = nullptr;
uint8_t hours = 7;
uint8_t minutes = 0;
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime;
AlarmState state = AlarmState::Not_Set;
RecurType recurrence = RecurType::None;
};
Expand Down
22 changes: 9 additions & 13 deletions src/displayapp/screens/Alarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,15 @@ void Alarm::ShowInfo() {
txtMessage = lv_label_create(btnMessage, nullptr);
lv_obj_set_style_local_bg_color(btnMessage, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_NAVY);

if (alarmController.State() == AlarmController::AlarmState::Set) {
auto timeToAlarm = alarmController.SecondsToAlarm();

auto daysToAlarm = timeToAlarm / 86400;
auto hrsToAlarm = (timeToAlarm % 86400) / 3600;
auto minToAlarm = (timeToAlarm % 3600) / 60;
auto secToAlarm = timeToAlarm % 60;

lv_label_set_text_fmt(
txtMessage, "Time to\nalarm:\n%2d Days\n%2d Hours\n%2d Minutes\n%2d Seconds", daysToAlarm, hrsToAlarm, minToAlarm, secToAlarm);
} else {
lv_label_set_text(txtMessage, "Alarm\nis not\nset.");
}
auto timeToAlarm = alarmController.SecondsToAlarm();

auto daysToAlarm = timeToAlarm / 86400;
auto hrsToAlarm = (timeToAlarm % 86400) / 3600;
auto minToAlarm = (timeToAlarm % 3600) / 60;
auto secToAlarm = timeToAlarm % 60;

lv_label_set_text_fmt(
txtMessage, "Time to\nalarm:\n%2d Days\n%2d Hours\n%2d Minutes\n%2d Seconds", daysToAlarm, hrsToAlarm, minToAlarm, secToAlarm);
}

void Alarm::SetRecurButtonState() {
Expand Down