libraries / Mitov / Mitov_Maxim_Potentiometer_I2C.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_MAXIM_POTENTIOMETER_I2C_h
  11#define _MITOV_MAXIM_POTENTIOMETER_I2C_h
  12
  13#include <Mitov.h>
  14#include <Mitov_Basic_I2C.h>
  15
  16namespace Mitov
  17{
  18//---------------------------------------------------------------------------
  19        class PotentiometerSPIChannel;
  20//---------------------------------------------------------------------------
  21        class Maxim_Potentiometer_I2C : public Mitov::Basic_MultiChannel_SourceI2C
  22        {
  23                typedef Mitov::Basic_MultiChannel_SourceI2C inherited;
  24
  25        public:
  26                byte    Address = 0;
  27
  28        protected:
  29                void TransmitAt( byte ARegister, byte Adata )
  30                {
  31                        byte AAddress = 0b0101000 | (( Address & 0b111 ) << 1 );
  32                        Wire.beginTransmission( AAddress );
  33
  34                        Wire.write( ARegister );
  35                        Wire.write( Adata );
  36
  37                        Wire.endTransmission();
  38                }
  39
  40        protected:
  41                virtual void UpdateOutput()
  42                {
  43                        float   ANewValue = FChannels[ 0 ]->FNewValue;
  44
  45                        TransmitAt( 0b00010001, (byte)ANewValue );
  46
  47                        FChannels[ 0 ]->FValue = ANewValue;
  48                }
  49
  50        protected:
  51                virtual void SystemStart() override
  52                {
  53                        inherited::SystemStart();
  54                        UpdateOutput();
  55                }
  56
  57                virtual void SystemLoopUpdateHardware() override
  58                {
  59                        if( FModified )
  60                                if( ! ClockInputPin.IsConnected() )
  61                                        UpdateOutput();
  62
  63                        inherited::SystemLoopUpdateHardware();
  64                }
  65
  66        protected:
  67                void DoClockReceive( void * )
  68                {
  69                        if( FModified )
  70                                UpdateOutput();
  71                
  72                }
  73
  74        };
  75//---------------------------------------------------------------------------
  76        class Maxim_Potentiometer_I2C2Channel : public Mitov::Maxim_Potentiometer_I2C
  77        {
  78                typedef Mitov::Maxim_Potentiometer_I2C2Channel inherited;
  79
  80        protected:
  81                virtual void UpdateOutput()
  82                {
  83                        float   ANewValue = FChannels[ 0 ]->FNewValue;
  84                        if( ANewValue == FChannels[ 1 ]->FNewValue )
  85                                // Update both channels at once
  86                                TransmitAt( 0b00010011, (byte)ANewValue );
  87
  88                        else
  89                        {
  90                                if( ANewValue != FChannels[ 0 ]->FValue )
  91                                        TransmitAt( 0b00010001, (byte)ANewValue );
  92
  93                                if( FChannels[ 1 ]->FNewValue != FChannels[ 1 ]->FValue )
  94                                        TransmitAt( 0b00010010, (byte)FChannels[ 1 ]->FNewValue );
  95                        }
  96
  97                        FChannels[ 0 ]->FValue = ANewValue;
  98                        FChannels[ 1 ]->FValue = FChannels[ 1 ]->FNewValue;
  99                }
 100
 101        };
 102//---------------------------------------------------------------------------
 103        template<int T_MULTIPLIER> class Maxim_Potentiometer_I2C_Channel : public Mitov::Basic_Typed_I2CChannel<Maxim_Potentiometer_I2C>
 104        {
 105                typedef Mitov::Basic_Typed_I2CChannel<Maxim_Potentiometer_I2C>  inherited;
 106
 107        protected:
 108                virtual void DoReceive( void *_Data ) override
 109                {
 110                        FNewValue = constrain( *((float *)_Data), 0, 1 ) * T_MULTIPLIER;
 111                        if( FNewValue == FValue )
 112                                return;
 113
 114                        FOwner.FModified = true;
 115                }
 116
 117        public:
 118                using inherited::inherited;
 119
 120        };
 121//---------------------------------------------------------------------------
 122}
 123
 124#endif