fix:尝试修复余晖效果

This commit is contained in:
SikongJueluo 2025-07-20 09:31:19 +08:00
parent a56a65cc0d
commit d30712d0f6
No known key found for this signature in database
1 changed files with 47 additions and 40 deletions

View File

@ -79,7 +79,6 @@ interface SevenSegmentDisplayProps {
size?: number;
color?: string;
AFTERGLOW_BUFFER_SIZE?: number; //
AFTERGLOW_UPDATE_INTERVAL?: number; //
pins?: {
pinId: string;
constraint: string;
@ -149,17 +148,38 @@ const afterglowBuffers = ref<Record<string, boolean[]>>({
dp: [],
});
//
let updateIntervalTimer: number | null = null;
// 10
const STABLE_THRESHOLD = 10;
// - true
//
const stableSegmentStates = ref({
a: false,
b: false,
c: false,
d: false,
e: false,
f: false,
g: false,
dp: false,
});
//
const segmentStableCounters = ref<Record<string, number>>({
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
dp: 0,
});
//
function isSegmentActive(
segment: "a" | "b" | "c" | "d" | "e" | "f" | "g" | "dp",
): boolean {
return (
segmentStates.value[segment] ||
afterglowBuffers.value[segment].some((state) => state)
);
return stableSegmentStates.value[segment];
}
//
@ -181,28 +201,38 @@ function updateSegmentStates() {
updateAfterglowBuffers();
}
// segmentStates
for (const pin of props.pins) {
if (["a", "b", "c", "d", "e", "f", "g", "dp"].includes(pin.pinId)) {
// constraint
if (!pin.constraint) {
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] =
false;
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = false;
continue;
}
const pinState = getConstraintState(pin.constraint);
let newState: boolean;
if (props.cathodeType === "common") {
// :
newState = pinState === "high";
} else {
// :
newState = pinState === "low";
}
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = newState && comActive;
}
}
//
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] =
newState && comActive;
// STABLE_THRESHOLD stableSegmentStates
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
const typedSegmentId = segmentId as keyof typeof segmentStates.value;
const current = segmentStates.value[typedSegmentId];
const stable = stableSegmentStates.value[typedSegmentId];
if (current === stable) {
segmentStableCounters.value[segmentId] = 0; //
} else {
segmentStableCounters.value[segmentId]++;
if (segmentStableCounters.value[segmentId] >= STABLE_THRESHOLD) {
stableSegmentStates.value[typedSegmentId] = current;
segmentStableCounters.value[segmentId] = 0;
}
}
}
}
@ -225,23 +255,6 @@ function updateAfterglowBuffers() {
}
}
//
function startAfterglowUpdates() {
if (updateIntervalTimer) return;
updateIntervalTimer = window.setInterval(() => {
updateSegmentStates();
}, props.AFTERGLOW_UPDATE_INTERVAL);
}
//
function stopAfterglowUpdates() {
if (updateIntervalTimer) {
window.clearInterval(updateIntervalTimer);
updateIntervalTimer = null;
}
}
//
function onConstraintChange(constraint: string, level: string) {
const affectedPin = props.pins.find((pin) => pin.constraint === constraint);
@ -261,12 +274,6 @@ onMounted(() => {
updateSegmentStates();
onConstraintStateChange(onConstraintChange);
startAfterglowUpdates();
});
onUnmounted(() => {
//
stopAfterglowUpdates();
});
//