WIP
[kaka/cakelight.git] / src / kaka / cakelight / Main.java
index 474683e..9786aac 100644 (file)
@@ -4,26 +4,25 @@ import org.opencv.core.Core;
 
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.HashMap;
 
 public class Main {
-
     public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 
        Configuration config = Configuration.from("config.properties");
-       System.out.println("Running with config:\n" + config);
-
-       FrameGrabber grabber = FrameGrabber.from(config);
-       grabber.prepare();
-       Frame frame = grabber.grabFrame();
-       double time = 0;
-       for (int i = 0; i < 100; i++) {
-           time += timeIt("frame", () -> grabber.grabFrame());
-       }
-       System.out.println("time = " + time);
-       grabber.close();
-//     byte[] data = frame.getData();
-//     saveFile(data, "/home/kaka/test.img");
+       log("Running with config:\n" + config);
+
+       CakeLight cakelight = new CakeLight(config);
+       cakelight.setMode(new VideoMode());
+       cakelight.startLoop();
+//     try {
+//         Thread.sleep(1000);
+//     } catch (InterruptedException e) {
+//         e.printStackTrace();
+//     }
+//     cakelight.setMode(null);
+       Runtime.getRuntime().addShutdownHook(new Thread(Main::printTimeStats));
     }
 
     public static void saveFile(byte[] data, String filepath) {
@@ -36,19 +35,39 @@ public class Main {
        }
     }
 
+    public static void log(String msg, Object... args) {
+       System.out.println(String.format(msg, args));
+    }
+
+    private static HashMap<String, Double> timeDurations = new HashMap<>();
+    private static HashMap<String, Integer> timeCounts = new HashMap<>();
     public static double timeIt(String tag, Runnable lambda) {
        long start = System.nanoTime();
        lambda.run();
        long end = System.nanoTime();
        double duration = (end - start) * 0.000001;
-       System.out.println("duration (ms): " + tag + " = " + duration);
+//     log("duration (ms): " + tag + " = " + duration);
+
+       if (!timeDurations.containsKey(tag)) {
+           timeDurations.put(tag, 0.0);
+           timeCounts.put(tag, 0);
+       }
+       timeDurations.put(tag, timeDurations.get(tag) + duration);
+       timeCounts.put(tag, timeCounts.get(tag) + 1);
        return duration;
     }
+
+    private static void printTimeStats() {
+        log("Average times in ms:");
+        timeDurations.forEach((tag, duration) -> {
+           log("%s: %s", tag, duration / timeCounts.get(tag));
+       });
+    }
 }
 
 /*
 FrameGrabber läser frames asynkront
 skickar frame till FrameConverter
 sparas i huvudklassen
-läses av FrameProcessor/LedController
- */
\ No newline at end of file
+läses av FrameProcessor/CakeLight
+ */