1 package kaka.cakelight;
3 import kaka.cakelight.mode.AmbientMode;
4 import kaka.cakelight.mode.SingleColorMode;
5 import kaka.cakelight.mode.TwoColorNoiseMode;
6 import kaka.cakelight.mode.VideoMode;
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
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<>();
20 public static void start(CakeLight cakelight, Configuration config) {
21 new Console(cakelight, config).start();
24 private Console(CakeLight cakelight, Configuration config) {
25 this.cakelight = cakelight;
27 reader = new BufferedReader(new InputStreamReader(System.in));
28 register(new HelpCommand());
29 register(TwoColorNoiseMode.getCommand());
32 public CakeLight getCakelight() {
36 public Configuration getConfig() {
40 private class HelpCommand implements Command {
42 public String[] getNames() {
43 return new String[] {"?", "h", "help"};
47 public void activate(Console console, String[] args) {
48 for (Command c : commandList) {
49 System.out.println(String.join("|", c.getNames()));
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() + "'!");
60 commands.put(name, cmd);
68 System.out.print("> ");
70 String input = reader.readLine();
71 String[] splitInput = input.split("\\s+", 2);
72 String name = splitInput[0];
73 String[] args = splitInput.length == 2
74 ? splitInput[1].split("\\s+")
77 Command cmd = commands.get(name);
79 cmd.activate(this, args);
83 if (input.matches("[0-5]")) {
84 cakelight.setMode(new AmbientMode(new String[] {input}));
85 System.out.println("setting ambient mode to " + input);
86 } else if (input.matches("v|video")) {
87 cakelight.setMode(new VideoMode());
88 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
89 String[] split = input.split("\\s+");
90 config.leds.brightness = Integer.parseInt(split[1]);
91 System.out.println("setting brightness to " + config.leds.brightness);
92 } else if (input.matches("q|quit")) {
94 System.out.println("stopping cakelight");
96 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
97 String[] split = input.split("\\s+");
99 Integer.parseInt(split[1]),
100 Integer.parseInt(split[2]),
101 Integer.parseInt(split[3])
103 cakelight.setMode(new SingleColorMode(c));
104 System.out.println("setting color to " + c);
105 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
106 String[] split = input.split("\\s+");
107 config.gamma = Double.parseDouble(split[1]);
108 Color.calculateGammaCorrection(config.gamma);
109 System.out.println("setting gamma to " + config.gamma);
110 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
111 String[] split = input.split("\\s+");
112 config.video.saturation = Double.parseDouble(split[1]);
113 System.out.println("setting saturation to " + config.video.saturation);
114 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
115 TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
116 System.out.println("setting two-color noise mode");
118 } catch (IOException e) {
119 System.out.println("Error reading from command line");
125 public interface Command {
127 void activate(Console console, String[] args);
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("#")) {
138 if (s.length() == 3) {
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)
144 } else if (s.length() == 6) {
146 Integer.parseInt(s.substring(0, 2), 16),
147 Integer.parseInt(s.substring(2, 4), 16),
148 Integer.parseInt(s.substring(4, 6), 16)
152 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
156 default void output(String text) {
157 System.out.println("(" + text + ")");