Added a custom Color class
[kaka/cakelight.git] / src / kaka / cakelight / Main.java
CommitLineData
e59e98fc
TW
1package kaka.cakelight;
2
3import org.opencv.core.Core;
4
5import java.io.FileOutputStream;
6import java.io.IOException;
4a2d6056 7import java.util.HashMap;
e59e98fc
TW
8
9public class Main {
e59e98fc
TW
10 public static void main(String[] args) {
11 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
12
13 Configuration config = Configuration.from("config.properties");
4a2d6056
TW
14 log("Running with config:\n" + config);
15
01ee91e7 16 CakeLight cakelight = new CakeLight(config);
4a2d6056
TW
17 cakelight.setMode(new VideoMode());
18 cakelight.startLoop();
19// try {
20// Thread.sleep(1000);
21// } catch (InterruptedException e) {
22// e.printStackTrace();
23// }
24// cakelight.setMode(null);
25 Runtime.getRuntime().addShutdownHook(new Thread(Main::printTimeStats));
e59e98fc
TW
26 }
27
28 public static void saveFile(byte[] data, String filepath) {
29 try {
30 FileOutputStream fos = new FileOutputStream(filepath);
31 fos.write(data);
32 fos.close();
33 } catch (IOException e) {
34 e.printStackTrace();
35 }
36 }
37
4a2d6056
TW
38 public static void log(String msg, Object... args) {
39 System.out.println(String.format(msg, args));
40 }
41
42 private static HashMap<String, Double> timeDurations = new HashMap<>();
43 private static HashMap<String, Integer> timeCounts = new HashMap<>();
e59e98fc
TW
44 public static double timeIt(String tag, Runnable lambda) {
45 long start = System.nanoTime();
46 lambda.run();
47 long end = System.nanoTime();
48 double duration = (end - start) * 0.000001;
4a2d6056
TW
49// log("duration (ms): " + tag + " = " + duration);
50
51 if (!timeDurations.containsKey(tag)) {
52 timeDurations.put(tag, 0.0);
53 timeCounts.put(tag, 0);
54 }
55 timeDurations.put(tag, timeDurations.get(tag) + duration);
56 timeCounts.put(tag, timeCounts.get(tag) + 1);
e59e98fc
TW
57 return duration;
58 }
4a2d6056
TW
59
60 private static void printTimeStats() {
61 log("Average times in ms:");
62 timeDurations.forEach((tag, duration) -> {
63 log("%s: %s", tag, duration / timeCounts.get(tag));
64 });
65 }
e59e98fc
TW
66}
67
68/*
69FrameGrabber läser frames asynkront
70skickar frame till FrameConverter
71sparas i huvudklassen
4a2d6056
TW
72läses av FrameProcessor/CakeLight
73 */