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////////////////////////////////////////////////////////////////////////////////
910
#ifndef _MITOV_MAXIM_MAX521X_h
11#define _MITOV_MAXIM_MAX521X_h
1213
#include <Mitov.h>
14#include <Mitov_Basic_SPI.h>
1516
namespace Mitov
17{
18//---------------------------------------------------------------------------
19enum Maxim_MAX521X_ShutDownMode { msdmHighImpedance, msdm100K, msdm1K };
20//---------------------------------------------------------------------------
21class Maxim_MAX521X : public Mitov::Basic_SPI, public ClockingSupport
22{
23typedef Mitov::Basic_SPI inherited;
2425
public:
26OpenWire::SinkPin InputPin;
2728
public:
29Maxim_MAX521X_ShutDownMode ShutDownMode = msdmHighImpedance;
3031
public:
32void SetEnabled( bool AValue )
33{
34if( Enabled == AValue )
35return;
3637
Enabled = AValue;
38if( Enabled )
39SPI_write( 2, 0 );
4041
else
42SPI_write( 2, ( ((word)ShutDownMode) + 1 ) << 10 );
4344
}
4546
protected:
47word FValue = 0;
4849
protected:
50void UpdateValue()
51{
52SPI_write( 1, FValue );
53}
5455
virtual void DoClockReceive( void *_Data ) override
56{
57UpdateValue();
58}
5960
protected:
61virtual void SPI_transfer( byte AAddress, word AData ) = 0;
6263
void SPI_write( byte AAddress, word AData )
64{
65ChipSelectOutputPin.SendValue( false );
66SPI_transfer( AAddress, AData );
67ChipSelectOutputPin.SendValue( true );
68}
6970
void DoReceive( void *_Data )
71{
72FValue = constrain( *(float*)_Data, 0, 1 ) * 65536 + 0.5;
73if( ! ClockInputPin.IsConnected() )
74UpdateValue();
75}
7677
public:
78Maxim_MAX521X( BasicSPI &ASPI ) :
79inherited( ASPI )
80{
81InputPin.SetCallback( MAKE_CALLBACK( Maxim_MAX521X::DoReceive ));
82}
83};
84//---------------------------------------------------------------------------
85class Maxim_MAX5214 : public Mitov::Maxim_MAX521X
86{
87typedef Mitov::Maxim_MAX521X inherited;
8889
protected:
90virtual void SPI_transfer( byte AAddress, word AData )
91{
92AData >>= 2;
93AData |= ((word)AAddress) << 14;
94FSPI.transfer16( AData );
95}
9697
public:
98using inherited::inherited;
99100
};
101//---------------------------------------------------------------------------
102class Maxim_MAX5216 : public Mitov::Maxim_MAX521X
103{
104typedef Mitov::Maxim_MAX521X inherited;
105106
protected:
107virtual void SPI_transfer( byte AAddress, word AData )
108{
109byte AHighByte = AData >> 10;
110AHighByte |= AAddress << 6;
111112
AData <<= 6;
113114
FSPI.transfer( AHighByte );
115FSPI.transfer16( AData );
116}
117118
public:
119using inherited::inherited;
120121
};
122//---------------------------------------------------------------------------
123}
124125
#endif