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_POTENTIOMETER_I2C_h
11#define _MITOV_MAXIM_POTENTIOMETER_I2C_h
1213
#include <Mitov.h>
14#include <Mitov_Basic_I2C.h>
1516
namespace Mitov
17{
18//---------------------------------------------------------------------------
19class PotentiometerSPIChannel;
20//---------------------------------------------------------------------------
21class Maxim_Potentiometer_I2C : public Mitov::Basic_MultiChannel_SourceI2C
22{
23typedef Mitov::Basic_MultiChannel_SourceI2C inherited;
2425
public:
26byte Address = 0;
2728
protected:
29void TransmitAt( byte ARegister, byte Adata )
30{
31byte AAddress = 0b0101000 | (( Address & 0b111 ) << 1 );
32Wire.beginTransmission( AAddress );
3334
Wire.write( ARegister );
35Wire.write( Adata );
3637
Wire.endTransmission();
38}
3940
protected:
41virtual void UpdateOutput()
42{
43float ANewValue = FChannels[ 0 ]->FNewValue;
4445
TransmitAt( 0b00010001, (byte)ANewValue );
4647
FChannels[ 0 ]->FValue = ANewValue;
48}
4950
protected:
51virtual void SystemStart() override
52{
53inherited::SystemStart();
54UpdateOutput();
55}
5657
virtual void SystemLoopUpdateHardware() override
58{
59if( FModified )
60if( ! ClockInputPin.IsConnected() )
61UpdateOutput();
6263
inherited::SystemLoopUpdateHardware();
64}
6566
protected:
67void DoClockReceive( void * )
68{
69if( FModified )
70UpdateOutput();
7172
}
7374
};
75//---------------------------------------------------------------------------
76class Maxim_Potentiometer_I2C2Channel : public Mitov::Maxim_Potentiometer_I2C
77{
78typedef Mitov::Maxim_Potentiometer_I2C2Channel inherited;
7980
protected:
81virtual void UpdateOutput()
82{
83float ANewValue = FChannels[ 0 ]->FNewValue;
84if( ANewValue == FChannels[ 1 ]->FNewValue )
85// Update both channels at once
86TransmitAt( 0b00010011, (byte)ANewValue );
8788
else
89{
90if( ANewValue != FChannels[ 0 ]->FValue )
91TransmitAt( 0b00010001, (byte)ANewValue );
9293
if( FChannels[ 1 ]->FNewValue != FChannels[ 1 ]->FValue )
94TransmitAt( 0b00010010, (byte)FChannels[ 1 ]->FNewValue );
95}
9697
FChannels[ 0 ]->FValue = ANewValue;
98FChannels[ 1 ]->FValue = FChannels[ 1 ]->FNewValue;
99}
100101
};
102//---------------------------------------------------------------------------
103template<int T_MULTIPLIER> class Maxim_Potentiometer_I2C_Channel : public Mitov::Basic_Typed_I2CChannel<Maxim_Potentiometer_I2C>
104{
105typedef Mitov::Basic_Typed_I2CChannel<Maxim_Potentiometer_I2C> inherited;
106107
protected:
108virtual void DoReceive( void *_Data ) override
109{
110FNewValue = constrain( *((float *)_Data), 0, 1 ) * T_MULTIPLIER;
111if( FNewValue == FValue )
112return;
113114
FOwner.FModified = true;
115}
116117
public:
118using inherited::inherited;
119120
};
121//---------------------------------------------------------------------------
122}
123124
#endif