libraries / Mitov / Mitov_Basic_GPIO.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_BASIC_GPIO_h
  11#define _MITOV_BASIC_GPIO_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17//---------------------------------------------------------------------------
  18        class BasicGPIOChannel;
  19//---------------------------------------------------------------------------
  20        template<typename T_BASECLASS> class BasicGPIO : public T_BASECLASS
  21        {
  22                typedef T_BASECLASS inherited;
  23
  24        public:
  25                OpenWire::ConnectSinkPin        ReadInputPin;
  26
  27        public:
  28                Mitov::SimpleList<BasicGPIOChannel *>   FChannels;
  29
  30        protected:
  31                virtual void PerformRead() = 0;
  32
  33                void DoReadInputReceive( void * _Data )
  34                {
  35                        PerformRead();
  36                }
  37
  38        protected:
  39                virtual void SystemLoopBegin( unsigned long currentMicros ) 
  40                {
  41                        if( ! ReadInputPin.IsConnected() )
  42                                PerformRead();
  43                }
  44
  45        public:
  46                BasicGPIO()
  47                {
  48                        ReadInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&BasicGPIO::DoReadInputReceive );
  49                }
  50
  51        };
  52//---------------------------------------------------------------------------
  53        template<typename T_BASECLASS> class EnableBasicGPIO : public BasicGPIO<T_BASECLASS>
  54        {
  55                typedef BasicGPIO<T_BASECLASS> inherited;
  56
  57        public:
  58                void    SetEnabled( bool AValue )
  59                {
  60                        if( inherited::Enabled == AValue )
  61                                return;
  62
  63                        inherited::Enabled = AValue;
  64                        UpdateEnable();
  65                }
  66
  67        protected:
  68                virtual void UpdateEnable() = 0;
  69
  70        };
  71//---------------------------------------------------------------------------
  72        class BasicGPIOChannel : public OpenWire::Object
  73        {
  74        public:
  75                OpenWire::SourcePin     OutputPin;
  76
  77        protected:
  78                bool FInValue = false;
  79
  80        public:
  81                void SendOutput()
  82                {
  83                        OutputPin.Notify( &FInValue );
  84                }
  85
  86                void UpdateOutput( bool AValue )
  87                {
  88                        if( FInValue == AValue )
  89                                return;
  90
  91                        FInValue = AValue;
  92
  93                        OutputPin.Notify( &FInValue );
  94                }
  95
  96                virtual void UpdateInput() = 0;
  97
  98        };
  99//---------------------------------------------------------------------------
 100        template<typename T_OWNER> class OwnedBasicGPIOChannel : public BasicGPIOChannel
 101        {
 102        public:
 103                OpenWire::SinkPin       InputPin;
 104
 105        public:
 106        void SetIsOutput( bool AValue )
 107        {
 108            if( FIsOutput == AValue )
 109                return;
 110
 111            FIsOutput = AValue;
 112            PinDirectionsInit();
 113        }
 114
 115        void SetIsPullUp( bool AValue )
 116        {
 117            if( FIsPullUp == AValue )
 118                return;
 119
 120            FIsPullUp = AValue;
 121            PinDirectionsInit();
 122        }
 123
 124        protected:
 125                T_OWNER &FOwner;
 126                bool FValue;
 127                int  FIndex;
 128        bool FIsOutput;
 129        bool FIsPullUp;
 130                bool FCombinedInOut;
 131
 132        protected:
 133        virtual void PinDirectionsInit() = 0;
 134
 135        protected:
 136                void DoDataReceive( void * _Data )
 137                {
 138                        bool AValue = *(bool *)_Data;
 139                        if( FValue == AValue )
 140                                return;
 141
 142                        FValue = AValue;
 143                        FOwner.SetChannelValue( FIndex, AValue );
 144                }
 145
 146        public:
 147                OwnedBasicGPIOChannel( T_OWNER &AOwner, int AIndex, bool AInitialValue, bool AIsOutput, bool AIsPullUp, bool AIsCombinedInOut ) :
 148                        FOwner( AOwner ),
 149                        FIndex( AIndex ),
 150                        FIsOutput( AIsOutput ),
 151                        FIsPullUp( AIsPullUp ),
 152                        FCombinedInOut( AIsCombinedInOut ),
 153                        FValue( AInitialValue )
 154                {
 155                        AOwner.FChannels.push_back( this );
 156
 157                        InputPin.SetCallback( this, (OpenWire::TOnPinReceive)&OwnedBasicGPIOChannel::DoDataReceive );
 158//                      PinDirectionsInit();
 159                }
 160
 161        };
 162//---------------------------------------------------------------------------
 163}
 164
 165#endif