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; | |
6 | ||
7 | class 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 | |
c9edf58d | 16 | public Object activate(Console console, String[] args) { |
2b49e4e2 TW |
17 | if (!activate.apply(console, args)) { |
18 | console.out("did NOT run command"); | |
19 | } | |
c9edf58d TW |
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; | |
2b49e4e2 TW |
39 | } |
40 | }; | |
41 | } | |
42 | ||
e4738966 TW |
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 | ||
2b49e4e2 TW |
52 | static Console.Command quit() { |
53 | return command(new String[] {"q", "quit"}, (console, args) -> { | |
e4738966 TW |
54 | console.quit(); |
55 | console.out("terminating"); | |
2b49e4e2 TW |
56 | return true; |
57 | }); | |
58 | } | |
59 | ||
c9edf58d TW |
60 | static Console.Command push() { |
61 | return command(new String[] {"push"}, (console, args) -> { | |
fe55a044 | 62 | Object obj = console.internalHandleInput(String.join(" ", args)); |
c9edf58d TW |
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; | |
2b49e4e2 TW |
68 | }); |
69 | } | |
70 | ||
c9edf58d TW |
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 | ||
2b49e4e2 | 82 | static Console.Command color() { |
c9edf58d | 83 | return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> { |
2b49e4e2 TW |
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) { | |
2b49e4e2 | 95 | console.out("setting color to " + c); |
c9edf58d | 96 | return new SingleColorMode(c); |
2b49e4e2 | 97 | } |
c9edf58d | 98 | return null; |
2b49e4e2 TW |
99 | }); |
100 | } | |
101 | ||
102 | static Console.Command brightness() { | |
103 | return command(new String[] {"b", "brightness"}, (console, args) -> { | |
104 | if (args.length == 1) { | |
cc124c32 | 105 | int b = Integer.parseInt(args[0].replaceAll("[+-]+", "")); |
c6384b55 TW |
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 | } | |
2b49e4e2 TW |
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() { | |
c9edf58d | 148 | return modeCommand(new String[] {"m", "mode"}, (console, args) -> { |
2b49e4e2 | 149 | if (args.length == 1) { |
2b49e4e2 | 150 | console.out("setting ambient mode to " + args[0]); |
c9edf58d | 151 | return new AmbientMode(new String[]{args[0]}); |
2b49e4e2 | 152 | } |
c9edf58d | 153 | return null; |
2b49e4e2 TW |
154 | }); |
155 | } | |
156 | ||
157 | static Console.Command twoColorNoiseMode() { | |
c9edf58d | 158 | return modeCommand(new String[] {"n", "noise"}, (console, args) -> { |
2b49e4e2 | 159 | if (args.length == 2) { |
c9edf58d TW |
160 | console.out("setting two-color noise mode"); |
161 | return new TwoColorNoiseMode( | |
2b49e4e2 TW |
162 | console.parseColor(args[0]), |
163 | console.parseColor(args[1]) | |
c9edf58d | 164 | ); |
2b49e4e2 | 165 | } |
c9edf58d | 166 | return null; |
2b49e4e2 TW |
167 | }); |
168 | } | |
40a06a9b TW |
169 | |
170 | static Console.Command sunriseMode() { | |
c9edf58d | 171 | return modeCommand(new String[] {"sunrise"}, (console, args) -> { |
40a06a9b TW |
172 | if (args.length == 1) { |
173 | int durationSeconds = Integer.parseInt(args[0]); | |
40a06a9b | 174 | console.out("setting sunrise mode with duration " + durationSeconds); |
c9edf58d | 175 | return new SunriseMode(durationSeconds); |
40a06a9b | 176 | } |
c9edf58d | 177 | return null; |
40a06a9b TW |
178 | }); |
179 | } | |
2b49e4e2 | 180 | } |