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_SCHMITT_TRIGGER_h
11#define _MITOV_SCHMITT_TRIGGER_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17//---------------------------------------------------------------------------
18 template<typename T> class SchmittTrigger : public CommonEnableFilter
19 {
20 typedef CommonEnableFilter inherited;
21
22 public:
23 bool InitialValue : 1;
24 bool Inverted : 1;
25
26 protected:
27 T FValue;
28 T FThreshold;
29 T FCurrentValue;
30
31 public:
32 void SetValue( T AValue )
33 {
34 if( FValue == AValue )
35 return;
36
37 FValue = AValue;
38 ProcessOutput();
39 }
40
41 void SetThreshold( T AValue )
42 {
43 if( FThreshold == AValue )
44 return;
45
46 FThreshold = AValue;
47 ProcessOutput();
48 }
49
50 void SetInverted( bool AValue )
51 {
52 if( Inverted == AValue )
53 return;
54
55 Inverted = AValue;
56 ProcessOutput();
57 }
58
59 protected:
60 void ProcessOutput()
61 {
62 if( ! inherited::Enabled )
63 return;
64
65 bool AValue;
66
67 if( Inverted )
68 {
69 if( InitialValue )
70 AValue = ( FCurrentValue < FValue + FThreshold );
71
72 else
73 AValue = ( FCurrentValue < FValue - FThreshold );
74
75 }
76
77 else
78 {
79 if( InitialValue )
80 AValue = ( FCurrentValue > FValue - FThreshold );
81
82 else
83 AValue = ( FCurrentValue > FValue + FThreshold );
84
85 }
86
87 if( InitialValue == AValue )
88 return;
89
90 InitialValue = AValue;
91 OutputPin.Notify( &AValue );
92 }
93
94 protected:
95 virtual void DoReceive( void *_Data )
96 {
97 T AValue = *(T*)_Data;
98 if( AValue == FCurrentValue )
99 return;
100
101 FCurrentValue = AValue;
102 ProcessOutput();
103 }
104
105 public:
106 SchmittTrigger( T AValue, T AThreshold ) :
107 FValue( AValue ),
108 FThreshold( AThreshold ),
109 InitialValue( false ),
110 Inverted( false )
111 {
112 }
113
114 };
115//---------------------------------------------------------------------------
116}
117
118#endif