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