Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_DayOfWeek.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <util/cs_Math.h>
10
11#include <cstdint>
12
13enum class DayOfWeek : uint8_t {
14 Sunday = 1 << 0,
15 Monday = 1 << 1,
16 Tuesday = 1 << 2,
17 Wednesday = 1 << 3,
18 Thursday = 1 << 4,
19 Friday = 1 << 5,
20 Saturday = 1 << 6,
21};
22
23typedef uint8_t DayOfWeekBitMask;
24
25inline uint8_t dayNumber(DayOfWeek day) {
26 for (int i = 0; i < 7; i++) {
27 if (day == DayOfWeek(1 << i)) {
28 return i;
29 }
30 }
31 return 0xff;
32}
33
34inline DayOfWeek operator+(DayOfWeek day, int offset) {
35 offset = CsMath::mod(offset, 7);
36 uint16_t shifted_day = static_cast<uint8_t>(day) << offset;
37
38 if (shifted_day & 0b01111111) {
39 // no overflow occured, simply return it.
40 return DayOfWeek(static_cast<uint8_t>(shifted_day & 0b01111111));
41 }
42 else {
43 // passed week boundary by shifting so shift it back by 7.
44 return DayOfWeek(static_cast<uint8_t>((shifted_day >> 7) & 0b01111111));
45 }
46}
47
48inline DayOfWeek operator-(DayOfWeek day, int offset) {
49 return day + -offset;
50}
uint8_t dayNumber(DayOfWeek day)
Definition: cs_DayOfWeek.h:25
DayOfWeek operator-(DayOfWeek day, int offset)
Definition: cs_DayOfWeek.h:48
DayOfWeek
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 24,...
Definition: cs_DayOfWeek.h:13
uint8_t DayOfWeekBitMask
Definition: cs_DayOfWeek.h:23
DayOfWeek operator+(DayOfWeek day, int offset)
Definition: cs_DayOfWeek.h:34
auto mod(T v, S m) -> decltype(v % m)
Returns the canonical representation of [v] considered as element of Z/mZ, regardless of any silly fe...
Definition: cs_Math.h:77