Created
March 27, 2016 15:30
-
-
Save TechplexEngineer/93604cac099b7c84ae79 to your computer and use it in GitHub Desktop.
A class to provide a clean interface to the Adafruit 16 channel PWM driver(PCA9685) for the RoboRIO.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment