Always return a configuration
[kaka/cakelight.git] / src / kaka / cakelight / Configuration.java
1 package kaka.cakelight;
2
3 import org.opencv.imgproc.Imgproc;
4
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.*;
9 import java.util.stream.Collectors;
10
11 public class Configuration {
12     private List<Map.Entry<String, String>> settings = new ArrayList<>();
13     public VideoConfiguration video;
14     public LedConfiguration leds;
15     public double gamma;
16
17     private Configuration(Properties prop) {
18         video = new VideoConfiguration(prop);
19         leds = new LedConfiguration(prop);
20         gamma = Double.parseDouble(get(prop,"gamma", "1"));
21     }
22
23     public static Configuration from(String propertiesFile) {
24         Properties prop = new Properties();
25         try (InputStream input = new FileInputStream(propertiesFile)) {
26             prop.load(input);
27         } catch (IOException ex) {
28             ex.printStackTrace();
29         }
30         return new Configuration(prop);
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 {
52         public int width;
53         public int height;
54         public int bpp;
55         public int format;
56         public CropConfiguration crop;
57
58         private VideoConfiguration(Properties prop) {
59             width = Integer.parseInt(get(prop, "video.width", "720"));
60             height = Integer.parseInt(get(prop, "video.height", "576"));
61             bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
62             switch (get(prop, "video.format", "").toUpperCase()) {
63                 case "YUYV":
64                     format = Imgproc.COLOR_YUV2BGR_YUYV;
65                     break;
66                 case "YVYU":
67                     format = Imgproc.COLOR_YUV2BGR_YVYU;
68                     break;
69                 default:
70                     format = Imgproc.COLOR_YUV2BGR_UYVY;
71             }
72             crop = new CropConfiguration(prop);
73         }
74
75         public class CropConfiguration {
76             public int left, right, top, bottom;
77
78             private CropConfiguration(Properties prop) {
79                 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
80                 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
81                 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
82                 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
83             }
84         }
85     }
86
87     public class LedConfiguration {
88         public int cols;
89         public int rows;
90         public int brightness;
91         public LedType type;
92
93         private LedConfiguration(Properties prop) {
94             cols = Integer.parseInt(get(prop, "leds.cols"));
95             rows = Integer.parseInt(get(prop, "leds.rows"));
96             brightness = Math.max(1, Math.min(31, Integer.parseInt(get(prop, "leds.brightness", "31"))));
97             switch (get(prop, "leds.type", "").toUpperCase()) {
98                 case "WS2801":
99                     type = LedType.WS2801;
100                     break;
101                 case "APA102":
102                 default:
103                     type = LedType.APA102;
104             }
105         }
106
107         public int getCount() {
108             return cols * 2 + rows * 2;
109         }
110     }
111
112     public enum LedType {
113         WS2801, APA102
114     }
115 }