libraries / Mitov / Mitov_PS2_Controller_Basic.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_PS2_CONTROLLER_BASIC_h
  11#define _MITOV_PS2_CONTROLLER_BASIC_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        class PS2BasicController : public Mitov::EnabledComponent, public Mitov::ClockingSupport
  18        {
  19                typedef Mitov::EnabledComponent inherited;
  20
  21        public:
  22                virtual bool    ReadDigital( unsigned int AIndex ) = 0;
  23                virtual float   ReadAnalog( unsigned int AIndex ) = 0;
  24
  25        public:
  26                class BaseSensor : public OpenWire::SourcePin
  27                {
  28                protected:
  29                        unsigned        int             FIndex;
  30
  31                public:
  32                        virtual void    Process( PS2BasicController *AOwner ) = 0;
  33                        virtual void    StartProcess( PS2BasicController *AOwner ) = 0;
  34
  35                public:
  36                        BaseSensor( unsigned int AIndex ) :
  37                                FIndex( AIndex )
  38                        {
  39                        }
  40
  41                };
  42
  43                class DigitalSensor : public BaseSensor
  44                {
  45                        typedef BaseSensor inherited;
  46
  47                protected:
  48                        bool    FValue;
  49
  50                public:
  51                        virtual void Process( PS2BasicController *AOwner ) override
  52                        {
  53                                bool AValue = AOwner->ReadDigital( FIndex );
  54
  55                                if( FValue == AValue )
  56                                        return;
  57
  58                                FValue = AValue;
  59                                Notify( &FValue );
  60                        }
  61
  62                        virtual void StartProcess( PS2BasicController *AOwner ) override
  63                        {
  64                                bool AValue = AOwner->ReadDigital( FIndex );
  65                                FValue = AValue;
  66                                Notify( &FValue );
  67                        }
  68
  69                public:
  70                        using inherited::inherited;
  71
  72                };
  73
  74                class AnalogSensor : public BaseSensor
  75                {
  76                        typedef BaseSensor inherited;
  77
  78                protected:
  79                        float   FValue;
  80
  81                public:
  82                        virtual void    Process( PS2BasicController *AOwner ) override
  83                        {
  84                                float   AValue = AOwner->ReadAnalog( FIndex );
  85
  86                                if( FValue == AValue )
  87                                        return;
  88
  89                                FValue = AValue;
  90                                Notify( &FValue );                              
  91                        }
  92
  93                        virtual void StartProcess( PS2BasicController *AOwner ) override
  94                        {
  95                                float   AValue = AOwner->ReadAnalog( FIndex );
  96
  97                                FValue = AValue;
  98                                Notify( &FValue );                              
  99                        }
 100
 101                public:
 102                        using inherited::inherited;
 103
 104                };
 105
 106        public:
 107                Mitov::SimpleList<Mitov::PS2BasicController::BaseSensor *> Sensors;
 108
 109        protected:
 110                virtual void SystemLoopBegin( unsigned long currentMicros ) override
 111                {
 112                        if( ! ClockInputPin.IsConnected() )
 113                                ReadData();
 114
 115                        inherited::SystemLoopBegin( currentMicros );
 116                }
 117
 118                virtual void SystemStart() override
 119                {
 120                        ReadController();
 121                        for( int i = 0; i < Sensors.size(); ++i )
 122                                Sensors[ i ]->StartProcess( this );
 123
 124                        inherited::SystemStart();
 125                }
 126
 127        protected:
 128                virtual void DoClockReceive( void *_Data ) override
 129                {
 130                        ReadData();
 131                }
 132
 133                void ReadData()
 134                {
 135                        if( ! Enabled )
 136                                return;
 137
 138                        ReadController();
 139
 140                        for( int i = 0; i < Sensors.size(); ++i )
 141                                Sensors[ i ]->Process( this );
 142                }
 143
 144                virtual void ReadController() = 0;
 145
 146/*
 147                virtual ~PS2BasicController()
 148                {
 149                        for( int i = 0; i < Sensors.size(); ++i )
 150                                delete Sensors[ i ];
 151                }
 152*/
 153        };
 154//---------------------------------------------------------------------------
 155}
 156
 157#endif