From: Tomas Wenström Date: Mon, 25 Nov 2019 22:33:48 +0000 (+0100) Subject: Replace old command parsing with new X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=2b49e4e255f8591fb730f88e71b6b006008ccd3d;p=kaka%2Fcakelight.git Replace old command parsing with new --- diff --git a/src/kaka/cakelight/Commands.java b/src/kaka/cakelight/Commands.java new file mode 100644 index 0000000..75b1b10 --- /dev/null +++ b/src/kaka/cakelight/Commands.java @@ -0,0 +1,130 @@ +package kaka.cakelight; + +import kaka.cakelight.mode.AmbientMode; +import kaka.cakelight.mode.SingleColorMode; +import kaka.cakelight.mode.TwoColorNoiseMode; +import kaka.cakelight.mode.VideoMode; + +import java.util.function.BiFunction; + +class Commands { + private static Console.Command command(String[] names, BiFunction activate) { + return new Console.Command() { + @Override + public String[] getNames() { + return names; + } + + @Override + public void activate(Console console, String[] args) { + if (!activate.apply(console, args)) { + console.out("did NOT run command"); + } + } + }; + } + + static Console.Command quit() { + return command(new String[] {"q", "quit"}, (console, args) -> { + console.getCakelight().turnOff(); + console.out("stopping cakelight"); + return true; + }); + } + + static Console.Command video() { + return command(new String[] {"v", "video"}, (console, args) -> { + console.getCakelight().setMode(new VideoMode()); + return true; + }); + } + + static Console.Command color() { + return command(new String[] {"c", "col", "color"}, (console, args) -> { + Color c = null; + if (args.length == 1) { + c = console.parseColor(args[0]); + } else if (args.length == 3) { + c = Color.rgb( + Integer.parseInt(args[0]), + Integer.parseInt(args[1]), + Integer.parseInt(args[2]) + ); + } + if (c != null) { + console.getCakelight().setMode(new SingleColorMode(c)); + console.out("setting color to " + c); + return true; + } else { + return false; + } + }); + } + + static Console.Command brightness() { + return command(new String[] {"b", "brightness"}, (console, args) -> { + if (args.length == 1) { + int b = Integer.parseInt(args[0]); + console.getConfig().leds.brightness = b; + console.out("setting brightness to " + b); + return true; + } else { + return false; + } + }); + } + + static Console.Command gamma() { + return command(new String[] {"g", "gamma"}, (console, args) -> { + if (args.length == 1) { + double g = Double.parseDouble(args[0]); + console.getConfig().gamma = g; + Color.calculateGammaCorrection(g); + console.out("setting gamma to " + g); + return true; + } else { + return false; + } + }); + } + + static Console.Command saturation() { + return command(new String[] {"s", "saturation"}, (console, args) -> { + if (args.length == 1) { + double s = Double.parseDouble(args[0]); + console.getConfig().video.saturation = s; + console.out("setting saturation to " + s); + return true; + } else { + return false; + } + }); + } + + static Console.Command ambientMode() { + return command(new String[] {"m", "mode"}, (console, args) -> { + if (args.length == 1) { + console.getCakelight().setMode(new AmbientMode(new String[] {args[0]})); + console.out("setting ambient mode to " + args[0]); + return true; + } else { + return false; + } + }); + } + + static Console.Command twoColorNoiseMode() { + return command(new String[] {"n", "noise"}, (console, args) -> { + if (args.length == 2) { + console.getCakelight().setMode(new TwoColorNoiseMode( + console.parseColor(args[0]), + console.parseColor(args[1]) + )); + console.out("setting two-color noise mode"); + return true; + } else { + return false; + } + }); + } +} diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index 76bd6e5..cf72cce 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -1,14 +1,12 @@ package kaka.cakelight; -import kaka.cakelight.mode.AmbientMode; -import kaka.cakelight.mode.SingleColorMode; -import kaka.cakelight.mode.TwoColorNoiseMode; -import kaka.cakelight.mode.VideoMode; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class Console extends Thread { private CakeLight cakelight; @@ -26,7 +24,14 @@ public class Console extends Thread { this.config = config; reader = new BufferedReader(new InputStreamReader(System.in)); register(new HelpCommand()); - register(TwoColorNoiseMode.getCommand()); + register(Commands.quit()); + register(Commands.video()); + register(Commands.color()); + register(Commands.brightness()); + register(Commands.gamma()); + register(Commands.saturation()); + register(Commands.ambientMode()); + register(Commands.twoColorNoiseMode()); } public CakeLight getCakelight() { @@ -77,43 +82,8 @@ public class Console extends Thread { Command cmd = commands.get(name); if (cmd != null) { cmd.activate(this, args); - continue; - } - - if (input.matches("[0-5]")) { - cakelight.setMode(new AmbientMode(new String[] {input})); - System.out.println("setting ambient mode to " + input); - } else if (input.matches("v|video")) { - cakelight.setMode(new VideoMode()); - } else if (input.matches("(b|brightness)\\s+[0-9]+")) { - String[] split = input.split("\\s+"); - config.leds.brightness = Integer.parseInt(split[1]); - System.out.println("setting brightness to " + config.leds.brightness); - } else if (input.matches("q|quit")) { - cakelight.turnOff(); - System.out.println("stopping cakelight"); - break; - } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) { - String[] split = input.split("\\s+"); - Color c = Color.rgb( - Integer.parseInt(split[1]), - Integer.parseInt(split[2]), - Integer.parseInt(split[3]) - ); - cakelight.setMode(new SingleColorMode(c)); - System.out.println("setting color to " + c); - } else if (input.matches("(g|gamma)\\s+[0-9.]+")) { - String[] split = input.split("\\s+"); - config.gamma = Double.parseDouble(split[1]); - Color.calculateGammaCorrection(config.gamma); - System.out.println("setting gamma to " + config.gamma); - } else if (input.matches("(s|saturation)\\s+[0-9.]+")) { - String[] split = input.split("\\s+"); - config.video.saturation = Double.parseDouble(split[1]); - System.out.println("setting saturation to " + config.video.saturation); - } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) { - TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+")); - System.out.println("setting two-color noise mode"); + } else { + out("no command named '" + name + "'"); } } catch (IOException e) { System.out.println("Error reading from command line"); @@ -122,39 +92,39 @@ public class Console extends Thread { } } - public interface Command { - String[] getNames(); - void activate(Console console, String[] args); + void out(String text) { + System.out.println("(" + text + ")"); + } - default Color parseColor(String s) { - switch (s.toLowerCase()) { - case "r": return Color.rgb(255, 0, 0); - case "g": return Color.rgb(0, 255, 0); - case "b": return Color.rgb(0, 0, 255); - default: // assume hexadecimal - if (s.startsWith("#")) { - s = s.substring(1); - } - if (s.length() == 3) { - return Color.rgb( - Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16), - Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16), - Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16) - ); - } else if (s.length() == 6) { - return Color.rgb( - Integer.parseInt(s.substring(0, 2), 16), - Integer.parseInt(s.substring(2, 4), 16), - Integer.parseInt(s.substring(4, 6), 16) - ); - } - } - System.out.println("Failed to parse color '" + s + "'. Using black instead."); - return Color.BLACK; + Color parseColor(String s) { + switch (s.toLowerCase()) { + case "r": return Color.rgb(255, 0, 0); + case "g": return Color.rgb(0, 255, 0); + case "b": return Color.rgb(0, 0, 255); + default: // assume hexadecimal + if (s.startsWith("#")) { + s = s.substring(1); + } + if (s.length() == 3) { + return Color.rgb( + Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16), + Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16), + Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16) + ); + } else if (s.length() == 6) { + return Color.rgb( + Integer.parseInt(s.substring(0, 2), 16), + Integer.parseInt(s.substring(2, 4), 16), + Integer.parseInt(s.substring(4, 6), 16) + ); + } } + System.out.println("Failed to parse color '" + s + "'. Using black instead."); + return Color.BLACK; + } - default void output(String text) { - System.out.println("(" + text + ")"); - } + public interface Command { + String[] getNames(); + void activate(Console console, String[] args); } } diff --git a/src/kaka/cakelight/mode/TwoColorNoiseMode.java b/src/kaka/cakelight/mode/TwoColorNoiseMode.java index 4046cbd..7e39705 100644 --- a/src/kaka/cakelight/mode/TwoColorNoiseMode.java +++ b/src/kaka/cakelight/mode/TwoColorNoiseMode.java @@ -1,7 +1,6 @@ package kaka.cakelight.mode; import kaka.cakelight.Color; -import kaka.cakelight.Console; import kaka.cakelight.LedFrame; import kaka.cakelight.util.SimplexNoise3D; @@ -9,26 +8,6 @@ public class TwoColorNoiseMode extends AmbientMode { private final Color primary, secondary; private SimplexNoise3D noise = new SimplexNoise3D(0); - public static Console.Command getCommand() { - return new Console.Command() { - @Override - public String[] getNames() { - return new String[] {"n", "noise"}; - } - - @Override - public void activate(Console console, String[] args) { - if (args.length == 2) { // col1 + col2 - console.getCakelight().setMode(new TwoColorNoiseMode( - parseColor(args[0]), - parseColor(args[1]) - )); - output("setting two-color noise mode"); - } - } - }; - } - public TwoColorNoiseMode(Color primary, Color secondary) { this.primary = primary; this.secondary = secondary;