Replace old command parsing with new
[kaka/cakelight.git] / src / kaka / cakelight / Commands.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.AmbientMode;
4 import kaka.cakelight.mode.SingleColorMode;
5 import kaka.cakelight.mode.TwoColorNoiseMode;
6 import kaka.cakelight.mode.VideoMode;
7
8 import java.util.function.BiFunction;
9
10 class Commands {
11     private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
12         return new Console.Command() {
13             @Override
14             public String[] getNames() {
15                 return names;
16             }
17
18             @Override
19             public void activate(Console console, String[] args) {
20                 if (!activate.apply(console, args)) {
21                     console.out("did NOT run command");
22                 }
23             }
24         };
25     }
26
27     static Console.Command quit() {
28         return command(new String[] {"q", "quit"}, (console, args) -> {
29             console.getCakelight().turnOff();
30             console.out("stopping cakelight");
31             return true;
32         });
33     }
34
35     static Console.Command video() {
36         return command(new String[] {"v", "video"}, (console, args) -> {
37             console.getCakelight().setMode(new VideoMode());
38             return true;
39         });
40     }
41
42     static Console.Command color() {
43         return command(new String[] {"c", "col", "color"}, (console, args) -> {
44             Color c = null;
45             if (args.length == 1) {
46                 c = console.parseColor(args[0]);
47             } else if (args.length == 3) {
48                 c = Color.rgb(
49                         Integer.parseInt(args[0]),
50                         Integer.parseInt(args[1]),
51                         Integer.parseInt(args[2])
52                 );
53             }
54             if (c != null) {
55                 console.getCakelight().setMode(new SingleColorMode(c));
56                 console.out("setting color to " + c);
57                 return true;
58             } else {
59                 return false;
60             }
61         });
62     }
63
64     static Console.Command brightness() {
65         return command(new String[] {"b", "brightness"}, (console, args) -> {
66             if (args.length == 1) {
67                 int b = Integer.parseInt(args[0]);
68                 console.getConfig().leds.brightness = b;
69                 console.out("setting brightness to " + b);
70                 return true;
71             } else {
72                 return false;
73             }
74         });
75     }
76
77     static Console.Command gamma() {
78         return command(new String[] {"g", "gamma"}, (console, args) -> {
79             if (args.length == 1) {
80                 double g = Double.parseDouble(args[0]);
81                 console.getConfig().gamma = g;
82                 Color.calculateGammaCorrection(g);
83                 console.out("setting gamma to " + g);
84                 return true;
85             } else {
86                 return false;
87             }
88         });
89     }
90
91     static Console.Command saturation() {
92         return command(new String[] {"s", "saturation"}, (console, args) -> {
93             if (args.length == 1) {
94                 double s = Double.parseDouble(args[0]);
95                 console.getConfig().video.saturation = s;
96                 console.out("setting saturation to " + s);
97                 return true;
98             } else {
99                 return false;
100             }
101         });
102     }
103
104     static Console.Command ambientMode() {
105         return command(new String[] {"m", "mode"}, (console, args) -> {
106             if (args.length == 1) {
107                 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
108                 console.out("setting ambient mode to " + args[0]);
109                 return true;
110             } else {
111                 return false;
112             }
113         });
114     }
115
116     static Console.Command twoColorNoiseMode() {
117         return command(new String[] {"n", "noise"}, (console, args) -> {
118             if (args.length == 2) {
119                 console.getCakelight().setMode(new TwoColorNoiseMode(
120                         console.parseColor(args[0]),
121                         console.parseColor(args[1])
122                 ));
123                 console.out("setting two-color noise mode");
124                 return true;
125             } else {
126                 return false;
127             }
128         });
129     }
130 }