Refactored the LED frames backing data
[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
16     private Configuration(Properties prop) {
17         video = new VideoConfiguration(prop);
18         leds = new LedConfiguration(prop);
19     }
20
21     public static Configuration from(String propertiesFile) {
22         InputStream input = null;
23         try {
24             input = new FileInputStream(propertiesFile);
25             Properties prop = new Properties();
26             prop.load(input);
27             return new Configuration(prop);
28         } catch (IOException ex) {
29             ex.printStackTrace();
30         } finally {
31             if (input != null) {
32                 try {
33                     input.close();
34                 } catch (IOException e) {
35                     e.printStackTrace();
36                 }
37             }
38         }
39         return null;
40     }
41
42     private String get(Properties prop, String name) {
43         return addSetting(name, prop.getProperty(name));
44     }
45
46     private String get(Properties prop, String name, String dflt) {
47         return addSetting(name, prop.getProperty(name, dflt));
48     }
49
50     private String addSetting(String name, String value) {
51         settings.add(new AbstractMap.SimpleEntry<>(name, value));
52         return value;
53     }
54
55     @Override
56     public String toString() {
57         return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
58     }
59
60     public class VideoConfiguration {
61         public int width;
62         public int height;
63         public int bpp;
64         public int format;
65         public CropConfiguration crop;
66
67         private VideoConfiguration(Properties prop) {
68             width = Integer.parseInt(get(prop, "video.width", "720"));
69             height = Integer.parseInt(get(prop, "video.height", "576"));
70             bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
71             switch (get(prop, "video.format", "").toUpperCase()) {
72                 case "YUYV":
73                     format = Imgproc.COLOR_YUV2RGB_YUYV;
74                     break;
75                 case "YVYU":
76                     format = Imgproc.COLOR_YUV2RGB_YVYU;
77                     break;
78                 default:
79                     format = Imgproc.COLOR_YUV2RGB_UYVY;
80             }
81             crop = new CropConfiguration(prop);
82         }
83
84         public class CropConfiguration {
85             public int left, right, top, bottom;
86
87             private CropConfiguration(Properties prop) {
88                 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
89                 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
90                 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
91                 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
92             }
93         }
94     }
95
96     public class LedConfiguration {
97         public int cols;
98         public int rows;
99
100         private LedConfiguration(Properties prop) {
101             cols = Integer.parseInt(get(prop, "leds.cols"));
102             rows = Integer.parseInt(get(prop, "leds.rows"));
103         }
104
105         public int getCount() {
106             return cols * 2 + rows * 2;
107         }
108     }
109 }