// SN74HC595 8-Bit Shift Register GPIO Pin Services
// Copyright (C)2020-2023, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
namespace IO.Devices.SN74HC595.GPIO
{
///
/// Encapsulates SN74HC595 GPIO outputs.
///
public class Pin : IO.Interfaces.GPIO.Pin
{
private readonly IO.Devices.SN74HC595.Device mydev;
private readonly int myindex;
private readonly byte mymask;
///
/// Constructor for a single GPIO output pin.
///
/// SN74HC595 device object.
/// Bit position, numbered left to right.
/// Zero indicates the most significant bit of the first shift
/// register stage.
/// Initial GPIO output state.
public Pin(IO.Devices.SN74HC595.Device dev, int pos, bool state = false)
{
// Validate parameters
if (pos < 0)
throw new System.Exception("Invalid bit index");
if (pos / 8 + 1 > dev.Length)
throw new System.Exception("Invalid bit index");
// Calculate byte index and bit mask
mydev = dev;
myindex = pos / 8;
mymask = (byte) (1 << (7 - pos % 8));
// Write initial state
this.state = state;
}
///
/// Read/Write GPIO pin state property.
///
public bool state
{
get
{
return mydev.ReadBit(myindex, mymask);
}
set
{
if (value)
mydev.SetBit(myindex, mymask);
else
mydev.ClrBit(myindex, mymask);
}
}
}
}