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_INFRARED_AVOIDANCE_SENSOR_h
11#define _MITOV_INFRARED_AVOIDANCE_SENSOR_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17 class InfraredAvoidanceSensor : public Mitov::CommonEnableFilter
18 {
19 typedef Mitov::CommonEnableFilter inherited;
20
21 public:
22 OpenWire::SinkPin EnableInputPin;
23 OpenWire::SourcePin EnableOutputPin;
24
25 public:
26 unsigned long EnableDelay = 500;
27
28 protected:
29 unsigned long FStartTime = 0;
30 bool FDisabled = false;
31 bool FCurrentValue = false;
32
33 protected:
34 virtual void DoReceive( void *_Data )
35 {
36 FCurrentValue = *(bool *)_Data;
37 if( ! FDisabled )
38 OutputPin.Notify( &FCurrentValue );
39 }
40
41 void DoEnableReceive( void *_Data )
42 {
43 bool AValue = *(bool *)_Data;
44 if( AValue == Enabled )
45 return;
46
47 Enabled = AValue;
48 TryStartTimer();
49 }
50
51
52 void TryStartTimer()
53 {
54 if( Enabled )
55 {
56 FDisabled = true;
57 FStartTime = millis();
58 }
59
60 EnableOutputPin.Notify( &Enabled );
61 }
62
63 protected:
64 virtual void SystemInit()
65 {
66 inherited::SystemInit();
67 TryStartTimer();
68 }
69
70 virtual void SystemLoopBegin( unsigned long currentMicros )
71 {
72 if( FDisabled )
73 {
74 unsigned long currentMilis = millis();
75 if( currentMilis - FStartTime >= EnableDelay )
76 {
77 FDisabled = false;
78 OutputPin.Notify( &FCurrentValue );
79 }
80 }
81
82 inherited::SystemLoopBegin( currentMicros );
83 }
84
85 public:
86 InfraredAvoidanceSensor()
87 {
88 EnableInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&InfraredAvoidanceSensor::DoEnableReceive );
89 }
90
91 };
92}
93
94#endif