-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libraries/EpoxyMockSTM32RTC: Add mock of the STM32RTC library
- Loading branch information
Showing
4 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
#ifndef EPOXY_MOCK_STM32RTC_H | ||
#define EPOXY_MOCK_STM32RTC_H | ||
|
||
#include <stdint.h> // uint8_t | ||
|
||
//----------------------------------------------------------------------------- | ||
// Typedefs from STM32RTC/rtc.h | ||
//----------------------------------------------------------------------------- | ||
|
||
typedef enum { | ||
HOUR_FORMAT_12, | ||
HOUR_FORMAT_24 | ||
} hourFormat_t; | ||
|
||
typedef enum { | ||
HOUR_AM, | ||
HOUR_PM | ||
} hourAM_PM_t; | ||
|
||
//----------------------------------------------------------------------------- | ||
// Typedefs from cores/arduino/stm32/clock.h | ||
//----------------------------------------------------------------------------- | ||
|
||
typedef enum { | ||
LSI_CLOCK, | ||
HSI_CLOCK, | ||
LSE_CLOCK, | ||
HSE_CLOCK | ||
} sourceClock_t; | ||
|
||
//----------------------------------------------------------------------------- | ||
|
||
/** | ||
* A mock version of the STM32RTC class in the | ||
* https://github.com/stm32duino/STM32RTC library. Only the clock-related | ||
* functions are mocked. The alarm-related functions are ignored. | ||
*/ | ||
class STM32RTC { | ||
public: | ||
|
||
enum Hour_Format : uint8_t { | ||
HOUR_12 = HOUR_FORMAT_12, | ||
HOUR_24 = HOUR_FORMAT_24 | ||
}; | ||
|
||
enum AM_PM : uint8_t { | ||
AM = HOUR_AM, | ||
PM = HOUR_PM | ||
}; | ||
|
||
enum Source_Clock : uint8_t { | ||
LSI_CLOCK = ::LSI_CLOCK, | ||
LSE_CLOCK = ::LSE_CLOCK, | ||
HSE_CLOCK = ::HSE_CLOCK | ||
}; | ||
|
||
static STM32RTC &getInstance() { | ||
static STM32RTC instance; // Guaranteed to be destroyed. | ||
// Instantiated on first use. | ||
return instance; | ||
} | ||
|
||
STM32RTC(STM32RTC const &) = delete; | ||
void operator=(STM32RTC const &) = delete; | ||
|
||
void begin(bool resetTime, Hour_Format format = HOUR_24) { | ||
(void) resetTime; | ||
(void) format; | ||
} | ||
void begin(Hour_Format format = HOUR_24) { | ||
(void) format; | ||
} | ||
|
||
void end(void) {} | ||
|
||
Source_Clock getClockSource(void) { return _clockSource; } | ||
void setClockSource(Source_Clock source) { _clockSource = source; } | ||
|
||
/* Get Functions */ | ||
|
||
uint32_t getSubSeconds(void) { return 0; } | ||
uint8_t getSeconds(void) { return 0; } | ||
uint8_t getMinutes(void) { return 0; } | ||
uint8_t getHours(AM_PM *period = nullptr) { | ||
(void) period; | ||
return 0; | ||
} | ||
void getTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, | ||
uint32_t *subSeconds, AM_PM *period = nullptr) { | ||
(void) hours; | ||
(void) minutes; | ||
(void) seconds; | ||
(void) subSeconds; | ||
(void) period; | ||
} | ||
|
||
uint8_t getWeekDay(void) { return 1; } | ||
uint8_t getDay(void) { return 1; } | ||
uint8_t getMonth(void) { return 1; } | ||
uint8_t getYear(void) { return 0; } | ||
void getDate( | ||
uint8_t *weekDay, uint8_t *day, uint8_t *month, uint8_t *year) { | ||
(void) weekDay; | ||
(void) day; | ||
(void) month; | ||
(void) year; | ||
} | ||
|
||
/* Set Functions */ | ||
|
||
void setSubSeconds(uint32_t subSeconds) { (void) subSeconds; } | ||
void setSeconds(uint8_t seconds) { (void) seconds; } | ||
void setMinutes(uint8_t minutes) { (void) minutes; } | ||
void setHours(uint8_t hours, AM_PM period = AM) { | ||
(void) hours; | ||
(void) period; | ||
} | ||
void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, | ||
uint32_t subSeconds = 1000, AM_PM period = AM) { | ||
(void) hours; | ||
(void) minutes; | ||
(void) seconds; | ||
(void) subSeconds; | ||
(void) period; | ||
} | ||
|
||
void setWeekDay(uint8_t weekDay) { (void) weekDay; } | ||
void setDay(uint8_t day) { (void) day; } | ||
void setMonth(uint8_t month) { (void) month; } | ||
void setYear(uint8_t year) { (void) year; } | ||
void setDate(uint8_t day, uint8_t month, uint8_t year) { | ||
(void) day; | ||
(void) month; | ||
(void) year; | ||
} | ||
void setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year) { | ||
(void) weekDay; | ||
(void) day; | ||
(void) month; | ||
(void) year; | ||
} | ||
|
||
bool isTimeSet(void) { return true; } | ||
|
||
private: | ||
STM32RTC(void): _clockSource(LSI_CLOCK) {} | ||
|
||
Source_Clock _clockSource; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# EpoxyMockSTM32RTC Library | ||
|
||
This library provides a mock implementation of the `STM32RTC` class from the | ||
[STM32RTC](https://github.com/stm32duino/STM32RTC) library. It provides just | ||
enough functionality to run unit tests and a small demo. Only the date and time | ||
functions are provided. The alarm functions are missing because AceTime does not | ||
support alarms. | ||
|
||
## Usage | ||
|
||
Add `EpoxyMockSTM32RTC` to the `ARDUINO_LIBS` in the EpoxyDuino `Makefile` of | ||
the application: | ||
|
||
```make | ||
APP_NAME := MyApp | ||
ARDUINO_LIBS := AceCommon AceSorting AceTime AceTimeClock EpoxyMockSTM32RTC ... | ||
include ../../../../EpoxyDuino.mk | ||
``` | ||
|
||
In the code where `#include <STM32RTC.h>` appears, use the following instead: | ||
|
||
```C++ | ||
#if defined(EPOXY_DUINO) | ||
#include <EpoxyMockSTM32RTC.h> | ||
#else | ||
#include <STM32RTC.h> | ||
#endif | ||
``` | ||
|
||
The `EpoxyMockSTM32RTC.h` could have been named `STM32RTC.h` to avoid the `#if | ||
defined()`, but I wanted to make it clear that `EpoxyMockSTM32RTC.h` does not | ||
provide an *emulation* of the `STM32RTC.h` functionality. If the | ||
`<EpoxyMockSTM32RTC.h>` implementation was hooked up to the Unix `time_t | ||
time(nullptr)` function, then I think it would be appropriate to rename it to | ||
just `<STM32RTC.h>`. |