Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PCAL955x.cpp
00001 #include "mbed.h" 00002 #include "PCAL955x.h" 00003 00004 PCAL955x::PCAL955x( PinName i2c_sda, PinName i2c_scl, char i2c_address ) 00005 : i2c_p( new I2C( i2c_sda, i2c_scl ) ), 00006 i2c( *i2c_p ), 00007 address( i2c_address ), 00008 pin_state( ~0x0 ), 00009 pin_direction( ~0x0 ) 00010 { 00011 } 00012 00013 PCAL955x::PCAL955x( I2C &i2c_, char i2c_address ) 00014 : i2c_p( NULL ), 00015 i2c( i2c_ ), 00016 address( i2c_address ), 00017 pin_state( ~0x0 ), 00018 pin_direction( ~0x0 ) 00019 { 00020 } 00021 00022 PCAL955x::~PCAL955x() 00023 { 00024 if ( NULL != i2c_p ) 00025 delete i2c_p; 00026 } 00027 00028 void PCAL955x::write( int bit_pattern ) 00029 { 00030 pin_state = bit_pattern; 00031 reg_index_write( OUTPUT, pin_state ); 00032 } 00033 00034 int PCAL955x::read( void ) 00035 { 00036 return ( reg_index_read( INPUT ) ); 00037 } 00038 00039 void PCAL955x::polarity( int bit_pattern ) 00040 { 00041 reg_index_write( POLARITY, pin_direction ); 00042 } 00043 00044 void PCAL955x::configure( int bit_pattern ) 00045 { 00046 pin_direction = bit_pattern; 00047 reg_index_write( CONFIG, pin_direction ); 00048 } 00049 00050 void PCAL955x::write_with_mask( int bitpattern, int mask_bits ) 00051 { 00052 pin_state &= (bitpattern | mask_bits); 00053 pin_state |= (bitpattern & ~mask_bits); 00054 00055 write( pin_state ); 00056 } 00057 00058 void PCAL955x::configure_with_mask( int bitpattern, int mask_bits ) 00059 { 00060 pin_direction &= (bitpattern | mask_bits); 00061 pin_direction |= (bitpattern & ~mask_bits); 00062 00063 configure( pin_direction ); 00064 } 00065 00066 void PCAL955x::interrupt_mask( int bit_pattern ) 00067 { 00068 int new_config; 00069 00070 new_config = (~bit_pattern) | reg_index_read( CONFIG ); 00071 reg_index_write( CONFIG, new_config ); 00072 reg_index_write( INT_MASK, bit_pattern ); 00073 } 00074 00075 int PCAL955x::interrupt_status( void ) 00076 { 00077 return ( reg_index_read( INT_STAT ) ); 00078 } 00079 00080 00081 void PCAL955x::bus_write( char *dp, int length ) 00082 { 00083 #if 0 00084 printf( "bus_write: 0x%02X - ", address ); 00085 for ( int i = 0; i < length; i++ ) 00086 printf( " 0x%02X", *(dp + i) ); 00087 printf( "\r\n" ); 00088 #endif 00089 00090 i2c.write( address, dp, length ); 00091 } 00092 00093 void PCAL955x::bus_read( char reg_addr, char *dp, int length ) 00094 { 00095 i2c.write( address, (char *)(®_addr), 1, true ); 00096 i2c.read( address, dp, length ); 00097 } 00098 00099 void PCAL955x::write( int pin, int v ) 00100 { 00101 if ( pin < number_of_pins() ) { 00102 if ( v ) 00103 pin_state |= 0x01 << pin; 00104 else 00105 pin_state &= ~(0x01 << pin); 00106 } else { 00107 pin_state = v ? ~0x0 : 0x0; 00108 } 00109 00110 write( pin_state ); 00111 } 00112 00113 int PCAL955x::read( int pin ) 00114 { 00115 return ( (reg_index_read( INPUT ) >> pin) & 0x1 ); 00116 } 00117 00118 void PCAL955x::configure( int pin, int v ) 00119 { 00120 if ( pin < number_of_pins() ) { 00121 if ( v ) 00122 pin_direction |= 0x01 << pin; 00123 else 00124 pin_direction &= ~(0x01 << pin); 00125 } else { 00126 pin_direction = v ? ~0x0 : 0x0; 00127 } 00128 00129 configure( pin_direction ); 00130 } 00131 00132 PCAL955x::operator int( void ) 00133 { 00134 return( read() ); 00135 }
Generated on Tue Jul 12 2022 14:15:36 by
