Replace old command parsing with new
[kaka/cakelight.git] / src / kaka / cakelight / Commands.java
diff --git a/src/kaka/cakelight/Commands.java b/src/kaka/cakelight/Commands.java
new file mode 100644 (file)
index 0000000..75b1b10
--- /dev/null
@@ -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<Console, String[], Boolean> 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;
+           }
+       });
+    }
+}