// Mikroelektronika Expand2 Click MIKROE-1838 (https://www.mikroe.com/expand-2-click) 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.ClickBoards.SimpleIO.Expand2 { /// /// Encapsulates the Mikroelektronika Expand 2 Click Board. /// public class Board { private readonly IO.Interfaces.GPIO.Pin myrst; private readonly IO.Devices.MCP23017.Device mydev; /// /// Default I2C slave address. /// public const int DefaultAddress = 0x20; /// /// Constructor for a single Expand 2 click. /// /// mikroBUS socket number. /// I2C slave address. public Board(int socknum, int addr = DefaultAddress) { // Create a mikroBUS socket object IO.Objects.SimpleIO.mikroBUS.Socket S = new IO.Objects.SimpleIO.mikroBUS.Socket(socknum); // Configure hardware reset GPIO pin myrst = new IO.Objects.SimpleIO.GPIO.Pin(S.RST, IO.Interfaces.GPIO.Direction.Output, true); // Issue hardware reset Reset(); // Configure I2C bus IO.Interfaces.I2C.Bus bus; bus = new IO.Objects.SimpleIO.I2C.Bus(S.I2CBus); // Configure the MCP23017 mydev = new IO.Devices.MCP23017.Device(bus, addr); } /// /// Returns the underlying MCP23017 device object. /// public IO.Devices.MCP23017.Device device { get { return mydev; } } /// /// Issue hardware reset to the MCP23017. /// public void Reset() { myrst.state = false; System.Threading.Thread.Sleep(1); myrst.state = true; } /// /// Factory function for creating GPIO pins. /// /// MCP23017 channel number (0 to 15). /// GPIO pin direction. /// Initial GPIO output state. /// GPIO pin object. public IO.Interfaces.GPIO.Pin GPIO(int channel, IO.Interfaces.GPIO.Direction dir, bool state = false) { return mydev.GPIO_Create(channel, dir, state); } } }