120d0f49071262aeb24b81df29c614a0874221c0
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.AmbientMode;
4 import kaka.cakelight.mode.SingleColorMode;
5 import kaka.cakelight.mode.TwoColorNoiseMode;
6 import kaka.cakelight.mode.VideoMode;
7
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.util.*;
12
13 public class Console extends Thread {
14     private CakeLight cakelight;
15     private Configuration config;
16     private BufferedReader reader;
17     private Map<String, Command> commands = new HashMap<>();
18     private List<Command> commandList = new ArrayList<>();
19
20     public static void start(CakeLight cakelight, Configuration config) {
21         new Console(cakelight, config).start();
22     }
23
24     private Console(CakeLight cakelight, Configuration config) {
25         this.cakelight = cakelight;
26         this.config = config;
27         reader = new BufferedReader(new InputStreamReader(System.in));
28         register(TwoColorNoiseMode.getCommand());
29
30     public CakeLight getCakelight() {
31         return cakelight;
32     }
33
34     public Configuration getConfig() {
35         return config;
36     }
37
38     private void register(Command cmd) {
39         for (String name : cmd.getNames()){
40             if (commands.containsKey(name)) {
41                 System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
42                 System.exit(1);
43             }
44             commands.put(name, cmd);
45         }
46         commandList.add(cmd);
47     }
48
49     @Override
50     public void run() {
51         while (true) {
52             System.out.print("> ");
53             try {
54                 String input = reader.readLine();
55                 String[] splitInput = input.split("\\s+", 2);
56                 String name = splitInput[0];
57                 String[] args = splitInput.length == 2
58                         ? splitInput[1].split("\\s+")
59                         : new String[]{};
60
61                 Command cmd = commands.get(name);
62                 if (cmd != null) {
63                     cmd.activate(this, args);
64                     continue;
65                 }
66
67                 if (input.matches("[0-5]")) {
68                     cakelight.setMode(new AmbientMode(new String[] {input}));
69                     System.out.println("setting ambient mode to " + input);
70                 } else if (input.matches("v|video")) {
71                     cakelight.setMode(new VideoMode());
72                 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
73                     String[] split = input.split("\\s+");
74                     config.leds.brightness = Integer.parseInt(split[1]);
75                     System.out.println("setting brightness to " + config.leds.brightness);
76                 } else if (input.matches("q|quit")) {
77                     cakelight.turnOff();
78                     System.out.println("stopping cakelight");
79                     break;
80                 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
81                     String[] split = input.split("\\s+");
82                     Color c = Color.rgb(
83                             Integer.parseInt(split[1]),
84                             Integer.parseInt(split[2]),
85                             Integer.parseInt(split[3])
86                     );
87                     cakelight.setMode(new SingleColorMode(c));
88                     System.out.println("setting color to " + c);
89                 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
90                     String[] split = input.split("\\s+");
91                     config.gamma = Double.parseDouble(split[1]);
92                     Color.calculateGammaCorrection(config.gamma);
93                     System.out.println("setting gamma to " + config.gamma);
94                 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
95                     String[] split = input.split("\\s+");
96                     config.video.saturation = Double.parseDouble(split[1]);
97                     System.out.println("setting saturation to " + config.video.saturation);
98                 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
99                     TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
100                     System.out.println("setting two-color noise mode");
101                 }
102             } catch (IOException e) {
103                 System.out.println("Error reading from command line");
104                 break;
105             }
106         }
107     }
108
109     public interface Command {
110         String[] getNames();
111         void activate(Console console, String[] args);
112
113         default Color parseColor(String s) {
114             switch (s.toLowerCase()) {
115                 case "r": return Color.rgb(255, 0, 0);
116                 case "g": return Color.rgb(0, 255, 0);
117                 case "b": return Color.rgb(0, 0, 255);
118                 default: // assume hexadecimal
119                     if (s.startsWith("#")) {
120                         s = s.substring(1);
121                     }
122                     if (s.length() == 3) {
123                         return Color.rgb(
124                                 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
125                                 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
126                                 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
127                         );
128                     } else if (s.length() == 6) {
129                         return Color.rgb(
130                                 Integer.parseInt(s.substring(0, 2), 16),
131                                 Integer.parseInt(s.substring(2, 4), 16),
132                                 Integer.parseInt(s.substring(4, 6), 16)
133                         );
134                     }
135             }
136             System.out.println("Failed to parse color '" + s + "'. Using black instead.");
137             return Color.BLACK;
138         }
139
140         default void output(String text) {
141             System.out.println("(" + text + ")");
142         }
143     }
144 }