-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.h
69 lines (57 loc) · 2.28 KB
/
utilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------
#ifdef DEBUG
#define debug(__info,...) printf ("# " __info "\n",##__VA_ARGS__)
#else
#define debug(__info,...)
#endif
// ------------------------------------------------------------------------------------------------------------------------
static bool __exceeded (uint32_t * const counter, const uint32_t boundary, const uint32_t interval) {
if (boundary == 0 || interval == 0)
return false;
if (*counter > boundary) {
*counter -= boundary;
return true;
} else if ((boundary - *counter) <= interval) {
*counter = (interval - (boundary - *counter));
return true;
} else {
*counter += interval;
return false;
}
}
static bool __isspaced (uint32_t * const last, const uint32_t time) {
uint32_t curr = (uint32_t) (time_us_64 () / 1000);
if ((curr - *last) < time)
return false;
*last = curr;
return true;
}
// ------------------------------------------------------------------------------------------------------------------------
#define __float_smooth_n 64
typedef struct {
float val;
float buf [__float_smooth_n];
int cnt;
int idx;
float sum;
} __float_smooth_t;
static float __float_smooth (const float val, __float_smooth_t * const smooth) {
if (smooth->cnt == __float_smooth_n)
smooth->sum -= smooth->buf [smooth->idx];
else
smooth->cnt ++;
smooth->sum += val;
smooth->buf [smooth->idx] = val;
smooth->idx = (smooth->idx + 1) & (__float_smooth_n - 1);
return smooth->sum / (float) smooth->cnt;
}
static inline bool __float_equals_one_place (const float a, const float b) {
return roundf (a * 10.0f) == roundf (b * 10.0f);
}
static inline float __float_clamp (const float v, const float l, const float h) {
return v < l ? l : (v > h ? h : v);
}
// ------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------