Bugfix - match arguments
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
CommitLineData
cd28f68c
TW
1package kaka.cakelight;
2
67b0a758
TW
3import kaka.cakelight.mode.AmbientMode;
4import kaka.cakelight.mode.SingleColorMode;
eca6fd31 5import kaka.cakelight.mode.TwoColorNoiseMode;
67b0a758
TW
6import kaka.cakelight.mode.VideoMode;
7
cd28f68c
TW
8import java.io.BufferedReader;
9import java.io.IOException;
10import java.io.InputStreamReader;
11
12public 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();
c8657a8d 33 if (input.matches("[0-5]")) {
cd28f68c
TW
34 cakelight.setMode(new AmbientMode(new String[] {input}));
35 System.out.println("setting ambient mode to " + input);
0b5bef0c
TW
36 } else if (input.matches("v|video")) {
37 cakelight.setMode(new VideoMode());
cd28f68c
TW
38 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
39 String[] split = input.split("\\s+");
40 config.leds.brightness = Integer.parseInt(split[1]);
8a0c98f8 41 System.out.println("setting brightness to " + config.leds.brightness);
cd28f68c 42 } else if (input.matches("q|quit")) {
a276d5ab 43 cakelight.turnOff();
cd28f68c
TW
44 System.out.println("stopping cakelight");
45 break;
88438046
TW
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);
6b446d77 55 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
8a0c98f8
TW
56 String[] split = input.split("\\s+");
57 config.gamma = Double.parseDouble(split[1]);
b1899e91 58 Color.calculateGammaCorrection(config.gamma);
8a0c98f8 59 System.out.println("setting gamma to " + config.gamma);
6b446d77 60 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
53b74d64
TW
61 String[] split = input.split("\\s+");
62 config.video.saturation = Double.parseDouble(split[1]);
63 System.out.println("setting saturation to " + config.video.saturation);
e2721d51 64 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
eca6fd31 65 TwoColorNoiseMode.getCommand().activate(cakelight, config, input.split("\\s+"));
e2721d51 66 System.out.println("setting two-color noise mode");
cd28f68c
TW
67 }
68 } catch (IOException e) {
69 System.out.println("Error reading from command line");
70 break;
71 }
72 }
73 }
eca6fd31
TW
74
75 public interface Command {
76 String[] getNames();
77 void activate(CakeLight cakelight, Configuration config, String[] args);
78 }
cd28f68c 79}