1 package kaka.cakelight;
3 import kaka.cakelight.mode.AmbientMode;
4 import kaka.cakelight.mode.SingleColorMode;
5 import kaka.cakelight.mode.SunriseMode;
6 import kaka.cakelight.mode.TwoColorNoiseMode;
7 import kaka.cakelight.mode.VideoMode;
9 import java.util.function.BiFunction;
12 private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
13 return new Console.Command() {
15 public String[] getNames() {
20 public void activate(Console console, String[] args) {
21 if (!activate.apply(console, args)) {
22 console.out("did NOT run command");
28 static Console.Command help() {
29 return command(new String[] {"?", "h", "help"}, (console, args) -> {
30 for (Console.Command c : console.getCommands()) {
31 System.out.println(String.join("|", c.getNames()));
37 static Console.Command quit() {
38 return command(new String[] {"q", "quit"}, (console, args) -> {
40 console.out("terminating");
45 static Console.Command video() {
46 return command(new String[] {"v", "video"}, (console, args) -> {
47 console.getCakelight().setMode(new VideoMode());
52 static Console.Command color() {
53 return command(new String[] {"c", "col", "color"}, (console, args) -> {
55 if (args.length == 1) {
56 c = console.parseColor(args[0]);
57 } else if (args.length == 3) {
59 Integer.parseInt(args[0]),
60 Integer.parseInt(args[1]),
61 Integer.parseInt(args[2])
65 console.getCakelight().setMode(new SingleColorMode(c));
66 console.out("setting color to " + c);
74 static Console.Command brightness() {
75 return command(new String[] {"b", "brightness"}, (console, args) -> {
76 if (args.length == 1) {
77 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
78 if (args[0].startsWith("+")) {
79 b = Math.min(console.getConfig().leds.brightness + b, 31);
80 } else if (args[0].startsWith("-")) {
81 b = Math.max(console.getConfig().leds.brightness - b, 0);
83 console.getConfig().leds.brightness = b;
84 console.out("setting brightness to " + b);
92 static Console.Command gamma() {
93 return command(new String[] {"g", "gamma"}, (console, args) -> {
94 if (args.length == 1) {
95 double g = Double.parseDouble(args[0]);
96 console.getConfig().gamma = g;
97 Color.calculateGammaCorrection(g);
98 console.out("setting gamma to " + g);
106 static Console.Command saturation() {
107 return command(new String[] {"s", "saturation"}, (console, args) -> {
108 if (args.length == 1) {
109 double s = Double.parseDouble(args[0]);
110 console.getConfig().video.saturation = s;
111 console.out("setting saturation to " + s);
119 static Console.Command ambientMode() {
120 return command(new String[] {"m", "mode"}, (console, args) -> {
121 if (args.length == 1) {
122 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
123 console.out("setting ambient mode to " + args[0]);
131 static Console.Command twoColorNoiseMode() {
132 return command(new String[] {"n", "noise"}, (console, args) -> {
133 if (args.length == 2) {
134 console.getCakelight().setMode(new TwoColorNoiseMode(
135 console.parseColor(args[0]),
136 console.parseColor(args[1])
138 console.out("setting two-color noise mode");
146 static Console.Command sunriseMode() {
147 return command(new String[] {"sunrise"}, (console, args) -> {
148 if (args.length == 1) {
149 int durationSeconds = Integer.parseInt(args[0]);
150 console.getCakelight().setMode(new SunriseMode(durationSeconds));
151 console.out("setting sunrise mode with duration " + durationSeconds);