package com.mmcvey; import java.awt.Color; import com.pi4j.io.gpio.GpioController; public class Main { private final LedStrip leds; private final SoundAnalyser sound; private boolean running; private int mode; private GpioController gpio; public Main(LedStrip leds, SoundAnalyser sound, GpioController gpio) { this.leds = leds; this.sound = sound; this.gpio = gpio; this.mode = 0; } public void start() { new Thread(() -> run()).start(); } public synchronized void run() { running = true; while (running) { soundChange(); softChange(); hueCycle(); // System.out.println(sound.getShift()); } sound.stop(); leds.stop(); try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } gpio.shutdown(); } private void softChange() { long lastTime = System.nanoTime(); float hue = 0.0f; int color; color = Color.HSBtoRGB(hue, 1.0f, 1.0f); leds.setRGB((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff); hue += 0.618033988749894f; lastTime = System.nanoTime(); while (running && mode == 1) { if ((System.nanoTime() - lastTime) / 1.0e9 > 6.0) { color = Color.HSBtoRGB(hue, 1.0f, 1.0f); leds.setRGB((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff); hue += 0.618033988749894f; lastTime = System.nanoTime(); } } } private void soundChange() { long lastTime = System.nanoTime(); while (running && mode == 0) { if ((System.nanoTime() - lastTime) / 1.0e9 > 0.00390625) { // ~120 Hz = 0.0083333333333333, 256Hz // =0.00390625 , 128Hz = 0.0078125, 64Hz = // 0.015625, 32Hz = 0.03125, 16Hz = 0.0625 // System.out.println(1.0e9 / (System.nanoTime() - lastTime)); //check Hz lastTime = System.nanoTime(); leds.setRGB(sound.getRed(), sound.getGreen(), sound.getBlue()); } } } private void hueCycle() { long lastTime = System.nanoTime(); float hue = 0.0f; int color; while (running && mode == 2) { if ((System.nanoTime() - lastTime) / 1.0e9 > 0.00390625) { // ~120 Hz = 0.0083333333333333, 256Hz // =0.00390625 , 128Hz = 0.0078125, 64Hz = // 0.015625, 32Hz = 0.03125, 16Hz = 0.0625 // System.out.println(1.0e9 / (System.nanoTime() - lastTime)); //check Hz lastTime = System.nanoTime(); hue += 1.0f / 256.0f / 64.0f; color = Color.HSBtoRGB(hue, 1.0f, 1.0f); leds.setRGB((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff); } } } public void nextMode() { mode++; mode %= 3; } public void stop() { running = false; } }