diff --git a/src/components/LogicAnalyzer/LogicAnalyzerManager.ts b/src/components/LogicAnalyzer/LogicAnalyzerManager.ts index 0d94d49..a2841dc 100644 --- a/src/components/LogicAnalyzer/LogicAnalyzerManager.ts +++ b/src/components/LogicAnalyzer/LogicAnalyzerManager.ts @@ -240,6 +240,57 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState( logicData.value = data; }; + const getCaptureData = async () => { + try { + const client = AuthManager.createAuthenticatedLogicAnalyzerClient(); + // 3. 获取捕获数据 + const base64Data = await client.getCaptureData(); + + // 4. 将base64数据转换为bytes + const binaryString = atob(base64Data); + const bytes = new Uint8Array(binaryString.length); + for (let i = 0; i < binaryString.length; i++) { + bytes[i] = binaryString.charCodeAt(i); + } + + // 5. 解析数据为8个通道的数字信号 + const sampleCount = bytes.length; + const timeStepNs = SAMPLE_PERIOD_NS; // 每个采样点间隔200ns (1/5MHz) + + // 创建时间轴(转换为合适的单位) + const x = Array.from( + { length: sampleCount }, + (_, i) => (i * timeStepNs) / 1000, + ); // 转换为微秒 + + // 创建8个通道的数据 + const y: number[][] = Array.from( + { length: 8 }, + () => new Array(sampleCount), + ); + + // 解析每个字节的8个位到对应通道 + for (let i = 0; i < sampleCount; i++) { + const byte = bytes[i]; + for (let channel = 0; channel < 8; channel++) { + // bit0对应ch0, bit1对应ch1, ..., bit7对应ch7 + y[channel][i] = (byte >> channel) & 1; + } + } + + // 6. 设置逻辑数据 + const logicData: LogicDataType = { + x, + y, + xUnit: "us", // 改为微秒单位 + }; + + setLogicData(logicData); + } catch (error) { + console.error("获取捕获数据失败:", error); + } + }; + const startCapture = async () => { // 检查是否有其他操作正在进行 if (operationMutex.isLocked()) { @@ -298,52 +349,9 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState( alert?.info("捕获已停止", 2000); return; } - - // 3. 获取捕获数据 - const base64Data = await client.getCaptureData(); - - // 4. 将base64数据转换为bytes - const binaryString = atob(base64Data); - const bytes = new Uint8Array(binaryString.length); - for (let i = 0; i < binaryString.length; i++) { - bytes[i] = binaryString.charCodeAt(i); - } - - // 5. 解析数据为8个通道的数字信号 - const sampleCount = bytes.length; - const timeStepNs = SAMPLE_PERIOD_NS; // 每个采样点间隔200ns (1/5MHz) - - // 创建时间轴(转换为合适的单位) - const x = Array.from( - { length: sampleCount }, - (_, i) => (i * timeStepNs) / 1000, - ); // 转换为微秒 - - // 创建8个通道的数据 - const y: number[][] = Array.from( - { length: 8 }, - () => new Array(sampleCount), - ); - - // 解析每个字节的8个位到对应通道 - for (let i = 0; i < sampleCount; i++) { - const byte = bytes[i]; - for (let channel = 0; channel < 8; channel++) { - // bit0对应ch0, bit1对应ch1, ..., bit7对应ch7 - y[channel][i] = (byte >> channel) & 1; - } - } - - // 6. 设置逻辑数据 - const logicData: LogicDataType = { - x, - y, - xUnit: "us", // 改为微秒单位 - }; - - setLogicData(logicData); - - alert?.success(`捕获完成!获得 ${sampleCount} 个采样点`, 3000); + + await getCaptureData(); + alert.success(`捕获完成!`, 3000); } catch (error) { console.error("捕获失败:", error); alert?.error( @@ -357,19 +365,6 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState( }; const stopCapture = async () => { - try { - await forceCapture(); // 尝试强制捕获来停止当前捕获 - - // 设置捕获状态为false,这会使轮询停止 - isCapturing.value = false; - - alert?.info("停止捕获...", 2000); - } catch (error) { - console.error("停止捕获失败:", error); - } - }; - - const forceCapture = async () => { // 检查是否正在捕获 if (!isCapturing.value) { alert.warn("当前没有正在进行的捕获操作", 2000); @@ -384,10 +379,25 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState( if (!forceSuccess) { throw new Error("无法执行强制捕获"); } + + // 设置捕获状态为false,这会使轮询停止 + isCapturing.value = false; + + alert.info("已停止强制捕获...", 2000); } catch (error) { - console.error("捕获失败:", error); - alert?.error( - `捕获失败: ${error instanceof Error ? error.message : "未知错误"}`, + console.error("停止捕获失败:", error); + } + }; + + const forceCapture = async () => { + try { + await stopCapture(); + await getCaptureData(); + alert.success(`捕获完成!`, 3000); + } catch (error) { + console.error("强制捕获失败:", error); + alert.error( + `强制捕获失败: ${error instanceof Error ? error.message : "未知错误"}`, 3000, ); }