Added a light level config (0-31) for APA102 only
[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) {
24 InputStream input = null;
25 try {
26 input = new FileInputStream(propertiesFile);
27 Properties prop = new Properties();
28 prop.load(input);
29 return new Configuration(prop);
30 } catch (IOException ex) {
31 ex.printStackTrace();
32 } finally {
33 if (input != null) {
34 try {
35 input.close();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40 }
41 return null;
42 }
43
44 private String get(Properties prop, String name) {
45 return addSetting(name, prop.getProperty(name));
46 }
47
48 private String get(Properties prop, String name, String dflt) {
49 return addSetting(name, prop.getProperty(name, dflt));
50 }
51
52 private String addSetting(String name, String value) {
53 settings.add(new AbstractMap.SimpleEntry<>(name, value));
54 return value;
55 }
56
57 @Override
58 public String toString() {
59 return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
60 }
61
62 public class VideoConfiguration {
e59e98fc
TW
63 public int width;
64 public int height;
65 public int bpp;
cc03403a 66 public int format;
100b82fe 67 public CropConfiguration crop;
e59e98fc
TW
68
69 private VideoConfiguration(Properties prop) {
e59e98fc
TW
70 width = Integer.parseInt(get(prop, "video.width", "720"));
71 height = Integer.parseInt(get(prop, "video.height", "576"));
72 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
cc03403a
TW
73 switch (get(prop, "video.format", "").toUpperCase()) {
74 case "YUYV":
da7bef43 75 format = Imgproc.COLOR_YUV2BGR_YUYV;
cc03403a
TW
76 break;
77 case "YVYU":
da7bef43 78 format = Imgproc.COLOR_YUV2BGR_YVYU;
cc03403a
TW
79 break;
80 default:
da7bef43 81 format = Imgproc.COLOR_YUV2BGR_UYVY;
cc03403a 82 }
100b82fe
TW
83 crop = new CropConfiguration(prop);
84 }
85
86 public class CropConfiguration {
87 public int left, right, top, bottom;
88
89 private CropConfiguration(Properties prop) {
90 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
91 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
92 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
93 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
94 }
e59e98fc
TW
95 }
96 }
97
98 public class LedConfiguration {
99 public int cols;
100 public int rows;
f2eedb6d 101 public int level;
aa9e49c2 102 public LedType type;
e59e98fc
TW
103
104 private LedConfiguration(Properties prop) {
105 cols = Integer.parseInt(get(prop, "leds.cols"));
106 rows = Integer.parseInt(get(prop, "leds.rows"));
f2eedb6d 107 level = Math.min(0, Math.max(31, Integer.parseInt(get(prop, "leds.level", "31"))));
aa9e49c2
TW
108 switch (get(prop, "leds.type", "").toUpperCase()) {
109 case "WS2801":
110 type = LedType.WS2801;
111 break;
112 case "APA102":
113 default:
114 type = LedType.APA102;
115 }
e59e98fc 116 }
ed56b145
TW
117
118 public int getCount() {
119 return cols * 2 + rows * 2;
120 }
e59e98fc 121 }
aa9e49c2
TW
122
123 public enum LedType {
124 WS2801, APA102
125 }
e59e98fc 126}