libraries / Mitov / Mitov_RotaryEncoderSensor.hon commit Added link to project report (97a3ba0)
   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_ROTARY_ENCODER_SENSOR_h
  11#define _MITOV_ROTARY_ENCODER_SENSOR_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        class BasicRotaryEncoderSensor : public OpenWire::Component
  18        {
  19                typedef OpenWire::Component inherited;
  20
  21        public:
  22                OpenWire::SinkPin       AInputPin;
  23                OpenWire::SinkPin       BInputPin;
  24
  25                OpenWire::SourcePin     UpOutputPin;
  26                OpenWire::SourcePin     DownOutputPin;
  27
  28        public:
  29                bool    Enabled = true;
  30
  31        protected:
  32                bool    FAValue = false;
  33                bool    FBValue = false;
  34                bool    FAPopulated = false;
  35                bool    FBPopulated = false;
  36
  37        protected:
  38                void DoValueReceiveA( bool AValue )
  39                {
  40                        FAPopulated = true;
  41                        if( FAValue == AValue )
  42                                return;
  43
  44                        FAValue = AValue;
  45                        if( Enabled && AValue && FBPopulated )
  46                        {
  47                                if( FBValue )
  48                                        DownOutputPin.Notify( NULL );
  49
  50                                else
  51                                        UpOutputPin.Notify( NULL );
  52                        }
  53                }
  54
  55                void DoValueReceiveB( bool AValue )
  56                {
  57                        FBPopulated = true;
  58                        if( FBValue == AValue )
  59                                return;
  60
  61                        FBValue = AValue;
  62                }
  63
  64        protected:
  65                virtual void DoReceiveA( void *_Data ) = 0;
  66                virtual void DoReceiveB( void *_Data ) = 0;
  67
  68        public:
  69                BasicRotaryEncoderSensor()
  70                {
  71                        AInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&BasicRotaryEncoderSensor::DoReceiveA );
  72                        BInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&BasicRotaryEncoderSensor::DoReceiveB );
  73                }
  74
  75        };
  76//---------------------------------------------------------------------------
  77        class RotaryEncoderSensor : public BasicRotaryEncoderSensor
  78        {
  79                typedef BasicRotaryEncoderSensor inherited;
  80
  81        protected:
  82                virtual void DoReceiveA( void *_Data ) override
  83                {
  84                        DoValueReceiveA( *(bool*)_Data );
  85                }
  86
  87                virtual void DoReceiveB( void *_Data ) override
  88                {
  89                        DoValueReceiveB( *(bool*)_Data );
  90                }
  91
  92        };
  93//---------------------------------------------------------------------------
  94        class RotaryEncoderSensor_Debounce : public BasicRotaryEncoderSensor
  95        {
  96                typedef BasicRotaryEncoderSensor inherited;
  97
  98        public:
  99                uint32_t        DebounceInterval = 0;
 100
 101        protected:
 102                bool    FALastValue : 1;
 103                bool    FBLastValue : 1;
 104
 105                unsigned long   FALastTime = 0;
 106                unsigned long   FBLastTime = 0;
 107
 108        protected:
 109                virtual void DoReceiveA( void *_Data )
 110                {
 111                        bool AValue = *( bool *)_Data;
 112                        if( AValue != FALastValue )
 113                                FALastTime = millis();
 114
 115                        FALastValue = AValue;
 116                }
 117
 118                virtual void DoReceiveB( void *_Data )
 119                {
 120                        bool AValue = *( bool *)_Data;
 121                        if( AValue != FBLastValue )
 122                                FBLastTime = millis();
 123
 124                        FBLastValue = AValue;
 125                }
 126
 127        protected:
 128                virtual void SystemLoopBegin( unsigned long currentMicros )
 129                {
 130                        if( FAValue != FALastValue )
 131                                if( millis() - FALastTime > DebounceInterval )
 132                                        DoValueReceiveA( FALastValue );
 133
 134                        if( FBValue != FBLastValue )
 135                                if( millis() - FBLastTime > DebounceInterval )
 136                                        DoValueReceiveB( FBLastValue );
 137
 138                        inherited::SystemLoopBegin( currentMicros );
 139                }
 140
 141        public:
 142                RotaryEncoderSensor_Debounce() :
 143                        FALastValue( false ),
 144                        FBLastValue( false )
 145                {
 146                }
 147        };
 148//---------------------------------------------------------------------------
 149}
 150
 151#endif