Let brightness command handle delta changes
[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].replaceAll("\\+-", ""));
68                 if (args[0].startsWith("+")) {
69                     b = Math.min(console.getConfig().leds.brightness + b, 31);
70                 } else if (args[0].startsWith("-")) {
71                     b = Math.max(console.getConfig().leds.brightness - b, 0);
72                 }
73                 console.getConfig().leds.brightness = b;
74                 console.out("setting brightness to " + b);
75                 return true;
76             } else {
77                 return false;
78             }
79         });
80     }
81
82     static Console.Command gamma() {
83         return command(new String[] {"g", "gamma"}, (console, args) -> {
84             if (args.length == 1) {
85                 double g = Double.parseDouble(args[0]);
86                 console.getConfig().gamma = g;
87                 Color.calculateGammaCorrection(g);
88                 console.out("setting gamma to " + g);
89                 return true;
90             } else {
91                 return false;
92             }
93         });
94     }
95
96     static Console.Command saturation() {
97         return command(new String[] {"s", "saturation"}, (console, args) -> {
98             if (args.length == 1) {
99                 double s = Double.parseDouble(args[0]);
100                 console.getConfig().video.saturation = s;
101                 console.out("setting saturation to " + s);
102                 return true;
103             } else {
104                 return false;
105             }
106         });
107     }
108
109     static Console.Command ambientMode() {
110         return command(new String[] {"m", "mode"}, (console, args) -> {
111             if (args.length == 1) {
112                 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
113                 console.out("setting ambient mode to " + args[0]);
114                 return true;
115             } else {
116                 return false;
117             }
118         });
119     }
120
121     static Console.Command twoColorNoiseMode() {
122         return command(new String[] {"n", "noise"}, (console, args) -> {
123             if (args.length == 2) {
124                 console.getCakelight().setMode(new TwoColorNoiseMode(
125                         console.parseColor(args[0]),
126                         console.parseColor(args[1])
127                 ));
128                 console.out("setting two-color noise mode");
129                 return true;
130             } else {
131                 return false;
132             }
133         });
134     }
135 }