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(TwoColorNoiseMode.getCommand());
30 public CakeLight getCakelight() {
34 public Configuration getConfig() {
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() + "'!");
44 commands.put(name, cmd);
52 System.out.print("> ");
54 String input = reader.readLine();
55 String[] splitInput = input.split("\\s+", 2);
56 String name = splitInput[0];
57 String[] args = splitInput.length == 2
58 ? splitInput[1].split("\\s+")
61 Command cmd = commands.get(name);
63 cmd.activate(this, args);
67 if (input.matches("[0-5]")) {
68 cakelight.setMode(new AmbientMode(new String[] {input}));
69 System.out.println("setting ambient mode to " + input);
70 } else if (input.matches("v|video")) {
71 cakelight.setMode(new VideoMode());
72 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
73 String[] split = input.split("\\s+");
74 config.leds.brightness = Integer.parseInt(split[1]);
75 System.out.println("setting brightness to " + config.leds.brightness);
76 } else if (input.matches("q|quit")) {
78 System.out.println("stopping cakelight");
80 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
81 String[] split = input.split("\\s+");
83 Integer.parseInt(split[1]),
84 Integer.parseInt(split[2]),
85 Integer.parseInt(split[3])
87 cakelight.setMode(new SingleColorMode(c));
88 System.out.println("setting color to " + c);
89 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
90 String[] split = input.split("\\s+");
91 config.gamma = Double.parseDouble(split[1]);
92 Color.calculateGammaCorrection(config.gamma);
93 System.out.println("setting gamma to " + config.gamma);
94 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
95 String[] split = input.split("\\s+");
96 config.video.saturation = Double.parseDouble(split[1]);
97 System.out.println("setting saturation to " + config.video.saturation);
98 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
99 TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
100 System.out.println("setting two-color noise mode");
102 } catch (IOException e) {
103 System.out.println("Error reading from command line");
109 public interface Command {
111 void activate(Console console, String[] args);
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("#")) {
122 if (s.length() == 3) {
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)
128 } else if (s.length() == 6) {
130 Integer.parseInt(s.substring(0, 2), 16),
131 Integer.parseInt(s.substring(2, 4), 16),
132 Integer.parseInt(s.substring(4, 6), 16)
136 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
140 default void output(String text) {
141 System.out.println("(" + text + ")");