Skip to content

Commit

Permalink
WIP food production light animation
Browse files Browse the repository at this point in the history
  • Loading branch information
jfabellera committed Sep 18, 2024
1 parent 88a8c4d commit 1ab1466
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 61 deletions.
37 changes: 29 additions & 8 deletions back-end/realtime/src/util/WLEDHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,36 @@ export const buildWledSetColorPacket = (
seg: []
};

const segments = new Map<number, any>();

packet.patterns.forEach((pattern) => {
wledJson.seg.push({
id: pattern.segment,
on: true,
frz: false,
fx: 0,
col: [pattern.color]
});
if (!pattern.subset) {
segments.set(pattern.segment, {
id: pattern.segment,
on: true,
frz: false,
fx: 0,
col: [pattern.color]
});
} else {
if (!segments.get(pattern.segment)) {
segments.set(pattern.segment, {
id: pattern.segment,
on: true,
transition: 10,
i: [pattern.subset.startIndex, pattern.subset.endIndex, pattern.color]
});
} else {
segments
.get(pattern.segment)
.i.push(
pattern.subset.startIndex,
pattern.subset.endIndex,
pattern.color
);
}
}
});

return JSON.stringify(wledJson);
return JSON.stringify({ seg: Array.from(segments.values()) });
};
4 changes: 4 additions & 0 deletions lib/models/src/base/FieldControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export type HubUpdateParameters = HubParameters<
export interface LedPatternUpdateParameters {
segment: number;
color: string;
subset?: {
startIndex: number;
endIndex: number;
};
}

export interface LedSegment {
Expand Down
139 changes: 86 additions & 53 deletions lib/models/src/fcs/FeedingTheFutureFCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { NexusGoalState } from '../seasons/FeedingTheFuture.js';
import { FeedingTheFuture } from '../seasons/index.js';
import {
applyPartialPatternToStrips,
applyPatternToStrips,
applySetpointToMotors,
LedStrip,
Expand Down Expand Up @@ -559,65 +560,25 @@ export class PacketManager {

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };

switch (currentState) {
case NexusGoalState.Full:
applyPatternToStrips(this.fieldOptions.goalFullColor, [strip], result);
break;
case NexusGoalState.BlueOnly:
applyPatternToStrips(
this.fieldOptions.goalBlueOnlyColor,
[strip],
result
);
break;
case NexusGoalState.GreenOnly:
applyPatternToStrips(
this.fieldOptions.goalGreenOnlyColor,
[strip],
result
);
break;
default:
applyPatternToStrips(this.fieldOptions.goalEmptyColor, [strip], result);
if (currentState === NexusGoalState.BlueOnly) {
applyPatternToStrips(
this.fieldOptions.goalBlueOnlyColor,
[strip],
result
);
} else if (currentState === NexusGoalState.GreenOnly) {
applyPatternToStrips(
this.fieldOptions.goalGreenOnlyColor,
[strip],
result
);
}

if (
currentState === NexusGoalState.Full &&
previousState !== NexusGoalState.Full
) {
// Start timer with callback
this.timers.set(
goal,
setTimeout(() => {
if (!this.matchInProgress) return;

// Set pattern
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips('ffffff', [strip], result);
applySetpointToMotors(
this.fieldOptions.foodProductionMotorSetpoint,
[motor],
result
);
broadcast(result);

setTimeout(() => {
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applySetpointToMotors(0, [motor], result);
broadcast(result);
}, this.fieldOptions.foodProductionMotorDurationMs);

this.matchEmitter.emit(MatchSocketEvent.MATCH_ADJUST_DETAILS_NUMBER, {
key: `${side}FoodProduced`,
adjustment: 1
} satisfies NumberAdjustment);

this.matchEmitter.emit(MatchSocketEvent.MATCH_UPDATE_DETAILS_ITEM, {
key: goal,
value: NexusGoalState.Produced
});
}, this.fieldOptions.foodProductionDelayMs)
);
this.funnyLightSequence(motor, strip, side, goal);
} else if (
currentState !== NexusGoalState.Full &&
previousState === NexusGoalState.Full
Expand Down Expand Up @@ -664,4 +625,76 @@ export class PacketManager {
value: balanced
} satisfies ItemUpdate);
};

funnyLightSequence = (
motor: MotorA,
strip: LedStripA,
side: string,
goal: string
) => {
const steps = 10;

const recurse = (count: number, strip: LedStripA) => {
if (!this.matchInProgress) return;
if (count === 0) {
// Set pattern
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(this.fieldOptions.goalFullColor, [strip], result);
applySetpointToMotors(
this.fieldOptions.foodProductionMotorSetpoint,
[motor],
result
);
this.broadcastCallback(result);

this.timers.set(
goal,
setTimeout(() => {
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applySetpointToMotors(0, [motor], result);
this.broadcastCallback(result);
}, this.fieldOptions.foodProductionMotorDurationMs)
);

this.matchEmitter.emit(MatchSocketEvent.MATCH_ADJUST_DETAILS_NUMBER, {
key: `${side}FoodProduced`,
adjustment: 1
} satisfies NumberAdjustment);

this.matchEmitter.emit(MatchSocketEvent.MATCH_UPDATE_DETAILS_ITEM, {
key: goal,
value: NexusGoalState.Produced
});

return;
}

const middle = Math.floor(23 / 2);
const length = Math.floor(23 / 2 / steps) * (steps - count);
const startIndex = middle - length;
const endIndex = middle + 1 + length;
console.log(`Action ${count} ${startIndex} ${endIndex}`);

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };

applyPartialPatternToStrips(
this.fieldOptions.goalFullColor,
startIndex,
endIndex,
[strip],
result
);

this.broadcastCallback(result);

this.timers.set(
goal,
setTimeout(() => {
recurse(count - 1, strip);
}, Math.floor(this.fieldOptions.foodProductionDelayMs / steps))
);
};

recurse(10, strip);
};
}
25 changes: 25 additions & 0 deletions lib/models/src/fcs/Packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ export function applyPatternToStrips(
});
}

export function applyPartialPatternToStrips(
color: string,
startIndex: number,
endIndex: number,
strips: LedStrip[],
packet: FieldControlUpdatePacket
): void {
strips.forEach((strip) => {
if (!packet.wleds[strip.controller]) {
packet.wleds[strip.controller] = {
patterns: []
};
}

packet.wleds[strip.controller].patterns.push({
segment: strip.segment,
color,
subset: {
startIndex,
endIndex
}
});
});
}

export function applySetpointToMotors(
setpoint: number,
motors: Motor[],
Expand Down

0 comments on commit 1ab1466

Please sign in to comment.