| 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 | 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 { |
| 63 | public int width; |
| 64 | public int height; |
| 65 | public int bpp; |
| 66 | public int format; |
| 67 | public CropConfiguration crop; |
| 68 | |
| 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()) { |
| 74 | case "YUYV": |
| 75 | format = Imgproc.COLOR_YUV2BGR_YUYV; |
| 76 | break; |
| 77 | case "YVYU": |
| 78 | format = Imgproc.COLOR_YUV2BGR_YVYU; |
| 79 | break; |
| 80 | default: |
| 81 | format = Imgproc.COLOR_YUV2BGR_UYVY; |
| 82 | } |
| 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 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public class LedConfiguration { |
| 99 | public int cols; |
| 100 | public int rows; |
| 101 | public int brightness; |
| 102 | public LedType type; |
| 103 | |
| 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()) { |
| 109 | case "WS2801": |
| 110 | type = LedType.WS2801; |
| 111 | break; |
| 112 | case "APA102": |
| 113 | default: |
| 114 | type = LedType.APA102; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public int getCount() { |
| 119 | return cols * 2 + rows * 2; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public enum LedType { |
| 124 | WS2801, APA102 |
| 125 | } |
| 126 | } |