Use stack of modes instead of just one
[kaka/cakelight.git] / src / kaka / cakelight / mode / AmbientMode.java
CommitLineData
67b0a758 1package kaka.cakelight.mode;
6b569670 2
67b0a758
TW
3import kaka.cakelight.Color;
4import kaka.cakelight.Configuration;
5import kaka.cakelight.LedFrame;
0bf6c885
TW
6import kaka.cakelight.util.SimplexNoise3D;
7
6b569670
TW
8public class AmbientMode extends Mode { // TODO split into DynamicAmbient and StaticAmbient?
9 private Thread thread; // TODO move to a dynamic sub class
eca6fd31 10 protected Configuration config;
0bf6c885
TW
11 private int type = 0;
12
eca6fd31
TW
13 AmbientMode() {}
14
0bf6c885
TW
15 public AmbientMode(String[] args) {
16 if (args.length > 0) {
17 type = Integer.parseInt(args[0]);
18 }
19 }
6b569670
TW
20
21 @Override
22 public void enter(Configuration config) {
23 this.config = config;
6b569670
TW
24 startThread();
25 }
26
27 @Override
28 public void exit() {
29 stopThread();
30 }
31
32 public void startThread() {
33 thread = new Thread() {
34 public void run() {
35 try {
36 long start = System.currentTimeMillis();
37 int index = 0;
38 while (!isInterrupted()) {
39 LedFrame frame = LedFrame.from(config);
40 updateFrame(frame, System.currentTimeMillis() - start, index);
41 updateWithFrame(frame);
42 index = (index + 1) % config.leds.getCount();
2e308eba 43 Thread.sleep(20);
6b569670
TW
44 }
45 } catch (InterruptedException e) {
46 }
47 }
48 };
49 thread.start();
50 }
51
52 public void stopThread() {
53 thread.interrupt();
54 }
55
56 /**
57 * @param frame
58 * @param time Time in milliseconds since start
59 * @param count Goes from 0 to number of LEDs - 1
60 */
eca6fd31 61 protected void updateFrame(LedFrame frame, long time, int count) {
0bf6c885
TW
62 if (type == 0) {
63 for (int i = 0; i < config.leds.getCount(); i++) {
64 double r = Math.sin(2 * i * Math.PI / config.leds.getCount() + time * 0.001) * 0.5 + 0.5;
65 double g = Math.cos(2 * i * Math.PI / config.leds.getCount() + time * 0.002) * 0.5 + 0.5;
66 frame.setLedColor(i, Color.rgb(r, g, 0));
67 }
68 } else if (type == 1) {
69 for (int i = 0; i < config.leds.getCount(); i++) {
65af4342
TW
70 double x = frame.xOf(i);
71 double y = frame.yOf(i);
14e552a7 72 double b = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5);
4c299de6 73 double g = Math.min(1, Math.max(0, noise.getr(-b, b, 0.5, x*3, y*3, time / 5000.0)));
0bf6c885
TW
74 frame.setLedColor(i, Color.rgb(0, g, b));
75 }
65af4342
TW
76 } else if (type == 2) {
77 int ledCount = config.leds.getCount();
78 double hueOffset = time * 0.00001;
79 double hueLength = 1.0 / 6;
80 for (int i = 0; i < config.leds.getCount(); i++) {
81 double ledOffset = (i + (hueOffset * ledCount)) % ledCount;
82 double value = Math.abs((ledOffset * 2 - ledCount) / ledCount); // 1 to 0 to 1
83 frame.setLedColor(i, Color.hsv(value * hueLength + hueOffset, 1, 1));
84 }
034031f3
TW
85 } else if (type == 3) {
86 for (int i = 0; i < config.leds.getCount(); i++) {
87 double x = frame.xOf(i);
88 double y = frame.yOf(i);
89 double g = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5);
90 frame.setLedColor(i, Color.rgb(0, g, 1 - g * 0.5));
91 }
92 } else if (type == 4) {
93 for (int i = 0; i < config.leds.getCount(); i++) {
94 double x = frame.xOf(i);
95 double y = frame.yOf(i);
96 double g = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5);
97 frame.setLedColor(i, Color.rgb(1, g, 0));
98 }
c8657a8d
TW
99 } else if (type == 5) {
100 for (int i = 0; i < config.leds.getCount(); i++) {
101 double x = frame.xOf(i);
102 double y = frame.yOf(i);
103 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;
104 frame.setLedColor(i, Color.hsv(hue, 1, 1));
105 }
8aea44b5 106 }
6b569670 107 }
0bf6c885
TW
108
109 private SimplexNoise3D noise = new SimplexNoise3D(0);
6b569670 110}