001package com.pi4j.wiringpi;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SoftPwm.java  
009 * 
010 * This file is part of the Pi4J project. More information about 
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2013 Pi4J
015 * %%
016 * Licensed under the Apache License, Version 2.0 (the "License");
017 * you may not use this file except in compliance with the License.
018 * You may obtain a copy of the License at
019 * 
020 *      http://www.apache.org/licenses/LICENSE-2.0
021 * 
022 * Unless required by applicable law or agreed to in writing, software
023 * distributed under the License is distributed on an "AS IS" BASIS,
024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
025 * See the License for the specific language governing permissions and
026 * limitations under the License.
027 * #L%
028 */
029
030
031import com.pi4j.util.NativeLibraryLoader;
032
033/**
034 * <p>
035 * WiringPi includes a software-driven PWM handler capable of outputting a PWM signal on any of the
036 * Raspberry Pi's GPIO pins.
037 * </p>
038 * 
039 * <p>
040 * There are some limitations. To maintain a low CPU usage, the minimum pulse width is 100uS. That
041 * combined with the default suggested range of 100 gives a PWM frequency of 100Hz. You can lower
042 * the range to get a higher frequency, at the expense of resolution, or increase to get more
043 * resolution, but that will lower the frequency. If you change the pulse-width in the drive code,
044 * then be aware that at delays of less than 100uS wiringPi does it in a software loop, which means
045 * that CPU usage will rise dramatically, and controlling more than one pin will be almost
046 * impossible.
047 * </p>
048 * 
049 * <p>
050 * Also note that while the routines run themselves at a higher and real-time priority, Linux can
051 * still affect the accuracy of the generated signal.
052 * </p>
053 * 
054 * <p>
055 * However, within these limitations, control of a light/LED or a motor is very achievable.
056 * </p>
057 * 
058 * <p>
059 * <b> You must initialize wiringPi with one of wiringPiSetup() or wiringPiSetupGpio() functions
060 * beforehand. wiringPiSetupSys() is not fast enough, so you must run your programs with sudo. </b>
061 * </p>
062 * 
063 * <p>
064 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
065 * the following system libraries:
066 * <ul>
067 * <li>pi4j</li>
068 * <li>wiringPi</li>
069 * <li>pthread</li>
070 * </ul>
071 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
072 * Gordon Henderson @ <a href="https://projects.drogon.net/">https://projects.drogon.net/</a>)
073 * </blockquote>
074 * </p>
075 * 
076 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
077 * @see <a
078 *      href="https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/">https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/</a>
079 * @author Robert Savage (<a
080 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
081 */
082public class SoftPwm {
083
084    // private constructor 
085    private SoftPwm() {
086        // forbid object construction 
087    }
088    
089    static {
090        // Load the platform library
091        NativeLibraryLoader.load("pi4j", "libpi4j.so");
092    }
093
094    /**
095     * <p>int softPwmCreate (int pin, int initialValue, int pwmRange);</p>
096     * 
097     * <p>
098     * This creates a software controlled PWM pin. You can use any GPIO pin and the pin numbering
099     * will be that of the wiringPiSetup function you used. Use 100 for the pwmRange, then the value
100     * can be anything from 0 (off) to 100 (fully on) for the given pin.
101     * </p>
102     * 
103     * @see <a
104     *      href="https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/">https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/</a>
105     * 
106     * @param pin The GPIO pin to use as a PWM pin.
107     *            </p>
108     * @param value The value to initialize the PWM pin (between 0 <i>(off)</i> to 100 <i>(fully
109     *            on)</i>)
110     * @param range The maximum range. Use 100 for the pwmRange.
111     * @return The return value is 0 for success. Anything else and you should check the global
112     *         errno variable to see what went wrong.
113     */
114    public static native int softPwmCreate(int pin, int value, int range);
115
116    /**
117     * <p>void softPwmWrite (int pin, int value);</p>
118     * 
119     * <p>
120     * This updates the PWM value on the given pin. The value is checked to be in-range and pins
121     * that haven't previously been initialized via softPwmCreate will be silently ignored.
122     * </p>
123     * 
124     * @see <a
125     *      href="https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/">https://projects.drogon.net/raspberry-pi/wiringpi/software-pwm-library/</a>
126     * @param pin The GPIO pin to use as a PWM pin.
127     * @param value The value to initialize the PWM pin (between 0 <i>(off)</i> to 100 <i>(fully
128     *            on)</i>)
129     */
130    public static native void softPwmWrite(int pin, int value);
131}