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