Bugfix - use pause flag
[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 11 private int type = 0;
fa9808cd 12 private boolean isPaused = false;
0bf6c885 13
eca6fd31
TW
14 AmbientMode() {}
15
0bf6c885
TW
16 public AmbientMode(String[] args) {
17 if (args.length > 0) {
18 type = Integer.parseInt(args[0]);
19 }
20 }
6b569670
TW
21
22 @Override
23 public void enter(Configuration config) {
24 this.config = config;
6b569670
TW
25 startThread();
26 }
27
28 @Override
d0afa6fb 29 public void pause() {
fa9808cd 30 isPaused = true;
d0afa6fb
TW
31 }
32
33 @Override
34 public void resume() {
fa9808cd 35 isPaused = false;
d0afa6fb
TW
36 thread.notify();
37 }
38
39 @Override
6b569670
TW
40 public void exit() {
41 stopThread();
42 }
43
fa9808cd 44 private void startThread() {
6b569670
TW
45 thread = new Thread() {
46 public void run() {
47 try {
48 long start = System.currentTimeMillis();
49 int index = 0;
50 while (!isInterrupted()) {
fa9808cd
TW
51 if (isPaused) {
52 wait();
53 }
6b569670
TW
54 LedFrame frame = LedFrame.from(config);
55 updateFrame(frame, System.currentTimeMillis() - start, index);
56 updateWithFrame(frame);
57 index = (index + 1) % config.leds.getCount();
2e308eba 58 Thread.sleep(20);
6b569670
TW
59 }
60 } catch (InterruptedException e) {
fa9808cd 61 e.printStackTrace();
6b569670
TW
62 }
63 }
64 };
65 thread.start();
66 }
67
fa9808cd 68 private void stopThread() {
6b569670
TW
69 thread.interrupt();
70 }
71
72 /**
73 * @param frame
74 * @param time Time in milliseconds since start
75 * @param count Goes from 0 to number of LEDs - 1
76 */
eca6fd31 77 protected void updateFrame(LedFrame frame, long time, int count) {
0bf6c885
TW
78 if (type == 0) {
79 for (int i = 0; i < config.leds.getCount(); i++) {
80 double r = Math.sin(2 * i * Math.PI / config.leds.getCount() + time * 0.001) * 0.5 + 0.5;
81 double g = Math.cos(2 * i * Math.PI / config.leds.getCount() + time * 0.002) * 0.5 + 0.5;
82 frame.setLedColor(i, Color.rgb(r, g, 0));
83 }
84 } else if (type == 1) {
85 for (int i = 0; i < config.leds.getCount(); i++) {
65af4342
TW
86 double x = frame.xOf(i);
87 double y = frame.yOf(i);
14e552a7 88 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 89 double g = Math.min(1, Math.max(0, noise.getr(-b, b, 0.5, x*3, y*3, time / 5000.0)));
0bf6c885
TW
90 frame.setLedColor(i, Color.rgb(0, g, b));
91 }
65af4342
TW
92 } else if (type == 2) {
93 int ledCount = config.leds.getCount();
94 double hueOffset = time * 0.00001;
95 double hueLength = 1.0 / 6;
96 for (int i = 0; i < config.leds.getCount(); i++) {
97 double ledOffset = (i + (hueOffset * ledCount)) % ledCount;
98 double value = Math.abs((ledOffset * 2 - ledCount) / ledCount); // 1 to 0 to 1
99 frame.setLedColor(i, Color.hsv(value * hueLength + hueOffset, 1, 1));
100 }
034031f3
TW
101 } else if (type == 3) {
102 for (int i = 0; i < config.leds.getCount(); i++) {
103 double x = frame.xOf(i);
104 double y = frame.yOf(i);
105 double g = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5);
106 frame.setLedColor(i, Color.rgb(0, g, 1 - g * 0.5));
107 }
108 } else if (type == 4) {
109 for (int i = 0; i < config.leds.getCount(); i++) {
110 double x = frame.xOf(i);
111 double y = frame.yOf(i);
112 double g = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y, time / 7000.0))), 1.5);
113 frame.setLedColor(i, Color.rgb(1, g, 0));
114 }
c8657a8d
TW
115 } else if (type == 5) {
116 for (int i = 0; i < config.leds.getCount(); i++) {
117 double x = frame.xOf(i);
118 double y = frame.yOf(i);
119 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;
120 frame.setLedColor(i, Color.hsv(hue, 1, 1));
121 }
8aea44b5 122 }
6b569670 123 }
0bf6c885
TW
124
125 private SimplexNoise3D noise = new SimplexNoise3D(0);
6b569670 126}