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 {
56 public CropConfiguration crop;
57 public ListConfiguration list;
59 private VideoConfiguration(Properties prop) {
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"));
63 switch (get(prop, "video.format", "").toUpperCase()) {
65 format = Imgproc.COLOR_YUV2BGR_YUYV;
68 format = Imgproc.COLOR_YUV2BGR_YVYU;
71 format = Imgproc.COLOR_YUV2BGR_UYVY;
73 crop = new CropConfiguration(prop);
74 list = new ListConfiguration(prop);
77 public class CropConfiguration {
78 public int left, right, top, bottom;
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"));
88 public class ListConfiguration {
89 public boolean top, bottom, left, right;
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");
100 public class LedConfiguration {
103 public int brightness;
106 private LedConfiguration(Properties prop) {
107 cols = Integer.parseInt(get(prop, "leds.cols"));
108 rows = Integer.parseInt(get(prop, "leds.rows"));
109 brightness = Math.max(1, Math.min(31, Integer.parseInt(get(prop, "leds.brightness", "31"))));
110 switch (get(prop, "leds.type", "").toUpperCase()) {
112 type = LedType.WS2801;
116 type = LedType.APA102;
120 public int getCount() {
121 return cols * 2 + rows * 2;
125 public enum LedType {