Skip to content

Commit

Permalink
#25 Allow channel to be specified in ws281x library
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjlewis committed Oct 29, 2017
1 parent 1e862fa commit 65cc1d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
27 changes: 23 additions & 4 deletions diozero-ws281x-java/src/main/java/com/diozero/ws281xj/WS281x.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@
* <a href="https://github.com/jgarff/rpi_ws281x">rpi_ws281x C library</a>.</p>
* <p>Also see <a href="https://github.com/626Pilot/RaspberryPi-NeoPixel-WS2812">this implementation</a>.</p>
* <p>All colours are represented as 24bit RGB values.</p>
* <p>Valid GPIO numbers:</p>
* <ul>
* <li>12 / 18 (PWM)</li>
* <li>21 / 31 (PCM)</li>
* <li>10 (SPI)</li>
* </ul>
*/
public class WS281x implements Closeable {
private static final int SIZE_OF_INT = 4;
private static final int DEFAULT_FREQUENCY = 800_000; // Or 400_000
// TODO Find out what options there are here... What do pigpio & wiringPi
// use?
// TODO Find out what options there are here... What do pigpio & wiringPi use?
private static final int DEFAULT_DMA_NUM = 5;
private static final int DEFAULT_CHANNEL = 0;

private static final String LIB_NAME = "ws281xj";
private static Boolean loaded = Boolean.FALSE;
Expand Down Expand Up @@ -90,7 +96,7 @@ private static void init() {
* @param numPixels The number of pixels connected.
*/
public WS281x(int gpioNum, int brightness, int numPixels) {
this(DEFAULT_FREQUENCY, DEFAULT_DMA_NUM, gpioNum, brightness, numPixels, StripType.WS2812_STRIP);
this(DEFAULT_FREQUENCY, DEFAULT_DMA_NUM, gpioNum, brightness, numPixels, StripType.WS2812_STRIP, DEFAULT_CHANNEL);
}

/**
Expand All @@ -102,11 +108,24 @@ public WS281x(int gpioNum, int brightness, int numPixels) {
* @param stripType Strip type
*/
public WS281x(int frequency, int dmaNum, int gpioNum, int brightness, int numPixels, StripType stripType) {
this(DEFAULT_FREQUENCY, DEFAULT_DMA_NUM, gpioNum, brightness, numPixels, stripType, DEFAULT_CHANNEL);
}

/**
* @param frequency Communication frequency, 800,000 or 400,000.
* @param dmaNum DMA number.
* @param gpioNum GPIO pin to use to drive the LEDs.
* @param brightness Brightness level (0..255).
* @param numPixels The number of pixels connected.
* @param stripType Strip type
* @param channel PWM channel
*/
public WS281x(int frequency, int dmaNum, int gpioNum, int brightness, int numPixels, StripType stripType, int channel) {
init();

this.numPixels = numPixels;

ch0LedBuffer = WS281xNative.initialise(frequency, dmaNum, gpioNum, brightness, numPixels, stripType.getType());
ch0LedBuffer = WS281xNative.initialise(frequency, dmaNum, gpioNum, brightness, numPixels, stripType.getType(), channel);
if (ch0LedBuffer == null) {
throw new RuntimeException("Error initialising the WS281x strip");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.nio.ByteBuffer;

public class WS281xNative {
public static native ByteBuffer initialise(int frequency, int dmaNum, int gpioNum, int brightness, int numPixels, int stripType);
public static native ByteBuffer initialise(int frequency, int dmaNum, int gpioNum, int brightness, int numPixels, int stripType, int channel);
public static native void terminate();
public static native int render();
}
Binary file modified diozero-ws281x-java/src/main/resources/lib/libws281xj.so
Binary file not shown.
31 changes: 17 additions & 14 deletions diozero-ws281x-native/src/main/native/com_diozero_ws281xj_WS281x.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@
#include "com_diozero_ws281xj_WS281x.h"

#define TARGET_FREQ WS2811_TARGET_FREQ
#define GPIO_PIN 18
#define DMA 5
#define LED_COUNT 60

ws2811_t led_string = {
.freq = TARGET_FREQ,
.dmanum = DMA,
.freq = 0,
.dmanum = 0,
.channel = {
[0] = {
.gpionum = GPIO_PIN,
.count = LED_COUNT,
.gpionum = 0,
.count = 0,
.invert = 0,
.brightness = 63,
.strip_type = WS2812_STRIP
.brightness = 0
}, [1] = {
.gpionum = 0,
.count = 0,
Expand All @@ -42,16 +39,22 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* jvm, void* reserved) {
/*
* Class: com_diozero_ws281xj_WS281xNative
* Method: initialise
* Signature: (IIIIII)Ljava/nio/ByteBuffer
* Signature: (IIIIIII)Ljava/nio/ByteBuffer
*/
JNIEXPORT jobject JNICALL Java_com_diozero_ws281xj_WS281xNative_initialise(
JNIEnv* env, jclass clz, jint frequency, jint dmaNum, jint gpioNum, jint brightness, jint numLeds, jint stripType) {
JNIEnv* env, jclass clz, jint frequency, jint dmaNum, jint gpioNum,
jint brightness, jint numLeds, jint stripType, jint channel) {
if (channel < 0 || channel >= RPI_PWM_CHANNELS) {
fprintf(stderr, "Error channel must be 0..%d\n", RPI_PWM_CHANNELS - 1);
return NULL;
}

led_string.freq = frequency;
led_string.dmanum = dmaNum;
led_string.channel[0].gpionum = gpioNum;
led_string.channel[0].count = numLeds;
led_string.channel[0].brightness = brightness;
led_string.channel[0].strip_type = stripType;
led_string.channel[channel].gpionum = gpioNum;
led_string.channel[channel].count = numLeds;
led_string.channel[channel].brightness = brightness;
led_string.channel[channel].strip_type = stripType;
int rc = ws2811_init(&led_string);
if (rc != 0) {
fprintf(stderr, "ws2811_init failed: %s\n", ws2811_get_return_t_str(rc));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65cc1d7

Please sign in to comment.