#include "stdafx.h"
#include "HC595.h"

int IoAHC595::Open( int )
{
    Close( );

    // Synchronize software and hardware registers
    if ( ! Write( m_Register ) ) return FALSE;

    IoADevice::Open( );  // flag device as open

    return TRUE;
}

int IoAHC595::Synchronize( int Value )
{
    Value &= 0xff;  // only use low-order 8-bits

    if ( Value != m_Register )
    {
        if ( IsOpen( ) )
        {
            if ( ! Write( Value ) ) return FALSE;
        }

        m_Register = Value;
    }

    return TRUE;
}

int IoAHC595::Write( int Value ) const
{
    int Bits[ 8 ];

    // Write 8 bits of channel index
    for ( int i = 0; i < 8; i++ )  // write MSB -> LSB
    {
        Bits[ i ] = ( Value & ( 0x1 << ( 7 - i ) ) ) != 0;
    }

//    if ( ! WriteBSP( 0, Bits, 8, 0 ) ) return FALSE;
    if ( ! WriteBSP( 1, Bits, 8, 0 ) ) return FALSE;

    return TRUE;
}
