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