core/units/wind.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file wind.c | ||
| 3 | * @brief Pure wind-speed conversions and unit labels, no SDK behind them. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "units/wind.h" | ||
| 8 | |||
| 9 | /** | ||
| 10 | * @brief Scales a speed by a factor in thousandths and rounds to the nearest whole number. | ||
| 11 | * | ||
| 12 | * A half rounds away from zero, so a negative speed rounds the same way as its positive. | ||
| 13 | * | ||
| 14 | * @param kmh The speed in km/h. | ||
| 15 | * @param per_thousand How many thousandths of the unit one km/h is. | ||
| 16 | * @return The speed in the unit, rounded. | ||
| 17 | */ | ||
| 18 | 6 | static int scale_rounded(int kmh, int per_thousand) | |
| 19 | { | ||
| 20 | 6 | int scaled = kmh * per_thousand; | |
| 21 | |||
| 22 |
1/2✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 4 not taken.
|
6 | return (scaled >= 0 ? scaled + 500 : scaled - 500) / 1000; |
| 23 | } | ||
| 24 | |||
| 25 | 8 | int wind_from_kmh(int kmh, WindUnit unit) | |
| 26 | { | ||
| 27 |
4/4✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 4 taken 2 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 2 → 6 taken 2 times.
|
8 | switch (unit) |
| 28 | { | ||
| 29 | 3 | case WIND_UNIT_MPH: return scale_rounded(kmh, 621); // mph | |
| 30 | 2 | case WIND_UNIT_KTS: return scale_rounded(kmh, 540); // kts | |
| 31 | 1 | case WIND_UNIT_MS: return scale_rounded(kmh, 278); // m/s | |
| 32 | 2 | default: return kmh; // km/h | |
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | 5 | const char *wind_unit_label(WindUnit unit) | |
| 37 | { | ||
| 38 |
4/4✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 2 → 6 taken 2 times.
|
5 | switch (unit) |
| 39 | { | ||
| 40 | 1 | case WIND_UNIT_MPH: return "MPH"; | |
| 41 | 1 | case WIND_UNIT_KTS: return "KTS"; | |
| 42 | 1 | case WIND_UNIT_MS: return "M/S"; | |
| 43 | 2 | default: return "KM/H"; | |
| 44 | } | ||
| 45 | } | ||
| 46 |