Skip to content

Commit

Permalink
新增byteAcc
Browse files Browse the repository at this point in the history
  • Loading branch information
l30014168 authored and smilelc3 committed Oct 21, 2024
1 parent c2b37fe commit fab63f8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
mkdir cd ${{github.workspace}}/js && cd ${{github.workspace}}/js/
git clone --depth=1 https://github.com/smilelc3/MyLittleTool.git
sudo apt install emscripten -y
emcc MyLittleTool/Hex2Ascii.cpp MyLittleTool/Ascii2Hex.cpp MyLittleTool/Linear11Trans.cpp -D WASM_EMCC -o MyLittleTool.js -Oz -flto -s WASM=1 -s "EXPORTED_RUNTIME_METHODS=['cwrap']"
emcc MyLittleTool/Hex2Ascii.cpp MyLittleTool/Ascii2Hex.cpp MyLittleTool/Linear11Trans.cpp MyLittleTool/checksum_byteacc.cpp -D WASM_EMCC -o MyLittleTool.js -Oz -flto -s WASM=1 -s EXPORTED_FUNCTIONS='["_malloc", "_free"]' -s "EXPORTED_RUNTIME_METHODS=['cwrap']" -s ALLOW_MEMORY_GROWTH -s ASSERTIONS=1
rm -rf MyLittleTool
cd -
Expand Down
68 changes: 63 additions & 5 deletions markdown/小工具集合.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,36 @@ date: 2024-10-20
<div class="output" id="outHexStr">输出: </div>
</div>
<hr>
<div class="echo-section" id="Linea11Trans">
<div class="tool-section" id="Linea11Trans">
<h3>Linear11 格式转换实数</h3>
<input type="text" placeholder="输入Linear11格式数,如:0x00dc" id="inLinear11" />
<button onclick="summit('Linea11Trans', 'inLinear11', 'outReal')">提交</button>
<div class="output" id="outReal">输出: </div>
</div>
<hr>
<script src="/js/MyLittleTool.js"></script>
<div class="tool-section" id="ByteAccByMem">
<h3>校验和计算(ByteAcc算法)</h3>
<input type="file" id="fileInput" />
<button onclick="summit('ByteAcc', null, 'outByteAcc')">提交</button>
<div class="output" id="outByteAcc">输出: </div>
</div>
<hr>
<script src="js/MyLittleTool.js"></script>
<script>
let C_Hex2Ascii, C_Ascii2Hex, C_Linear11Trans;
let C_Hex2Ascii, C_Ascii2Hex, C_Linear11Trans, C_ByteAccByMem;
// 等待 wasm 模块加载
Module.onRuntimeInitialized = async () => {
C_Hex2Ascii = Module.cwrap('C_Hex2Ascii', 'string', ['string']);
C_Ascii2Hex = Module.cwrap('C_Ascii2Hex', 'string', ['string']);
C_Linear11Trans = Module.cwrap('C_Linear11Trans', 'string', ['string']);
C_ByteAccByMem = Module.cwrap('C_ByteAccByMem', 'string', ['number', 'number']);
};
// 调用 wasm 中导出的函数
function summit(funcName, inputId, outputId) {
const input = document.getElementById(inputId).value;
let input;
if (document.getElementById(inputId)) {
input = document.getElementById(inputId).value;
}
let output;
switch (funcName) {
case 'Hex2Ascii':
Expand All @@ -53,12 +64,59 @@ date: 2024-10-20
case 'Linea11Trans':
output = C_Linear11Trans(input);
break;
case 'ByteAcc':
return doByteAcc(outputId);
}
updateOutput(outputId, output);
}
function updateOutput(outputId, output) {
document.getElementById(outputId).innerText = `输出: ${output}`;
}
function doByteAcc(outputId) {
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('请先选择一个文件');
return;
}
const file = fileInput.files[0];
if (file.size > 2147483648 / 2 * 0.9) {
console.log(file.size + " is out of range")
updateOutput(outputId, "文件过大");
return;
}
const reader = new FileReader();
reader.onload = (event) => {
updateOutput(outputId, "处理中");
arrayBuffer = event.target.result;
byteArray = new Uint8Array(arrayBuffer);
const length = byteArray.length;
// 分配内存并拷贝数据
const buffer = Module._malloc(length);
try {
Module.HEAPU8.set(byteArray, buffer);
// 调用 C++ 函数打印字节
const checksumByteAcc = C_ByteAccByMem(buffer, length);
updateOutput(outputId, checksumByteAcc);
} catch (error) {
console.error("Error during C function call:", error);
updateOutput(outputId, "处理出错");
} finally {
// 释放内存
Module._free(buffer);
// 清理引用以帮助释放内存
arrayBuffer = null;
byteArray = null;
reader.onload = null; // 清理事件处理程序
}
};
reader.onprogress = (event) => {
updateOutput(outputId, "处理中");
}
reader.readAsArrayBuffer(file);
}
</script>
<style>
input[type="text"] {
input[type="text"], input[type="file"] {
width: 100%;
padding: 10px;
margin: 10px 0;
Expand Down

0 comments on commit fab63f8

Please sign in to comment.