Add video device to config
[kaka/cakelight.git] / src / kaka / cakelight / Configuration.java
... / ...
CommitLineData
1package kaka.cakelight;
2
3import org.opencv.imgproc.Imgproc;
4
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;
15 public double gamma;
16
17 private Configuration(Properties prop) {
18 video = new VideoConfiguration(prop);
19 leds = new LedConfiguration(prop);
20 gamma = Double.parseDouble(get(prop,"gamma", "1"));
21 }
22
23 public static Configuration from(String propertiesFile) {
24 Properties prop = new Properties();
25 try (InputStream input = new FileInputStream(propertiesFile)) {
26 prop.load(input);
27 } catch (IOException ex) {
28 ex.printStackTrace();
29 }
30 return new Configuration(prop);
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 {
52 public int width;
53 public int height;
54 public int bpp;
55 public int format;
56 public double saturation;
57 public String device;
58 public boolean deviceIsAutomatic;
59 public CropConfiguration crop;
60 public ListConfiguration list;
61
62 private VideoConfiguration(Properties prop) {
63 width = Integer.parseInt(get(prop, "video.width", "720"));
64 height = Integer.parseInt(get(prop, "video.height", "576"));
65 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
66 device = get(prop, "video.device", "auto");
67 deviceIsAutomatic = "auto".equals(device);
68 switch (get(prop, "video.format", "").toUpperCase()) {
69 case "YUYV":
70 format = Imgproc.COLOR_YUV2BGR_YUYV;
71 break;
72 case "YVYU":
73 format = Imgproc.COLOR_YUV2BGR_YVYU;
74 break;
75 default:
76 format = Imgproc.COLOR_YUV2BGR_UYVY;
77 }
78 saturation = inRange(Double.parseDouble(get(prop, "video.saturation", "0.5")), 0, 1);
79 crop = new CropConfiguration(prop);
80 list = new ListConfiguration(prop);
81 }
82
83 public class CropConfiguration {
84 public int left, right, top, bottom;
85
86 private CropConfiguration(Properties prop) {
87 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
88 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
89 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
90 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
91 }
92 }
93
94 public class ListConfiguration {
95 public boolean top, bottom, left, right;
96
97 private ListConfiguration(Properties prop) {
98 top = get(prop, "video.list.top", "on").equals("on");
99 bottom = get(prop, "video.list.bottom", "on").equals("on");
100 left = get(prop, "video.list.left", "on").equals("on");
101 right = get(prop, "video.list.right", "on").equals("on");
102 }
103 }
104 }
105
106 public class LedConfiguration {
107 public int cols;
108 public int rows;
109 public int brightness;
110 public LedType type;
111
112 private LedConfiguration(Properties prop) {
113 cols = Integer.parseInt(get(prop, "leds.cols"));
114 rows = Integer.parseInt(get(prop, "leds.rows"));
115 brightness = (int) inRange(Integer.parseInt(get(prop, "leds.brightness", "31")), 1, 31);
116 switch (get(prop, "leds.type", "").toUpperCase()) {
117 case "WS2801":
118 type = LedType.WS2801;
119 break;
120 case "APA102":
121 default:
122 type = LedType.APA102;
123 }
124 }
125
126 public int getCount() {
127 return cols * 2 + rows * 2;
128 }
129 }
130
131 public enum LedType {
132 WS2801, APA102
133 }
134
135 private double inRange(double value, double lower, double upper) {
136 return value < lower ? lower
137 : value > upper ? upper
138 : value;
139 }
140}