core/clock/nightsched.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file nightsched.c | ||
| 3 | * @brief The night window, by containment rather than by edge. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "nightsched.h" | ||
| 8 | |||
| 9 | /// Minutes in a day, which is the only range any of these readings may sit in | ||
| 10 | #define DAY_MINUTES 1440 | ||
| 11 | |||
| 12 | /** @brief Whether a reading is a real minute of the day rather than a missing one. */ | ||
| 13 | 77 | static bool is_reading(int minute) | |
| 14 | { | ||
| 15 |
4/4✓ Branch 0 taken 70 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 2 times.
|
77 | return minute >= 0 && minute < DAY_MINUTES; |
| 16 | } | ||
| 17 | |||
| 18 | 24 | bool clock_window_contains(int start, int end, int now) | |
| 19 | { | ||
| 20 |
6/6✓ Branch 1 taken 23 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 19 times.
|
24 | if (!is_reading(start) || !is_reading(end) || !is_reading(now)) |
| 21 | { | ||
| 22 | 5 | return false; | |
| 23 | } | ||
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
|
19 | if (start == end) |
| 26 | { | ||
| 27 | 2 | return false; // an empty window, not an eternal one | |
| 28 | } | ||
| 29 | |||
| 30 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
|
17 | if (start < end) |
| 31 | { | ||
| 32 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
|
7 | return now >= start && now < end; |
| 33 | } | ||
| 34 | |||
| 35 | // the window runs past midnight, so it is everything from the start to the end of the day | ||
| 36 | // plus everything from the start of the day to the end | ||
| 37 |
4/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
|
10 | return now >= start || now < end; |
| 38 | } | ||
| 39 | |||
| 40 | 11 | bool night_schedule_active(int mode, int now, int rise, int set, | |
| 41 | int fixed_start, int fixed_end, bool have_night) | ||
| 42 | { | ||
| 43 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
|
11 | if (!have_night) |
| 44 | { | ||
| 45 | 2 | return false; // nothing to switch to, so the window may as well be shut | |
| 46 | } | ||
| 47 | |||
| 48 |
6/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 time.
|
9 | if (mode == NIGHT_SCHED_SOLAR && is_reading(rise) && is_reading(set)) |
| 49 | { | ||
| 50 | // night runs from sunset round to sunrise, which is the wrapping case nearly always | ||
| 51 | 2 | return clock_window_contains(set, rise, now); | |
| 52 | } | ||
| 53 | |||
| 54 | // the fixed pair covers its own mode and stands in for the sun when there is no reading | ||
| 55 | // nothing here knows how old a sun time is, so an old one still counts as a reading | ||
| 56 | // near the solstices last month's sunset is within minutes of tonight's, but near the equinoxes | ||
| 57 | // it moves about an hour a month, so night can switch on up to an hour off | ||
| 58 | // that costs less than dropping to the fixed hours for a reason the user cannot see | ||
| 59 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
|
7 | if (mode == NIGHT_SCHED_SOLAR || mode == NIGHT_SCHED_FIXED) |
| 60 | { | ||
| 61 | 5 | return clock_window_contains(fixed_start, fixed_end, now); | |
| 62 | } | ||
| 63 | |||
| 64 | 2 | return false; | |
| 65 | } | ||
| 66 |