Skip to content

Commit

Permalink
Fixes based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
maksalees committed Sep 29, 2021
1 parent 77bc346 commit 25034cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/components/alarm/AlarmController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
minutes = alarmMin;
}

std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> AlarmController::TimePoint() {
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 Down Expand Up @@ -82,18 +81,17 @@ void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the app_timer
app_timer_stop(alarmAppTimer);

alarmTime = TimePoint();
auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - dateTimeController.CurrentDateTime()).count();
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() {
if (state == AlarmState::Set) {
return std::chrono::duration_cast<std::chrono::seconds>(alarmTime - dateTimeController.CurrentDateTime()).count();
return std::chrono::duration_cast<std::chrono::seconds>(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count();
} else {
return std::chrono::duration_cast<std::chrono::seconds>(TimePoint() - dateTimeController.CurrentDateTime()).count();
return std::chrono::duration_cast<std::chrono::seconds>(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/alarm/AlarmController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace Pinetime {

void Init(System::SystemTask* systemTask);
void SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin);
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> TimePoint();
void ScheduleAlarm();
void DisableAlarm();
void SetOffAlarmNow();
Expand All @@ -58,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

0 comments on commit 25034cc

Please sign in to comment.