Skip to content

Commit

Permalink
Cleaned up demo
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Nov 6, 2024
1 parent 9cdf38b commit bb33628
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
59 changes: 29 additions & 30 deletions example/demo.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { configure, InMemory, fs } from '@zenfs/core';
import { IndexedDB } from '@zenfs/dom';
import { configure, fs, type DeviceFS } from '@zenfs/core';
import { framebuffer, dsp } from '@zenfs/devices';

// this is optional, but I set them, so I have control
const canvas = document.getElementById('fb');
const canvas = document.querySelector<HTMLCanvasElement>('#fb')!;
const audioContext = new AudioContext();

// add initial devices like /dev/null, etc
configure({ addDevices: true }).then(() => {
// mount framebuffer & dsp
fs.mounts.get('/dev').createDevice('/fb0', framebuffer({ canvas }));
fs.mounts.get('/dev').createDevice('/dsp', dsp({ audioContext }));
await configure({ addDevices: true });

// example: write static to framebuffer
const screen = new Uint8Array(canvas.width * canvas.height * 4);
function makestaticFb() {
for (let i = 0; i < screen.byteLength; i += 4) {
screen[i] = Math.random() * 255;
screen[i + 1] = Math.random() * 255;
screen[i + 2] = Math.random() * 255;
screen[i + 3] = 255;
}
fs.promises.writeFile('/dev/fb0', screen);
requestAnimationFrame(makestaticFb);
const devfs = fs.mounts.get('/dev') as DeviceFS;

// mount framebuffer & dsp
devfs.createDevice('/fb0', framebuffer({ canvas }));
devfs.createDevice('/dsp', await dsp({ audioContext }));

// example: write static to framebuffer
const screen = new Uint8Array(canvas.width * canvas.height * 4);
function makestaticFb() {
for (let i = 0; i < screen.byteLength; i += 4) {
screen[i] = Math.random() * 255;
screen[i + 1] = Math.random() * 255;
screen[i + 2] = Math.random() * 255;
screen[i + 3] = 255;
}
makestaticFb();
fs.promises.writeFile('/dev/fb0', screen);
requestAnimationFrame(makestaticFb);
}
makestaticFb();

// example: write static to audio
const audioBuffer = new ArrayBuffer(audioContext.sampleRate * 4);
const audioBytes = new Uint8Array(audioBuffer);
const audioFloats = new Float32Array(audioBuffer);
setInterval(() => {
for (let i in audioFloats) {
audioFloats[i] = Math.random() * 2 - 1;
}
fs.promises.writeFile('/dev/dsp', audioBytes);
}, 1000);
});
// example: write static to audio
const audioBuffer = new Float32Array(new ArrayBuffer(audioContext.sampleRate * 4));
setInterval(() => {
for (let i in audioBuffer) {
audioBuffer[i] = Math.random() * 2 - 1;
}
fs.promises.writeFile('/dev/dsp', audioBuffer);
}, 1000);
8 changes: 8 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"include": ["*.ts", "demo.ts"],
"exclude": ["node_modules"]
}
2 changes: 1 addition & 1 deletion src/framebuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Errno, ErrnoError } from '@zenfs/core';
import type { DeviceDriver, DeviceFile } from '@zenfs/core';

interface FramebufferOptions {
canvas?: HTMLCanvasElement;
canvas?: HTMLCanvasElement | null;
}

let framebufferN = 0;
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"module": "NodeNext",
"target": "ES2020",
"outDir": "dist",
"rootDir": ".",
"lib": ["ESNext", "ESNext.Disposable", "DOM"],
"moduleResolution": "NodeNext",
"declaration": true,
"strict": true,
"noEmit": false
"strict": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
Expand Down

0 comments on commit bb33628

Please sign in to comment.