Added a draft for an ambient mode
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 23 Apr 2017 15:36:23 +0000 (17:36 +0200)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 23 Apr 2017 15:36:23 +0000 (17:36 +0200)
src/kaka/cakelight/AmbientMode.java [new file with mode: 0644]
src/kaka/cakelight/CakeLight.java
src/kaka/cakelight/LedController.java
src/kaka/cakelight/Main.java
src/kaka/cakelight/Mode.java
src/kaka/cakelight/VideoMode.java

diff --git a/src/kaka/cakelight/AmbientMode.java b/src/kaka/cakelight/AmbientMode.java
new file mode 100644 (file)
index 0000000..528a1c3
--- /dev/null
@@ -0,0 +1,66 @@
+package kaka.cakelight;
+
+public class AmbientMode extends Mode { // TODO split into DynamicAmbient and StaticAmbient?
+    private Thread thread; // TODO move to a dynamic sub class
+    private Configuration config;
+
+    @Override
+    public void enter(Configuration config) {
+        this.config = config;
+//        LedFrame frame = LedFrame.from(config);
+//        frame.fillColor(255, 0, 128);
+//        frame.setLedColor(0, Color.rgb(255, 0, 0));
+//        frame.setLedColor(1, Color.rgb(0, 255, 0));
+//        frame.setLedColor(2, Color.rgb(0, 0, 255));
+//        for (int i = 0; i < config.leds.getCount(); i++) {
+////            int v = 255 * i / config.leds.getCount();
+//            int v = 60 * (i % 3);
+//            frame.setLedColor(i, Color.rgb(v, v, v));
+//        }
+//        frameListener.accept(frame);
+        startThread();
+    }
+
+    @Override
+    public void exit() {
+        stopThread();
+    }
+
+    public void startThread() {
+        thread = new Thread() {
+            public void run() {
+                try {
+                    long start = System.currentTimeMillis();
+                    int index = 0;
+                    while (!isInterrupted()) {
+                        LedFrame frame = LedFrame.from(config);
+                        updateFrame(frame, System.currentTimeMillis() - start, index);
+                        updateWithFrame(frame);
+                        index = (index + 1) % config.leds.getCount();
+                        Thread.sleep(0);
+                    }
+                } catch (InterruptedException e) {
+                }
+            }
+        };
+        thread.start();
+    }
+
+    public void stopThread() {
+        thread.interrupt();
+    }
+
+    /**
+     * @param frame
+     * @param time  Time in milliseconds since start
+     * @param count Goes from 0 to number of LEDs - 1
+     */
+    private void updateFrame(LedFrame frame, long time, int count) {
+        frame.setLedColor(count, Color.rgb(255, 0, 0));
+        frame.setLedColor((count + 1) % config.leds.getCount(), Color.rgb(0, 255, 0));
+        frame.setLedColor((count + 2) % config.leds.getCount(), Color.rgb(0, 0, 255));
+//        for (int i = 0; i < config.leds.getCount(); i++) {
+//
+//        }
+    }
+}
index 9b32c02..5401c0f 100644 (file)
@@ -5,9 +5,9 @@ public class CakeLight {
     private Mode mode;
     private LedController ledController;
 
-    public CakeLight(Configuration config) {
+    public CakeLight(Configuration config, LedController ledController) {
         this.config = config;
-        this.ledController = new LedController(config);
+        this.ledController = ledController;
     }
 
     public void setMode(Mode mode) {
index 9a23331..97f14c9 100644 (file)
@@ -7,24 +7,30 @@ import com.pi4j.io.spi.SpiFactory;
 import java.io.IOException;
 
 public class LedController {
+    private SpiDevice spi;
+
     public static void main(String args[]) {
         new LedController(null);
     }
 
     public LedController(Configuration config) {
         try {
-            SpiDevice spi = SpiFactory.getInstance(SpiChannel.CS0);
-            spi.write(
-                    (byte)0xff, (byte)0x0, (byte)0x0,
-                    (byte)0x00, (byte)0xff, (byte)0x0,
-                    (byte)0x0, (byte)0x00, (byte)0xff
-            );
+            spi = SpiFactory.getInstance(SpiChannel.CS0);
+//            spi.write(
+//                    (byte)0xff, (byte)0x0, (byte)0x0,
+//                    (byte)0x00, (byte)0xff, (byte)0x0,
+//                    (byte)0x0, (byte)0x00, (byte)0xff
+//            );
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
     public void onFrame(LedFrame ledFrame) {
-       // TODO
+        try {
+            spi.write(ledFrame.getBytes());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
     }
 }
index 9786aac..bc13c7f 100644 (file)
@@ -13,8 +13,8 @@ public class Main {
        Configuration config = Configuration.from("config.properties");
        log("Running with config:\n" + config);
 
-       CakeLight cakelight = new CakeLight(config);
-       cakelight.setMode(new VideoMode());
+       CakeLight cakelight = new CakeLight(config, new LedController(config));
+       cakelight.setMode(new AmbientMode());
        cakelight.startLoop();
 //     try {
 //         Thread.sleep(1000);
index 7b0227b..ad9bc8d 100644 (file)
@@ -3,7 +3,7 @@ package kaka.cakelight;
 import java.util.function.Consumer;
 
 public abstract class Mode {
-    protected Consumer<LedFrame> frameListener;
+    private Consumer<LedFrame> frameListener;
 
     public abstract void enter(Configuration config);
     public abstract void exit();
@@ -11,4 +11,9 @@ public abstract class Mode {
     public void setFrameListener(Consumer<LedFrame> listener) {
         frameListener = listener;
     }
+
+    public void updateWithFrame(LedFrame frame) {
+        assert frameListener != null;
+        frameListener.accept(frame);
+    }
 }
index 960f985..6da36f2 100644 (file)
@@ -54,8 +54,7 @@ public class VideoMode extends Mode {
     }
 
     private void onFrame(Frame frame) {
-        assert frameListener != null;
-        frameListener.accept(frame.getLedFrame());
+        updateWithFrame(frame.getLedFrame());
     }
 
     public void onVideoDeviceChange(Optional<File> videoDevice) {