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_COUNTER_h
11#define _MITOV_COUNTER_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17#define Min Min
18#define Max Max
19 class CounterLimit
20 {
21 public:
22 long Value;
23 bool RollOver = true;
24
25 public:
26 CounterLimit( long AValue ) :
27 Value( AValue )
28 {
29 }
30 };
31//---------------------------------------------------------------------------
32 class BasicCounter : public Mitov::CommonEnableSource
33 {
34 typedef Mitov::CommonEnableSource inherited;
35
36 public:
37 OpenWire::SinkPin ResetInputPin;
38
39 public:
40 long InitialValue = 0;
41
42 public:
43 CounterLimit Min = -2147483648;
44 CounterLimit Max = 2147483647;
45
46 protected:
47 long FCount = 0;
48
49 protected:
50 void DoReceiveReset( void *_Data )
51 {
52 FCount = InitialValue;
53 OutputPin.Notify( &FCount );
54 }
55
56 virtual void SystemInit()
57 {
58// Serial.println( "Counter::SystemInit" );
59 FCount = InitialValue;
60
61 inherited::SystemInit();
62 OutputPin.Notify( &FCount );
63 }
64
65 virtual void SystemStart()
66 {
67 inherited::SystemStart();
68 if( FCount != InitialValue )
69 OutputPin.Notify( &FCount );
70
71 }
72
73 protected:
74 inline void CountUp()
75 {
76 if( Max.RollOver || ( FCount < Max.Value ))
77 {
78 ++FCount;
79
80 if( FCount > Max.Value )
81 FCount = Min.Value;
82
83 OutputPin.Notify( &FCount );
84 }
85 }
86
87 inline void CountDown()
88 {
89 if( Min.RollOver || ( FCount > Min.Value ))
90 {
91 --FCount;
92 if( FCount < Min.Value )
93 FCount = Max.Value;
94
95 OutputPin.Notify( &FCount );
96 }
97 }
98
99 public:
100 BasicCounter()
101 {
102 ResetInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&BasicCounter::DoReceiveReset );
103 }
104
105 };
106//---------------------------------------------------------------------------
107 class Counter : public Mitov::BasicCounter
108 {
109 typedef Mitov::BasicCounter inherited;
110
111 public:
112 OpenWire::SinkPin InputPin;
113
114 public:
115 bool Reversed = false;
116
117 public:
118 virtual void DoReceive( void *_Data )
119 {
120 if( ! Enabled )
121 return;
122
123 if( Reversed )
124 CountDown();
125
126 else
127 CountUp();
128
129 }
130
131 public:
132 Counter()
133 {
134 InputPin.SetCallback( this, (OpenWire::TOnPinReceive)&Counter::DoReceive );
135 }
136
137 };
138//---------------------------------------------------------------------------
139 class UpDownCounter : public Mitov::BasicCounter
140 {
141 typedef Mitov::BasicCounter inherited;
142
143 public:
144 OpenWire::SinkPin UpInputPin;
145 OpenWire::SinkPin DownInputPin;
146
147 protected:
148 void DoReceiveUp( void *_Data )
149 {
150 if( Enabled )
151 CountUp();
152 }
153
154 void DoReceiveDown( void *_Data )
155 {
156 if( Enabled )
157 CountDown();
158 }
159
160 public:
161 UpDownCounter()
162 {
163 UpInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&UpDownCounter::DoReceiveUp );
164 DownInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&UpDownCounter::DoReceiveDown );
165 }
166
167 };
168//---------------------------------------------------------------------------
169#undef Min
170#undef Max
171}
172
173#endif