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