java 为RoboRIO提供Adafruit 16通道PWM驱动器(PCA9685)的干净接口。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 为RoboRIO提供Adafruit 16通道PWM驱动器(PCA9685)的干净接口。相关的知识,希望对你有一定的参考价值。

import edu.wpi.first.wpilibj.I2C;

/**
 * Lights class to provide clean interface to the Adafruit 16 channel PWM driver(PCA9685) for the RoboRIO.
 * @author Techplex (Blake Bourque)
 */
public class Lights {
    I2C LightController = new I2C(I2C.Port.kOnboard, 0x40);

    private final byte MAX_CHAN = 14;

    /**
     * Set the off time of the specified channel.
     * @param chan the pwm channel to change
     * @param off the off count to set 0-4096
     */
    public void setRaw(byte chan, short off)
    {
        chan = constrain((byte)0, chan, MAX_CHAN);
        off  = constrain((short)0, off, (short)4096);

        short on = 0; //On time will always be 0.

        final byte LED0_ON_L = 0x6;
        byte data[] = {
                (byte)(LED0_ON_L +4*chan),
                (byte)(on),
                (byte)(on>>8),
                (byte)(off),
                (byte)(off>>8),
        };
        LightController.writeBulk(data);
    }

    /**
     * Set the duty cycle of the specified channel
     * @param chan the pwm channel to change
     * @param duty the duty cycle to set 0-100
     */
    public void setDuty(byte chan, byte duty) {
        chan = constrain((byte)0, chan, MAX_CHAN);
        duty = constrain((byte)0, duty, (byte)100);
        short off = map(duty, (short)0, (short)100, (short)0, (short)4096);

        setRaw(chan, off);
    }

    /**
     * Set the brightness of the specified channel
     * @param chan the pwm channel to change
     * @param duty the duty cycle to set 0-255
     */
    public void setBrightness(byte chan, byte duty) {
        chan = constrain((byte)0, chan, MAX_CHAN);
        duty = constrain((byte)0, duty, (byte)100);
        short off = map(duty, (short)0, (short)255, (short)0, (short)4096);

        setRaw(chan, off);
    }

    /**
     * Set the color of one of the 5 strands
     * @param strand Which strand 1-5
     * @param red The amount of red 0-255
     * @param green The amount of green 0-255
     * @param blue The amount of blue 0-255
     */
    public void setStrand(byte strand, byte red, byte green, byte blue) {
        strand = constrain((byte)1, strand, (byte)5);

        //this order might need to change if red != 0, green != 1 and blue != 2
        setBrightness((byte)(3*strand-3), red);
        setBrightness((byte)(3*strand-2), green);
        setBrightness((byte)(3*strand-1), blue);

    }

    /**
     * constrain a number between an upper and lower bound
     * @param  min   lower bound
     * @param  value value to be constrained
     * @param  max   upper bound
     * @return       the constrained value
     */
    public byte constrain(byte min, byte value, byte max) {
        if (value > max)
            return max;
        else if (value < min)
            return min;
        else
            return value;
    }

    /**
     * constrain a number between an upper and lower bound
     * @param  min   lower bound
     * @param  value value to be constrained
     * @param  max   upper bound
     * @return       the constrained value
     */
    public short constrain(short min, short value, short max) {
        if (value > max)
            return max;
        else if (value < min)
            return min;
        else
            return value;
    }


    /**
     * map a number from one range to another
     * @param  value   the value to be mapped
     * @param  old_min the minimum of value
     * @param  old_max the maximum of value
     * @param  new_min the new minimum value
     * @param  new_max the new maximum value
     * @return         the value remaped on the range [new_min new_max]
     */
    public short map(short value, short old_min, short old_max, short new_min, short new_max) {
        return (short)((value - old_min) / (old_max - old_min) * (new_max - new_min) + new_min);
    }
}

以上是关于java 为RoboRIO提供Adafruit 16通道PWM驱动器(PCA9685)的干净接口。的主要内容,如果未能解决你的问题,请参考以下文章

为啥此代码不适用于 mcp3008? (来自 adafruit 官方网站)

ATtiny85 + Adafruit_NeoPixel 不适用于数组

输入在 pygame 和 adafruit-touchscreen 中移动

Adafruit 的“adafruit_servokit”库在设置伺服角度时返回错误

ESP8266使用Adafruit_SH1106库来驱动1.3“OLED屏幕

Arduino Adafruit NeoMatrix 库