Skip to content

Commit

Permalink
libraries/EpoxyMockSTM32RTC: Add mock of the STM32RTC library
Browse files Browse the repository at this point in the history
  • Loading branch information
bxparks committed Mar 27, 2022
1 parent 556ca82 commit 221aa7e
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Add an empty `EpoxyDuino.h` at the top level to stop warning messages from
the Arduino IDE. Fixes
[Issue#62](https://github.com/bxparks/EpoxyDuino/issues/62).
* Add [libraries/EpoxyMockSTM32RTC](libraries/EpoxyMockSTM32RTC) which is a
mock of the [STM32RTC](https://github.com/stm32duino/STM32RTC) library.
* 1.2.3 (2022-02-24)
* Rename `unixhostduino_main()` to `epoxyduino_main()`, and make it
static. No need to expose it publicly.
Expand Down
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ also provided:
TimerOne (https://github.com/PaulStoffregen/TimerOne) library
* [EpoxyMockFastLED](libraries/EpoxyMockFastLED/): mock version of the
FastLED (https://github.com/FastLED/FastLED) library
* [EpoxyMockSTM32RTC](libraries/EpoxyMockSTM32RTC/): mock version of the
STM32RTC (https://github.com/stm32duino/STM32RTC) library

These mock libraries may be sufficient for a CI pipeline.

Expand Down Expand Up @@ -1065,6 +1067,9 @@ intended. This limitation may be sufficient for Continuous Integration purposes.
library.
* [EpoxyMockFastLED](libraries/EpoxyMockFastLED/)
* Mock version of the FastLED (https://github.com/FastLED/FastLED) library.
* [EpoxyMockSTM32RTC](libraries/EpoxyMockSTM32RTC/)
* Mock version of the STM32RTC (https://github.com/stm32duino/STM32RTC)
library.
* EspMock (https://github.com/hsaturn/EspMock)
* This is a separate project that provides various mocks for functions and
libraries included with the ESP8266 and the ESP32 processors.
Expand All @@ -1076,18 +1081,18 @@ intended. This limitation may be sufficient for Continuous Integration purposes.

This library has Tier 1 support on:

* Ubuntu 18.04
* g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
* clang++ 8.0.0-3~ubuntu18.04.2
* clang++ 6.0.0-1ubuntu2
* GNU Make 4.1
* Ubuntu 20.04
* g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
* Ubuntu 20.04.4 LTS
* g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
* clang++ version 10.0.0-4ubuntu1
* GNU Make 4.2.1

The following environments are Tier 2 because I do not test them often enough:

* Ubuntu 18.04 LTS
* g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
* clang++ 8.0.0-3~ubuntu18.04.2
* clang++ 6.0.0-1ubuntu2
* GNU Make 4.1
* Raspbian GNU/Linux 10 (buster)
* On Raspberry Pi Model 3B
* g++ (Raspbian 8.3.0-6+rpi1) 8.3.0
Expand Down
151 changes: 151 additions & 0 deletions libraries/EpoxyMockSTM32RTC/EpoxyMockSTM32RTC.h
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
35 changes: 35 additions & 0 deletions libraries/EpoxyMockSTM32RTC/README.md
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>`.

0 comments on commit 221aa7e

Please sign in to comment.