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_CONVERTERS_h
11#define _MITOV_CONVERTERS_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17//---------------------------------------------------------------------------
18 template<typename T> class TypedToAnalog : public CommonFilter
19 {
20 typedef CommonFilter inherited;
21
22 public:
23 float Scale = 1.0;
24
25 protected:
26 virtual void DoReceive( void *_Data ) override
27 {
28 float AFloatValue = ( *(T*)_Data ) * Scale;
29 OutputPin.Notify( &AFloatValue );
30 }
31
32 };
33//---------------------------------------------------------------------------
34 class AnalogToInteger : public CommonFilter
35 {
36 typedef CommonFilter inherited;
37
38 public:
39 bool Round = true;
40 float Scale = 1.0;
41
42 protected:
43 virtual void DoReceive( void *_Data ) override
44 {
45 float AFloatValue = *(float*)_Data * Scale;
46 if( Round )
47 AFloatValue += 0.5f;
48
49 long AIntValue = AFloatValue;
50 OutputPin.Notify( &AIntValue );
51 }
52
53 };
54//---------------------------------------------------------------------------
55 class AnalogToUnsigned : public CommonFilter
56 {
57 typedef CommonFilter inherited;
58
59 public:
60 bool Round = true;
61 bool Constrain = true;
62 float Scale = 1.0;
63
64 protected:
65 virtual void DoReceive( void *_Data ) override
66 {
67 float AFloatValue = *(float*)_Data * Scale;
68 if( Round )
69 AFloatValue += 0.5f;
70
71 long AIntValue = AFloatValue;
72 if( Constrain )
73 AIntValue &= 0x7FFFFFFF;
74
75 OutputPin.Notify( &AIntValue );
76 }
77
78 };
79//---------------------------------------------------------------------------
80 class AnalogToText : public CommonFilter
81 {
82 typedef CommonFilter inherited;
83
84 public:
85 int MinWidth = 1;
86 int Precision = 3;
87
88 protected:
89 virtual void DoReceive( void *_Data ) override
90 {
91 float AFloatValue = *(float*)_Data;
92 char AText[ 50 ];
93 dtostrf( AFloatValue, MinWidth, Precision, AText );
94 OutputPin.Notify( AText );
95 }
96
97 };
98//---------------------------------------------------------------------------
99 template<typename T> class TypedToText : public CommonFilter
100 {
101 typedef CommonFilter inherited;
102
103 public:
104 int Base = 10;
105
106 protected:
107 virtual void DoReceive( void *_Data ) override
108 {
109 T AValue = *(T*)_Data;
110 char AText[ 50 ];
111 itoa( AValue, AText, Base );
112 OutputPin.Notify( AText );
113 }
114
115 };
116//---------------------------------------------------------------------------
117 class DigitalToText : public CommonFilter
118 {
119 typedef CommonFilter inherited;
120
121 public:
122 String TrueValue = "true";
123 String FalseValue = "false";
124
125 protected:
126 virtual void DoReceive( void *_Data ) override
127 {
128 bool AValue = *(bool *)_Data;
129 const char *AText;
130 if( AValue )
131 AText = TrueValue.c_str();
132
133 else
134 AText = FalseValue.c_str();
135
136 OutputPin.Notify( (void*)AText );
137 }
138
139 };
140//---------------------------------------------------------------------------
141 template <typename T, T C_TRUE, T T_FALSE> class DigitalToType : public CommonFilter
142 {
143 typedef CommonFilter inherited;
144
145 public:
146 T TrueValue = C_TRUE;
147 T FalseValue = T_FALSE;
148
149 protected:
150 virtual void DoReceive( void *_Data ) override
151 {
152 if( *(bool *)_Data )
153 OutputPin.Notify( &TrueValue );
154
155 else
156 OutputPin.Notify( &FalseValue );
157
158 }
159
160 };
161//---------------------------------------------------------------------------
162 template <typename T> class DigitalToTypeParams : public CommonFilter
163 {
164 typedef CommonFilter inherited;
165
166 public:
167 T TrueValue;
168 T FalseValue;
169
170 protected:
171 virtual void DoReceive( void *_Data ) override
172 {
173 if( *(bool *)_Data )
174 OutputPin.Notify( &TrueValue );
175
176 else
177 OutputPin.Notify( &FalseValue );
178
179 }
180
181 public:
182 DigitalToTypeParams( T ATrueValue, T AFalseValue ) :
183 TrueValue( ATrueValue ),
184 FalseValue( AFalseValue )
185 {
186 }
187 };
188//---------------------------------------------------------------------------
189 class TextToAnalog : public CommonFilter
190 {
191 typedef CommonFilter inherited;
192
193 protected:
194 virtual void DoReceive( void *_Data ) override
195 {
196 char * AText = (char*)_Data;
197 float AValue = strtod( AText, NULL );
198 OutputPin.Notify( &AValue );
199 }
200
201 };
202//---------------------------------------------------------------------------
203 class TextToInteger : public CommonFilter
204 {
205 typedef CommonFilter inherited;
206
207 protected:
208 virtual void DoReceive( void *_Data ) override
209 {
210 char * AText = (char*)_Data;
211 long AValue = atoi( AText );
212 OutputPin.Notify( &AValue );
213 }
214
215 };
216//---------------------------------------------------------------------------
217 class TextToUnsigned : public CommonFilter
218 {
219 typedef CommonFilter inherited;
220
221 protected:
222 virtual void DoReceive( void *_Data ) override
223 {
224 char * AText = (char*)_Data;
225 unsigned long AValue = atoi( AText );
226 OutputPin.Notify( &AValue );
227 }
228
229 };
230//---------------------------------------------------------------------------
231 class UnsignedToInteger : public CommonFilter
232 {
233 typedef CommonFilter inherited;
234
235 public:
236 bool Constrain = true;
237
238 protected:
239 virtual void DoReceive( void *_Data ) override
240 {
241 unsigned long AValue = *(unsigned long*)_Data;
242 if( Constrain )
243 AValue &= 0x7FFFFFFF;
244
245 OutputPin.Notify( &AValue );
246 }
247
248 };
249//---------------------------------------------------------------------------
250 class IntegerToUnsigned : public CommonFilter
251 {
252 typedef CommonFilter inherited;
253
254 public:
255 bool Constrain = true;
256
257 protected:
258 virtual void DoReceive( void *_Data ) override
259 {
260 long AValue = *(long*)_Data;
261 if( Constrain )
262 AValue &= 0x7FFFFFFF;
263
264 OutputPin.Notify( &AValue );
265 }
266
267 };
268//---------------------------------------------------------------------------
269 template<int C_NUM_INPUTS> class UnsignedToDigital : public CommonSink
270 {
271 typedef CommonSink inherited;
272
273 public:
274 OpenWire::SourcePin OutputPins[ C_NUM_INPUTS ];
275
276 public:
277 uint32_t InitialValue = 0;
278
279 protected:
280 uint32_t FOldValue = 0;
281
282 protected:
283 void SetValue( uint32_t AValue, bool AUpdate )
284 {
285 for( long i = 0; i < C_NUM_INPUTS; ++i )
286 {
287 unsigned long ABit = ((unsigned long)1) << i;
288 bool AOldBitValue = ( FOldValue & ABit );
289 bool ANewBitValue = ( AValue & ABit );
290 if( AUpdate || AOldBitValue != ANewBitValue )
291 OutputPins[ i ].Notify( (void *)&GBooleanConst[ ANewBitValue ] );
292
293 }
294
295 FOldValue = AValue;
296 }
297
298 protected:
299 virtual void DoReceive( void *_Data ) override
300 {
301 unsigned long AValue = *(unsigned long *)_Data;
302 if( FOldValue == AValue )
303 return;
304
305 SetValue( AValue, false );
306 }
307
308 virtual void SystemInit()
309 {
310 inherited::SystemInit();
311 SetValue( InitialValue, true );
312 }
313
314 };
315//---------------------------------------------------------------------------
316 class ReversableConverter : public CommonTypedFilter<float>
317 {
318 typedef CommonTypedFilter<float> inherited;
319
320 public:
321 bool Reverse = false;
322
323 };
324//---------------------------------------------------------------------------
325 class CelsiusToFahrenheit : public ReversableConverter
326 {
327 typedef ReversableConverter inherited;
328
329 protected:
330 virtual float FilterValue( float AValue ) override
331 {
332 if( Reverse )
333 return ( AValue - 32.0 ) / ( 9.0/5.0 );
334
335 else
336 return AValue * ( 9.0/5.0 ) + 32.0;
337 }
338
339 };
340//---------------------------------------------------------------------------
341 class CelsiusToKelvin : public ReversableConverter
342 {
343 typedef ReversableConverter inherited;
344
345 protected:
346 virtual float FilterValue( float AValue ) override
347 {
348 if( Reverse )
349 return AValue - 273.15;
350
351 else
352 return AValue + 273.15;
353 }
354
355 };
356//---------------------------------------------------------------------------
357 template<int T_SIZE> class CharToText : public CommonImplementedEnableFilter
358 {
359 typedef CommonImplementedEnableFilter inherited;
360
361 public:
362 uint32_t MaxLength = 100;
363 bool Truncate = false;
364 bool UpdateOnEachChar = false;
365
366 protected:
367 byte FBuffer[ T_SIZE + 1 ];
368 uint32_t FIndex = 0;
369
370 protected:
371 void SendBufferNoReset()
372 {
373 FBuffer[ FIndex ] = '\0';
374 inherited::OutputPin.Notify( FBuffer );
375 }
376
377 void SendBuffer()
378 {
379 SendBufferNoReset();
380 FIndex = 0;
381 }
382
383 virtual void ReceiveValue( void *_Data ) override
384 {
385 char AValue = *(char *)_Data;
386 if( AValue == '\n' )
387 return;
388
389 if( AValue == '\r' )
390 {
391 SendBuffer();
392 return;
393 }
394
395 if( FIndex >= T_SIZE )
396 {
397 if( Truncate )
398 return;
399
400 SendBuffer();
401 }
402
403 FBuffer[ FIndex++ ] = AValue;
404 if( UpdateOnEachChar )
405 SendBufferNoReset();
406
407 }
408
409 };
410//---------------------------------------------------------------------------
411 class TextToChar : public CommonImplementedEnableFilter
412 {
413 typedef CommonImplementedEnableFilter inherited;
414
415 public:
416 bool AddReturn = true;
417 bool AddNewLine = true;
418
419 protected:
420 virtual void ReceiveValue( void *_Data ) override
421 {
422 char *AValue = (char *)_Data;
423 while( *AValue )
424 {
425 inherited::OutputPin.Notify( AValue );
426 ++AValue;
427 }
428
429 if( AddReturn )
430 inherited::OutputPin.SendValue( '\r' );
431
432 if( AddNewLine )
433 inherited::OutputPin.SendValue( '\n' );
434 }
435 };
436//---------------------------------------------------------------------------
437 class PressureToAltitude : public CommonTypedFilter<float>
438 {
439 public:
440 float BaseLinePressure = 0;
441
442 protected:
443 virtual float FilterValue( float AValue ) override
444 {
445 return ( 44330.0 * ( 1 - pow( AValue / BaseLinePressure, 1 / 5.255 )));
446 }
447
448 };
449//---------------------------------------------------------------------------
450 class AltitudePressureToSeaLevelPressure : public CommonTypedFilter<float>
451 {
452 public:
453 float Altitude = 0;
454
455 protected:
456 virtual float FilterValue( float AValue ) override
457 {
458 return ( AValue / pow( 1 - ( Altitude / 44330.0 ), 5.255 ));
459 }
460
461 };
462//---------------------------------------------------------------------------
463}
464
465#endif