Skip to content

Commit

Permalink
Merge pull request #72 from Pecamo/pulse
Browse files Browse the repository at this point in the history
Pulse animation
  • Loading branch information
BinaryBrain authored Aug 18, 2024
2 parents 713c4eb + e92d144 commit 1a98d3f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion server/lamp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import StrobeAnimation from "./strobe";
import Strobe2Animation from "./strobe2";
import AlternatingAnimation from "./alternating";
import MatrixAnimation from "./matrix";
import PulseAnimation from "./pulse";
import SlidingWindowAnimation from "./slidingWindow";
import RainbowSlidingWindowAnimation from './rainbowSlidingWindow';
import FlashingApertureAnimation from './flashingAperture';
Expand All @@ -33,7 +34,7 @@ lamp.use(express.json());

export function initLamp() {
const LAMP_FPS: number = env.LAMP_FPS;
const GRADIENT_DURATION = 1000;
const GRADIENT_DURATION: number = 1000;

const animations: LampAnimation[] = [
new NoneAnimation(),
Expand All @@ -45,6 +46,7 @@ export function initLamp() {
new Strobe2Animation(),
new AlternatingAnimation(20),
new MatrixAnimation(),
new PulseAnimation(),
new GameOfLifeAnimation(),
new SlidingWindowAnimation(),
new RainbowSlidingWindowAnimation(),
Expand Down
45 changes: 45 additions & 0 deletions server/lamp/pulse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { NB_LED } from '../NB_LED';
import { HtmlColors } from "../htmlColors";
import {ColorOption, LampAnimation, NumberOption, SelectOption} from "../types/LampAnimation";
import {normalize} from "../utils";
import {LAMP_FPS, TOP_LED_NB} from '../env';
import { Color } from '../color';

export default class PulseAnimation implements LampAnimation<[ColorOption, ColorOption, SelectOption, NumberOption, NumberOption, NumberOption, NumberOption]> {
public name = "Pulse";
public options: [ColorOption, ColorOption, SelectOption, NumberOption, NumberOption, NumberOption, NumberOption] = [
{ name: "Start color", type: "color", default: HtmlColors.cyan },
{ name: "End color", type: "color", default: HtmlColors.cyan },
{ name: "Branches", type: "select", options: [
{optionValue: "rl", label: "Both"},
{optionValue: "l", label: "Clockwise"},
{optionValue: "r", label: "Counter-Clockwise"},
], default: "rl" },
{ name: "Speed", type: "number", default: 10, min: 1, max: 100, step: 1, display: 'range' },
{ name: "# of Pulses", type: "number", default: 1, min: 1, max: 10, step: 1, display: 'range' },
{ name: "Rotation speed", type: "number", default: 0, min: 0, max: 360, step: 1, display: 'range' },
{ name: "Top Led Number", type: "number", default: TOP_LED_NB, min: 0, max: NB_LED, step: 1, display: 'range' }
];

public animate(t, display, options) {
const [color1, color2, branches, speed, pulses, rotationSpeed, topLedNb] = options;
const realSpeed = speed / 10 * LAMP_FPS / 60;

const isFull = branches === 'rl';
const maxProgress = Math.ceil(NB_LED / (isFull ? 2 : 1));
const progress = (t * realSpeed) % maxProgress;
const rotationProgress = t * rotationSpeed / 360;

for (let pulse = 0; pulse < pulses; pulse++) {
const pulseProgress = (progress + (pulse / pulses * maxProgress)) % maxProgress;
const progressedColor = Color.overlap(color1, color2, pulseProgress / maxProgress);
if (branches.includes('l')) {
display.drawDot(normalize(Math.round(pulseProgress + topLedNb + rotationProgress)), progressedColor);
}
if (branches.includes('r')) {
display.drawDot(normalize(Math.round(-pulseProgress + topLedNb + rotationProgress)), progressedColor);
}
}
}
}

0 comments on commit 1a98d3f

Please sign in to comment.