package com.mmcvey; import java.awt.Color; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinPwmOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.RaspiPin; public class LedStrip { private static final int PWM_F = 1024; private final GpioPinPwmOutput red, green, blue; private final GpioController gpio; public LedStrip(GpioController gpio) { this.gpio = gpio; green = gpio.provisionPwmOutputPin(RaspiPin.GPIO_24, "Green", 0); red = gpio.provisionSoftPwmOutputPin(RaspiPin.GPIO_25, "Red", 0); blue = gpio.provisionPwmOutputPin(RaspiPin.GPIO_26, "Blue", 0); red.setShutdownOptions(true, PinState.LOW); green.setShutdownOptions(true, PinState.LOW); blue.setShutdownOptions(true, PinState.LOW); red.setPwmRange(PWM_F); green.setPwmRange(PWM_F); blue.setPwmRange(PWM_F);; try { red.setPwm((int) (255.0 * PWM_F / 255.0)); Thread.sleep(500); red.setPwm(0); green.setPwm((int) (255.0 * PWM_F / 255.0)); Thread.sleep(500); green.setPwm(0); blue.setPwm((int) (255.0 * PWM_F / 255.0)); Thread.sleep(500); blue.setPwm(0); setRGB(255,255,255); Thread.sleep(500); setRGB(0,0,0); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } startUpColors(); } public synchronized void setRGB(int r, int g, int b) throws ArithmeticException { if (r > 255 || r < 0) { throw new ArithmeticException("Invalid color value for red channel. Red equal to " + r); } if (g > 255 || g < 0) { throw new ArithmeticException("Invalid color value for green channel. Green equal to " + g); } if (b > 255 || b < 0) { throw new ArithmeticException("Invalid color value for blue channel. Blue equal to " + b); } red.setPwm((int) (r * PWM_F / 255.0)); green.setPwm((int) (g * PWM_F / 255.0)); blue.setPwm((int) (b * PWM_F / 255.0)); } public void stop() { setRGB(0,0,0); } private void startUpColors() { try { int c; for (float f = 0.0f; f < 2.0; f += 0.005f) { c = Color.HSBtoRGB(f, 1.0f, 1.0f); setRGB((c & 0xff0000) >> 16, (c & 0xff00) >> 8, (c & 0xff)); Thread.sleep(5); } for (int i = 0; i < 256; i++) { setRGB(i, i, i); Thread.sleep(10); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }