// Mikroelektronika PWM Click MIKROE-1898 (https://www.mikroe.com/pwm-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.PWM { /// /// Encapsulates the Mikroelektronika PWM Click Board. /// MIKROE-1898. /// public class Board { private readonly IO.Devices.PCA9685.Device mydev; /// /// Default I2C slave address. /// public const byte DefaultAddress = 0x40; /// /// Constructor for a single PWM click. /// /// mikroBUS socket number. /// PWM pulse frequency in Hz. /// I2C slave address. /// Remote I/O device object. public Board(int socknum, int freq, 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); 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; mydev = new IO.Devices.PCA9685.Device(bus, addr, freq); } /// /// Returns the underlying PCA9685 device object. /// public IO.Devices.PCA9685.Device dev { get { return mydev; } } /// /// Factory function for creating GPIO output pins. /// /// PCA9685 output channel number. /// Initial GPIO output state. /// GPIO output pin object. public IO.Interfaces.GPIO.Pin GPIO(int channel, bool state = false) { return new IO.Devices.PCA9685.GPIO.Pin(mydev, channel, state); } /// /// Factory function for creating PWM outputs. /// /// PCA9685 output channel number. /// Initial PWM output duty cycle. /// PWM output object. public IO.Interfaces.PWM.Output PWM(int channel, double dutycycle = IO.Interfaces.PWM.DutyCycles.Minimum) { return new IO.Devices.PCA9685.PWM.Output(mydev, channel, dutycycle); } /// /// Factory function for creating servo outputs. /// /// PCA9685 output channel number. /// Initial servo position.> /// Servo output object. public IO.Interfaces.Servo.Output Servo(int channel, double position = IO.Interfaces.Servo.Positions.Neutral) { return new IO.Devices.PCA9685.Servo.Output(mydev, channel, position); } } }