// PWM output services using IO.Objects.SimpleIO // 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; namespace IO.Objects.SimpleIO.DAC { /// /// Encapsulates Linux Industrial I/O Subsystem DAC outputs using /// libsimpleio. /// public class Sample: IO.Interfaces.DAC.Sample { private readonly int myfd; private readonly int nbits; /// /// Retrieve the subsystem name string for a Linux Industrial /// I/O Subsystem DAC device. /// /// DAC chip number. /// Subsystem name. public static string name(int chip) { System.Text.StringBuilder name = new System.Text.StringBuilder(256); if (chip < 0) { throw new Exception("Invalid chip number"); } IO.Bindings.libsimpleio.DAC_get_name(chip, name, name.Capacity, out int error); if (error != 0) { throw new Exception("DAC_get_name() failed, " + errno.strerror(error)); } return name.ToString(); } /// /// Constructor for a single DAC output. /// /// DAC output designator. /// Bits of resolution. /// Initial DAC output sample. public Sample(IO.Objects.SimpleIO.Device.Designator desg, int resolution, int sample = 0) { // Validate the DAC output designator if ((desg.chip == IO.Objects.SimpleIO.Device.Designator.Unavailable.chip) || (desg.chan == IO.Objects.SimpleIO.Device.Designator.Unavailable.chan)) { throw new Exception("Invalid designator"); } IO.Bindings.libsimpleio.DAC_open((int)desg.chip, (int)desg.chan, out this.myfd, out int error); if (error != 0) { throw new Exception("DAC_open() failed, " + errno.strerror(error)); } this.nbits = resolution; this.sample = sample; } /// /// Write-only property for writing an integer analog sample to a DAC /// output. /// public int sample { set { IO.Bindings.libsimpleio.DAC_write(this.myfd, value, out int error); if (error != 0) { throw new Exception("DAC_write() failed" + errno.strerror(error)); } } } /// /// Read-only property returning the number of bits of resolution. /// public int resolution { get { return this.nbits; } } /// /// Read-only property returning the Linux file descriptor for the DAC /// output. /// public int fd { get { return this.myfd; } } } }