1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_MATH_h
11#define _MITOV_MATH_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17#define Min Min
18#define Max Max
19//---------------------------------------------------------------------------
20 #define MITOV_PI 3.14159265359
21//---------------------------------------------------------------------------
22 template<typename T> class CommonValueMathFilter : public CommonTypedFilter<T>
23 {
24 typedef CommonTypedFilter<T> inherited;
25
26 public:
27 T Value;
28
29 public:
30 CommonValueMathFilter( T AValue = 0 ) : Value( AValue ) {}
31
32 };
33//---------------------------------------------------------------------------
34 template<typename T> class AddValue : public CommonValueMathFilter<T>
35 {
36 typedef CommonValueMathFilter<T> inherited;
37
38 protected:
39 virtual T FilterValue( T AValue )
40 {
41 return AValue + inherited::Value;
42 }
43
44 };
45//---------------------------------------------------------------------------
46 template<typename T> class SubtractValue : public CommonValueMathFilter<T>
47 {
48 typedef CommonValueMathFilter<T> inherited;
49
50 protected:
51 virtual T FilterValue( T AValue )
52 {
53 return AValue - inherited::Value;
54 }
55
56 };
57//---------------------------------------------------------------------------
58 template<typename T> class SubtractFromValue : public CommonValueMathFilter<T>
59 {
60 typedef CommonValueMathFilter<T> inherited;
61
62 protected:
63 virtual T FilterValue( T AValue )
64 {
65 return inherited::Value - AValue;
66 }
67
68 };
69//---------------------------------------------------------------------------
70 template<typename T> class MultiplyByValue : public CommonValueMathFilter<T>
71 {
72 typedef CommonValueMathFilter<T> inherited;
73
74 protected:
75 virtual T FilterValue( T AValue )
76 {
77 return AValue * inherited::Value;
78 }
79
80 public:
81 MultiplyByValue() :
82 inherited( 1 )
83 {
84 }
85 };
86//---------------------------------------------------------------------------
87 template<typename T> class DivideByValue : public CommonValueMathFilter<T>
88 {
89 typedef CommonValueMathFilter<T> inherited;
90
91 protected:
92 virtual T FilterValue( T AValue )
93 {
94 return AValue / inherited::Value;
95 }
96
97 public:
98 DivideByValue() :
99 inherited( 1 )
100 {
101 }
102 };
103//---------------------------------------------------------------------------
104 template<typename T> class DivideValue : public CommonValueMathFilter<T>
105 {
106 typedef CommonValueMathFilter<T> inherited;
107
108 protected:
109 virtual T FilterValue( T AValue )
110 {
111 return inherited::Value / AValue;
112 }
113
114 };
115//---------------------------------------------------------------------------
116 template<typename T> class Inverse : public CommonTypedFilter<T>
117 {
118 typedef CommonTypedFilter<T> inherited;
119
120 protected:
121 virtual T FilterValue( T AValue )
122 {
123 return -AValue;
124 }
125
126 };
127//---------------------------------------------------------------------------
128 template<typename T> class Abs : public CommonTypedFilter<T>
129 {
130 typedef CommonTypedFilter<T> inherited;
131
132 protected:
133 virtual T FilterValue( T AValue )
134 {
135 return abs( AValue );
136 }
137
138 };
139//---------------------------------------------------------------------------
140 class MinLimit : public CommonValueMathFilter<float>
141 {
142 protected:
143 virtual float FilterValue( float AValue )
144 {
145 return ( AValue < Value ) ? Value : AValue;
146 }
147 };
148//---------------------------------------------------------------------------
149 class MaxLimit : public CommonValueMathFilter<float>
150 {
151 typedef CommonValueMathFilter<float> inherited;
152
153 protected:
154 virtual float FilterValue( float AValue )
155 {
156 return ( AValue > Value ) ? Value : AValue;
157 }
158
159 public:
160 MaxLimit() :
161 inherited( 1.0 )
162 {
163 }
164 };
165//---------------------------------------------------------------------------
166 class Limit : public CommonTypedFilter<float>
167 {
168 public:
169 float Min = 0.0;
170 float Max = 1.0;
171
172 protected:
173 virtual float FilterValue( float AValue )
174 {
175 return ( AValue > Max ) ?
176 Max :
177 ( AValue < Min ) ? Min : AValue;
178 }
179
180 };
181//---------------------------------------------------------------------------
182 enum CompareValueType { ctEqual, ctNotEqual, ctBigger, ctSmaller, ctBiggerOrEqual, ctSmallerOrEqual };
183//---------------------------------------------------------------------------
184 template<typename T> class CompareValue : public CommonEnableFilter
185 {
186 typedef CommonEnableFilter inherited;
187
188 public:
189 T Value;
190
191 protected:
192 T FCurrentValue;
193
194 public:
195 CompareValueType CompareType : 3;
196 bool OnlyChanged : 1;
197
198 protected:
199 bool FOutputValue : 1;
200 bool FStarted : 1;
201 bool FProcessedOnce : 1;
202
203 public:
204 void SetValue( T AValue )
205 {
206 if( OnlyChanged )
207 if( Value == AValue )
208 return;
209
210 Value = AValue;
211 ProcessOutput();
212 }
213
214 protected:
215 void ProcessOutput()
216 {
217 if( ! inherited::Enabled )
218 return;
219
220// Serial.print( FCurrentValue ); Serial.print( " ? " ); Serial.println( Value );
221
222 bool ABoolValue;
223 switch( CompareType )
224 {
225 case ctEqual:
226 ABoolValue = ( FCurrentValue == Value );
227 break;
228
229 case ctNotEqual:
230 ABoolValue = ( FCurrentValue != Value );
231 break;
232
233 case ctBigger:
234 ABoolValue = ( FCurrentValue > Value );
235 break;
236
237 case ctSmaller:
238 ABoolValue = ( FCurrentValue < Value );
239 break;
240
241 case ctBiggerOrEqual:
242 ABoolValue = ( FCurrentValue >= Value );
243 break;
244
245 case ctSmallerOrEqual:
246 ABoolValue = ( FCurrentValue <= Value );
247 break;
248
249 }
250
251 if( ! OnlyChanged )
252 if( FOutputValue )
253 {
254 FOutputValue = false;
255 OutputPin.SendValue( false );
256 }
257
258 if( ( !FStarted ) || ( FOutputValue != ABoolValue ) )
259 {
260 OutputPin.Notify( &ABoolValue );
261 FOutputValue = ABoolValue;
262 FStarted = true;
263 }
264 }
265
266 protected:
267 virtual void DoReceive( void *_Data ) override
268 {
269 T AValue = *(T*)_Data;
270
271 if( OnlyChanged )
272 if( FProcessedOnce )
273 if( AValue == FCurrentValue )
274 return;
275
276 FCurrentValue = AValue;
277 FProcessedOnce = true;
278
279 ProcessOutput();
280 }
281
282 public:
283 CompareValue( T AInitialValue ) :
284 Value( AInitialValue ),
285 FCurrentValue( AInitialValue ),
286 CompareType( ctEqual ),
287 OnlyChanged( true ),
288 FProcessedOnce( false ),
289 FStarted( false ),
290 FOutputValue( false )
291 {
292 }
293
294 };
295//---------------------------------------------------------------------------
296 class CompareStringValue : public CompareValue<String>
297 {
298 typedef CompareValue<String> inherited;
299
300 protected:
301 virtual void DoReceive( void *_Data ) override
302 {
303 String AValue = String( (char*)_Data );
304
305 if( OnlyChanged )
306 if( FProcessedOnce )
307 if( AValue == FCurrentValue )
308 return;
309
310 FCurrentValue = AValue;
311 FProcessedOnce = true;
312
313 ProcessOutput();
314 }
315
316 public:
317 CompareStringValue() :
318 inherited( String() )
319 {
320 }
321 };
322//---------------------------------------------------------------------------
323 class AveragePeriod : public CommonFilter
324 {
325 public:
326 unsigned long Period = 1000;
327
328 protected:
329 float FSum = 0.0f;
330 unsigned long FCount = 0;
331 unsigned long FLastTime = 0;
332
333 protected:
334 virtual void DoReceive( void *_Data ) override
335 {
336 FSum += *(float *)_Data;
337 ++FCount;
338 }
339
340 virtual void SystemLoopBegin( unsigned long currentMicros ) override
341 {
342 if( currentMicros - FLastTime < Period )
343 return;
344
345 if( ! FCount )
346 return;
347
348 float AValue = FSum / FCount;
349
350 FSum = 0.0;
351 FCount = 0;
352 FLastTime = currentMicros;
353
354 OutputPin.Notify( &AValue );
355 }
356
357 };
358//---------------------------------------------------------------------------
359 template<typename T, int C_NUM_INPUTS> class CommonMathMultiInput : public CommonClockedMultiInput<T, C_NUM_INPUTS>
360 {
361 typedef CommonClockedMultiInput<T, C_NUM_INPUTS> inherited;
362
363 public:
364 bool Enabled = true;
365
366 };
367//---------------------------------------------------------------------------
368 template<typename T, int C_NUM_INPUTS> class Add : public CommonMathMultiInput<T, C_NUM_INPUTS>
369 {
370 typedef CommonMathMultiInput<T, C_NUM_INPUTS> inherited;
371
372 protected:
373 virtual T CalculateOutput() override
374 {
375 T AValue = 0;
376 for( int i = 0; i < C_NUM_INPUTS; ++i )
377 AValue += inherited::InputPins[ i ].Value;
378
379 return AValue;
380 }
381 };
382//---------------------------------------------------------------------------
383 template<typename T, int C_NUM_INPUTS> class Multiply : public CommonMathMultiInput<T, C_NUM_INPUTS>
384 {
385 typedef CommonMathMultiInput<T, C_NUM_INPUTS> inherited;
386
387 protected:
388 virtual T CalculateOutput() override
389 {
390 T AValue = 1;
391 for( int i = 0; i < C_NUM_INPUTS; ++i )
392 AValue *= inherited::InputPins[ i ].Value;
393
394 return AValue;
395 }
396 };
397//---------------------------------------------------------------------------
398 class ValueRange
399 {
400 public:
401 float Min = 0;
402 float Max = 1;
403
404 };
405//---------------------------------------------------------------------------
406 class MapRange : public CommonTypedFilter<float>
407 {
408 typedef CommonTypedFilter<float> inherited;
409
410 public:
411 ValueRange InputRange;
412 ValueRange OutputRange;
413
414 protected:
415 virtual float FilterValue( float AValue ) override
416 {
417 return (( AValue - InputRange.Min ) * (OutputRange.Max - OutputRange.Min) / (InputRange.Max - InputRange.Min)) + OutputRange.Min;
418 }
419 };
420//---------------------------------------------------------------------------
421 template<typename T> class RaiseToPower : public CommonValueMathFilter<T>
422 {
423 typedef CommonValueMathFilter<T> inherited;
424
425 protected:
426 virtual T FilterValue( T AValue ) override
427 {
428 return pow( AValue, inherited::Value );
429 }
430
431 };
432//---------------------------------------------------------------------------
433 class Sine : public CommonTypedFilter<float>
434 {
435 typedef CommonTypedFilter<float> inherited;
436
437 protected:
438 virtual float FilterValue( float AValue ) override
439 {
440 return sin( AValue );
441 }
442
443 };
444//---------------------------------------------------------------------------
445 class Cosine : public CommonTypedFilter<float>
446 {
447 typedef CommonTypedFilter<float> inherited;
448
449 protected:
450 virtual float FilterValue( float AValue ) override
451 {
452 return cos( AValue );
453 }
454
455 };
456//---------------------------------------------------------------------------
457 class RadToDegrees : public CommonTypedFilter<float>
458 {
459 typedef CommonTypedFilter<float> inherited;
460
461 protected:
462 virtual float FilterValue( float AValue ) override
463 {
464 return AValue * 180 / MITOV_PI;
465 }
466
467 };
468//---------------------------------------------------------------------------
469 class DegreesToRad : public CommonTypedFilter<float>
470 {
471 typedef CommonTypedFilter<float> inherited;
472
473 protected:
474 virtual float FilterValue( float AValue ) override
475 {
476 return AValue * MITOV_PI / 180;
477 }
478
479 };
480//---------------------------------------------------------------------------
481 class AndUnsignedValue : public CommonValueMathFilter<uint32_t>
482 {
483 typedef CommonValueMathFilter<uint32_t> inherited;
484
485 protected:
486 virtual uint32_t FilterValue( uint32_t AValue ) override
487 {
488 return AValue & Value;
489 }
490
491 };
492//---------------------------------------------------------------------------
493 class OrUnsignedValue : public CommonValueMathFilter<uint32_t>
494 {
495 typedef CommonValueMathFilter<uint32_t> inherited;
496
497 protected:
498 virtual uint32_t FilterValue( uint32_t AValue ) override
499 {
500 return AValue | Value;
501 }
502
503 };
504//---------------------------------------------------------------------------
505 class XorUnsignedValue : public CommonValueMathFilter<uint32_t>
506 {
507 typedef CommonValueMathFilter<uint32_t> inherited;
508
509 protected:
510 virtual uint32_t FilterValue( uint32_t AValue ) override
511 {
512 return AValue ^ Value;
513 }
514
515 };
516//---------------------------------------------------------------------------
517 class NotUnsignedValue : public CommonTypedFilter<uint32_t>
518 {
519 typedef CommonTypedFilter<uint32_t> inherited;
520
521 protected:
522 virtual uint32_t FilterValue( uint32_t AValue ) override
523 {
524 return ~AValue;
525 }
526
527 };
528//---------------------------------------------------------------------------
529#undef Min
530#undef Max
531}
532
533#endif