Skip to content

Commit

Permalink
Create folders
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jun 25, 2024
1 parent b1e953c commit b2de76a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
1 change: 1 addition & 0 deletions frontend/wasm/infiniemu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const module: EmscriptenModuleFactory<EmscriptenModule & {
_lfs_info_name(info: LFSInfo): Pointer;
_lfs_open_file(lfs: LFS, path: Pointer, flags: number): LFSFile;
_lfs_file_read(lfs: LFS, file: LFSFile, buffer: Pointer, size: number): number;
_lfs_mkdir(lfs: LFS, path: Pointer): number;

UTF8ToString(ptr: Pointer, maxBytes?: number): string;
stringToNewUTF8(str: string): Pointer;
Expand Down
4 changes: 3 additions & 1 deletion frontend/wasm/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export type MessageToWorkerType =
{ type: "releaseButton", data: void } |
{ type: "setProgram", data: ArrayBuffer } |
{ type: "readDir", data: string } |
{ type: "readFile", data: string };
{ type: "readFile", data: string } |
{ type: "createDir", data: string };

export type MessageFromWorkerType =
{ type: "ready", data: void } |
{ type: "error", data: { message: string | undefined, stack: string | undefined, string: string | undefined } } |
{ type: "dirFiles", data: FileInfo[] } |
{ type: "fileData", data: { path: string, data: ArrayBuffer } } |
{ type: "createdDir", data: string } |
{ type: "running", data: boolean } |
{ type: "rttFound", data: void } |
{ type: "rttData", data: string } |
Expand Down
24 changes: 23 additions & 1 deletion frontend/wasm/src/components/FileBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ div
import { ref } from 'vue';
import type { FileInfo, MessageFromWorkerType } from '@/common';
import { downloadURL, sendMessage } from '@/utils';
import { downloadURL, joinLFSPaths, sendMessage } from '@/utils';
const props = defineProps<{
worker: Worker,
Expand Down Expand Up @@ -61,6 +61,7 @@ function navigate(dir: FileInfo) {
currentPath.value = currentPathParts.join("/");
}
else {
console.log(dir.fullPath);
currentPath.value = dir.fullPath;
}
Expand Down Expand Up @@ -89,6 +90,27 @@ function openFile(file: FileInfo) {
props.worker.addEventListener("message", listener);
sendMessage(props.worker, "readFile", file.fullPath);
}
function createFolder() {
const folderName = prompt("Enter folder name");
if (folderName) {
const path = joinLFSPaths(currentPath.value, folderName);
const listener = (event: MessageEvent) => {
const { type, data } = event.data as MessageFromWorkerType;
if (type == "createdDir" && data == path) {
props.worker.removeEventListener("message", listener);
emit("loadEnd");
refresh();
}
};
props.worker.addEventListener("message", listener);
sendMessage(props.worker, "createDir", path);
}
}
</script>

<style lang="scss">
Expand Down
4 changes: 4 additions & 0 deletions frontend/wasm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ export function downloadURL(url: string, filename: string) {
a.click();
a.remove();
}

export function joinLFSPaths(...paths: string[]) {
return paths.join("/").replace(/^\//, "").replace(/\/+/g, "/");
}
54 changes: 37 additions & 17 deletions frontend/wasm/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CPU, CST816S, Commander, LFS, NRF52832, Pinetime, Pins, Pointer, RTT, SPINorFlash, ST7789 } from "../infiniemu.js"
import createModule from "../infiniemu.js"
import type { FileInfo, MessageFromWorkerType, MessageToWorkerType } from "./common";
import { joinLFSPaths } from "./utils.js";

const iterations = 700000;

Expand Down Expand Up @@ -165,6 +166,26 @@ class Emulator {
}
}

doTouch(gesture: number, x: number, y: number, duration?: number) {
this.readDir("");

this.Module._cst816s_do_touch(this.touch, gesture, x, y);

if (duration && duration > 0)
setTimeout(() => this.Module._cst816s_release_touch(this.touch), duration);
}

clearTouch() {
this.Module._cst816s_release_touch(this.touch);
}

changePin(pin: number, isSet: boolean) {
if (isSet)
this.Module._pins_set(this.pins, pin);
else
this.Module._pins_clear(this.pins, pin);
}

private useLFS<T>(fn: (lfs: LFS) => T) {
const bufferPtr = this.Module._spinorflash_get_buffer(this.spiFlash);
const lfs = this.Module._lfs_init(pointerAdd(bufferPtr, 0x0B4000), 0x400000 - 0x0B4000);
Expand Down Expand Up @@ -199,7 +220,7 @@ class Emulator {
const name = this.Module.UTF8ToString(this.Module._lfs_info_name(info));
files.push({
name,
fullPath: path == "" ? name : (path + "/" + name),
fullPath: joinLFSPaths(path, name),
size: this.Module._lfs_info_size(info),
type: this.Module._lfs_info_type(info) == 1 ? "file" : "dir",
});
Expand Down Expand Up @@ -261,24 +282,16 @@ class Emulator {
});
}

doTouch(gesture: number, x: number, y: number, duration?: number) {
this.readDir("");

this.Module._cst816s_do_touch(this.touch, gesture, x, y);

if (duration && duration > 0)
setTimeout(() => this.Module._cst816s_release_touch(this.touch), duration);
}
createDir(path: string) {
return this.useLFS(lfs => {
const pathBytes = this.Module.stringToNewUTF8(path);

clearTouch() {
this.Module._cst816s_release_touch(this.touch);
}
const ret = this.Module._lfs_mkdir(lfs, pathBytes);
if (ret < 0)
throw new Error("Error creating dir: " + ret);

changePin(pin: number, isSet: boolean) {
if (isSet)
this.Module._pins_set(this.pins, pin);
else
this.Module._pins_clear(this.pins, pin);
this.Module._free(pointerToNumber(pathBytes));
});
}
};

Expand Down Expand Up @@ -363,6 +376,13 @@ function handleMessage(msg: MessageToWorkerType) {
if (emulator)
sendMessage("fileData", { path: data, data: emulator.readFile(data) });
break;

case "createDir":
if (emulator) {
emulator.createDir(data);
sendMessage("createdDir", data);
}
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ set(WASM_EXPORTS
program_new program_load_binary program_load_elf
commander_new commander_set_output commander_run_command commander_output
rtt_new rtt_find_control rtt_flush_buffers
lfs_init lfs_free_wasm lfs_open_dir lfs_dir_read
lfs_init lfs_mkdir lfs_free_wasm lfs_open_dir lfs_dir_read
lfs_open_file lfs_file_read
lfs_info_malloc lfs_info_type lfs_info_size lfs_info_name
)
Expand Down

0 comments on commit b2de76a

Please sign in to comment.