core/weather/uv_band.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file uv_band.c | ||
| 3 | * @brief The risk band a UV index falls in, with a long and a short name for each. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "weather/uv_band.h" | ||
| 8 | |||
| 9 | /// How many bands there are, UV_BAND_NONE included | ||
| 10 | #define UV_BAND_COUNT (UV_BAND_EXTREME + 1) | ||
| 11 | |||
| 12 | 13 | UvBand uv_band(int uv) | |
| 13 | { | ||
| 14 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 12 times.
|
13 | if (uv < 0) |
| 15 | { | ||
| 16 | 1 | return UV_BAND_NONE; | |
| 17 | } | ||
| 18 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 10 times.
|
12 | if (uv <= 2) |
| 19 | { | ||
| 20 | 2 | return UV_BAND_LOW; | |
| 21 | } | ||
| 22 |
2/2✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 8 times.
|
10 | if (uv <= 5) |
| 23 | { | ||
| 24 | 2 | return UV_BAND_MODERATE; | |
| 25 | } | ||
| 26 |
2/2✓ Branch 8 → 9 taken 2 times.
✓ Branch 8 → 10 taken 6 times.
|
8 | if (uv <= 7) |
| 27 | { | ||
| 28 | 2 | return UV_BAND_HIGH; | |
| 29 | } | ||
| 30 |
2/2✓ Branch 10 → 11 taken 4 times.
✓ Branch 10 → 12 taken 2 times.
|
6 | if (uv <= 10) |
| 31 | { | ||
| 32 | 4 | return UV_BAND_VERY_HIGH; | |
| 33 | } | ||
| 34 | 2 | return UV_BAND_EXTREME; | |
| 35 | } | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @brief Looks a band up in one of the name tables. | ||
| 39 | * | ||
| 40 | * A band is an enum, so a caller can still hand in any number. Anything outside the table gets | ||
| 41 | * the empty name rather than a read past its end. The compare is unsigned, so a negative value | ||
| 42 | * wraps to a big one and is turned away by the same check. A `band < 0` test on an enum the | ||
| 43 | * compiler made unsigned would fail the watch build under -Werror. | ||
| 44 | * | ||
| 45 | * @param names The table, one name per band. | ||
| 46 | * @param band The band. | ||
| 47 | * @return The band's name, or an empty string. | ||
| 48 | */ | ||
| 49 | 12 | static const char *band_lookup(const char *const names[UV_BAND_COUNT], UvBand band) | |
| 50 | { | ||
| 51 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 10 times.
|
12 | if ((unsigned)band >= UV_BAND_COUNT) |
| 52 | { | ||
| 53 | 2 | return ""; | |
| 54 | } | ||
| 55 | |||
| 56 | 10 | return names[band]; | |
| 57 | } | ||
| 58 | |||
| 59 | 3 | const char *uv_band_name(UvBand band) | |
| 60 | { | ||
| 61 | static const char *const names[UV_BAND_COUNT] = { | ||
| 62 | "", "LOW", "MODERATE", "HIGH", "VERY HIGH", "EXTREME", | ||
| 63 | }; | ||
| 64 | |||
| 65 | 3 | return band_lookup(names, band); | |
| 66 | } | ||
| 67 | |||
| 68 | 9 | const char *uv_band_short_name(UvBand band) | |
| 69 | { | ||
| 70 | static const char *const names[UV_BAND_COUNT] = { | ||
| 71 | "", "LOW", "MOD", "HIGH", "V.HIGH", "EXTRM", | ||
| 72 | }; | ||
| 73 | |||
| 74 | 9 | return band_lookup(names, band); | |
| 75 | } | ||
| 76 |