34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
export const state = {
|
|
maxPoints: 1000,
|
|
signalHistory: [],
|
|
smoothValue: 0,
|
|
lastBufferSize: 0,
|
|
};
|
|
export function smooth(target) {
|
|
state.smoothValue += 0.2 * (target - state.smoothValue);
|
|
return state.smoothValue;
|
|
}
|
|
export function addSignal(value) {
|
|
state.signalHistory.push(value);
|
|
if (state.signalHistory.length > state.maxPoints) {
|
|
state.signalHistory = state.signalHistory.slice(-state.maxPoints);
|
|
}
|
|
}
|
|
export function addSignals(values, bufferSize) {
|
|
if (!Array.isArray(values) || values.length === 0)
|
|
return;
|
|
const newBytes = bufferSize - state.lastBufferSize;
|
|
if (newBytes > 0 && newBytes <= values.length) {
|
|
const fresh = values.slice(-newBytes);
|
|
const decoded = fresh.map(b => b & 0x3F);
|
|
state.signalHistory.push(...decoded);
|
|
}
|
|
state.lastBufferSize = bufferSize;
|
|
if (state.signalHistory.length > state.maxPoints) {
|
|
state.signalHistory = state.signalHistory.slice(-state.maxPoints);
|
|
}
|
|
}
|
|
export function resetBufferTracking() {
|
|
state.lastBufferSize = 0;
|
|
}
|