Register and use commands
[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;
35990bbd 11import java.util.*;
cd28f68c
TW
12
13public class Console extends Thread {
14 private CakeLight cakelight;
15 private Configuration config;
16 private BufferedReader reader;
35990bbd
TW
17 private Map<String, Command> commands = new HashMap<>();
18 private List<Command> commandList = new ArrayList<>();
cd28f68c
TW
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));
35990bbd 28 register(TwoColorNoiseMode.getCommand());
fa013e4b
TW
29
30 public CakeLight getCakelight() {
31 return cakelight;
32 }
33
34 public Configuration getConfig() {
35 return config;
36 }
35990bbd
TW
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);
cd28f68c
TW
47 }
48
49 @Override
50 public void run() {
51 while (true) {
52 System.out.print("> ");
53 try {
54 String input = reader.readLine();
35990bbd
TW
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
c8657a8d 67 if (input.matches("[0-5]")) {
cd28f68c
TW
68 cakelight.setMode(new AmbientMode(new String[] {input}));
69 System.out.println("setting ambient mode to " + input);
0b5bef0c
TW
70 } else if (input.matches("v|video")) {
71 cakelight.setMode(new VideoMode());
cd28f68c
TW
72 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
73 String[] split = input.split("\\s+");
74 config.leds.brightness = Integer.parseInt(split[1]);
8a0c98f8 75 System.out.println("setting brightness to " + config.leds.brightness);
cd28f68c 76 } else if (input.matches("q|quit")) {
a276d5ab 77 cakelight.turnOff();
cd28f68c
TW
78 System.out.println("stopping cakelight");
79 break;
88438046
TW
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);
6b446d77 89 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
8a0c98f8
TW
90 String[] split = input.split("\\s+");
91 config.gamma = Double.parseDouble(split[1]);
b1899e91 92 Color.calculateGammaCorrection(config.gamma);
8a0c98f8 93 System.out.println("setting gamma to " + config.gamma);
6b446d77 94 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
53b74d64
TW
95 String[] split = input.split("\\s+");
96 config.video.saturation = Double.parseDouble(split[1]);
97 System.out.println("setting saturation to " + config.video.saturation);
e2721d51 98 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
fa013e4b 99 TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
e2721d51 100 System.out.println("setting two-color noise mode");
cd28f68c
TW
101 }
102 } catch (IOException e) {
103 System.out.println("Error reading from command line");
104 break;
105 }
106 }
107 }
eca6fd31
TW
108
109 public interface Command {
110 String[] getNames();
fa013e4b 111 void activate(Console console, String[] args);
baaaa10b
TW
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 }
35990bbd
TW
139
140 default void output(String text) {
141 System.out.println("(" + text + ")");
142 }
eca6fd31 143 }
cd28f68c 144}