Commit | Line | Data |
---|---|---|
2b49e4e2 TW |
1 | package kaka.cakelight; |
2 | ||
c9edf58d | 3 | import kaka.cakelight.mode.*; |
2b49e4e2 TW |
4 | |
5 | import java.util.function.BiFunction; | |
0c8d61b6 | 6 | import java.util.stream.Stream; |
2b49e4e2 TW |
7 | |
8 | class Commands { | |
9 | private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) { | |
10 | return new Console.Command() { | |
11 | @Override | |
12 | public String[] getNames() { | |
13 | return names; | |
14 | } | |
15 | ||
16 | @Override | |
c9edf58d | 17 | public Object activate(Console console, String[] args) { |
2b49e4e2 TW |
18 | if (!activate.apply(console, args)) { |
19 | console.out("did NOT run command"); | |
20 | } | |
c9edf58d TW |
21 | return null; |
22 | } | |
23 | }; | |
24 | } | |
25 | ||
26 | private static Console.Command modeCommand(String[] names, BiFunction<Console, String[], Mode> activate) { | |
27 | return new Console.Command() { | |
28 | @Override | |
29 | public String[] getNames() { | |
30 | return names; | |
31 | } | |
32 | ||
33 | @Override | |
34 | public Object activate(Console console, String[] args) { | |
35 | Mode mode = activate.apply(console, args); | |
36 | if (mode == null) { | |
37 | console.out("did NOT run command"); | |
38 | } | |
39 | return mode; | |
2b49e4e2 TW |
40 | } |
41 | }; | |
42 | } | |
43 | ||
e4738966 TW |
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())); | |
48 | } | |
49 | return true; | |
50 | }); | |
51 | } | |
52 | ||
2b49e4e2 TW |
53 | static Console.Command quit() { |
54 | return command(new String[] {"q", "quit"}, (console, args) -> { | |
e4738966 TW |
55 | console.quit(); |
56 | console.out("terminating"); | |
2b49e4e2 TW |
57 | return true; |
58 | }); | |
59 | } | |
60 | ||
c9edf58d TW |
61 | static Console.Command push() { |
62 | return command(new String[] {"push"}, (console, args) -> { | |
fe55a044 | 63 | Object obj = console.internalHandleInput(String.join(" ", args)); |
c9edf58d TW |
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); | |
67 | } | |
68 | return true; | |
2b49e4e2 TW |
69 | }); |
70 | } | |
71 | ||
c9edf58d TW |
72 | static Console.Command pop() { |
73 | return command(new String[] {"pop"}, (console, args) -> { | |
74 | console.out("popping mode " + console.getCakelight().popMode().getClass().getSimpleName()); | |
75 | return true; | |
76 | }); | |
77 | } | |
78 | ||
79 | static Console.Command video() { | |
80 | return modeCommand(new String[] {"v", "video"}, (console, args) -> new VideoMode()); | |
81 | } | |
82 | ||
2b49e4e2 | 83 | static Console.Command color() { |
c9edf58d | 84 | return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> { |
2b49e4e2 TW |
85 | Color c = null; |
86 | if (args.length == 1) { | |
87 | c = console.parseColor(args[0]); | |
88 | } else if (args.length == 3) { | |
89 | c = Color.rgb( | |
90 | Integer.parseInt(args[0]), | |
91 | Integer.parseInt(args[1]), | |
92 | Integer.parseInt(args[2]) | |
93 | ); | |
94 | } | |
95 | if (c != null) { | |
2b49e4e2 | 96 | console.out("setting color to " + c); |
c9edf58d | 97 | return new SingleColorMode(c); |
2b49e4e2 | 98 | } |
c9edf58d | 99 | return null; |
2b49e4e2 TW |
100 | }); |
101 | } | |
102 | ||
103 | static Console.Command brightness() { | |
104 | return command(new String[] {"b", "brightness"}, (console, args) -> { | |
105 | if (args.length == 1) { | |
cc124c32 | 106 | int b = Integer.parseInt(args[0].replaceAll("[+-]+", "")); |
c6384b55 TW |
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); | |
111 | } | |
2b49e4e2 TW |
112 | console.getConfig().leds.brightness = b; |
113 | console.out("setting brightness to " + b); | |
114 | return true; | |
115 | } else { | |
116 | return false; | |
117 | } | |
118 | }); | |
119 | } | |
120 | ||
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); | |
128 | return true; | |
129 | } else { | |
130 | return false; | |
131 | } | |
132 | }); | |
133 | } | |
134 | ||
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); | |
141 | return true; | |
142 | } else { | |
143 | return false; | |
144 | } | |
145 | }); | |
146 | } | |
147 | ||
148 | static Console.Command ambientMode() { | |
c9edf58d | 149 | return modeCommand(new String[] {"m", "mode"}, (console, args) -> { |
2b49e4e2 | 150 | if (args.length == 1) { |
2b49e4e2 | 151 | console.out("setting ambient mode to " + args[0]); |
c9edf58d | 152 | return new AmbientMode(new String[]{args[0]}); |
2b49e4e2 | 153 | } |
c9edf58d | 154 | return null; |
2b49e4e2 TW |
155 | }); |
156 | } | |
157 | ||
6a3d0eda | 158 | static Console.Command noiseMode() { |
c9edf58d | 159 | return modeCommand(new String[] {"n", "noise"}, (console, args) -> { |
0c8d61b6 TW |
160 | if (args.length > 1) { |
161 | console.out("setting multi-color noise mode"); | |
6a3d0eda TW |
162 | return new NoiseMode(Stream.of(args) |
163 | .map(console::parseColor) | |
0c8d61b6 | 164 | .toArray(Color[]::new) |
c9edf58d | 165 | ); |
2b49e4e2 | 166 | } |
c9edf58d | 167 | return null; |
2b49e4e2 TW |
168 | }); |
169 | } | |
40a06a9b | 170 | |
e147b561 TW |
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) | |
178 | ); | |
179 | } | |
180 | return null; | |
181 | }); | |
182 | } | |
183 | ||
40a06a9b | 184 | static Console.Command sunriseMode() { |
c9edf58d | 185 | return modeCommand(new String[] {"sunrise"}, (console, args) -> { |
40a06a9b TW |
186 | if (args.length == 1) { |
187 | int durationSeconds = Integer.parseInt(args[0]); | |
40a06a9b | 188 | console.out("setting sunrise mode with duration " + durationSeconds); |
c9edf58d | 189 | return new SunriseMode(durationSeconds); |
40a06a9b | 190 | } |
c9edf58d | 191 | return null; |
40a06a9b TW |
192 | }); |
193 | } | |
2b49e4e2 | 194 | } |