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_AMPLITUDE_METER_h
11#define _MITOV_AMPLITUDE_METER_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17 const float FLT_MAX = 3.4028235E+38;
18
19 class AmplitudeMeter : public Mitov::CommonEnableFilter
20 {
21 typedef Mitov::CommonEnableFilter inherited;
22
23 public:
24 OpenWire::SourcePin MinOutputPin;
25 OpenWire::SourcePin MaxOutputPin;
26
27 protected:
28 unsigned long FStartTime = 0;
29 double FAccumulator = 0.0f;
30 float FMin = 0;
31 float FMax = 0;
32 unsigned long FCount = 0;
33
34 public:
35 long Period = 100;
36
37 protected:
38 virtual void DoReceive( void *_Data )
39 {
40 if( ! Enabled )
41 return;
42
43 float AValue = *(float *)_Data;
44 FAccumulator += AValue;
45 if( FCount == 0 )
46 {
47 FMin = AValue;
48 FMax = AValue;
49 }
50
51 else
52 {
53 if( AValue < FMin )
54 FMin = AValue;
55
56 if( AValue > FMax )
57 FMax = AValue;
58
59 }
60
61 ++FCount;
62
63 unsigned long ANow = micros();
64 unsigned long APeriod = ANow - FStartTime;
65
66 if( APeriod < ((unsigned long)Period) * 1000 )
67 return;
68
69 FStartTime = ANow;
70 if( FCount == 0 )
71 --FCount;
72
73 float AAverage = FAccumulator / FCount;
74 FAccumulator = 0;
75 FCount = 0;
76
77 OutputPin.Notify( &AAverage );
78 MinOutputPin.Notify( &FMin );
79 MaxOutputPin.Notify( &FMax );
80 }
81
82 virtual void SystemInit()
83 {
84 FAccumulator = 0;
85 FCount = 0;
86
87 inherited::SystemInit();
88 }
89
90 };
91}
92
93#endif