Help command for listing all 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));
be3f2496 28 register(new HelpCommand());
35990bbd 29 register(TwoColorNoiseMode.getCommand());
be3f2496 30 }
fa013e4b
TW
31
32 public CakeLight getCakelight() {
33 return cakelight;
34 }
35
36 public Configuration getConfig() {
37 return config;
38 }
35990bbd 39
be3f2496
TW
40 private class HelpCommand implements Command {
41 @Override
42 public String[] getNames() {
43 return new String[] {"?", "h", "help"};
44 }
45
46 @Override
47 public void activate(Console console, String[] args) {
48 for (Command c : commandList) {
49 System.out.println(String.join("|", c.getNames()));
50 }
51 }
52 }
53
35990bbd
TW
54 private void register(Command cmd) {
55 for (String name : cmd.getNames()){
56 if (commands.containsKey(name)) {
57 System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
58 System.exit(1);
59 }
60 commands.put(name, cmd);
61 }
62 commandList.add(cmd);
cd28f68c
TW
63 }
64
65 @Override
66 public void run() {
67 while (true) {
68 System.out.print("> ");
69 try {
70 String input = reader.readLine();
35990bbd
TW
71 String[] splitInput = input.split("\\s+", 2);
72 String name = splitInput[0];
73 String[] args = splitInput.length == 2
74 ? splitInput[1].split("\\s+")
75 : new String[]{};
76
77 Command cmd = commands.get(name);
78 if (cmd != null) {
79 cmd.activate(this, args);
80 continue;
81 }
82
c8657a8d 83 if (input.matches("[0-5]")) {
cd28f68c
TW
84 cakelight.setMode(new AmbientMode(new String[] {input}));
85 System.out.println("setting ambient mode to " + input);
0b5bef0c
TW
86 } else if (input.matches("v|video")) {
87 cakelight.setMode(new VideoMode());
cd28f68c
TW
88 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
89 String[] split = input.split("\\s+");
90 config.leds.brightness = Integer.parseInt(split[1]);
8a0c98f8 91 System.out.println("setting brightness to " + config.leds.brightness);
cd28f68c 92 } else if (input.matches("q|quit")) {
a276d5ab 93 cakelight.turnOff();
cd28f68c
TW
94 System.out.println("stopping cakelight");
95 break;
88438046
TW
96 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
97 String[] split = input.split("\\s+");
98 Color c = Color.rgb(
99 Integer.parseInt(split[1]),
100 Integer.parseInt(split[2]),
101 Integer.parseInt(split[3])
102 );
103 cakelight.setMode(new SingleColorMode(c));
104 System.out.println("setting color to " + c);
6b446d77 105 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
8a0c98f8
TW
106 String[] split = input.split("\\s+");
107 config.gamma = Double.parseDouble(split[1]);
b1899e91 108 Color.calculateGammaCorrection(config.gamma);
8a0c98f8 109 System.out.println("setting gamma to " + config.gamma);
6b446d77 110 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
53b74d64
TW
111 String[] split = input.split("\\s+");
112 config.video.saturation = Double.parseDouble(split[1]);
113 System.out.println("setting saturation to " + config.video.saturation);
e2721d51 114 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
fa013e4b 115 TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
e2721d51 116 System.out.println("setting two-color noise mode");
cd28f68c
TW
117 }
118 } catch (IOException e) {
119 System.out.println("Error reading from command line");
120 break;
121 }
122 }
123 }
eca6fd31
TW
124
125 public interface Command {
126 String[] getNames();
fa013e4b 127 void activate(Console console, String[] args);
baaaa10b
TW
128
129 default Color parseColor(String s) {
130 switch (s.toLowerCase()) {
131 case "r": return Color.rgb(255, 0, 0);
132 case "g": return Color.rgb(0, 255, 0);
133 case "b": return Color.rgb(0, 0, 255);
134 default: // assume hexadecimal
135 if (s.startsWith("#")) {
136 s = s.substring(1);
137 }
138 if (s.length() == 3) {
139 return Color.rgb(
140 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
141 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
142 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
143 );
144 } else if (s.length() == 6) {
145 return Color.rgb(
146 Integer.parseInt(s.substring(0, 2), 16),
147 Integer.parseInt(s.substring(2, 4), 16),
148 Integer.parseInt(s.substring(4, 6), 16)
149 );
150 }
151 }
152 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
153 return Color.BLACK;
154 }
35990bbd
TW
155
156 default void output(String text) {
157 System.out.println("(" + text + ")");
158 }
eca6fd31 159 }
cd28f68c 160}