The PCAL9555, PCAL9554 series is a low-voltage 16-bit/8-bit General Purpose Input/Output (GPIO) expander with interrupt. This conponent library is compatible to basic operation os GPIO expanders: PCAL9555, PCA9555, PCA9535, PCA9539, PCAL9554, PCA9554 and PCA9538. On addition to this, this library is including mbed-SDK-style APIs. APIs that similar to DigitaiInOut, DigitalOut, DigitalIn, BusInOUt, BusOut and BusIn are available.

Dependents:   PCAL9555_Hello OM13082-JoyStick OM13082_LED OM13082-test ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GpioBusInOut.cpp Source File

GpioBusInOut.cpp

00001 #include    "GpioBusInOut.h"
00002 
00003 GpioBusInOut::GpioBusInOut(
00004     CompGpioExp &gpiop,
00005     GpioPinName p0,  GpioPinName p1,  GpioPinName p2,  GpioPinName p3,
00006     GpioPinName p4,  GpioPinName p5,  GpioPinName p6,  GpioPinName p7,
00007     GpioPinName p8,  GpioPinName p9,  GpioPinName p10, GpioPinName p11,
00008     GpioPinName p12, GpioPinName p13, GpioPinName p14, GpioPinName p15  )
00009     : gpio_p( &gpiop )
00010 {
00011     GpioPinName pins[16] = { p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 };
00012     init( gpiop, pins );
00013 }
00014 
00015 GpioBusInOut::GpioBusInOut( CompGpioExp &gpiop, GpioPinName pins[16] )
00016 {
00017     init( gpiop, pins );
00018 }
00019 
00020 void GpioBusInOut::init( CompGpioExp &gpiop, GpioPinName pins[16] )
00021 {
00022     for ( int i = 0; i < 16; i++ )
00023         pin_list[ i ]   = pins[ i ];
00024 
00025     mask_bits    = ~(make_bitpattern( 0xFFFF ));
00026 }
00027 
00028 GpioBusInOut::~GpioBusInOut()
00029 {
00030 }
00031 
00032 void GpioBusInOut::write( int value )
00033 {
00034     int bitpattern;
00035 
00036     bitpattern  = make_bitpattern( value );
00037     gpio_p->write_with_mask( bitpattern, mask_bits );
00038 }
00039 
00040 int GpioBusInOut::make_bitpattern( int value )
00041 {
00042     int bit_pattern = 0;
00043 
00044     for ( int i = 0; i < 16; i++ )
00045         bit_pattern     |= (pin_list[ i ] != X_NC) ? ((value >> i) & 0x1) << pin_list[ i ] : 0x0;
00046 
00047     return ( bit_pattern );
00048 }
00049 
00050 int GpioBusInOut::read( void )
00051 {
00052     int r;
00053     int v   = 0;
00054 
00055     r   = gpio_p->read();
00056 
00057     for ( int i = 0; i < 16; i++ )
00058         v   |= (pin_list[ i ] != X_NC) ? ((r >> pin_list[ i ]) & 0x1) << i : 0x0;
00059     
00060     return v;
00061 }
00062 
00063 void GpioBusInOut::output( void )
00064 {
00065     gpio_p->configure_with_mask( 0x0, mask_bits );
00066 }
00067 
00068 void GpioBusInOut::input( void )
00069 {
00070     gpio_p->configure_with_mask( ~0x0, mask_bits );
00071 }
00072 
00073 GpioBusInOut& GpioBusInOut::operator=( int rhs )
00074 {
00075     write( rhs );
00076     return *this;
00077 }
00078 
00079 GpioBusInOut& GpioBusInOut::operator=( GpioBusInOut& rhs )
00080 {
00081     write(rhs.read());
00082     return *this;
00083 }
00084 
00085 
00086 GpioBusInOut::operator int( void )
00087 {
00088     return( read() );
00089 }