Skip to content

Commit e7aefb8

Browse files
authored
Merge pull request #4 from fpistm/Singleton
Use Singleton to have only one STM32RTC instance
2 parents ff701cd + a4eb64a commit e7aefb8

File tree

8 files changed

+27
-14
lines changed

8 files changed

+27
-14
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ A RTC library for STM32.
66
This library is based on the Arduino RTCZero library.
77
The library allows to take control of the internal RTC of the STM32 boards.
88

9+
Singleton design pattern is used to ensure that only one STM32RTC instance is instantiated:
10+
```
11+
/* Get the rtc object */
12+
STM32RTC& rtc = STM32RTC::getInstance();
13+
```
14+
915
The following functions are not supported:
1016

1117
* **`void standbyMode()`**: use the STM32 Low Power library instead.

examples/Epoch/Epoch.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
#include <STM32RTC.h>
4040

41-
/* Create a rtc object */
42-
STM32RTC rtc;
41+
/* Get the rtc object */
42+
STM32RTC& rtc = STM32RTC::getInstance();
4343

4444
void setup() {
4545
Serial.begin(9600);

examples/RTCClockSelection/RTCClockSelection.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
#include <STM32RTC.h>
4141

42-
/* Create a rtc object */
43-
STM32RTC rtc;
42+
/* Get the rtc object */
43+
STM32RTC& rtc = STM32RTC::getInstance();
4444

4545
/* Change these values to set the current initial time */
4646
const byte seconds = 0;

examples/SimpleRTC/SimpleRTC.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
#include <STM32RTC.h>
4040

41-
/* Create a rtc object */
42-
STM32RTC rtc;
41+
/* Get the rtc object */
42+
STM32RTC& rtc = STM32RTC::getInstance();
4343

4444
/* Change these values to set the current initial time */
4545
const byte seconds = 0;

examples/simpleRTCAlarm/simpleRTCAlarm.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
#include <STM32RTC.h>
4040

41-
/* Create a rtc object */
42-
STM32RTC rtc;
41+
/* Get the rtc object */
42+
STM32RTC& rtc = STM32RTC::getInstance();
4343

4444
/* Change these values to set the current initial time */
4545
const byte seconds = 0;

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ STM32RTC KEYWORD1
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
1414

15+
getInstance KEYWORD2
16+
1517
getWeekDay KEYWORD2
1618
getDay KEYWORD2
1719
getMonth KEYWORD2

src/STM32RTC.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@
4646
// Initialize static variable
4747
bool STM32RTC::_configured = false;
4848

49-
STM32RTC::STM32RTC(void): _clockSource(RTC_LSI_CLOCK)
50-
{
51-
52-
}
53-
5449
/**
5550
* @brief initializes the RTC
5651
* @param resetTime: if true reconfigures the RTC

src/STM32RTC.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ class STM32RTC {
8686
RTC_HSE_CLOCK = HSE_CLOCK
8787
};
8888

89-
STM32RTC();
89+
static STM32RTC& getInstance() {
90+
static STM32RTC instance; // Guaranteed to be destroyed.
91+
// Instantiated on first use.
92+
return instance;
93+
}
94+
95+
STM32RTC(STM32RTC const&) = delete;
96+
void operator=(STM32RTC const&) = delete;
9097

9198
void begin(bool resetTime, RTC_Hour_Format format = RTC_HOUR_24);
9299
void begin(RTC_Hour_Format format = RTC_HOUR_24);
@@ -177,6 +184,8 @@ class STM32RTC {
177184
}
178185

179186
private:
187+
STM32RTC(void): _clockSource(RTC_LSI_CLOCK) {}
188+
180189
static bool _configured;
181190

182191
RTC_AM_PM _hoursPeriod;
@@ -202,6 +211,7 @@ class STM32RTC {
202211
void syncTime(void);
203212
void syncDate(void);
204213
void syncAlarmTime(void);
214+
205215
};
206216

207217
#endif // __STM32_RTC_H

0 commit comments

Comments
 (0)