Skip to content

Commit

Permalink
examples/rust-matter-light: Make periph_rtc feature optional. If not …
Browse files Browse the repository at this point in the history
…supported, get seconds since UNIX epoch from ztimer_sec
  • Loading branch information
maikerlab committed Apr 19, 2024
1 parent 1d35f0f commit c9573fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/rust-matter-light/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ APPLICATION_RUST_MODULE = rust_matter_light
BASELIBS += $(APPLICATION_RUST_MODULE).module

FEATURES_REQUIRED += rust_target
FEATURES_REQUIRED += periph_rtc

# Use RTC for sys_epoch, otherwise ztimer
FEATURES_OPTIONAL += periph_rtc

# Comment this line in to activate storing & loading runtime configurations
# NOTE: To use this feature, a PersistenceManager (in 'src/persist.rs') must be implemented for the target platform
Expand All @@ -43,6 +45,7 @@ USEMODULE += shell_cmd_heap
USEMODULE += ztimer
USEMODULE += ztimer_msec
USEMODULE += ztimer_usec
USEMODULE += ztimer_sec
USEMODULE += ps
USEMODULE += random
USEMODULE += periph_hwrng
Expand Down
19 changes: 15 additions & 4 deletions examples/rust-matter-light/utils.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#include <stdint.h>
#include "modules.h"

#if IS_USED(MODULE_PERIPH_RTC)
#include "periph/rtc.h"
#include <time.h>
#else
#include "ztimer.h"
#endif

// RIOT_EPOCH(2020) in seconds since UNIX Epoch
#define RIOT_EPOCH_SECS 1577833200;

uint32_t get_seconds_since_unix_epoch(void) {
// Get time since RIOT_EPOCH from RTC
#if IS_USED(MODULE_PERIPH_RTC)
// Get seconds since RIOT_EPOCH from RTC
struct tm now;
rtc_get_time(&now);
// Add to RIOT_EPOCH to get current seconds since UNIX epoch
return rtc_mktime(&now) + RIOT_EPOCH_SECS;
// Add to RIOT_EPOCH to get seconds since UNIX epoch
return RIOT_EPOCH_SECS + rtc_mktime(&now);
#else
// Get elapsed seconds from ztimer
return RIOT_EPOCH_SECS + ztimer_now(ZTIMER_SEC);
#endif
}

0 comments on commit c9573fc

Please sign in to comment.