// 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; using IO.Interfaces.Message64; namespace IO.Objects.RemoteIO { public partial class Device { /// /// Query available A/D inputs. /// /// List of available A/D input numbers. public System.Collections.Generic.List ADC_Available() { return Available(PeripheralTypes.ADC); } /// /// Create a remote A/D input. /// /// A/D input number: 0 to 127. /// A/D input object. public IO.Interfaces.ADC.Sample ADC_Create(int num) { return new ADC(this, num); } } /// /// Encapsulates remote A/D inputs. /// public class ADC: IO.Interfaces.ADC.Sample { private readonly Device device; private readonly int num; private readonly int nbits; /// /// Create a remote A/D input. /// /// Remote I/O device object. /// A/D input number: 0 to 127. /// Use Device.ADC_Create() instead of this constructor. public ADC(Device dev, int num) { this.device = dev; this.num = (byte)num; // Validate parameters if ((num < 0) || (num >= Device.MAX_CHANNELS)) throw new Exception("Invalid A/D input number"); // Dispatch command message Message cmd = new Message(0); Message resp = new Message(); cmd.payload[0] = (byte)MessageTypes.ADC_CONFIGURE_REQUEST; cmd.payload[2] = (byte)num; device.Dispatcher(cmd, resp); // Save resolution from the response message this.nbits = resp.payload[3]; } /// /// Read-only property returning an integer analog input sample. /// public int sample { get { Message cmd = new Message(0); Message resp = new Message(); cmd.payload[0] = (byte)MessageTypes.ADC_READ_REQUEST; cmd.payload[2] = (byte)this.num; this.device.Dispatcher(cmd, resp); return unchecked((int)( ((uint)(resp.payload[3]) << 24) + ((uint)(resp.payload[4]) << 16) + ((uint)(resp.payload[5]) << 8) + (uint)resp.payload[6])); } } /// /// Read-only property returning the number of bits of resolution. /// public int resolution { get { return this.nbits; } } } }