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 quit() {
29 return command(new String[] {"q", "quit"}, (console, args) -> {
30 console.getCakelight().turnOff();
31 console.out("stopping cakelight");
36 static Console.Command video() {
37 return command(new String[] {"v", "video"}, (console, args) -> {
38 console.getCakelight().setMode(new VideoMode());
43 static Console.Command color() {
44 return command(new String[] {"c", "col", "color"}, (console, args) -> {
46 if (args.length == 1) {
47 c = console.parseColor(args[0]);
48 } else if (args.length == 3) {
50 Integer.parseInt(args[0]),
51 Integer.parseInt(args[1]),
52 Integer.parseInt(args[2])
56 console.getCakelight().setMode(new SingleColorMode(c));
57 console.out("setting color to " + c);
65 static Console.Command brightness() {
66 return command(new String[] {"b", "brightness"}, (console, args) -> {
67 if (args.length == 1) {
68 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
69 if (args[0].startsWith("+")) {
70 b = Math.min(console.getConfig().leds.brightness + b, 31);
71 } else if (args[0].startsWith("-")) {
72 b = Math.max(console.getConfig().leds.brightness - b, 0);
74 console.getConfig().leds.brightness = b;
75 console.out("setting brightness to " + b);
83 static Console.Command gamma() {
84 return command(new String[] {"g", "gamma"}, (console, args) -> {
85 if (args.length == 1) {
86 double g = Double.parseDouble(args[0]);
87 console.getConfig().gamma = g;
88 Color.calculateGammaCorrection(g);
89 console.out("setting gamma to " + g);
97 static Console.Command saturation() {
98 return command(new String[] {"s", "saturation"}, (console, args) -> {
99 if (args.length == 1) {
100 double s = Double.parseDouble(args[0]);
101 console.getConfig().video.saturation = s;
102 console.out("setting saturation to " + s);
110 static Console.Command ambientMode() {
111 return command(new String[] {"m", "mode"}, (console, args) -> {
112 if (args.length == 1) {
113 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
114 console.out("setting ambient mode to " + args[0]);
122 static Console.Command twoColorNoiseMode() {
123 return command(new String[] {"n", "noise"}, (console, args) -> {
124 if (args.length == 2) {
125 console.getCakelight().setMode(new TwoColorNoiseMode(
126 console.parseColor(args[0]),
127 console.parseColor(args[1])
129 console.out("setting two-color noise mode");
137 static Console.Command sunriseMode() {
138 return command(new String[] {"sunrise"}, (console, args) -> {
139 if (args.length == 1) {
140 int durationSeconds = Integer.parseInt(args[0]);
141 console.getCakelight().setMode(new SunriseMode(durationSeconds));
142 console.out("setting sunrise mode with duration " + durationSeconds);