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 Object activate(Console console, String[] args) {
17 if (!activate.apply(console, args)) {
18 console.out("did NOT run command");
25 private static Console.Command modeCommand(String[] names, BiFunction<Console, String[], Mode> activate) {
26 return new Console.Command() {
28 public String[] getNames() {
33 public Object activate(Console console, String[] args) {
34 Mode mode = activate.apply(console, args);
36 console.out("did NOT run command");
43 static Console.Command help() {
44 return command(new String[] {"?", "h", "help"}, (console, args) -> {
45 for (Console.Command c : console.getCommands()) {
46 System.out.println(String.join("|", c.getNames()));
52 static Console.Command quit() {
53 return command(new String[] {"q", "quit"}, (console, args) -> {
55 console.out("terminating");
60 static Console.Command push() {
61 return command(new String[] {"push"}, (console, args) -> {
62 Object obj = console.internalHandleInput(String.join(" ", args));
63 if (obj instanceof Mode) { // obj could be anything, which should be fixed
64 console.out("pushing mode " + obj.getClass().getSimpleName());
65 console.getCakelight().pushMode((Mode) obj);
71 static Console.Command pop() {
72 return command(new String[] {"pop"}, (console, args) -> {
73 console.out("popping mode " + console.getCakelight().popMode().getClass().getSimpleName());
78 static Console.Command video() {
79 return modeCommand(new String[] {"v", "video"}, (console, args) -> new VideoMode());
82 static Console.Command color() {
83 return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> {
85 if (args.length == 1) {
86 c = console.parseColor(args[0]);
87 } else if (args.length == 3) {
89 Integer.parseInt(args[0]),
90 Integer.parseInt(args[1]),
91 Integer.parseInt(args[2])
95 console.out("setting color to " + c);
96 return new SingleColorMode(c);
102 static Console.Command brightness() {
103 return command(new String[] {"b", "brightness"}, (console, args) -> {
104 if (args.length == 1) {
105 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
106 if (args[0].startsWith("+")) {
107 b = Math.min(console.getConfig().leds.brightness + b, 31);
108 } else if (args[0].startsWith("-")) {
109 b = Math.max(console.getConfig().leds.brightness - b, 0);
111 console.getConfig().leds.brightness = b;
112 console.out("setting brightness to " + b);
120 static Console.Command gamma() {
121 return command(new String[] {"g", "gamma"}, (console, args) -> {
122 if (args.length == 1) {
123 double g = Double.parseDouble(args[0]);
124 console.getConfig().gamma = g;
125 Color.calculateGammaCorrection(g);
126 console.out("setting gamma to " + g);
134 static Console.Command saturation() {
135 return command(new String[] {"s", "saturation"}, (console, args) -> {
136 if (args.length == 1) {
137 double s = Double.parseDouble(args[0]);
138 console.getConfig().video.saturation = s;
139 console.out("setting saturation to " + s);
147 static Console.Command ambientMode() {
148 return modeCommand(new String[] {"m", "mode"}, (console, args) -> {
149 if (args.length == 1) {
150 console.out("setting ambient mode to " + args[0]);
151 return new AmbientMode(new String[]{args[0]});
157 static Console.Command twoColorNoiseMode() {
158 return modeCommand(new String[] {"n", "noise"}, (console, args) -> {
159 if (args.length == 2) {
160 console.out("setting two-color noise mode");
161 return new TwoColorNoiseMode(
162 console.parseColor(args[0]),
163 console.parseColor(args[1])
170 static Console.Command sunriseMode() {
171 return modeCommand(new String[] {"sunrise"}, (console, args) -> {
172 if (args.length == 1) {
173 int durationSeconds = Integer.parseInt(args[0]);
174 console.out("setting sunrise mode with duration " + durationSeconds);
175 return new SunriseMode(durationSeconds);