Bugfix - match arguments
[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
12 public class Console extends Thread {
13     private CakeLight cakelight;
14     private Configuration config;
15     private BufferedReader reader;
16
17     public static void start(CakeLight cakelight, Configuration config) {
18         new Console(cakelight, config).start();
19     }
20
21     private Console(CakeLight cakelight, Configuration config) {
22         this.cakelight = cakelight;
23         this.config = config;
24         reader = new BufferedReader(new InputStreamReader(System.in));
25     }
26
27     @Override
28     public void run() {
29         while (true) {
30             System.out.print("> ");
31             try {
32                 String input = reader.readLine();
33                 if (input.matches("[0-5]")) {
34                     cakelight.setMode(new AmbientMode(new String[] {input}));
35                     System.out.println("setting ambient mode to " + input);
36                 } else if (input.matches("v|video")) {
37                     cakelight.setMode(new VideoMode());
38                 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
39                     String[] split = input.split("\\s+");
40                     config.leds.brightness = Integer.parseInt(split[1]);
41                     System.out.println("setting brightness to " + config.leds.brightness);
42                 } else if (input.matches("q|quit")) {
43                     cakelight.turnOff();
44                     System.out.println("stopping cakelight");
45                     break;
46                 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
47                     String[] split = input.split("\\s+");
48                     Color c = Color.rgb(
49                             Integer.parseInt(split[1]),
50                             Integer.parseInt(split[2]),
51                             Integer.parseInt(split[3])
52                     );
53                     cakelight.setMode(new SingleColorMode(c));
54                     System.out.println("setting color to " + c);
55                 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
56                     String[] split = input.split("\\s+");
57                     config.gamma = Double.parseDouble(split[1]);
58                     Color.calculateGammaCorrection(config.gamma);
59                     System.out.println("setting gamma to " + config.gamma);
60                 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
61                     String[] split = input.split("\\s+");
62                     config.video.saturation = Double.parseDouble(split[1]);
63                     System.out.println("setting saturation to " + config.video.saturation);
64                 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
65                     TwoColorNoiseMode.getCommand().activate(cakelight, config, input.split("\\s+"));
66                     System.out.println("setting two-color noise mode");
67                 }
68             } catch (IOException e) {
69                 System.out.println("Error reading from command line");
70                 break;
71             }
72         }
73     }
74
75     public interface Command {
76         String[] getNames();
77         void activate(CakeLight cakelight, Configuration config, String[] args);
78     }
79 }