Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.2% 25 / 0 / 26
Functions: 100.0% 5 / 0 / 5
Branches: 91.7% 11 / 0 / 12

core/math/scale.c
Line Branch Exec Source
1 /**
2 * @file scale.c
3 * @brief The clamp and segment maths a bar or gauge is built from.
4 *
5 * @ingroup lib_core
6 */
7 #include "math/scale.h"
8
9 73 int clamp_int(int value, int lo, int hi)
10 {
11
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 67 times.
73 if (value < lo)
12 {
13 6 return lo;
14 }
15
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 59 times.
67 if (value > hi)
16 {
17 8 return hi;
18 }
19 59 return value;
20 }
21
22 3 int segment_width(int total, int gap, int count)
23 {
24
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 if (count <= 0)
25 {
26 1 return 0;
27 }
28 2 return (total - gap * (count - 1)) / count;
29 }
30
31 5 int fraction_px(int total, int num, int den)
32 {
33
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (den <= 0)
34 {
35 1 return 0;
36 }
37 4 return total * num / den;
38 }
39
40 3 int segments_filled(int level, int segments)
41 {
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (segments <= 0)
43 {
44 ✗ return 0;
45 }
46
47 3 level = clamp_int(level, 0, 100);
48
49 // round up so any charge lights at least one cell and 100% lights them all
50 3 int filled = (level * segments + 99) / 100;
51 3 return filled > segments ? segments : filled;
52 }
53
54 5 int plot_y(int y0, int height, int lo, int hi, int value)
55 {
56 5 int span = hi - lo;
57
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (span <= 0)
58 {
59 1 return y0 + height - 1;
60 }
61
62 // pin an out-of-window value to an edge so it never maps to a row outside the area
63 4 value = clamp_int(value, lo, hi);
64 4 return y0 + height - 1 - ((value - lo) * (height - 1)) / span;
65 }
66