Limit range of saturation config
[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;
a80ebf3e 56 public double saturation;
100b82fe 57 public CropConfiguration crop;
6a03452e 58 public ListConfiguration list;
e59e98fc
TW
59
60 private VideoConfiguration(Properties prop) {
e59e98fc
TW
61 width = Integer.parseInt(get(prop, "video.width", "720"));
62 height = Integer.parseInt(get(prop, "video.height", "576"));
63 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
cc03403a
TW
64 switch (get(prop, "video.format", "").toUpperCase()) {
65 case "YUYV":
da7bef43 66 format = Imgproc.COLOR_YUV2BGR_YUYV;
cc03403a
TW
67 break;
68 case "YVYU":
da7bef43 69 format = Imgproc.COLOR_YUV2BGR_YVYU;
cc03403a
TW
70 break;
71 default:
da7bef43 72 format = Imgproc.COLOR_YUV2BGR_UYVY;
cc03403a 73 }
34d8fc09 74 saturation = inRange(Double.parseDouble(get(prop, "video.saturation", "0.5")), 0, 1);
100b82fe 75 crop = new CropConfiguration(prop);
6a03452e 76 list = new ListConfiguration(prop);
100b82fe
TW
77 }
78
79 public class CropConfiguration {
80 public int left, right, top, bottom;
81
82 private CropConfiguration(Properties prop) {
83 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
84 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
85 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
86 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
87 }
e59e98fc 88 }
6a03452e
TW
89
90 public class ListConfiguration {
91 public boolean top, bottom, left, right;
92
93 private ListConfiguration(Properties prop) {
94 top = get(prop, "video.list.top", "on").equals("on");
95 bottom = get(prop, "video.list.bottom", "on").equals("on");
96 left = get(prop, "video.list.left", "on").equals("on");
97 right = get(prop, "video.list.right", "on").equals("on");
98 }
99 }
e59e98fc
TW
100 }
101
102 public class LedConfiguration {
103 public int cols;
104 public int rows;
d3261df8 105 public int brightness;
aa9e49c2 106 public LedType type;
e59e98fc
TW
107
108 private LedConfiguration(Properties prop) {
109 cols = Integer.parseInt(get(prop, "leds.cols"));
110 rows = Integer.parseInt(get(prop, "leds.rows"));
34d8fc09 111 brightness = (int) inRange(Integer.parseInt(get(prop, "leds.brightness", "31")), 1, 31);
aa9e49c2
TW
112 switch (get(prop, "leds.type", "").toUpperCase()) {
113 case "WS2801":
114 type = LedType.WS2801;
115 break;
116 case "APA102":
117 default:
118 type = LedType.APA102;
119 }
e59e98fc 120 }
ed56b145
TW
121
122 public int getCount() {
123 return cols * 2 + rows * 2;
124 }
e59e98fc 125 }
aa9e49c2
TW
126
127 public enum LedType {
128 WS2801, APA102
129 }
34d8fc09
TW
130
131 private double inRange(double value, double lower, double upper) {
132 return value < lower ? lower
133 : value > upper ? upper
134 : value;
135 }
e59e98fc 136}