Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulse animation #72

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colors should be stored on dots (if possible) to be compatible with Random Colors and future features.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voilà : #77

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);
}
}
}
}

Loading