Added config for turning off lists
[kaka/cakelight.git] / src / kaka / cakelight / Configuration.java
CommitLineData
e59e98fc
TW
1package kaka.cakelight;
2
cc03403a
TW
3import org.opencv.imgproc.Imgproc;
4
e59e98fc
TW
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.*;
9import java.util.stream.Collectors;
10
11public class Configuration {
12 private List<Map.Entry<String, String>> settings = new ArrayList<>();
13 public VideoConfiguration video;
14 public LedConfiguration leds;
38c759f8 15 public double gamma;
e59e98fc
TW
16
17 private Configuration(Properties prop) {
18 video = new VideoConfiguration(prop);
19 leds = new LedConfiguration(prop);
38c759f8 20 gamma = Double.parseDouble(get(prop,"gamma", "1"));
e59e98fc
TW
21 }
22
23 public static Configuration from(String propertiesFile) {
2d8ddb23
TW
24 Properties prop = new Properties();
25 try (InputStream input = new FileInputStream(propertiesFile)) {
e59e98fc 26 prop.load(input);
e59e98fc
TW
27 } catch (IOException ex) {
28 ex.printStackTrace();
e59e98fc 29 }
2d8ddb23 30 return new Configuration(prop);
e59e98fc
TW
31 }
32
33 private String get(Properties prop, String name) {
34 return addSetting(name, prop.getProperty(name));
35 }
36
37 private String get(Properties prop, String name, String dflt) {
38 return addSetting(name, prop.getProperty(name, dflt));
39 }
40
41 private String addSetting(String name, String value) {
42 settings.add(new AbstractMap.SimpleEntry<>(name, value));
43 return value;
44 }
45
46 @Override
47 public String toString() {
48 return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
49 }
50
51 public class VideoConfiguration {
e59e98fc
TW
52 public int width;
53 public int height;
54 public int bpp;
cc03403a 55 public int format;
100b82fe 56 public CropConfiguration crop;
6a03452e 57 public ListConfiguration list;
e59e98fc
TW
58
59 private VideoConfiguration(Properties prop) {
e59e98fc
TW
60 width = Integer.parseInt(get(prop, "video.width", "720"));
61 height = Integer.parseInt(get(prop, "video.height", "576"));
62 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
cc03403a
TW
63 switch (get(prop, "video.format", "").toUpperCase()) {
64 case "YUYV":
da7bef43 65 format = Imgproc.COLOR_YUV2BGR_YUYV;
cc03403a
TW
66 break;
67 case "YVYU":
da7bef43 68 format = Imgproc.COLOR_YUV2BGR_YVYU;
cc03403a
TW
69 break;
70 default:
da7bef43 71 format = Imgproc.COLOR_YUV2BGR_UYVY;
cc03403a 72 }
100b82fe 73 crop = new CropConfiguration(prop);
6a03452e 74 list = new ListConfiguration(prop);
100b82fe
TW
75 }
76
77 public class CropConfiguration {
78 public int left, right, top, bottom;
79
80 private CropConfiguration(Properties prop) {
81 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
82 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
83 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
84 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
85 }
e59e98fc 86 }
6a03452e
TW
87
88 public class ListConfiguration {
89 public boolean top, bottom, left, right;
90
91 private ListConfiguration(Properties prop) {
92 top = get(prop, "video.list.top", "on").equals("on");
93 bottom = get(prop, "video.list.bottom", "on").equals("on");
94 left = get(prop, "video.list.left", "on").equals("on");
95 right = get(prop, "video.list.right", "on").equals("on");
96 }
97 }
e59e98fc
TW
98 }
99
100 public class LedConfiguration {
101 public int cols;
102 public int rows;
d3261df8 103 public int brightness;
aa9e49c2 104 public LedType type;
e59e98fc
TW
105
106 private LedConfiguration(Properties prop) {
107 cols = Integer.parseInt(get(prop, "leds.cols"));
108 rows = Integer.parseInt(get(prop, "leds.rows"));
d3261df8 109 brightness = Math.max(1, Math.min(31, Integer.parseInt(get(prop, "leds.brightness", "31"))));
aa9e49c2
TW
110 switch (get(prop, "leds.type", "").toUpperCase()) {
111 case "WS2801":
112 type = LedType.WS2801;
113 break;
114 case "APA102":
115 default:
116 type = LedType.APA102;
117 }
e59e98fc 118 }
ed56b145
TW
119
120 public int getCount() {
121 return cols * 2 + rows * 2;
122 }
e59e98fc 123 }
aa9e49c2
TW
124
125 public enum LedType {
126 WS2801, APA102
127 }
e59e98fc 128}