Run in video mode unless arguments are supplied
[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
6b569670 16 CakeLight cakelight = new CakeLight(config, new LedController(config));
d48de1b4
TW
17 if (args.length > 0) {
18 cakelight.setMode(new AmbientMode(args));
19 } else {
20 cakelight.setMode(new VideoMode());
21 }
4a2d6056 22 cakelight.startLoop();
4a2d6056 23 Runtime.getRuntime().addShutdownHook(new Thread(Main::printTimeStats));
e59e98fc
TW
24 }
25
26 public static void saveFile(byte[] data, String filepath) {
27 try {
28 FileOutputStream fos = new FileOutputStream(filepath);
29 fos.write(data);
30 fos.close();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35
4a2d6056
TW
36 public static void log(String msg, Object... args) {
37 System.out.println(String.format(msg, args));
38 }
39
40 private static HashMap<String, Double> timeDurations = new HashMap<>();
41 private static HashMap<String, Integer> timeCounts = new HashMap<>();
e59e98fc
TW
42 public static double timeIt(String tag, Runnable lambda) {
43 long start = System.nanoTime();
44 lambda.run();
45 long end = System.nanoTime();
46 double duration = (end - start) * 0.000001;
4a2d6056
TW
47// log("duration (ms): " + tag + " = " + duration);
48
49 if (!timeDurations.containsKey(tag)) {
50 timeDurations.put(tag, 0.0);
51 timeCounts.put(tag, 0);
52 }
53 timeDurations.put(tag, timeDurations.get(tag) + duration);
54 timeCounts.put(tag, timeCounts.get(tag) + 1);
e59e98fc
TW
55 return duration;
56 }
4a2d6056
TW
57
58 private static void printTimeStats() {
59 log("Average times in ms:");
60 timeDurations.forEach((tag, duration) -> {
61 log("%s: %s", tag, duration / timeCounts.get(tag));
62 });
63 }
e59e98fc
TW
64}
65
66/*
67FrameGrabber läser frames asynkront
68skickar frame till FrameConverter
69sparas i huvudklassen
4a2d6056
TW
70läses av FrameProcessor/CakeLight
71 */