-
Notifications
You must be signed in to change notification settings - Fork 10
/
Util.h
141 lines (117 loc) · 2.15 KB
/
Util.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include "CompileSwitches.h"
#ifdef DEBUG_OUTPUT
extern bool serial_port_initialised;
bool _assert_fail( const char* assert, const char* msg )
{
if( serial_port_initialised )
{
Serial.print(assert);
Serial.print(" ");
Serial.print(msg);
Serial.print("\n");
}
return true;
}
#define ASSERT_MSG(x, msg) ((void)((x) || (_assert_fail(#x,msg))))
#define DEBUG_TEXT(x) if(serial_port_initialised) Serial.print(x);
#else
#define ASSERT_MSG(x, msg)
#define DEBUG_TEXT(x)
#endif
/////////////////////////////////////////////////////
template <typename T>
T clamp( const T& value, const T& min, const T& max )
{
if( value < min )
{
return min;
}
if( value > max )
{
return max;
}
return value;
}
template <typename T>
T max_val( const T& v1, const T& v2 )
{
if( v1 > v2 )
{
return v1;
}
else
{
return v2;
}
}
template <typename T>
T min_val( const T& v1, const T& v2 )
{
if( v1 < v2 )
{
return v1;
}
else
{
return v2;
}
}
/////////////////////////////////////////////////////
template <typename T>
T lerp( const T& v1, const T& v2, float t )
{
return v1 + ( (v2 - v1) * t );
}
/////////////////////////////////////////////////////
int trunc_to_int( float v )
{
return static_cast<int>( trunc(v) );
}
/////////////////////////////////////////////////////
template < typename TYPE, int CAPACITY >
class RUNNING_AVERAGE
{
TYPE m_values[ CAPACITY ];
int m_current;
int m_size;
public:
RUNNING_AVERAGE() :
m_values(),
m_current(0),
m_size(0)
{
}
void add( TYPE value )
{
m_values[ m_current ] = value;
m_current = ( m_current + 1 ) % CAPACITY;
++m_size;
if( m_size > CAPACITY )
{
m_size = CAPACITY;
}
}
void reset()
{
m_size = 0;
m_current = 0;
}
TYPE average() const
{
if( m_size == 0 )
{
return 0;
}
TYPE avg = 0;
for( int x = 0; x < m_size; ++x )
{
avg += m_values[ x ];
}
return avg / m_size;
}
int size() const
{
return m_size;
}
};