1 package kaka.cakelight;
3 import kaka.cakelight.mode.*;
5 import java.util.function.BiFunction;
6 import java.util.stream.Stream;
9 private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
10 return new Console.Command() {
12 public String[] getNames() {
17 public Object activate(Console console, String[] args) {
18 if (!activate.apply(console, args)) {
19 console.out("did NOT run command");
26 private static Console.Command modeCommand(String[] names, BiFunction<Console, String[], Mode> activate) {
27 return new Console.Command() {
29 public String[] getNames() {
34 public Object activate(Console console, String[] args) {
35 Mode mode = activate.apply(console, args);
37 console.out("did NOT run command");
44 static Console.Command help() {
45 return command(new String[] {"?", "h", "help"}, (console, args) -> {
46 for (Console.Command c : console.getCommands()) {
47 System.out.println(String.join("|", c.getNames()));
53 static Console.Command quit() {
54 return command(new String[] {"q", "quit"}, (console, args) -> {
56 console.out("terminating");
61 static Console.Command push() {
62 return command(new String[] {"push"}, (console, args) -> {
63 Object obj = console.internalHandleInput(String.join(" ", args));
64 if (obj instanceof Mode) { // obj could be anything, which should be fixed
65 console.out("pushing mode " + obj.getClass().getSimpleName());
66 console.getCakelight().pushMode((Mode) obj);
72 static Console.Command pop() {
73 return command(new String[] {"pop"}, (console, args) -> {
74 console.out("popping mode " + console.getCakelight().popMode().getClass().getSimpleName());
79 static Console.Command video() {
80 return modeCommand(new String[] {"v", "video"}, (console, args) -> new VideoMode());
83 static Console.Command color() {
84 return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> {
86 if (args.length == 1) {
87 c = console.parseColor(args[0]);
88 } else if (args.length == 3) {
90 Integer.parseInt(args[0]),
91 Integer.parseInt(args[1]),
92 Integer.parseInt(args[2])
96 console.out("setting color to " + c);
97 return new SingleColorMode(c);
103 static Console.Command brightness() {
104 return command(new String[] {"b", "brightness"}, (console, args) -> {
105 if (args.length == 1) {
106 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
107 if (args[0].startsWith("+")) {
108 b = Math.min(console.getConfig().leds.brightness + b, 31);
109 } else if (args[0].startsWith("-")) {
110 b = Math.max(console.getConfig().leds.brightness - b, 0);
112 console.getConfig().leds.brightness = b;
113 console.out("setting brightness to " + b);
121 static Console.Command gamma() {
122 return command(new String[] {"g", "gamma"}, (console, args) -> {
123 if (args.length == 1) {
124 double g = Double.parseDouble(args[0]);
125 console.getConfig().gamma = g;
126 Color.calculateGammaCorrection(g);
127 console.out("setting gamma to " + g);
135 static Console.Command saturation() {
136 return command(new String[] {"s", "saturation"}, (console, args) -> {
137 if (args.length == 1) {
138 double s = Double.parseDouble(args[0]);
139 console.getConfig().video.saturation = s;
140 console.out("setting saturation to " + s);
148 static Console.Command ambientMode() {
149 return modeCommand(new String[] {"m", "mode"}, (console, args) -> {
150 if (args.length == 1) {
151 console.out("setting ambient mode to " + args[0]);
152 return new AmbientMode(new String[]{args[0]});
158 static Console.Command noiseMode() {
159 return modeCommand(new String[] {"n", "noise"}, (console, args) -> {
160 if (args.length > 1) {
161 console.out("setting multi-color noise mode");
162 return new NoiseMode(Stream.of(args)
163 .map(console::parseColor)
164 .toArray(Color[]::new)
171 static Console.Command fireMode() {
172 return modeCommand(new String[] {"f", "fire"}, (console, args) -> {
173 if (args.length > 1) {
174 console.out("setting multi-color fire mode");
175 return new FireMode(Stream.of(args)
176 .map(console::parseColor)
177 .toArray(Color[]::new)
184 static Console.Command sunriseMode() {
185 return modeCommand(new String[] {"sunrise"}, (console, args) -> {
186 if (args.length == 1) {
187 int durationSeconds = Integer.parseInt(args[0]);
188 console.out("setting sunrise mode with duration " + durationSeconds);
189 return new SunriseMode(durationSeconds);