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