// C# binding for GPIO pin services in libsimpleio.so // Copyright (C)2017-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. using System.Runtime.InteropServices; namespace IO.Bindings { public static partial class libsimpleio { /// /// Input data direction. /// public const int GPIO_DIRECTION_INPUT = 0; /// /// Out data direction. /// public const int GPIO_DIRECTION_OUTPUT = 1; /// /// Push-pull (source and sink) output driver. /// public const int GPIO_DRIVER_PUSHPULL = 0; /// /// Open drain (sink only) output driver. /// public const int GPIO_DRIVER_OPENDRAIN = 1; /// /// Open source (source only) output driver /// public const int GPIO_DRIVER_OPENSOURCE = 2; /// /// Interrupts are disabled. /// public const int GPIO_EDGE_NONE = 0; /// /// Interrupt on rising edge. /// public const int GPIO_EDGE_RISING = 1; /// /// Interrupt on falling edge. /// public const int GPIO_EDGE_FALLING = 2; /// /// Interrupt on both edges. /// public const int GPIO_EDGE_BOTH = 3; /// /// Active low (inverted) polarity. /// public const int GPIO_POLARITY_ACTIVELOW = 0; /// /// Active high (normal) polarity. /// public const int GPIO_POLARITY_ACTIVEHIGH = 1; // Old GPIO sysfs API: /// /// Configure a Linux GPIO pin. /// /// Pin number. /// Data direction. /// Initial GPIO output state. /// Interrupt edge for input pin. /// Polarity /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_configure(int pin, int direction, int state, int edge, int polarity, out int error); /// /// Open a Linux GPIO pin device. /// /// Pin number. /// File descriptor. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_open(int pin, out int fd, out int error); /// /// Read a Linux GPIO pin. /// /// File descriptor. /// Pin state. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_read(int fd, out int state, out int error); /// /// Write a Linux GPIO pin. /// /// File descriptor. /// Pin state. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_write(int fd, int state, out int error); /// /// Close a Linux GPIO pin device. /// /// File descriptor. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_close(int fd, out int error); // New GPIO descriptor API: /// /// GPIO line is being used by the kernel. /// public const int GPIO_LINE_INFO_KERNEL = 0x0001; /// /// GPIO line is configured as an output. /// public const int GPIO_LINE_INFO_OUTPUT = 0x0002; /// /// GPIO line is configured as active low (inverted). /// public const int GPIO_LINE_INFO_ACTIVE_LOW = 0x0004; /// /// GPIO line is configured as open drain (current sink only). /// public const int GPIO_LINE_INFO_OPEN_DRAIN = 0x0008; /// /// GPIO line is configured as open source (current source only). /// public const int GPIO_LINE_INFO_OPEN_SOURCE = 0x0010; /// /// Select GPIO line direction input. /// public const int GPIO_LINE_REQUEST_INPUT = 0x0001; /// /// Select GPIO line direction output. /// public const int GPIO_LINE_REQUEST_OUTPUT = 0x0002; /// /// Select GPIO line polarity active high (normal). /// public const int GPIO_LINE_REQUEST_ACTIVE_HIGH = 0x0000; /// /// Select GPIO line polarity active low (inverted). /// public const int GPIO_LINE_REQUEST_ACTIVE_LOW = 0x0004; /// /// Select GPIO line driver push-pull (current source and sink). /// public const int GPIO_LINE_REQUEST_PUSH_PULL = 0x0000; /// /// Select GPIO line driver open drain (current sink only). /// public const int GPIO_LINE_REQUEST_OPEN_DRAIN = 0x0008; /// /// Select GPIO line driver open source (current source only). /// public const int GPIO_LINE_REQUEST_OPEN_SOURCE = 0x0010; /// /// Disable GPIO input interrupt. /// public const int GPIO_EVENT_REQUEST_NONE = 0x0000; /// /// Enable GPIO input interrupt on rising edge. /// public const int GPIO_EVENT_REQUEST_RISING = 0x0001; /// /// Enable GPIO input interrupt on falling edge. /// public const int GPIO_EVENT_REQUEST_FALLING = 0x0002; /// /// Enable GPIO input interrupt on both edges. /// public const int GPIO_EVENT_REQUEST_BOTH = 0x0003; /// /// Get GPIO chip information. /// /// GPIO chip number. /// GPIO chip name. /// Maximum size of name. /// GPIO chip label. /// Maximum size of label. /// Number of GPIO lines. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_chip_info(int chip, System.Text.StringBuilder name, int namesize, System.Text.StringBuilder label, int labelsize, out int lines, out int error); /// /// Get GPIO line information. /// /// GPIO chip number. /// GPIO line number. /// GPIO line name. /// Maximum size of name. /// GPIO line label. /// Maximum size of label. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_line_info(int chip, int line, System.Text.StringBuilder name, int namesize, System.Text.StringBuilder label, int labelsize, out int error); /// /// Open a single GPIO line. /// /// GPIO chip number. /// GPIO line number. /// GPIO line configuration flags. /// GPIO line event flags. /// Initial GPIO output state. /// Linux file descriptor. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_line_open(int chip, int line, int flags, int events, int state, out int fd, out int error); /// /// Read the state of a single GPIO line. /// /// Linux file descriptor. /// State of the GPIO line. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_line_read(int fd, out int state, out int error); /// /// Write the state of a single GPIO line. /// /// Linux file descriptor. /// State of the GPIO line. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_line_write(int fd, int state, out int error); /// /// Read an edge trigger event from single GPIO line. /// /// Linux file descriptor. /// State of the GPIO line after the edge trigger /// event. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_line_event(int fd, out int state, out int error); /// /// Close a single GPIO line. /// /// Linux file descriptor. /// Error code. Zero upon success or an errno /// value upon failure. [DllImport("simpleio")] public static extern void GPIO_line_close(int fd, out int error); } }