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