Bugfix - let PipeController set modes
[kaka/cakelight.git] / src / kaka / cakelight / Commands.java
... / ...
CommitLineData
1package kaka.cakelight;
2
3import kaka.cakelight.mode.*;
4
5import java.util.function.BiFunction;
6
7class Commands {
8 private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
9 return new Console.Command() {
10 @Override
11 public String[] getNames() {
12 return names;
13 }
14
15 @Override
16 public Object activate(Console console, String[] args) {
17 if (!activate.apply(console, args)) {
18 console.out("did NOT run command");
19 }
20 return null;
21 }
22 };
23 }
24
25 private static Console.Command modeCommand(String[] names, BiFunction<Console, String[], Mode> activate) {
26 return new Console.Command() {
27 @Override
28 public String[] getNames() {
29 return names;
30 }
31
32 @Override
33 public Object activate(Console console, String[] args) {
34 Mode mode = activate.apply(console, args);
35 if (mode == null) {
36 console.out("did NOT run command");
37 }
38 return mode;
39 }
40 };
41 }
42
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()));
47 }
48 return true;
49 });
50 }
51
52 static Console.Command quit() {
53 return command(new String[] {"q", "quit"}, (console, args) -> {
54 console.quit();
55 console.out("terminating");
56 return true;
57 });
58 }
59
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);
66 }
67 return true;
68 });
69 }
70
71 static Console.Command pop() {
72 return command(new String[] {"pop"}, (console, args) -> {
73 console.out("popping mode " + console.getCakelight().popMode().getClass().getSimpleName());
74 return true;
75 });
76 }
77
78 static Console.Command video() {
79 return modeCommand(new String[] {"v", "video"}, (console, args) -> new VideoMode());
80 }
81
82 static Console.Command color() {
83 return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> {
84 Color c = null;
85 if (args.length == 1) {
86 c = console.parseColor(args[0]);
87 } else if (args.length == 3) {
88 c = Color.rgb(
89 Integer.parseInt(args[0]),
90 Integer.parseInt(args[1]),
91 Integer.parseInt(args[2])
92 );
93 }
94 if (c != null) {
95 console.out("setting color to " + c);
96 return new SingleColorMode(c);
97 }
98 return null;
99 });
100 }
101
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);
110 }
111 console.getConfig().leds.brightness = b;
112 console.out("setting brightness to " + b);
113 return true;
114 } else {
115 return false;
116 }
117 });
118 }
119
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);
127 return true;
128 } else {
129 return false;
130 }
131 });
132 }
133
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);
140 return true;
141 } else {
142 return false;
143 }
144 });
145 }
146
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]});
152 }
153 return null;
154 });
155 }
156
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])
164 );
165 }
166 return null;
167 });
168 }
169
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);
176 }
177 return null;
178 });
179 }
180}