Skip to content

Commit

Permalink
Add additional convenience methods
Browse files Browse the repository at this point in the history
As well as some minor documentation mistakes and inconsistencies
  • Loading branch information
HoldYourWaffle committed Aug 15, 2018
1 parent 14b3564 commit b683eb2
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions src/main/java/jimbo/pijava/blinkt/BlinktController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package jimbo.pijava.blinkt;

import java.awt.Color;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
Expand All @@ -40,7 +42,12 @@ public class BlinktController {
/** The data for each LED in the chain */
private final int[] data;

/** Brightness (0 - 31) field used to set colors */
/**
* Brightness (0 - 31) field used to set colors<br>
* <br>
* <b>Note that most methods ({@link #set(int, int, int, int, float) set}, {@link #setBrightness(float) setBrightness}, ...) accept a float value of 0-1,
* {@link #getBrightness() getBrightness} converts between these formats</b>
*/
private int brightness;

/**
Expand Down Expand Up @@ -89,24 +96,28 @@ public void push() {
latch();
}


/** Both {@link #clear()} and {@link #setBrightness(float)} to 0 */
public void reset() {
clear();
setBrightness(1);
}


/** Clear the data for pixel n */
public void clear(int n) {
data[n] = 0;
}


/** Clear the data of all pixels */
public void clear() {
for (int i = 0; i < 8; ++i)
data[i] = 0;
}



/**
* Set the brightness field used for {@link #set(int, int, int, int)}
*
Expand All @@ -118,16 +129,17 @@ public void setBrightness(float brightness) {
}


/** Get {@link #brightness} */
/** Get {@link #brightness} as a float in the range of 0-1 */
public float getBrightness() {
return brightness / 31F;
}



/**
* Set an LED to a specific red, green, blue and brightness value
*
* @param n The LED number (0-7)
* @param n The LED index (0-7)
* @param red The red value (0-255)
* @param green The green value (0-255)
* @param blue The blue value (0-255)
Expand All @@ -136,7 +148,7 @@ public float getBrightness() {
* @see #push()
*/
public void set(int n, int red, int green, int blue, float brightness) {
if (n < 0 || n >= 8) throw new IllegalArgumentException("n must be larget than 0 and smaller than 8");
if (n < 0 || n >= 8) throw new IllegalArgumentException("n must be larger than 0 and smaller than 8");
if (red < 0 || red > 255) throw new IllegalArgumentException("red must be between 0 and 255");
if (green < 0 || green > 255) throw new IllegalArgumentException("green must be between 0 and 255");
if (blue < 0 || blue > 255) throw new IllegalArgumentException("blue must be between 0 and 255");
Expand All @@ -145,9 +157,9 @@ public void set(int n, int red, int green, int blue, float brightness) {
}

/**
* Set a LED to a specific red, green and blue value using a brightness of {@link #brightness}
* Set an LED to a specific red, green and blue value using the set {@link #setBrightness(float) default brightness}
*
* @param n The LED number (0-7)
* @param n The LED index (0-7)
* @param red The red value (0-255)
* @param green The green value (0-255)
* @param blue The blue value (0-255)
Expand All @@ -156,15 +168,40 @@ public void set(int n, int red, int green, int blue, float brightness) {
* @see #push()
*/
public void set(int n, int red, int green, int blue) {
if (n < 0 || n >= 8) throw new IllegalArgumentException("n must be larget than 0 and smaller than 8");
if (red < 0 || red > 255) throw new IllegalArgumentException("red must be between 0 and 255");
if (green < 0 || green > 255) throw new IllegalArgumentException("green must be between 0 and 255");
if (blue < 0 || blue > 255) throw new IllegalArgumentException("blue must be between 0 and 255");

data[n] = (brightness << 24) | (red << 16) | (green << 8) | blue;
set(n, red, green, blue, getBrightness());
}

/**
* Set an LED to the specified color
*
* @param n The LED index (0-7)
* @param col The color
* @param brightness The brightness (0-1)
*
* @see #push()
* @since 1.1
*/
public void set(int n, Color col, float brightness) {
set(n, col.getRed(), col.getGreen(), col.getBlue(), brightness);
}

/**
* Set an LED to the specified color using the set {@link #setBrightness(float) default brightness}
*
* @param n The LED index (0-7)
* @param col The color
*
* @see #setBrightness(float)
* @see #push()
* @since 1.1
*/
public void set(int n, Color col) {
set(n, col, getBrightness());
}



//================ INTERNAL STUFF ================//

/** Write out a single byte. It goes out MSB first */
private void write_byte(byte out) {
Expand Down

0 comments on commit b683eb2

Please sign in to comment.