-
Notifications
You must be signed in to change notification settings - Fork 6
/
Util.h
171 lines (139 loc) · 3.08 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h> // Arduino compiler can get confused if you don't include include all required headers in this file?!?
#include "CompileSwitches.h"
#ifdef DEBUG_OUTPUT
extern bool serial_port_initialised;
inline 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 // !DEBUG_OUPUT
#define ASSERT_MSG(x, msg)
#define DEBUG_TEXT(x)
#endif // !DEBUG_OUTPUT
/////////////////////////////////////////////////////
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 );
}
// from http://polymathprogrammer.com/2008/09/29/linear-and-cubic-interpolation/
inline float cubic_interpolation( float p0, float p1, float p2, float p3, float t )
{
const float one_minus_t = 1.0f - t;
return ( one_minus_t * one_minus_t * one_minus_t * p0 ) + ( 3 * one_minus_t * one_minus_t * t * p1 ) + ( 3 * one_minus_t * t * t * p2 ) + ( t * t * t * p3 );
}
/////////////////////////////////////////////////////
inline constexpr int trunc_to_int( float v )
{
return static_cast<int>( v );
}
inline constexpr int round_to_int( float v )
{
return static_cast<int>( v + 0.5f );
}
/////////////////////////////////////////////////////
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;
}
};
inline float random_ranged( float min, float max )
{
const float range = max - min;
return ( ( static_cast<float>( rand() ) / RAND_MAX ) * range ) + min;
}
/////////////////////////////////////////////////////