Skip to content

Commit a901c86

Browse files
committed
Enhance setTime() and setDate() methods
Avoid call several time syncTime() and syncDate() methods. Up to 4 calls before instead of one time now. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent d5c9ffa commit a901c86

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

src/STM32RTC.cpp

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,17 @@ void STM32RTC::setHours(uint8_t hours, RTC_AM_PM period)
534534
void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
535535
{
536536
if (_configured) {
537-
setSeconds(seconds);
538-
setMinutes(minutes);
539-
setHours(hours);
537+
syncTime();
538+
if(seconds < 60) {
539+
_seconds = seconds;
540+
}
541+
if(minutes < 60) {
542+
_minutes = minutes;
543+
}
544+
if(hours < 24) {
545+
_hours = hours;
546+
}
547+
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
540548
}
541549
}
542550

@@ -551,10 +559,21 @@ void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
551559
void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, RTC_AM_PM period)
552560
{
553561
if (_configured) {
554-
setSubSeconds(subSeconds);
555-
setSeconds(seconds);
556-
setMinutes(minutes);
557-
setHours(hours, period);
562+
syncTime();
563+
if(subSeconds < 1000) {
564+
_subSeconds = subSeconds;
565+
}
566+
if(seconds < 60) {
567+
_seconds = seconds;
568+
}
569+
if(minutes < 60) {
570+
_minutes = minutes;
571+
}
572+
if(hours < 24) {
573+
_hours = hours;
574+
}
575+
_hoursPeriod = period;
576+
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
558577
}
559578
}
560579

@@ -566,7 +585,7 @@ void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t
566585
void STM32RTC::setWeekDay(uint8_t weekDay)
567586
{
568587
if (_configured) {
569-
RTC_GetDate(&_year, &_month, &_day, &_wday);
588+
syncDate();
570589
if((weekDay >= 1) && (weekDay <= 7)) {
571590
_wday = weekDay;
572591
}
@@ -582,7 +601,7 @@ void STM32RTC::setWeekDay(uint8_t weekDay)
582601
void STM32RTC::setDay(uint8_t day)
583602
{
584603
if (_configured) {
585-
RTC_GetDate(&_year, &_month, &_day, &_wday);
604+
syncDate();
586605
if((day >= 1) && (day <= 31)) {
587606
_day = day;
588607
}
@@ -598,7 +617,7 @@ void STM32RTC::setDay(uint8_t day)
598617
void STM32RTC::setMonth(uint8_t month)
599618
{
600619
if (_configured) {
601-
RTC_GetDate(&_year, &_month, &_day, &_wday);
620+
syncDate();
602621
if((month >= 1) && (month <= 12)) {
603622
_month = month;
604623
}
@@ -614,7 +633,7 @@ void STM32RTC::setMonth(uint8_t month)
614633
void STM32RTC::setYear(uint8_t year)
615634
{
616635
if (_configured) {
617-
RTC_GetDate(&_year, &_month, &_day, &_wday);
636+
syncDate();
618637
if(year < 100) {
619638
_year = year;
620639
}
@@ -632,9 +651,17 @@ void STM32RTC::setYear(uint8_t year)
632651
void STM32RTC::setDate(uint8_t day, uint8_t month, uint8_t year)
633652
{
634653
if (_configured) {
635-
setDay(day);
636-
setMonth(month);
637-
setYear(year);
654+
syncDate();
655+
if((day >= 1) && (day <= 31)) {
656+
_day = day;
657+
}
658+
if((month >= 1) && (month <= 12)) {
659+
_month = month;
660+
}
661+
if(year < 100) {
662+
_year = year;
663+
}
664+
RTC_SetDate(_year, _month, _day, _wday);
638665
}
639666
}
640667

@@ -649,11 +676,20 @@ void STM32RTC::setDate(uint8_t day, uint8_t month, uint8_t year)
649676
void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year)
650677
{
651678
if (_configured) {
652-
setWeekDay(weekDay);
653-
setDay(day);
654-
setMonth(month);
655-
setYear(year);
656-
}
679+
syncDate();
680+
if((weekDay >= 1) && (weekDay <= 7)) {
681+
_wday = weekDay;
682+
}
683+
if((day >= 1) && (day <= 31)) {
684+
_day = day;
685+
}
686+
if((month >= 1) && (month <= 12)) {
687+
_month = month;
688+
}
689+
if(year < 100) {
690+
_year = year;
691+
}
692+
RTC_SetDate(_year, _month, _day, _wday); }
657693
}
658694

659695
/**

0 commit comments

Comments
 (0)