New ambient mode
[kaka/cakelight.git] / src / kaka / cakelight / AmbientMode.java
CommitLineData
6b569670
TW
1package kaka.cakelight;
2
0bf6c885
TW
3import kaka.cakelight.util.SimplexNoise3D;
4
6b569670
TW
5public class AmbientMode extends Mode { // TODO split into DynamicAmbient and StaticAmbient?
6 private Thread thread; // TODO move to a dynamic sub class
7 private Configuration config;
0bf6c885
TW
8 private int type = 0;
9
10 public AmbientMode(String[] args) {
11 if (args.length > 0) {
12 type = Integer.parseInt(args[0]);
13 }
14 }
6b569670
TW
15
16 @Override
17 public void enter(Configuration config) {
18 this.config = config;
6b569670
TW
19 startThread();
20 }
21
22 @Override
23 public void exit() {
24 stopThread();
25 }
26
27 public void startThread() {
28 thread = new Thread() {
29 public void run() {
30 try {
31 long start = System.currentTimeMillis();
32 int index = 0;
33 while (!isInterrupted()) {
34 LedFrame frame = LedFrame.from(config);
35 updateFrame(frame, System.currentTimeMillis() - start, index);
36 updateWithFrame(frame);
37 index = (index + 1) % config.leds.getCount();
38 Thread.sleep(0);
39 }
40 } catch (InterruptedException e) {
41 }
42 }
43 };
44 thread.start();
45 }
46
47 public void stopThread() {
48 thread.interrupt();
49 }
50
51 /**
52 * @param frame
53 * @param time Time in milliseconds since start
54 * @param count Goes from 0 to number of LEDs - 1
55 */
56 private void updateFrame(LedFrame frame, long time, int count) {
0bf6c885
TW
57 if (type == 0) {
58 for (int i = 0; i < config.leds.getCount(); i++) {
59 double r = Math.sin(2 * i * Math.PI / config.leds.getCount() + time * 0.001) * 0.5 + 0.5;
60 double g = Math.cos(2 * i * Math.PI / config.leds.getCount() + time * 0.002) * 0.5 + 0.5;
61 frame.setLedColor(i, Color.rgb(r, g, 0));
62 }
63 } else if (type == 1) {
64 for (int i = 0; i < config.leds.getCount(); i++) {
65 double g = noise.getr(0, 0.5, 0.5, frame.xOf(i), frame.yOf(i), time / 5000.0);
66 double b = noise.getr(0, 1, 1, frame.xOf(i), frame.yOf(i), time / 7000.0);
67 frame.setLedColor(i, Color.rgb(0, g, b));
68 }
8aea44b5 69 }
6b569670 70 }
0bf6c885
TW
71
72 private SimplexNoise3D noise = new SimplexNoise3D(0);
6b569670 73}