Skip to content

Commit

Permalink
update filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-j committed Nov 20, 2024
1 parent e0f85b2 commit c9d67e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions examples/filesystem/hello_filesystem_flash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ else:
close(fp)
echo "HELLO.TXT: ", buffer

echo "list files in root:"
for file in fsWalkDir("/"):
echo file

echo "unmounting: ", fsStrerror(fsUnmount("/"))

while true:
Expand Down
8 changes: 1 addition & 7 deletions examples/filesystem/hello_filesystem_sd.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import picostdlib
import picostdlib/pico/filesystem
import std/posix
import std/os

# see hello_filesystem_sd.nims

# workaround
#{.emit: "#define lstat stat".}
proc lstat(path: cstring; buf: var Stat): cint {.exportc.} = stat(path, buf)

const csPin = Gpio(22) # Change to the pin your sdcard uses

stdioInitAll()
Expand Down Expand Up @@ -48,7 +42,7 @@ else:
echo "HELLO.TXT: ", buffer

echo "list files in sdcard root:"
for file in walkDir("/sd"):
for file in fsWalkDir("/sd"):
echo file

echo "unmounting: ", fsStrerror(fsUnmount("/sd"))
Expand Down
21 changes: 19 additions & 2 deletions src/picostdlib/pico/filesystem.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import std/os, std/strutils, std/posix

export os, posix

const picoVfsPath = currentSourcePath.replace('\\', DirSep).parentDir.parentDir / "vendor" / "pico-vfs"

{.compile: picoVfsPath / "src" / "filesystem" / "vfs.c".}
Expand Down Expand Up @@ -37,8 +39,8 @@ type

type
DirectoryEntry* {.importc: "struct dirent".} = object
dtype* {.importc: "d_type".}: DirectoryType
dname* {.importc: "d_name".}: array[255 + 1, char]
d_type*: DirectoryType
d_name*: array[255 + 1, char]

FilesystemFile* {.importc: "fs_file_t".} = object
fd*: cint
Expand Down Expand Up @@ -311,3 +313,18 @@ when defined(pico_filesystem_filesystem_fat) or defined(nimcheck):
## \param fs littlefs file system object

{.pop.}


# old workaround
#{.emit: "#define lstat stat".}
# proc lstat(path: cstring; buf: var Stat): cint {.exportc.} = stat(path, buf)
iterator fsWalkDir*(directory: string): tuple[kind: DirectoryType, name: string] =
var dirent: ptr Dirent
var dir = opendir(directory.cstring)
assert(not dir.isNil)
defer: assert(closedir(dir) == 0)
while (dirent = readdir(dir); not dirent.isNil):
yield (
kind: cast[DirectoryType](dirent.d_type),
name: $cast[cstring](dirent.d_name[0].addr)
)
2 changes: 1 addition & 1 deletion src/picostdlib/vendor/pico-vfs
Submodule pico-vfs updated 1 files
+10 −0 src/filesystem/fat.c

0 comments on commit c9d67e8

Please sign in to comment.