muse/book-reading-clip-lamp
A Wi‑Fi/USB‑C ESP32‑S3 reading lamp with OV5640 camera, microphone, speaker amplifier, rechargeable Li‑Po power management, six-LED boost light, USB protection/charging detection, and a clip/gooseneck enclosure with external FlexPIFA antenna.
- Version
- 0.4.3
- License
- unset
- Stars
- 0
split-pcb-preview.mjs
import { readFileSync, writeFileSync } from "node:fs"
import { dirname, join } from "node:path"
// Registry requests cannot carry the monolithic PCB GLB. Keep every buffer view
// byte-identical, but place them in small external glTF buffers for uploading.
export function splitPcbPreview(path) {
const glb = readFileSync(path)
if (glb.readUInt32LE(0) !== 0x46546c67 || glb.readUInt32LE(4) !== 2) {
throw new Error("Expected a glTF 2 GLB")
}
const jsonLength = glb.readUInt32LE(12)
const model = JSON.parse(glb.subarray(20, 20 + jsonLength).toString())
const binHeader = 20 + jsonLength
if (model.buffers.length !== 1 || glb.readUInt32LE(binHeader + 4) !== 0x004e4942) {
throw new Error("Expected one embedded GLB buffer")
}
const source = glb.subarray(binHeader + 8)
const chunks = [Buffer.alloc(0)]
const limit = 2 * 1024 * 1024
for (const view of model.bufferViews) {
if (view.buffer !== 0 || view.byteLength > limit) throw new Error("Buffer view exceeds upload chunk limit")
const bytes = source.subarray(view.byteOffset ?? 0, (view.byteOffset ?? 0) + view.byteLength)
let index = chunks.length - 1
if (chunks[index].length + 3 + bytes.length > limit) { chunks.push(Buffer.alloc(0)); index++ }
const offset = Math.ceil(chunks[index].length / 4) * 4
chunks[index] = Buffer.concat([chunks[index], Buffer.alloc(offset - chunks[index].length), bytes])
if (!chunks[index].subarray(offset, offset + bytes.length).equals(bytes)) throw new Error("Buffer copy mismatch")
view.buffer = index
view.byteOffset = offset
}
model.buffers = chunks.map((data, index) => {
const uri = `pcb-buffer-${index}.bin`
writeFileSync(join(dirname(path), uri), data)
return { uri, byteLength: data.length }
})
writeFileSync(join(dirname(path), "pcb.gltf"), JSON.stringify(model))
console.log(`Exported byte-identical PCB preview in ${chunks.length} upload-sized buffers`)
}