libraries / Mitov / Mitov_Maxim_MAX521X.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_MAX521X_h
  11#define _MITOV_MAXIM_MAX521X_h
  12
  13#include <Mitov.h>
  14#include <Mitov_Basic_SPI.h>
  15
  16namespace Mitov
  17{
  18//---------------------------------------------------------------------------
  19        enum Maxim_MAX521X_ShutDownMode { msdmHighImpedance, msdm100K, msdm1K };
  20//---------------------------------------------------------------------------
  21        class Maxim_MAX521X : public Mitov::Basic_SPI, public ClockingSupport
  22        {
  23                typedef Mitov::Basic_SPI inherited;
  24
  25        public:
  26                OpenWire::SinkPin       InputPin;
  27
  28        public:
  29                Maxim_MAX521X_ShutDownMode      ShutDownMode = msdmHighImpedance;
  30
  31        public:
  32                void SetEnabled( bool AValue )
  33                {
  34                        if( Enabled == AValue )
  35                                return;
  36
  37                        Enabled = AValue;
  38                        if( Enabled )
  39                                SPI_write( 2, 0 );
  40
  41                        else
  42                                SPI_write( 2, ( ((word)ShutDownMode) + 1 ) << 10 );
  43
  44                }
  45
  46        protected:
  47                word FValue = 0;
  48
  49        protected:
  50                void UpdateValue()
  51                {
  52                        SPI_write( 1, FValue );
  53                }
  54
  55                virtual void DoClockReceive( void *_Data ) override
  56                {
  57                        UpdateValue();
  58                }
  59
  60        protected:
  61                virtual void SPI_transfer( byte AAddress, word AData ) = 0;
  62
  63                void SPI_write( byte AAddress, word AData )
  64                {
  65                        ChipSelectOutputPin.SendValue( false );
  66                        SPI_transfer( AAddress, AData );
  67                        ChipSelectOutputPin.SendValue( true );
  68                }
  69
  70                void DoReceive( void *_Data )
  71        {
  72                        FValue = constrain( *(float*)_Data, 0, 1 ) * 65536 + 0.5;
  73                        if( ! ClockInputPin.IsConnected() )
  74                                UpdateValue();
  75        }
  76
  77        public:
  78                Maxim_MAX521X( BasicSPI &ASPI ) :
  79                        inherited( ASPI )
  80                {
  81                        InputPin.SetCallback( MAKE_CALLBACK( Maxim_MAX521X::DoReceive ));
  82                }
  83        };
  84//---------------------------------------------------------------------------
  85        class Maxim_MAX5214 : public Mitov::Maxim_MAX521X
  86        {
  87                typedef Mitov::Maxim_MAX521X inherited;
  88
  89        protected:
  90                virtual void SPI_transfer( byte AAddress, word AData )
  91                {
  92                        AData >>= 2;
  93                        AData |= ((word)AAddress) << 14;
  94                        FSPI.transfer16( AData );
  95                }
  96
  97        public:
  98                using inherited::inherited;
  99
 100        };
 101//---------------------------------------------------------------------------
 102        class Maxim_MAX5216 : public Mitov::Maxim_MAX521X
 103        {
 104                typedef Mitov::Maxim_MAX521X inherited;
 105
 106        protected:
 107                virtual void SPI_transfer( byte AAddress, word AData )
 108                {
 109                        byte AHighByte = AData >> 10;
 110                        AHighByte |= AAddress << 6;
 111
 112                        AData <<= 6;
 113
 114                        FSPI.transfer( AHighByte );
 115                        FSPI.transfer16( AData );
 116                }
 117
 118        public:
 119                using inherited::inherited;
 120
 121        };
 122//---------------------------------------------------------------------------
 123}
 124
 125#endif