Skip to content

Commit 3cd4998

Browse files
authored
Restrukture Code, Improve Dokumentation and Indroduce Unit Testing (#10) (#11)
* rename all names to DS3231-RTC * header include guard needs underscores * Update README.md - added getYearDay() * rework docu to time getting functions * update Tests and class structure * Update GitHub Actions workflow for compiling examples * Downgrade arduino/compile-sketches action to v1 * Add C++17 compiler flags to compile workflow Add extra compiler flags for C++17 support in Arduino workflow. * remove Unix_offset and NTP_offset from Constants * remove std::optional * update examples to reowked code * reorder includes * rework clock mode setter * Bugfix for RTC::now in hour calculation, add begin() method; add method to get hour mode * adapt examples with begin() method * update keyword.txt * Remove build folder from version control * add tests to readme; comment bin2dec function * remove double defined function bcd2bin; replace functional code by more performant bcd functiontional code. * restructure static funtions => make them constexpr * Update unit tests; update libraries informations; adapt Readme * adapt work flow rund for unit tests * add status badge for Readme * Delete .DS_Store * update Readme
1 parent 33ecab1 commit 3cd4998

27 files changed

Lines changed: 2861 additions & 2110 deletions

.github/workflows/compile_examples.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ on:
77
jobs:
88
compile-examples:
99
runs-on: ubuntu-latest
10-
10+
11+
strategy:
12+
fail-fast: false
13+
14+
matrix:
15+
board:
16+
- fqbn: arduino:avr:uno
17+
- fqbn: arduino:avr:nano
18+
1119
steps:
12-
- uses: actions/checkout@v3
20+
- uses: actions/checkout@v6
1321
- uses: arduino/compile-sketches@v1
1422
with:
23+
fqbn: ${{ matrix.board.fqbn }}
24+
cli-compile-flags: |
25+
- --build-property
26+
- compiler.cpp.extra_flags=-std=gnu++17
1527
libraries: |
16-
- source-path: ./
28+
- source-path: .

.github/workflows/unit_tests.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches: [ "**" ]
6+
7+
pull_request:
8+
branches: [ "**" ]
9+
10+
jobs:
11+
unit-tests:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@main
17+
18+
- name: Configure CMake
19+
run: cmake -B build -DCMAKE_BUILD_TYPE=Release
20+
21+
- name: Build
22+
run: cmake --build build
23+
24+
- name: Run Tests
25+
run: ctest --test-dir build --output-on-failure

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
# PlatformIO files/folders
3535
.pio
3636

37+
# cmake
38+
build
39+
40+
# mac
41+
.DS_Store
42+
3743
# vscode files /folders
3844
.vscode/.browse.c_cpp.db*
3945
.vscode/c_cpp_properties.json

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(Ds3231_Tests CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
# Fetch GoogleTest
8+
include(FetchContent)
9+
FetchContent_Declare(
10+
googletest
11+
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
12+
)
13+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
14+
FetchContent_MakeAvailable(googletest)
15+
16+
enable_testing()
17+
add_subdirectory(test)

README.md

Lines changed: 195 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# DS3231-RTC Library
22
[![Spell Check](https://github.com/hasenradball/DS3231-RTC/actions/workflows/spell_checker.yml/badge.svg)](https://github.com/hasenradball/DS3231-RTC/actions/workflows/spell_checker.yml)
33
[![Compile Examples](https://github.com/hasenradball/DS3231-RTC/actions/workflows/compile_examples.yml/badge.svg)](https://github.com/hasenradball/DS3231-RTC/actions/workflows/compile_examples.yml)
4+
[![Unit Tests](https://github.com/hasenradball/DS3231-RTC/actions/workflows/unit_tests.yml/badge.svg)](https://github.com/hasenradball/DS3231-RTC/actions/workflows/unit_tests.yml)
45

56
The **great** C++ Library for the DS3231 real-time clock (RTC) module.
67

@@ -17,70 +18,112 @@ This document explains the installation and usage of the Library with the Arduin
1718
You do have to install the Library in your Arduino IDE environment before you can use it. Installation instructions are provided, below.
1819

1920
**REMARK**:<br>
20-
This library was based on the master branch of [NorthernWidget/DS3231](https://github.com/NorthernWidget/DS3231) Library in Oct/2023. It was reworked and refractured with respect of the following main topics:
21+
This library was based on the master branch of [NorthernWidget/DS3231](https://github.com/NorthernWidget/DS3231) Library in Oct/2023. It was maintained, reworked and refractured with respect of the following main topics:
2122
* using standardized functions of the `time.h` library.
2223
* introduce a `struct tm` which holds all relevant date and time values.
2324
* restructure comments, so that syntax highlighting works fine.
2425
* add a `strf_DateTime()` function with can be used to print a user specific(self defined) DateTime string easily.
26+
* restructure of code with respect of clean code
27+
* usage of Doxygen Documentation generation
28+
* introduce Google Unit Testing
29+
2530

2631
## Contents
2732

28-
* [Summary](#summary)
33+
* [Summary and How to Start](#summary-and-how-to-start)
34+
* [Get Datetime from DS3231](#setup-and-get-datetime-from-ds3231)
35+
* [Set the DS3231 Module with Date and Time](#setup-and-set-the-date-and-time-in-the-ds3231-module)
36+
* [Use Date and Time](#use-date-and-time)
2937
* [About the DS3231](#about-the-ds3231-module)
3038
* [The DS3231 Battery Problem](#the-ds3231-battery-problem)
39+
* [Unit Testing](#unit-tests)
3140
* [How to Install the Library](#installation)
3241
* [Functions Provided in the Library](#functions)
3342
* [Examples of Using the Library](#examples-of-use)
3443
* [Helpful Resources](#additional-resources-and-references)
3544
* [Contributing, Credits and License](#contributing)
3645
* [To-Do List](#to-do)
3746

38-
3947
<hr>
4048

41-
## Summary
42-
43-
After installing the Library in your Arduino IDE, using it in a program starts with three, simple steps:
44-
45-
<ol start="1">
46-
<li>Import the Library into the program code:</li>
47-
</ol>
49+
## Summary and How to Start
4850

51+
### Setup and get DateTime from DS3231
52+
After installing the Library in your Arduino IDE, you can retrieve a DateTime from the DS3231 via:
4953

5054
```
55+
#include <Arduino.h>
56+
#include <Wire.h>
5157
#include <DS3231-RTC.h>
52-
```
53-
54-
<ol start="2">
55-
<li>Declare a DS3231 object, for example:</li>
56-
</ol>
5758
59+
DS3231::RTClib myRTC;
60+
61+
void setup () {
62+
Serial.begin(57600);
63+
Wire.begin();
64+
delay(500);
65+
Serial.println("Nano Ready!");
66+
}
67+
68+
void loop () {
69+
70+
delay(1000);
71+
72+
// get each second a timestamp
73+
DS3231::DateTime now = myRTC.now();
74+
75+
Serial.print(now.getYear(), DEC);
76+
Serial.print('/');
77+
Serial.print(now.getMonth(), DEC);
78+
Serial.print('/');
79+
Serial.print(now.getDay(), DEC);
80+
Serial.print(' ');
81+
Serial.print(now.getHour(), DEC);
82+
Serial.print(':');
83+
Serial.print(now.getMinute(), DEC);
84+
Serial.print(':');
85+
Serial.print(now.getSecond(), DEC);
86+
Serial.println();
87+
88+
Serial.print(" since midnight 1/1/1970 = ");
89+
Serial.print(now.getUnixTime());
90+
Serial.print("s = ");
91+
Serial.print(now.getUnixTime() / 86400L);
92+
Serial.println("d");
93+
}
94+
```
5895

96+
or Serial for the **ESP8266** like:
5997
```
60-
DS3231 myRTC;
98+
Wire.begin(SDA, SCL);
6199
```
62100

63-
<ol start="3">
64-
<li>Start the Wire library to enable I2C communications with the DS3231 hardware, typically in the setup() code block:</li>
65-
</ol>
101+
### Setup and set the Date and Time in the DS3231 module
102+
The feed the DS3231 Module the easiest way is to set th Epoch (unix timestamp).
66103

67-
68-
```
69-
Wire.begin();
70-
```
71-
or for the **ESP8266** like:
72-
```
73-
Wire.begin(SDA, SCL);
74104
```
105+
#include <Arduino.h>
106+
#include <Wire.h>
107+
#include <DS3231-RTC.h>
75108
76-
Then, Library functions are typically accessed through the DS3231 object.
109+
// unix timestamp of: Tue Aug 16 2022 10:00:00 GMT+0000
110+
constexpr time_t timestamp{1660644000UL};
77111
78-
For example, to read the current date of the month (1...31), depending on the month and the year:
112+
DS3231::RTClib myRTC;
113+
DS3231::DS3231 Clock;
79114
80-
```
81-
byte theDate = myRTC.getDate();
115+
void setup() {
116+
Serial.begin(115200);
117+
Wire.begin();
118+
Clock.begin();
119+
delay(500);
120+
121+
// feed UnixTimeStamp
122+
Clock.setEpoch(timestamp);
123+
}
82124
```
83125

126+
### Use Date and Time
84127
The Library incorporates two other classes to assist with managing `date` and `time` data:
85128

86129
* `DateTime` class enables a object for managing date and time data.
@@ -92,7 +135,7 @@ The `DateTime` class can be instantiated by a specific date and time in three di
92135
year, month, day, hour, minute and second
93136

94137
or
95-
138+
96139
* 2.) by a single, `time_t` unix timestamp.<br>
97140

98141
* 3.) by giving a separate `const char *` string for Date and Time like:<br>
@@ -158,7 +201,126 @@ See the corresponding link for the problem description in detail:<br>
158201
[back to top](#ds3231-rtc-library)
159202
<hr>
160203

204+
## Unit Tests
205+
The project contains unit tests.
206+
These tests validate:
207+
208+
* some helper functions
209+
* getter functions
210+
211+
### Run tests locally
212+
The tests are based on GoogleTest and are built with CMake.
213+
214+
Required tools:
215+
216+
* CMake
217+
* a C++17 compatible compiler
218+
219+
#### Windows
220+
With Visual Studio or Visual Studio Build Tools installed, run the commands in a Developer PowerShell:
221+
222+
```powershell
223+
# Generate Build System
224+
cmake -B build -DCMAKE_BUILD_TYPE=Release
225+
# Build a Project
226+
cmake --build build
227+
# Execute Tests
228+
ctest --test-dir build --output-on-failure
229+
or
230+
./build/test/test_DS3231 --gtest_color=yes
231+
```
232+
233+
#### Linux
234+
Install CMake and a compiler toolchain, for example `g++` or `clang++`, and run:
235+
236+
```bash
237+
# Generate Build System
238+
cmake -B build -DCMAKE_BUILD_TYPE=Release
239+
# Build a Project
240+
cmake --build build
241+
# Execute Tests
242+
ctest --test-dir build --output-on-failure
243+
./build/test/test_DS3231 --gtest_color=yes
244+
```
245+
246+
#### macOS
247+
Install Xcode Command Line Tools and CMake, then run:
248+
249+
```bash
250+
# Generate Build System
251+
cmake -B build -DCMAKE_BUILD_TYPE=Release
252+
# Build a Project
253+
cmake --build build
254+
# Execute Tests
255+
ctest --test-dir build --output-on-failure
256+
./build/test/test_DS3231 --gtest_color=yes
257+
```
161258

259+
Example Output:
260+
```
261+
[==========] Running 23 tests from 3 test suites.
262+
[----------] Global test environment set-up.
263+
[----------] 8 tests from DS3231Tools_BCD
264+
[ RUN ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_0
265+
[ OK ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_0 (0 ms)
266+
[ RUN ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_9
267+
[ OK ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_9 (0 ms)
268+
[ RUN ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_10
269+
[ OK ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_10 (0 ms)
270+
[ RUN ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_59
271+
[ OK ] DS3231Tools_BCD.BinaryDecodedDecimalToDecimal_59 (0 ms)
272+
[ RUN ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_0
273+
[ OK ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_0 (0 ms)
274+
[ RUN ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_9
275+
[ OK ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_9 (0 ms)
276+
[ RUN ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_10
277+
[ OK ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_10 (0 ms)
278+
[ RUN ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_59
279+
[ OK ] DS3231Tools_BCD.DecimalToBinaryDecodedDecimal_59 (0 ms)
280+
[----------] 8 tests from DS3231Tools_BCD (0 ms total)
281+
282+
[----------] 7 tests from DS3231Tools_leapYear
283+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_2023
284+
[ OK ] DS3231Tools_leapYear.IsLeapYear_2023 (0 ms)
285+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_2024
286+
[ OK ] DS3231Tools_leapYear.IsLeapYear_2024 (0 ms)
287+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_2032
288+
[ OK ] DS3231Tools_leapYear.IsLeapYear_2032 (0 ms)
289+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_1700
290+
[ OK ] DS3231Tools_leapYear.IsLeapYear_1700 (0 ms)
291+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_1800
292+
[ OK ] DS3231Tools_leapYear.IsLeapYear_1800 (0 ms)
293+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_1900
294+
[ OK ] DS3231Tools_leapYear.IsLeapYear_1900 (0 ms)
295+
[ RUN ] DS3231Tools_leapYear.IsLeapYear_2000
296+
[ OK ] DS3231Tools_leapYear.IsLeapYear_2000 (0 ms)
297+
[----------] 7 tests from DS3231Tools_leapYear (0 ms total)
298+
299+
[----------] 8 tests from DS3231MockTest
300+
[ RUN ] DS3231MockTest.GetSecondReadsSecondRegister
301+
[ OK ] DS3231MockTest.GetSecondReadsSecondRegister (0 ms)
302+
[ RUN ] DS3231MockTest.GetMinuteReadsMinuteRegister
303+
[ OK ] DS3231MockTest.GetMinuteReadsMinuteRegister (0 ms)
304+
[ RUN ] DS3231MockTest.GetHourReadsFlagsAndBcdValue
305+
[ OK ] DS3231MockTest.GetHourReadsFlagsAndBcdValue (0 ms)
306+
[ RUN ] DS3231MockTest.SetHourWritesUpdatedHourRegister
307+
[ OK ] DS3231MockTest.SetHourWritesUpdatedHourRegister (0 ms)
308+
[ RUN ] DS3231MockTest.GetDoWReadsDoWRegister
309+
[ OK ] DS3231MockTest.GetDoWReadsDoWRegister (0 ms)
310+
[ RUN ] DS3231MockTest.GetDayReadsDateRegister
311+
[ OK ] DS3231MockTest.GetDayReadsDateRegister (0 ms)
312+
[ RUN ] DS3231MockTest.GetMonthReadsMonthRegister
313+
[ OK ] DS3231MockTest.GetMonthReadsMonthRegister (0 ms)
314+
[ RUN ] DS3231MockTest.GetYearReadsYearRegister
315+
[ OK ] DS3231MockTest.GetYearReadsYearRegister (0 ms)
316+
[----------] 8 tests from DS3231MockTest (0 ms total)
317+
318+
[----------] Global test environment tear-down
319+
[==========] 23 tests from 3 test suites ran. (1 ms total)
320+
[ PASSED ] 23 tests.
321+
```
322+
323+
[back to top](#ds3231-rtc-library)
162324
## Installation
163325

164326
### First Method
@@ -324,6 +486,8 @@ See also [Working with the DS3231 libraries and interrupts](https://github.com/I
324486
[back to top](#ds3231-rtc-library)
325487
<hr>
326488

489+
490+
327491
## Contributing
328492

329493
If you want to contribute to this project:

docs/DS3231_datasheet.pdf

824 KB
Binary file not shown.

0 commit comments

Comments
 (0)