From 40a06a9bffb56d41e931038fcfdb8f10fe8ca441 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Wed, 27 Nov 2019 22:53:11 +0100 Subject: [PATCH] Add sunrise mode --- src/kaka/cakelight/Commands.java | 18 ++++++++++++---- src/kaka/cakelight/Console.java | 1 + src/kaka/cakelight/mode/SunriseMode.java | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/kaka/cakelight/mode/SunriseMode.java diff --git a/src/kaka/cakelight/Commands.java b/src/kaka/cakelight/Commands.java index f527c12..38fb77e 100644 --- a/src/kaka/cakelight/Commands.java +++ b/src/kaka/cakelight/Commands.java @@ -1,9 +1,6 @@ package kaka.cakelight; -import kaka.cakelight.mode.AmbientMode; -import kaka.cakelight.mode.SingleColorMode; -import kaka.cakelight.mode.TwoColorNoiseMode; -import kaka.cakelight.mode.VideoMode; +import kaka.cakelight.mode.*; import java.util.function.BiFunction; @@ -132,4 +129,17 @@ class Commands { } }); } + + static Console.Command sunriseMode() { + return command(new String[] {"sunrise"}, (console, args) -> { + if (args.length == 1) { + int durationSeconds = Integer.parseInt(args[0]); + console.getCakelight().setMode(new SunriseMode(durationSeconds)); + console.out("setting sunrise mode with duration " + durationSeconds); + return true; + } else { + return false; + } + }); + } } diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index f32ba98..3d27a06 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -32,6 +32,7 @@ public class Console extends Thread { register(Commands.saturation()); register(Commands.ambientMode()); register(Commands.twoColorNoiseMode()); + register(Commands.sunriseMode()); } public CakeLight getCakelight() { diff --git a/src/kaka/cakelight/mode/SunriseMode.java b/src/kaka/cakelight/mode/SunriseMode.java new file mode 100644 index 0000000..896646f --- /dev/null +++ b/src/kaka/cakelight/mode/SunriseMode.java @@ -0,0 +1,36 @@ +package kaka.cakelight.mode; + +import kaka.cakelight.Color; +import kaka.cakelight.LedFrame; + +public class SunriseMode extends AmbientMode { + private int durationSeconds; + + public SunriseMode(int durationSeconds) { + this.durationSeconds = durationSeconds; + } + + @Override + protected void updateFrame(LedFrame frame, long time, int count) { + double progress = clamp(time / (durationSeconds * 1000.0)); + double elevation = 2.0 - progress; + double radius = progress * 2.12; + for (int i = 0; i < config.leds.getCount(); i++) { + double x = frame.xOf(i); + double y = frame.yOf(i); + double distance = distanceFromSun(x, y, elevation); + double r = clamp(1.0 - distance + radius); + double g = clamp(0.5 - distance + radius); + double b = clamp(0.0 - distance + radius); + frame.setLedColor(i, Color.rgb(r, g, b)); + } + } + + private double clamp(double value) { + return Math.min(Math.max(value, 0), 1.0); + } + + private double distanceFromSun(double x, double y, double elevation) { + return Math.sqrt(Math.pow(0.5 - x, 2) + Math.pow(elevation - y, 2)); + } +} -- 2.11.0