UNPKG

@ankhzet/goo

Version:

Elegoo .goo file format reader/writer

111 lines (110 loc) 4.17 kB
const format = new Intl.DateTimeFormat('en-EN', { hourCycle: 'h24', day: '2-digit', year: 'numeric', month: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }); const INCHES_PER_MM = 0.0393701; export const chunk = (str, chunk) => (Array(Math.ceil(str.length / chunk)) .fill(0).map((_, index) => index * chunk).map((begin) => str.slice(begin, begin + chunk))); export const printBuffer = (buffer) => { console.log(buffer.length.toString().padStart(7, ' ') + ' |', chunk(buffer.toString('hex', 0, 16).padEnd(32, ' '), 2).map((i) => (i === '00' ? ' ' : (i === ' ' ? '__' : i))).join(' '), '| ' + buffer.toString('utf8', 0, 32).replace(/[^\w \p{P}]/gu, ' ')); }; export const formatDate = (date) => { const parts = format.formatToParts(date); const find = (n) => { var _a; return (_a = parts.find(({ type }) => type === n)) === null || _a === void 0 ? void 0 : _a.value; }; return `${find('year')}-${find('month')}-${find('day')} ${find('hour')}:${find('minute')}:${find('second')}`; }; export const formatBytes = (data, len = 1) => '0x' + data.toString(16).padStart(2 * len, '0'); export const rgb565 = (rgba) => { const r = (rgba >> 8) & 0xFF; const g = (rgba >> 16) & 0xFF; const b = (rgba >> 24) & 0xFF; const r1 = ~~(r / 8); const g1 = ~~(g / 4); const b1 = ~~(b / 8); return ((r1) | (g1 << 5) | (b1 << 11)) >>> 0; }; export const rgba8888 = (rgb) => { const r = (rgb >> 0) & 0b11111; const g = (rgb >> 5) & 0b111111; const b = (rgb >> 11) & 0b11111; const r1 = r * 8; const g1 = g * 4; const b1 = b * 8; return ((r1 << 8) | (g1 << 16) | (b1 << 24) | 0xFF) >>> 0; }; export const rgb565Buffer = (buffer, channels) => { // RGB_565, 32, 64, 32, (/8, /4, /8) let offset = 0; let pos = 0; const total = buffer.length; const pixels = ~~(total / channels); const target = Buffer.alloc(2 * pixels); while (offset < total) { target.writeUInt16BE(rgb565(buffer.readUint32BE(offset)), pos); offset += channels; pos += 2; } return target; }; export const rgba8888Buffer = (buffer) => { // RGB_565, 32, 64, 32, (/8, /4, /8) let offset = 0; let pos = 0; const total = buffer.length; const pixels = ~~(total / 2); const target = Buffer.alloc(4 * pixels); while (offset < total) { target.writeUInt32BE(rgba8888(buffer.readUint16BE(offset)), pos); offset += 2; pos += 4; } return target; }; export const bit = (value, width) => value.toString(2).padStart(width, '0'); export const promisify = (fn) => new Promise((resolve, reject) => { fn((error, ...args) => { if (error) { reject(error); } else { resolve(args); } }); }); export const sizeInCoordinateSystem = ({ resolution, platform }, { density, width, height }) => { const widthPixelPerMM = resolution.x / platform.x; const heightPixelPerMM = resolution.y / platform.y; const scaler = density * INCHES_PER_MM; const mmWidth = width / scaler; const mmHeight = height / scaler; return { width: Math.round(widthPixelPerMM * mmWidth), height: Math.round(heightPixelPerMM * mmHeight), }; }; export const transformInCoordinateSystem = (printer, transform, metadata) => { const { translate: { x, y } = { x: 0, y: 0 }, scale = { x: 1, y: 1 } } = transform || {}; const size = sizeInCoordinateSystem(printer, metadata); const width = Math.round(size.width * scale.x); const height = Math.round(size.height * scale.y); return { x, y, width, height, }; }; export const assert = (value, message) => { if (!value) { throw new Error(message); } }; export const motionTime = (config) => config.distance / (config.speed || 1); export const layerTime = ({ definition: { exposure, times, motions } }) => (exposure + (times.before.lift + times.after.lift + times.after.retract) + (motionTime(motions.lift.first) + motionTime(motions.lift.second) + motionTime(motions.retract.first) + motionTime(motions.retract.second)));