// Mikroelektronika ADAC Click MIKROE-2690 (https://www.mikroe.com/adac-click-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.RemoteIO.ADAC { /// /// Encapsulates the Mikroelektronika ADAC Click Board. /// MIKROE-2690. /// public class Board { private readonly IO.Interfaces.GPIO.Pin myrst; private readonly IO.Devices.AD5593R.Device mydev; /// /// Default I2C slave address. /// public const byte DefaultAddress = 0x10; /// /// Constructor for a single ADAC click. /// /// mikroBUS socket number. /// I2C slave address. /// Remote I/O server device object. public Board(int socknum, int addr = DefaultAddress, IO.Objects.RemoteIO.Device remdev = null) { // Create Remote I/O server device object, if one wasn't supplied if (remdev == null) remdev = new IO.Objects.RemoteIO.Device(); // Create a mikroBUS socket object IO.Objects.RemoteIO.mikroBUS.Socket S = new IO.Objects.RemoteIO.mikroBUS.Socket(socknum); // Configure hardware reset GPIO pin myrst = remdev.GPIO_Create(S.RST, IO.Interfaces.GPIO.Direction.Output, true); // Issue hardware reset Reset(); // Configure I2C bus IO.Interfaces.I2C.Bus bus; if (IO.Objects.RemoteIO.mikroBUS.Shield.I2CBus is null) bus = remdev.I2C_Create(S.I2CBus); else bus = IO.Objects.RemoteIO.mikroBUS.Shield.I2CBus; // Configure AD5593R mydev = new IO.Devices.AD5593R.Device(bus, addr); // The ADAC click is wired for 0-5.0V on both ADC and DAC mydev.ADC_Reference = IO.Devices.AD5593R.ReferenceMode.Internalx2; mydev.DAC_Reference = IO.Devices.AD5593R.ReferenceMode.Internalx2; } /// /// Returns the underlying AD5593R device object. /// public IO.Devices.AD5593R.Device device { get { return mydev; } } /// /// Issue hardware reset to the AD5593R. /// public void Reset() { myrst.state = false; System.Threading.Thread.Sleep(1); myrst.state = true; } /// /// Factory function for creating ADC inputs. /// /// AD5593R I/O channel number (0 to 7). /// ADC input object. public IO.Interfaces.ADC.Sample ADC(int channel) { return mydev.ADC_Create(channel); } /// /// Factory function for creating DAC outputs. /// /// AD5593R I/O channel number (0 to 7). /// Initial DAC output sample. /// DAC output object. public IO.Interfaces.DAC.Sample DAC(int channel, int sample = 0) { return mydev.DAC_Create(channel, sample); } /// /// Factory function for creating GPIO pins. /// /// AD5593R I/O channel number (0 to 7). /// 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); } } }