| 1 | package kaka.cakelight; |
| 2 | |
| 3 | public class AmbientMode extends Mode { // TODO split into DynamicAmbient and StaticAmbient? |
| 4 | private Thread thread; // TODO move to a dynamic sub class |
| 5 | private Configuration config; |
| 6 | |
| 7 | @Override |
| 8 | public void enter(Configuration config) { |
| 9 | this.config = config; |
| 10 | // LedFrame frame = LedFrame.from(config); |
| 11 | // frame.fillColor(255, 0, 128); |
| 12 | // frame.setLedColor(0, Color.rgb(255, 0, 0)); |
| 13 | // frame.setLedColor(1, Color.rgb(0, 255, 0)); |
| 14 | // frame.setLedColor(2, Color.rgb(0, 0, 255)); |
| 15 | // for (int i = 0; i < config.leds.getCount(); i++) { |
| 16 | //// int v = 255 * i / config.leds.getCount(); |
| 17 | // int v = 60 * (i % 3); |
| 18 | // frame.setLedColor(i, Color.rgb(v, v, v)); |
| 19 | // } |
| 20 | // frameListener.accept(frame); |
| 21 | startThread(); |
| 22 | } |
| 23 | |
| 24 | @Override |
| 25 | public void exit() { |
| 26 | stopThread(); |
| 27 | } |
| 28 | |
| 29 | public void startThread() { |
| 30 | thread = new Thread() { |
| 31 | public void run() { |
| 32 | try { |
| 33 | long start = System.currentTimeMillis(); |
| 34 | int index = 0; |
| 35 | while (!isInterrupted()) { |
| 36 | LedFrame frame = LedFrame.from(config); |
| 37 | updateFrame(frame, System.currentTimeMillis() - start, index); |
| 38 | updateWithFrame(frame); |
| 39 | index = (index + 1) % config.leds.getCount(); |
| 40 | Thread.sleep(0); |
| 41 | } |
| 42 | } catch (InterruptedException e) { |
| 43 | } |
| 44 | } |
| 45 | }; |
| 46 | thread.start(); |
| 47 | } |
| 48 | |
| 49 | public void stopThread() { |
| 50 | thread.interrupt(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param frame |
| 55 | * @param time Time in milliseconds since start |
| 56 | * @param count Goes from 0 to number of LEDs - 1 |
| 57 | */ |
| 58 | private void updateFrame(LedFrame frame, long time, int count) { |
| 59 | frame.setLedColor(count, Color.rgb(255, 0, 0)); |
| 60 | frame.setLedColor((count + 1) % config.leds.getCount(), Color.rgb(0, 255, 0)); |
| 61 | frame.setLedColor((count + 2) % config.leds.getCount(), Color.rgb(0, 0, 255)); |
| 62 | // for (int i = 0; i < config.leds.getCount(); i++) { |
| 63 | // |
| 64 | // } |
| 65 | } |
| 66 | } |