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_COMMON_MUX_h
11#define _MITOV_COMMON_MUX_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17 template<typename T, typename T_OUT> class ToggleSwitch : public Mitov::BasicMultiInput<T, T_OUT>
18 {
19 typedef Mitov::BasicMultiInput<T, T_OUT> inherited;
20
21 public:
22 OpenWire::VlaueSinkPin<T> TrueInputPin;
23 OpenWire::VlaueSinkPin<T> FalseInputPin;
24
25 OpenWire::SinkPin SelectInputPin;
26
27 public:
28 bool InitialSelectValue = false;
29
30 protected:
31 bool FSelectValue = false;
32
33 protected:
34 virtual void SystemStart()
35 {
36 FSelectValue = InitialSelectValue;
37 inherited::SystemStart();
38 }
39
40 protected:
41 virtual void DoReceiveSelect( void *_Data )
42 {
43 bool AValue = *(bool *)_Data;
44 if( FSelectValue == AValue )
45 return;
46
47 FSelectValue = AValue;
48 inherited::CallCalculateSendOutput( false );
49 }
50
51 protected:
52 virtual T_OUT CalculateOutput() override
53 {
54 if( FSelectValue )
55 return (T_OUT)TrueInputPin.Value;
56
57 else
58 return (T_OUT)FalseInputPin.Value;
59 }
60
61 public:
62 ToggleSwitch()
63 {
64 SelectInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&ToggleSwitch::DoReceiveSelect );
65 TrueInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&ToggleSwitch::DoReceive );
66 FalseInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&ToggleSwitch::DoReceive );
67 }
68
69 };
70//---------------------------------------------------------------------------
71 template<typename T, int C_NUM_INPUTS> class CommonMux : public Mitov::CommonMultiInput<T, C_NUM_INPUTS>
72 {
73 typedef Mitov::CommonMultiInput<T, C_NUM_INPUTS> inherited;
74
75 protected:
76 unsigned int FChannel = 0;
77
78 public:
79 unsigned int InitialChannel = 0;
80
81 public:
82 OpenWire::SinkPin SelectInputPin;
83
84 protected:
85 virtual T CalculateOutput() override
86 {
87 return inherited::InputPins[ FChannel ].Value;
88 }
89
90 protected:
91 virtual void DoReceiveSelect( void *_Data )
92 {
93 uint32_t AChannel = *(uint32_t *)_Data;
94 if( AChannel >= C_NUM_INPUTS )
95 AChannel = C_NUM_INPUTS - 1;
96
97 if( FChannel == AChannel )
98 return;
99
100// Serial.println( AChannel );
101
102 FChannel = AChannel;
103 inherited::CallCalculateSendOutput( false );
104 }
105
106 protected:
107 virtual void SystemStart()
108 {
109 FChannel = InitialChannel;
110 inherited::SystemStart();
111 }
112
113 public:
114 CommonMux()
115 {
116 SelectInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&CommonMux::DoReceiveSelect );
117 }
118
119 };
120//---------------------------------------------------------------------------
121}
122
123#endif