fix:尝试修复余晖效果
This commit is contained in:
parent
a56a65cc0d
commit
d30712d0f6
|
@ -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();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 暴露属性和方法
|
// 暴露属性和方法
|
||||||
|
|
Loading…
Reference in New Issue