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 InputStream input = null;
26 input = new FileInputStream(propertiesFile);
27 Properties prop = new Properties();
29 return new Configuration(prop);
30 } catch (IOException ex) {
36 } catch (IOException e) {
44 private String get(Properties prop, String name) {
45 return addSetting(name, prop.getProperty(name));
48 private String get(Properties prop, String name, String dflt) {
49 return addSetting(name, prop.getProperty(name, dflt));
52 private String addSetting(String name, String value) {
53 settings.add(new AbstractMap.SimpleEntry<>(name, value));
58 public String toString() {
59 return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
62 public class VideoConfiguration {
67 public CropConfiguration crop;
69 private VideoConfiguration(Properties prop) {
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"));
73 switch (get(prop, "video.format", "").toUpperCase()) {
75 format = Imgproc.COLOR_YUV2BGR_YUYV;
78 format = Imgproc.COLOR_YUV2BGR_YVYU;
81 format = Imgproc.COLOR_YUV2BGR_UYVY;
83 crop = new CropConfiguration(prop);
86 public class CropConfiguration {
87 public int left, right, top, bottom;
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"));
98 public class LedConfiguration {
101 public int brightness;
104 private LedConfiguration(Properties prop) {
105 cols = Integer.parseInt(get(prop, "leds.cols"));
106 rows = Integer.parseInt(get(prop, "leds.rows"));
107 brightness = Math.max(1, Math.min(31, Integer.parseInt(get(prop, "leds.brightness", "31"))));
108 switch (get(prop, "leds.type", "").toUpperCase()) {
110 type = LedType.WS2801;
114 type = LedType.APA102;
118 public int getCount() {
119 return cols * 2 + rows * 2;
123 public enum LedType {