1 package kaka.cakelight;
3 import org.opencv.imgproc.Imgproc;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
9 import java.util.stream.Collectors;
11 public class Configuration {
12 private List<Map.Entry<String, String>> settings = new ArrayList<>();
13 public VideoConfiguration video;
14 public LedConfiguration leds;
17 private Configuration(Properties prop) {
18 video = new VideoConfiguration(prop);
19 leds = new LedConfiguration(prop);
20 gamma = Double.parseDouble(get(prop,"gamma", "1"));
23 public static Configuration from(String propertiesFile) {
24 Properties prop = new Properties();
25 try (InputStream input = new FileInputStream(propertiesFile)) {
27 } catch (IOException ex) {
30 return new Configuration(prop);
33 private String get(Properties prop, String name) {
34 return addSetting(name, prop.getProperty(name));
37 private String get(Properties prop, String name, String dflt) {
38 return addSetting(name, prop.getProperty(name, dflt));
41 private String addSetting(String name, String value) {
42 settings.add(new AbstractMap.SimpleEntry<>(name, value));
47 public String toString() {
48 return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
51 public class VideoConfiguration {
57 public double saturation;
59 public boolean deviceIsAutomatic;
60 public CropConfiguration crop;
61 public ListConfiguration list;
63 private VideoConfiguration(Properties prop) {
64 width = Integer.parseInt(get(prop, "video.width", "720"));
65 height = Integer.parseInt(get(prop, "video.height", "576"));
66 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
67 device = get(prop, "video.device", "auto");
68 deviceIsAutomatic = "auto".equals(device);
69 switch (get(prop, "video.format", "").toUpperCase()) {
71 format = Imgproc.COLOR_YUV2BGR_YUYV;
74 format = Imgproc.COLOR_YUV2BGR_YVYU;
80 format = Imgproc.COLOR_YUV2BGR_UYVY;
82 saturation = inRange(Double.parseDouble(get(prop, "video.saturation", "0.5")), 0, 1);
83 crop = new CropConfiguration(prop);
84 list = new ListConfiguration(prop);
87 public class CropConfiguration {
88 public int left, right, top, bottom;
90 private CropConfiguration(Properties prop) {
91 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
92 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
93 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
94 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
98 public class ListConfiguration {
99 public boolean top, bottom, left, right;
101 private ListConfiguration(Properties prop) {
102 top = get(prop, "video.list.top", "on").equals("on");
103 bottom = get(prop, "video.list.bottom", "on").equals("on");
104 left = get(prop, "video.list.left", "on").equals("on");
105 right = get(prop, "video.list.right", "on").equals("on");
110 public class LedConfiguration {
113 public int brightness;
116 private LedConfiguration(Properties prop) {
117 cols = Integer.parseInt(get(prop, "leds.cols"));
118 rows = Integer.parseInt(get(prop, "leds.rows"));
119 brightness = (int) inRange(Integer.parseInt(get(prop, "leds.brightness", "31")), 1, 31);
120 switch (get(prop, "leds.type", "").toUpperCase()) {
122 type = LedType.WS2801;
126 type = LedType.APA102;
130 public int getCount() {
131 return cols * 2 + rows * 2;
135 public enum LedType {
139 private double inRange(double value, double lower, double upper) {
140 return value < lower ? lower
141 : value > upper ? upper