Skip to content

Commit

Permalink
新增byteAcc
Browse files Browse the repository at this point in the history
  • Loading branch information
l30014168 committed Oct 21, 2024
1 parent c2b37fe commit 82c07fd
Show file tree
Hide file tree
Showing 2 changed files with 52 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_RUNTIME_METHODS=['cwrap']" -s ALLOW_MEMORY_GROWTH
rm -rf MyLittleTool
cd -
Expand Down
56 changes: 51 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,47 @@ 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];
console.log(file.size)
if (file.size > 2145386496) {
console.log(file.size + " is out of range")
return;
}
const reader = new FileReader();
reader.onload = (event) => {
const arrayBuffer = event.target.result;
const byteArray = new Uint8Array(arrayBuffer);
const length = byteArray.length;
// 分配内存并拷贝数据
const buffer = Module._malloc(length);
Module.HEAPU8.set(byteArray, buffer);
// 调用 C++ 函数打印字节
const checksumByteAcc = C_ByteAccByMem(buffer, length);
// 释放内存
Module._free(buffer);
updateOutput(outputId, checksumByteAcc);
};
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 82c07fd

Please sign in to comment.