X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2Fmode%2FAmbientMode.java;fp=src%2Fkaka%2Fcakelight%2Fmode%2FAmbientMode.java;h=afe36ab9a657abf2a6520ff908d834ca4e82abf8;hb=67b0a75891f19e91cc35e23fa56915cfd7cd52de;hp=0000000000000000000000000000000000000000;hpb=8304abc8107c4c4d7334ddc7276e95b8f535ee55;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/mode/AmbientMode.java b/src/kaka/cakelight/mode/AmbientMode.java new file mode 100644 index 0000000..afe36ab --- /dev/null +++ b/src/kaka/cakelight/mode/AmbientMode.java @@ -0,0 +1,108 @@ +package kaka.cakelight.mode; + +import kaka.cakelight.Color; +import kaka.cakelight.Configuration; +import kaka.cakelight.LedFrame; +import kaka.cakelight.util.SimplexNoise3D; + +public class AmbientMode extends Mode { // TODO split into DynamicAmbient and StaticAmbient? + private Thread thread; // TODO move to a dynamic sub class + private Configuration config; + private int type = 0; + + public AmbientMode(String[] args) { + if (args.length > 0) { + type = Integer.parseInt(args[0]); + } + } + + @Override + public void enter(Configuration config) { + this.config = config; + startThread(); + } + + @Override + public void exit() { + stopThread(); + } + + public void startThread() { + thread = new Thread() { + public void run() { + try { + long start = System.currentTimeMillis(); + int index = 0; + while (!isInterrupted()) { + LedFrame frame = LedFrame.from(config); + updateFrame(frame, System.currentTimeMillis() - start, index); + updateWithFrame(frame); + index = (index + 1) % config.leds.getCount(); + Thread.sleep(20); + } + } catch (InterruptedException e) { + } + } + }; + thread.start(); + } + + public void stopThread() { + thread.interrupt(); + } + + /** + * @param frame + * @param time Time in milliseconds since start + * @param count Goes from 0 to number of LEDs - 1 + */ + private void updateFrame(LedFrame frame, long time, int count) { + if (type == 0) { + for (int i = 0; i < config.leds.getCount(); i++) { + double r = Math.sin(2 * i * Math.PI / config.leds.getCount() + time * 0.001) * 0.5 + 0.5; + double g = Math.cos(2 * i * Math.PI / config.leds.getCount() + time * 0.002) * 0.5 + 0.5; + frame.setLedColor(i, Color.rgb(r, g, 0)); + } + } else if (type == 1) { + for (int i = 0; i < config.leds.getCount(); i++) { + double x = frame.xOf(i); + double y = frame.yOf(i); + double b = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5); + double g = Math.min(1, Math.max(0, noise.getr(-b, b, 0.5, x*3, y*3, time / 5000.0))); + frame.setLedColor(i, Color.rgb(0, g, b)); + } + } else if (type == 2) { + int ledCount = config.leds.getCount(); + double hueOffset = time * 0.00001; + double hueLength = 1.0 / 6; + for (int i = 0; i < config.leds.getCount(); i++) { + double ledOffset = (i + (hueOffset * ledCount)) % ledCount; + double value = Math.abs((ledOffset * 2 - ledCount) / ledCount); // 1 to 0 to 1 + frame.setLedColor(i, Color.hsv(value * hueLength + hueOffset, 1, 1)); + } + } else if (type == 3) { + for (int i = 0; i < config.leds.getCount(); i++) { + double x = frame.xOf(i); + double y = frame.yOf(i); + double g = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5); + frame.setLedColor(i, Color.rgb(0, g, 1 - g * 0.5)); + } + } else if (type == 4) { + for (int i = 0; i < config.leds.getCount(); i++) { + double x = frame.xOf(i); + double y = frame.yOf(i); + double g = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5); + frame.setLedColor(i, Color.rgb(1, g, 0)); + } + } else if (type == 5) { + for (int i = 0; i < config.leds.getCount(); i++) { + double x = frame.xOf(i); + double y = frame.yOf(i); + double hue = (Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 4, x, y, time / 7000.0))) + (time / 100000.0)) % 1.0; + frame.setLedColor(i, Color.hsv(hue, 1, 1)); + } + } + } + + private SimplexNoise3D noise = new SimplexNoise3D(0); +}