Add sunrise mode
[kaka/cakelight.git] / src / kaka / cakelight / Commands.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.*;
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
16             public void activate(Console console, String[] args) {
17                 if (!activate.apply(console, args)) {
18                     console.out("did NOT run command");
19                 }
20             }
21         };
22     }
23
24     static Console.Command quit() {
25         return command(new String[] {"q", "quit"}, (console, args) -> {
26             console.getCakelight().turnOff();
27             console.out("stopping cakelight");
28             return true;
29         });
30     }
31
32     static Console.Command video() {
33         return command(new String[] {"v", "video"}, (console, args) -> {
34             console.getCakelight().setMode(new VideoMode());
35             return true;
36         });
37     }
38
39     static Console.Command color() {
40         return command(new String[] {"c", "col", "color"}, (console, args) -> {
41             Color c = null;
42             if (args.length == 1) {
43                 c = console.parseColor(args[0]);
44             } else if (args.length == 3) {
45                 c = Color.rgb(
46                         Integer.parseInt(args[0]),
47                         Integer.parseInt(args[1]),
48                         Integer.parseInt(args[2])
49                 );
50             }
51             if (c != null) {
52                 console.getCakelight().setMode(new SingleColorMode(c));
53                 console.out("setting color to " + c);
54                 return true;
55             } else {
56                 return false;
57             }
58         });
59     }
60
61     static Console.Command brightness() {
62         return command(new String[] {"b", "brightness"}, (console, args) -> {
63             if (args.length == 1) {
64                 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
65                 if (args[0].startsWith("+")) {
66                     b = Math.min(console.getConfig().leds.brightness + b, 31);
67                 } else if (args[0].startsWith("-")) {
68                     b = Math.max(console.getConfig().leds.brightness - b, 0);
69                 }
70                 console.getConfig().leds.brightness = b;
71                 console.out("setting brightness to " + b);
72                 return true;
73             } else {
74                 return false;
75             }
76         });
77     }
78
79     static Console.Command gamma() {
80         return command(new String[] {"g", "gamma"}, (console, args) -> {
81             if (args.length == 1) {
82                 double g = Double.parseDouble(args[0]);
83                 console.getConfig().gamma = g;
84                 Color.calculateGammaCorrection(g);
85                 console.out("setting gamma to " + g);
86                 return true;
87             } else {
88                 return false;
89             }
90         });
91     }
92
93     static Console.Command saturation() {
94         return command(new String[] {"s", "saturation"}, (console, args) -> {
95             if (args.length == 1) {
96                 double s = Double.parseDouble(args[0]);
97                 console.getConfig().video.saturation = s;
98                 console.out("setting saturation to " + s);
99                 return true;
100             } else {
101                 return false;
102             }
103         });
104     }
105
106     static Console.Command ambientMode() {
107         return command(new String[] {"m", "mode"}, (console, args) -> {
108             if (args.length == 1) {
109                 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
110                 console.out("setting ambient mode to " + args[0]);
111                 return true;
112             } else {
113                 return false;
114             }
115         });
116     }
117
118     static Console.Command twoColorNoiseMode() {
119         return command(new String[] {"n", "noise"}, (console, args) -> {
120             if (args.length == 2) {
121                 console.getCakelight().setMode(new TwoColorNoiseMode(
122                         console.parseColor(args[0]),
123                         console.parseColor(args[1])
124                 ));
125                 console.out("setting two-color noise mode");
126                 return true;
127             } else {
128                 return false;
129             }
130         });
131     }
132
133     static Console.Command sunriseMode() {
134         return command(new String[] {"sunrise"}, (console, args) -> {
135             if (args.length == 1) {
136                 int durationSeconds = Integer.parseInt(args[0]);
137                 console.getCakelight().setMode(new SunriseMode(durationSeconds));
138                 console.out("setting sunrise mode with duration " + durationSeconds);
139                 return true;
140             } else {
141                 return false;
142             }
143         });
144     }
145 }