1 package kaka.cakelight;
3 import kaka.cakelight.mode.*;
5 import java.util.function.BiFunction;
8 private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
9 return new Console.Command() {
11 public String[] getNames() {
16 public void activate(Console console, String[] args) {
17 if (!activate.apply(console, args)) {
18 console.out("did NOT run command");
24 static Console.Command quit() {
25 return command(new String[] {"q", "quit"}, (console, args) -> {
26 console.getCakelight().turnOff();
27 console.out("stopping cakelight");
32 static Console.Command video() {
33 return command(new String[] {"v", "video"}, (console, args) -> {
34 console.getCakelight().setMode(new VideoMode());
39 static Console.Command color() {
40 return command(new String[] {"c", "col", "color"}, (console, args) -> {
42 if (args.length == 1) {
43 c = console.parseColor(args[0]);
44 } else if (args.length == 3) {
46 Integer.parseInt(args[0]),
47 Integer.parseInt(args[1]),
48 Integer.parseInt(args[2])
52 console.getCakelight().setMode(new SingleColorMode(c));
53 console.out("setting color to " + c);
61 static Console.Command brightness() {
62 return command(new String[] {"b", "brightness"}, (console, args) -> {
63 if (args.length == 1) {
64 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
65 if (args[0].startsWith("+")) {
66 b = Math.min(console.getConfig().leds.brightness + b, 31);
67 } else if (args[0].startsWith("-")) {
68 b = Math.max(console.getConfig().leds.brightness - b, 0);
70 console.getConfig().leds.brightness = b;
71 console.out("setting brightness to " + b);
79 static Console.Command gamma() {
80 return command(new String[] {"g", "gamma"}, (console, args) -> {
81 if (args.length == 1) {
82 double g = Double.parseDouble(args[0]);
83 console.getConfig().gamma = g;
84 Color.calculateGammaCorrection(g);
85 console.out("setting gamma to " + g);
93 static Console.Command saturation() {
94 return command(new String[] {"s", "saturation"}, (console, args) -> {
95 if (args.length == 1) {
96 double s = Double.parseDouble(args[0]);
97 console.getConfig().video.saturation = s;
98 console.out("setting saturation to " + s);
106 static Console.Command ambientMode() {
107 return command(new String[] {"m", "mode"}, (console, args) -> {
108 if (args.length == 1) {
109 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
110 console.out("setting ambient mode to " + args[0]);
118 static Console.Command twoColorNoiseMode() {
119 return command(new String[] {"n", "noise"}, (console, args) -> {
120 if (args.length == 2) {
121 console.getCakelight().setMode(new TwoColorNoiseMode(
122 console.parseColor(args[0]),
123 console.parseColor(args[1])
125 console.out("setting two-color noise mode");
133 static Console.Command sunriseMode() {
134 return command(new String[] {"sunrise"}, (console, args) -> {
135 if (args.length == 1) {
136 int durationSeconds = Integer.parseInt(args[0]);
137 console.getCakelight().setMode(new SunriseMode(durationSeconds));
138 console.out("setting sunrise mode with duration " + durationSeconds);