Tedd OKANO / PCA9546A

Dependents:   PCA9546A_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCA9546A.cpp Source File

PCA9546A.cpp

00001 /**
00002  *  PCA9546A library
00003  *
00004  *  @author  Tedd OKANO
00005  *  @version 0.1
00006  *  @date    Feb-2015
00007  *
00008  *  PCA9546A: an I2C bus switch control library
00009  *
00010  *  The PCA9546A is a quad bidirectional translating switch controlled 
00011  *  via the I2C-bus. The SCL/SDA upstream pair fans out to four downstream 
00012  *  pairs, or channels. Any individual SCx/SDx channel or combination of 
00013  *  channels can be selected, determined by the contents of the programmable 
00014  *  control register.
00015  *
00016  *  For more information about PCA9546A:
00017  *    http://www.nxp.com/documents/data_sheet/PCA9546A.pdf
00018  *
00019  */
00020 
00021 #include "PCA9546A.h"
00022 
00023 PCA9546A::PCA9546A( PinName sda, PinName scl, char i2c_address ) 
00024     : i2c_p( new I2C( sda, scl ) ), i2c( *i2c_p ), i2c_addr( i2c_address ), enable_pattern( 0x00 )
00025 {
00026     //  do nothing.
00027     //  leave it in default state.
00028 }
00029 
00030 PCA9546A::PCA9546A( I2C &i2c_, char i2c_address )
00031     : i2c_p( NULL ), i2c( i2c_ ), i2c_addr( i2c_address ), enable_pattern( 0x00 )
00032 {
00033     //  do nothing.
00034     //  leave it in default state.
00035 }
00036 
00037 PCA9546A::~PCA9546A()
00038 {
00039     if ( NULL != i2c_p )
00040         delete  i2c_p;
00041 }
00042 
00043 void PCA9546A::on( char channel )
00044 {
00045     enable_pattern  |= 0x1 << channel;
00046 
00047     bitpattern( enable_pattern );
00048 }
00049 
00050 void PCA9546A::off( char channel )
00051 {
00052     enable_pattern  &= ~(0x1 << channel);
00053 
00054     bitpattern( enable_pattern );
00055 }
00056 
00057 void PCA9546A::bitpattern( char pattern )
00058 {
00059     i2c.write( i2c_addr, &pattern, 1 );
00060 }