libraries / Mitov / Mitov_Basic_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_BASIC_I2C_h
  11#define _MITOV_BASIC_I2C_h
  12
  13#include <Mitov.h>
  14#include <Wire.h>
  15
  16namespace Mitov
  17{
  18//      class I2C : public OpenWire::Component
  19        namespace I2C
  20        {
  21                const uint16_t  I2C_DEFAULT_READ_TIMEOUT        = 1000;
  22//      public:
  23                bool ReadBytes( uint8_t devAddr, uint8_t regAddr, uint8_t length, void *data, uint16_t timeout = I2C_DEFAULT_READ_TIMEOUT )
  24                {
  25                        int8_t count = 0;
  26                        uint32_t t1 = millis();
  27
  28            // Arduino v1.0.1+, Wire library
  29            // Adds official support for repeated start condition, yay!
  30
  31            // I2C/TWI subsystem uses internal buffer that breaks with large data requests
  32            // so if user requests more than BUFFER_LENGTH bytes, we have to do it in
  33            // smaller chunks instead of all at once
  34            for (uint8_t k = 0; k < length; k += min(length, BUFFER_LENGTH)) 
  35                        {
  36                Wire.beginTransmission(devAddr);
  37                Wire.write(regAddr);
  38                Wire.endTransmission();
  39                Wire.beginTransmission(devAddr);
  40                Wire.requestFrom(devAddr, (uint8_t)min(length - k, BUFFER_LENGTH));
  41        
  42                for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); count++)
  43                    ((uint8_t *)data )[count] = Wire.read();
  44        
  45                Wire.endTransmission();
  46            }
  47
  48                        return ( count == length );
  49                }
  50
  51                void WriteByte( uint8_t devAddr, uint8_t regAddr, uint8_t AValue )
  52                {
  53//                      Serial.print( "Address: " ); Serial.print( devAddr, HEX ); Serial.print( " Reg: " ); Serial.print( regAddr, HEX );  Serial.print( " = " ); Serial.println( AValue, BIN );
  54                        Wire.beginTransmission( devAddr );
  55                        Wire.write( regAddr );
  56                        Wire.write( AValue );
  57                        Wire.endTransmission();
  58                }
  59        };
  60//---------------------------------------------------------------------------
  61        class Basic_I2CChannel;
  62//---------------------------------------------------------------------------
  63        class Basic_MultiChannel_SourceI2C : public Mitov::EnabledComponent, public ClockingSupport
  64        {
  65                typedef Mitov::EnabledComponent inherited;
  66
  67        public:
  68                bool    FModified = false;
  69
  70        public:
  71                Mitov::SimpleList<Basic_I2CChannel *>   FChannels;
  72
  73//      protected:
  74//              virtual void SystemInit();
  75
  76//      protected:
  77//              virtual void DoClockReceive( void * );
  78
  79        };
  80//---------------------------------------------------------------------------
  81        class Basic_I2CChannel : public Mitov::CommonSink
  82        {
  83                typedef Mitov::CommonSink       inherited;
  84
  85        public:
  86                float   FValue = 0.0f;
  87                float   FNewValue = 0.0f;
  88
  89//      public:
  90//              virtual void InitChannel() {}
  91//              virtual void SendOutput() = 0;
  92
  93        };
  94//---------------------------------------------------------------------------
  95        template<typename T_OWNER> class Basic_Typed_I2CChannel : public Mitov::Basic_I2CChannel
  96        {
  97                typedef Mitov::Basic_I2CChannel inherited;
  98
  99        public:
 100                float   InitialValue = 0.0f;
 101
 102        protected:
 103                int             FIndex;
 104
 105        protected:
 106                T_OWNER &FOwner;
 107
 108/*
 109        protected:
 110                virtual void DoReceive( void *_Data )
 111                {
 112                        FNewValue = constrain( *((float *)_Data), 0, 1 );
 113                        if( FNewValue == FValue )
 114                                return;
 115
 116                        FOwner.FModified = true;
 117
 118                        if( FOwner.ClockInputPin.IsConnected() )
 119                                FOwner.FModified = true;
 120
 121                        else
 122                                SendOutput();
 123
 124                }
 125*/
 126
 127        public:
 128                Basic_Typed_I2CChannel( T_OWNER &AOwner, int AIndex ) :
 129                        FOwner( AOwner ),
 130                        FIndex( AIndex )
 131                {
 132                        AOwner.FChannels.push_back( this );
 133                }
 134
 135        };
 136//---------------------------------------------------------------------------
 137/*
 138        void Basic_MultiChannel_SourceI2C::DoClockReceive( void * )
 139        {
 140                if( ! FModified )
 141                        return;
 142
 143                for( int i =0; i < FChannels.size(); ++i )
 144                        FChannels[ i ]->SendOutput();
 145        }
 146*/
 147//---------------------------------------------------------------------------
 148/*
 149        void Basic_MultiChannel_SourceI2C::SystemInit()
 150        {
 151                inherited::SystemInit();
 152
 153                for( int i =0; i < FChannels.size(); ++i )
 154                        FChannels[ i ]->InitChannel();
 155        }
 156*/
 157//---------------------------------------------------------------------------
 158}
 159#endif